Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(298)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/sdk/CSSProperty.js

Issue 2769843003: DevTools: split text_utils out of common module (Closed)
Patch Set: rebaseline Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 /** 4 /**
5 * @unrestricted 5 * @unrestricted
6 */ 6 */
7 SDK.CSSProperty = class { 7 SDK.CSSProperty = class {
8 /** 8 /**
9 * @param {!SDK.CSSStyleDeclaration} ownerStyle 9 * @param {!SDK.CSSStyleDeclaration} ownerStyle
10 * @param {number} index 10 * @param {number} index
11 * @param {string} name 11 * @param {string} name
12 * @param {string} value 12 * @param {string} value
13 * @param {boolean} important 13 * @param {boolean} important
14 * @param {boolean} disabled 14 * @param {boolean} disabled
15 * @param {boolean} parsedOk 15 * @param {boolean} parsedOk
16 * @param {boolean} implicit 16 * @param {boolean} implicit
17 * @param {?string=} text 17 * @param {?string=} text
18 * @param {!Protocol.CSS.SourceRange=} range 18 * @param {!Protocol.CSS.SourceRange=} range
19 */ 19 */
20 constructor(ownerStyle, index, name, value, important, disabled, parsedOk, imp licit, text, range) { 20 constructor(ownerStyle, index, name, value, important, disabled, parsedOk, imp licit, text, range) {
21 this.ownerStyle = ownerStyle; 21 this.ownerStyle = ownerStyle;
22 this.index = index; 22 this.index = index;
23 this.name = name; 23 this.name = name;
24 this.value = value; 24 this.value = value;
25 this.important = important; 25 this.important = important;
26 this.disabled = disabled; 26 this.disabled = disabled;
27 this.parsedOk = parsedOk; 27 this.parsedOk = parsedOk;
28 this.implicit = implicit; // A longhand, implicitly set by missing values o f shorthand. 28 this.implicit = implicit; // A longhand, implicitly set by missing values o f shorthand.
29 this.text = text; 29 this.text = text;
30 this.range = range ? Common.TextRange.fromObject(range) : null; 30 this.range = range ? TextUtils.TextRange.fromObject(range) : null;
31 this._active = true; 31 this._active = true;
32 this._nameRange = null; 32 this._nameRange = null;
33 this._valueRange = null; 33 this._valueRange = null;
34 } 34 }
35 35
36 /** 36 /**
37 * @param {!SDK.CSSStyleDeclaration} ownerStyle 37 * @param {!SDK.CSSStyleDeclaration} ownerStyle
38 * @param {number} index 38 * @param {number} index
39 * @param {!Protocol.CSS.CSSProperty} payload 39 * @param {!Protocol.CSS.CSSProperty} payload
40 * @return {!SDK.CSSProperty} 40 * @return {!SDK.CSSProperty}
41 */ 41 */
42 static parsePayload(ownerStyle, index, payload) { 42 static parsePayload(ownerStyle, index, payload) {
43 // The following default field values are used in the payload: 43 // The following default field values are used in the payload:
44 // important: false 44 // important: false
45 // parsedOk: true 45 // parsedOk: true
46 // implicit: false 46 // implicit: false
47 // disabled: false 47 // disabled: false
48 var result = new SDK.CSSProperty( 48 var result = new SDK.CSSProperty(
49 ownerStyle, index, payload.name, payload.value, payload.important || fal se, payload.disabled || false, 49 ownerStyle, index, payload.name, payload.value, payload.important || fal se, payload.disabled || false,
50 ('parsedOk' in payload) ? !!payload.parsedOk : true, !!payload.implicit, payload.text, payload.range); 50 ('parsedOk' in payload) ? !!payload.parsedOk : true, !!payload.implicit, payload.text, payload.range);
51 return result; 51 return result;
52 } 52 }
53 53
54 _ensureRanges() { 54 _ensureRanges() {
55 if (this._nameRange && this._valueRange) 55 if (this._nameRange && this._valueRange)
56 return; 56 return;
57 var range = this.range; 57 var range = this.range;
58 var text = this.text ? new Common.Text(this.text) : null; 58 var text = this.text ? new TextUtils.Text(this.text) : null;
59 if (!range || !text) 59 if (!range || !text)
60 return; 60 return;
61 61
62 var nameIndex = text.value().indexOf(this.name); 62 var nameIndex = text.value().indexOf(this.name);
63 var valueIndex = text.value().lastIndexOf(this.value); 63 var valueIndex = text.value().lastIndexOf(this.value);
64 if (nameIndex === -1 || valueIndex === -1 || nameIndex > valueIndex) 64 if (nameIndex === -1 || valueIndex === -1 || nameIndex > valueIndex)
65 return; 65 return;
66 66
67 var nameSourceRange = new Common.SourceRange(nameIndex, this.name.length); 67 var nameSourceRange = new TextUtils.SourceRange(nameIndex, this.name.length) ;
68 var valueSourceRange = new Common.SourceRange(valueIndex, this.value.length) ; 68 var valueSourceRange = new TextUtils.SourceRange(valueIndex, this.value.leng th);
69 69
70 this._nameRange = rebase(text.toTextRange(nameSourceRange), range.startLine, range.startColumn); 70 this._nameRange = rebase(text.toTextRange(nameSourceRange), range.startLine, range.startColumn);
71 this._valueRange = rebase(text.toTextRange(valueSourceRange), range.startLin e, range.startColumn); 71 this._valueRange = rebase(text.toTextRange(valueSourceRange), range.startLin e, range.startColumn);
72 72
73 /** 73 /**
74 * @param {!Common.TextRange} oneLineRange 74 * @param {!TextUtils.TextRange} oneLineRange
75 * @param {number} lineOffset 75 * @param {number} lineOffset
76 * @param {number} columnOffset 76 * @param {number} columnOffset
77 * @return {!Common.TextRange} 77 * @return {!TextUtils.TextRange}
78 */ 78 */
79 function rebase(oneLineRange, lineOffset, columnOffset) { 79 function rebase(oneLineRange, lineOffset, columnOffset) {
80 if (oneLineRange.startLine === 0) { 80 if (oneLineRange.startLine === 0) {
81 oneLineRange.startColumn += columnOffset; 81 oneLineRange.startColumn += columnOffset;
82 oneLineRange.endColumn += columnOffset; 82 oneLineRange.endColumn += columnOffset;
83 } 83 }
84 oneLineRange.startLine += lineOffset; 84 oneLineRange.startLine += lineOffset;
85 oneLineRange.endLine += lineOffset; 85 oneLineRange.endLine += lineOffset;
86 return oneLineRange; 86 return oneLineRange;
87 } 87 }
88 } 88 }
89 89
90 /** 90 /**
91 * @return {?Common.TextRange} 91 * @return {?TextUtils.TextRange}
92 */ 92 */
93 nameRange() { 93 nameRange() {
94 this._ensureRanges(); 94 this._ensureRanges();
95 return this._nameRange; 95 return this._nameRange;
96 } 96 }
97 97
98 /** 98 /**
99 * @return {?Common.TextRange} 99 * @return {?TextUtils.TextRange}
100 */ 100 */
101 valueRange() { 101 valueRange() {
102 this._ensureRanges(); 102 this._ensureRanges();
103 return this._valueRange; 103 return this._valueRange;
104 } 104 }
105 105
106 /** 106 /**
107 * @param {!SDK.CSSModel.Edit} edit 107 * @param {!SDK.CSSModel.Edit} edit
108 */ 108 */
109 rebase(edit) { 109 rebase(edit) {
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 if (overwrite && propertyText === this.propertyText) { 158 if (overwrite && propertyText === this.propertyText) {
159 if (majorChange) 159 if (majorChange)
160 this.ownerStyle.cssModel().domModel().markUndoableState(); 160 this.ownerStyle.cssModel().domModel().markUndoableState();
161 return Promise.resolve(true); 161 return Promise.resolve(true);
162 } 162 }
163 163
164 var range = this.range.relativeTo(this.ownerStyle.range.startLine, this.owne rStyle.range.startColumn); 164 var range = this.range.relativeTo(this.ownerStyle.range.startLine, this.owne rStyle.range.startColumn);
165 var indentation = this.ownerStyle.cssText ? this._detectIndentation(this.own erStyle.cssText) : 165 var indentation = this.ownerStyle.cssText ? this._detectIndentation(this.own erStyle.cssText) :
166 Common.moduleSetting('textEditor Indent').get(); 166 Common.moduleSetting('textEditor Indent').get();
167 var endIndentation = this.ownerStyle.cssText ? indentation.substring(0, this .ownerStyle.range.endColumn) : ''; 167 var endIndentation = this.ownerStyle.cssText ? indentation.substring(0, this .ownerStyle.range.endColumn) : '';
168 var text = new Common.Text(this.ownerStyle.cssText || ''); 168 var text = new TextUtils.Text(this.ownerStyle.cssText || '');
169 var newStyleText = text.replaceRange(range, String.sprintf(';%s;', propertyT ext)); 169 var newStyleText = text.replaceRange(range, String.sprintf(';%s;', propertyT ext));
170 170
171 return self.runtime.extension(Common.TokenizerFactory) 171 return self.runtime.extension(TextUtils.TokenizerFactory)
172 .instance() 172 .instance()
173 .then(this._formatStyle.bind(this, newStyleText, indentation, endIndenta tion)) 173 .then(this._formatStyle.bind(this, newStyleText, indentation, endIndenta tion))
174 .then(setStyleText.bind(this)); 174 .then(setStyleText.bind(this));
175 175
176 /** 176 /**
177 * @param {string} styleText 177 * @param {string} styleText
178 * @this {SDK.CSSProperty} 178 * @this {SDK.CSSProperty}
179 * @return {!Promise.<boolean>} 179 * @return {!Promise.<boolean>}
180 */ 180 */
181 function setStyleText(styleText) { 181 function setStyleText(styleText) {
182 return this.ownerStyle.setText(styleText, majorChange); 182 return this.ownerStyle.setText(styleText, majorChange);
183 } 183 }
184 } 184 }
185 185
186 /** 186 /**
187 * @param {string} styleText 187 * @param {string} styleText
188 * @param {string} indentation 188 * @param {string} indentation
189 * @param {string} endIndentation 189 * @param {string} endIndentation
190 * @param {!Common.TokenizerFactory} tokenizerFactory 190 * @param {!TextUtils.TokenizerFactory} tokenizerFactory
191 * @return {string} 191 * @return {string}
192 */ 192 */
193 _formatStyle(styleText, indentation, endIndentation, tokenizerFactory) { 193 _formatStyle(styleText, indentation, endIndentation, tokenizerFactory) {
194 if (indentation) 194 if (indentation)
195 indentation = '\n' + indentation; 195 indentation = '\n' + indentation;
196 var result = ''; 196 var result = '';
197 var propertyText; 197 var propertyText;
198 var insideProperty = false; 198 var insideProperty = false;
199 var tokenize = tokenizerFactory.createTokenizer('text/css'); 199 var tokenize = tokenizerFactory.createTokenizer('text/css');
200 200
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 } 250 }
251 251
252 /** 252 /**
253 * @param {string} text 253 * @param {string} text
254 * @return {string} 254 * @return {string}
255 */ 255 */
256 _detectIndentation(text) { 256 _detectIndentation(text) {
257 var lines = text.split('\n'); 257 var lines = text.split('\n');
258 if (lines.length < 2) 258 if (lines.length < 2)
259 return ''; 259 return '';
260 return Common.TextUtils.lineIndent(lines[1]); 260 return TextUtils.TextUtils.lineIndent(lines[1]);
261 } 261 }
262 262
263 /** 263 /**
264 * @param {string} newValue 264 * @param {string} newValue
265 * @param {boolean} majorChange 265 * @param {boolean} majorChange
266 * @param {boolean} overwrite 266 * @param {boolean} overwrite
267 * @param {function(boolean)=} userCallback 267 * @param {function(boolean)=} userCallback
268 */ 268 */
269 setValue(newValue, majorChange, overwrite, userCallback) { 269 setValue(newValue, majorChange, overwrite, userCallback) {
270 var text = this.name + ': ' + newValue + (this.important ? ' !important' : ' ') + ';'; 270 var text = this.name + ': ' + newValue + (this.important ? ' !important' : ' ') + ';';
271 this.setText(text, majorChange, overwrite).then(userCallback); 271 this.setText(text, majorChange, overwrite).then(userCallback);
272 } 272 }
273 273
274 /** 274 /**
275 * @param {boolean} disabled 275 * @param {boolean} disabled
276 * @return {!Promise.<boolean>} 276 * @return {!Promise.<boolean>}
277 */ 277 */
278 setDisabled(disabled) { 278 setDisabled(disabled) {
279 if (!this.ownerStyle) 279 if (!this.ownerStyle)
280 return Promise.resolve(false); 280 return Promise.resolve(false);
281 if (disabled === this.disabled) 281 if (disabled === this.disabled)
282 return Promise.resolve(true); 282 return Promise.resolve(true);
283 var propertyText = this.text.trim(); 283 var propertyText = this.text.trim();
284 var text = disabled ? '/* ' + propertyText + ' */' : this.text.substring(2, propertyText.length - 2).trim(); 284 var text = disabled ? '/* ' + propertyText + ' */' : this.text.substring(2, propertyText.length - 2).trim();
285 return this.setText(text, true, true); 285 return this.setText(text, true, true);
286 } 286 }
287 }; 287 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698