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

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/common/CSSShadowModel.js

Issue 2466123002: DevTools: reformat front-end code to match chromium style. (Closed)
Patch Set: all done Created 4 years, 1 month 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
5 /** 4 /**
6 * @constructor 5 * @unrestricted
7 * @param {boolean} isBoxShadow
8 */ 6 */
9 WebInspector.CSSShadowModel = function(isBoxShadow) 7 WebInspector.CSSShadowModel = class {
10 { 8 /**
9 * @param {boolean} isBoxShadow
10 */
11 constructor(isBoxShadow) {
11 this._isBoxShadow = isBoxShadow; 12 this._isBoxShadow = isBoxShadow;
12 this._inset = false; 13 this._inset = false;
13 this._offsetX = WebInspector.CSSLength.zero(); 14 this._offsetX = WebInspector.CSSLength.zero();
14 this._offsetY = WebInspector.CSSLength.zero(); 15 this._offsetY = WebInspector.CSSLength.zero();
15 this._blurRadius = WebInspector.CSSLength.zero(); 16 this._blurRadius = WebInspector.CSSLength.zero();
16 this._spreadRadius = WebInspector.CSSLength.zero(); 17 this._spreadRadius = WebInspector.CSSLength.zero();
17 this._color = /** @type {!WebInspector.Color} */ (WebInspector.Color.parse(" black")); 18 this._color = /** @type {!WebInspector.Color} */ (WebInspector.Color.parse(' black'));
18 this._format = [WebInspector.CSSShadowModel._Part.OffsetX, WebInspector.CSSS hadowModel._Part.OffsetY]; 19 this._format = [WebInspector.CSSShadowModel._Part.OffsetX, WebInspector.CSSS hadowModel._Part.OffsetY];
19 }; 20 }
20 21
21 /** 22 /**
22 * @enum {string} 23 * @param {string} text
23 */ 24 * @return {!Array<!WebInspector.CSSShadowModel>}
24 WebInspector.CSSShadowModel._Part = { 25 */
25 Inset: "I", 26 static parseTextShadow(text) {
26 OffsetX: "X", 27 return WebInspector.CSSShadowModel._parseShadow(text, false);
27 OffsetY: "Y", 28 }
28 BlurRadius: "B",
29 SpreadRadius: "S",
30 Color: "C"
31 };
32 29
33 /** 30 /**
34 * @param {string} text 31 * @param {string} text
35 * @return {!Array<!WebInspector.CSSShadowModel>} 32 * @return {!Array<!WebInspector.CSSShadowModel>}
36 */ 33 */
37 WebInspector.CSSShadowModel.parseTextShadow = function(text) 34 static parseBoxShadow(text) {
38 { 35 return WebInspector.CSSShadowModel._parseShadow(text, true);
39 return WebInspector.CSSShadowModel._parseShadow(text, false); 36 }
40 };
41 37
42 /** 38 /**
43 * @param {string} text 39 * @param {string} text
44 * @return {!Array<!WebInspector.CSSShadowModel>} 40 * @param {boolean} isBoxShadow
45 */ 41 * @return {!Array<!WebInspector.CSSShadowModel>}
46 WebInspector.CSSShadowModel.parseBoxShadow = function(text) 42 */
47 { 43 static _parseShadow(text, isBoxShadow) {
48 return WebInspector.CSSShadowModel._parseShadow(text, true);
49 };
50
51 WebInspector.CSSShadowModel.prototype = {
52 /**
53 * @param {boolean} inset
54 */
55 setInset: function(inset)
56 {
57 this._inset = inset;
58 if (this._format.indexOf(WebInspector.CSSShadowModel._Part.Inset) === -1 )
59 this._format.unshift(WebInspector.CSSShadowModel._Part.Inset);
60 },
61
62 /**
63 * @param {!WebInspector.CSSLength} offsetX
64 */
65 setOffsetX: function(offsetX)
66 {
67 this._offsetX = offsetX;
68 },
69
70 /**
71 * @param {!WebInspector.CSSLength} offsetY
72 */
73 setOffsetY: function(offsetY)
74 {
75 this._offsetY = offsetY;
76 },
77
78 /**
79 * @param {!WebInspector.CSSLength} blurRadius
80 */
81 setBlurRadius: function(blurRadius)
82 {
83 this._blurRadius = blurRadius;
84 if (this._format.indexOf(WebInspector.CSSShadowModel._Part.BlurRadius) = == -1) {
85 var yIndex = this._format.indexOf(WebInspector.CSSShadowModel._Part. OffsetY);
86 this._format.splice(yIndex + 1, 0, WebInspector.CSSShadowModel._Part .BlurRadius);
87 }
88 },
89
90 /**
91 * @param {!WebInspector.CSSLength} spreadRadius
92 */
93 setSpreadRadius: function(spreadRadius)
94 {
95 this._spreadRadius = spreadRadius;
96 if (this._format.indexOf(WebInspector.CSSShadowModel._Part.SpreadRadius) === -1) {
97 this.setBlurRadius(this._blurRadius);
98 var blurIndex = this._format.indexOf(WebInspector.CSSShadowModel._Pa rt.BlurRadius);
99 this._format.splice(blurIndex + 1, 0, WebInspector.CSSShadowModel._P art.SpreadRadius);
100 }
101 },
102
103 /**
104 * @param {!WebInspector.Color} color
105 */
106 setColor: function(color)
107 {
108 this._color = color;
109 if (this._format.indexOf(WebInspector.CSSShadowModel._Part.Color) === -1 )
110 this._format.push(WebInspector.CSSShadowModel._Part.Color);
111 },
112
113 /**
114 * @return {boolean}
115 */
116 isBoxShadow: function()
117 {
118 return this._isBoxShadow;
119 },
120
121 /**
122 * @return {boolean}
123 */
124 inset: function()
125 {
126 return this._inset;
127 },
128
129 /**
130 * @return {!WebInspector.CSSLength}
131 */
132 offsetX: function()
133 {
134 return this._offsetX;
135 },
136
137 /**
138 * @return {!WebInspector.CSSLength}
139 */
140 offsetY: function()
141 {
142 return this._offsetY;
143 },
144
145 /**
146 * @return {!WebInspector.CSSLength}
147 */
148 blurRadius: function()
149 {
150 return this._blurRadius;
151 },
152
153 /**
154 * @return {!WebInspector.CSSLength}
155 */
156 spreadRadius: function()
157 {
158 return this._spreadRadius;
159 },
160
161 /**
162 * @return {!WebInspector.Color}
163 */
164 color: function()
165 {
166 return this._color;
167 },
168
169 /**
170 * @return {string}
171 */
172 asCSSText: function()
173 {
174 var parts = [];
175 for (var i = 0; i < this._format.length; i++) {
176 var part = this._format[i];
177 if (part === WebInspector.CSSShadowModel._Part.Inset && this._inset)
178 parts.push("inset");
179 else if (part === WebInspector.CSSShadowModel._Part.OffsetX)
180 parts.push(this._offsetX.asCSSText());
181 else if (part === WebInspector.CSSShadowModel._Part.OffsetY)
182 parts.push(this._offsetY.asCSSText());
183 else if (part === WebInspector.CSSShadowModel._Part.BlurRadius)
184 parts.push(this._blurRadius.asCSSText());
185 else if (part === WebInspector.CSSShadowModel._Part.SpreadRadius)
186 parts.push(this._spreadRadius.asCSSText());
187 else if (part === WebInspector.CSSShadowModel._Part.Color)
188 parts.push(this._color.asString(this._color.format()));
189 }
190 return parts.join(" ");
191 }
192 };
193
194 /**
195 * @param {string} text
196 * @param {boolean} isBoxShadow
197 * @return {!Array<!WebInspector.CSSShadowModel>}
198 */
199 WebInspector.CSSShadowModel._parseShadow = function(text, isBoxShadow)
200 {
201 var shadowTexts = []; 44 var shadowTexts = [];
202 // Split by commas that aren't inside of color values to get the individual shadow values. 45 // Split by commas that aren't inside of color values to get the individual shadow values.
203 var splits = WebInspector.TextUtils.splitStringByRegexes(text, [WebInspector .Color.Regex, /,/g]); 46 var splits = WebInspector.TextUtils.splitStringByRegexes(text, [WebInspector .Color.Regex, /,/g]);
204 var currentIndex = 0; 47 var currentIndex = 0;
205 for (var i = 0; i < splits.length; i++) { 48 for (var i = 0; i < splits.length; i++) {
206 if (splits[i].regexIndex === 1) { 49 if (splits[i].regexIndex === 1) {
207 var comma = splits[i]; 50 var comma = splits[i];
208 shadowTexts.push(text.substring(currentIndex, comma.position)); 51 shadowTexts.push(text.substring(currentIndex, comma.position));
209 currentIndex = comma.position + 1; 52 currentIndex = comma.position + 1;
210 } 53 }
211 } 54 }
212 shadowTexts.push(text.substring(currentIndex, text.length)); 55 shadowTexts.push(text.substring(currentIndex, text.length));
213 56
214 var shadows = []; 57 var shadows = [];
215 for (var i = 0; i < shadowTexts.length; i++) { 58 for (var i = 0; i < shadowTexts.length; i++) {
216 var shadow = new WebInspector.CSSShadowModel(isBoxShadow); 59 var shadow = new WebInspector.CSSShadowModel(isBoxShadow);
217 shadow._format = []; 60 shadow._format = [];
218 var nextPartAllowed = true; 61 var nextPartAllowed = true;
219 var regexes = [/inset/gi, WebInspector.Color.Regex, WebInspector.CSSLeng th.Regex]; 62 var regexes = [/inset/gi, WebInspector.Color.Regex, WebInspector.CSSLength .Regex];
220 var results = WebInspector.TextUtils.splitStringByRegexes(shadowTexts[i] , regexes); 63 var results = WebInspector.TextUtils.splitStringByRegexes(shadowTexts[i], regexes);
221 for (var j = 0; j < results.length; j++) { 64 for (var j = 0; j < results.length; j++) {
222 var result = results[j]; 65 var result = results[j];
223 if (result.regexIndex === -1) { 66 if (result.regexIndex === -1) {
224 // Don't allow anything other than inset, color, length values, and whitespace. 67 // Don't allow anything other than inset, color, length values, and wh itespace.
225 if (/\S/.test(result.value)) 68 if (/\S/.test(result.value))
226 return []; 69 return [];
227 // All parts must be separated by whitespace. 70 // All parts must be separated by whitespace.
228 nextPartAllowed = true; 71 nextPartAllowed = true;
72 } else {
73 if (!nextPartAllowed)
74 return [];
75 nextPartAllowed = false;
76
77 if (result.regexIndex === 0) {
78 shadow._inset = true;
79 shadow._format.push(WebInspector.CSSShadowModel._Part.Inset);
80 } else if (result.regexIndex === 1) {
81 var color = WebInspector.Color.parse(result.value);
82 if (!color)
83 return [];
84 shadow._color = color;
85 shadow._format.push(WebInspector.CSSShadowModel._Part.Color);
86 } else if (result.regexIndex === 2) {
87 var length = WebInspector.CSSLength.parse(result.value);
88 if (!length)
89 return [];
90 var previousPart = shadow._format.length > 0 ? shadow._format[shadow ._format.length - 1] : '';
91 if (previousPart === WebInspector.CSSShadowModel._Part.OffsetX) {
92 shadow._offsetY = length;
93 shadow._format.push(WebInspector.CSSShadowModel._Part.OffsetY);
94 } else if (previousPart === WebInspector.CSSShadowModel._Part.Offset Y) {
95 shadow._blurRadius = length;
96 shadow._format.push(WebInspector.CSSShadowModel._Part.BlurRadius);
97 } else if (previousPart === WebInspector.CSSShadowModel._Part.BlurRa dius) {
98 shadow._spreadRadius = length;
99 shadow._format.push(WebInspector.CSSShadowModel._Part.SpreadRadius );
229 } else { 100 } else {
230 if (!nextPartAllowed) 101 shadow._offsetX = length;
231 return []; 102 shadow._format.push(WebInspector.CSSShadowModel._Part.OffsetX);
232 nextPartAllowed = false;
233
234 if (result.regexIndex === 0) {
235 shadow._inset = true;
236 shadow._format.push(WebInspector.CSSShadowModel._Part.Inset) ;
237 } else if (result.regexIndex === 1) {
238 var color = WebInspector.Color.parse(result.value);
239 if (!color)
240 return [];
241 shadow._color = color;
242 shadow._format.push(WebInspector.CSSShadowModel._Part.Color) ;
243 } else if (result.regexIndex === 2) {
244 var length = WebInspector.CSSLength.parse(result.value);
245 if (!length)
246 return [];
247 var previousPart = shadow._format.length > 0 ? shadow._forma t[shadow._format.length - 1] : "";
248 if (previousPart === WebInspector.CSSShadowModel._Part.Offse tX) {
249 shadow._offsetY = length;
250 shadow._format.push(WebInspector.CSSShadowModel._Part.Of fsetY);
251 } else if (previousPart === WebInspector.CSSShadowModel._Par t.OffsetY) {
252 shadow._blurRadius = length;
253 shadow._format.push(WebInspector.CSSShadowModel._Part.Bl urRadius);
254 } else if (previousPart === WebInspector.CSSShadowModel._Par t.BlurRadius) {
255 shadow._spreadRadius = length;
256 shadow._format.push(WebInspector.CSSShadowModel._Part.Sp readRadius);
257 } else {
258 shadow._offsetX = length;
259 shadow._format.push(WebInspector.CSSShadowModel._Part.Of fsetX);
260 }
261 }
262 } 103 }
104 }
263 } 105 }
264 if (invalidCount(WebInspector.CSSShadowModel._Part.OffsetX, 1, 1) 106 }
265 || invalidCount(WebInspector.CSSShadowModel._Part.OffsetY, 1, 1) 107 if (invalidCount(WebInspector.CSSShadowModel._Part.OffsetX, 1, 1) ||
266 || invalidCount(WebInspector.CSSShadowModel._Part.Color, 0, 1) 108 invalidCount(WebInspector.CSSShadowModel._Part.OffsetY, 1, 1) ||
267 || invalidCount(WebInspector.CSSShadowModel._Part.BlurRadius, 0, 1) 109 invalidCount(WebInspector.CSSShadowModel._Part.Color, 0, 1) ||
268 || invalidCount(WebInspector.CSSShadowModel._Part.Inset, 0, isBo xShadow ? 1 : 0) 110 invalidCount(WebInspector.CSSShadowModel._Part.BlurRadius, 0, 1) ||
269 || invalidCount(WebInspector.CSSShadowModel._Part.SpreadRadius, 0, isBoxShadow ? 1 : 0)) 111 invalidCount(WebInspector.CSSShadowModel._Part.Inset, 0, isBoxShadow ? 1 : 0) ||
270 return []; 112 invalidCount(WebInspector.CSSShadowModel._Part.SpreadRadius, 0, isBoxS hadow ? 1 : 0))
271 shadows.push(shadow); 113 return [];
114 shadows.push(shadow);
272 } 115 }
273 return shadows; 116 return shadows;
274 117
275 /** 118 /**
276 * @param {string} part 119 * @param {string} part
277 * @param {number} min 120 * @param {number} min
278 * @param {number} max 121 * @param {number} max
279 * @return {boolean} 122 * @return {boolean}
280 */ 123 */
281 function invalidCount(part, min, max) 124 function invalidCount(part, min, max) {
282 { 125 var count = 0;
283 var count = 0; 126 for (var i = 0; i < shadow._format.length; i++) {
284 for (var i = 0; i < shadow._format.length; i++) { 127 if (shadow._format[i] === part)
285 if (shadow._format[i] === part) 128 count++;
286 count++; 129 }
287 } 130 return count < min || count > max;
288 return count < min || count > max; 131 }
289 } 132 }
133
134 /**
135 * @param {boolean} inset
136 */
137 setInset(inset) {
138 this._inset = inset;
139 if (this._format.indexOf(WebInspector.CSSShadowModel._Part.Inset) === -1)
140 this._format.unshift(WebInspector.CSSShadowModel._Part.Inset);
141 }
142
143 /**
144 * @param {!WebInspector.CSSLength} offsetX
145 */
146 setOffsetX(offsetX) {
147 this._offsetX = offsetX;
148 }
149
150 /**
151 * @param {!WebInspector.CSSLength} offsetY
152 */
153 setOffsetY(offsetY) {
154 this._offsetY = offsetY;
155 }
156
157 /**
158 * @param {!WebInspector.CSSLength} blurRadius
159 */
160 setBlurRadius(blurRadius) {
161 this._blurRadius = blurRadius;
162 if (this._format.indexOf(WebInspector.CSSShadowModel._Part.BlurRadius) === - 1) {
163 var yIndex = this._format.indexOf(WebInspector.CSSShadowModel._Part.Offset Y);
164 this._format.splice(yIndex + 1, 0, WebInspector.CSSShadowModel._Part.BlurR adius);
165 }
166 }
167
168 /**
169 * @param {!WebInspector.CSSLength} spreadRadius
170 */
171 setSpreadRadius(spreadRadius) {
172 this._spreadRadius = spreadRadius;
173 if (this._format.indexOf(WebInspector.CSSShadowModel._Part.SpreadRadius) === -1) {
174 this.setBlurRadius(this._blurRadius);
175 var blurIndex = this._format.indexOf(WebInspector.CSSShadowModel._Part.Blu rRadius);
176 this._format.splice(blurIndex + 1, 0, WebInspector.CSSShadowModel._Part.Sp readRadius);
177 }
178 }
179
180 /**
181 * @param {!WebInspector.Color} color
182 */
183 setColor(color) {
184 this._color = color;
185 if (this._format.indexOf(WebInspector.CSSShadowModel._Part.Color) === -1)
186 this._format.push(WebInspector.CSSShadowModel._Part.Color);
187 }
188
189 /**
190 * @return {boolean}
191 */
192 isBoxShadow() {
193 return this._isBoxShadow;
194 }
195
196 /**
197 * @return {boolean}
198 */
199 inset() {
200 return this._inset;
201 }
202
203 /**
204 * @return {!WebInspector.CSSLength}
205 */
206 offsetX() {
207 return this._offsetX;
208 }
209
210 /**
211 * @return {!WebInspector.CSSLength}
212 */
213 offsetY() {
214 return this._offsetY;
215 }
216
217 /**
218 * @return {!WebInspector.CSSLength}
219 */
220 blurRadius() {
221 return this._blurRadius;
222 }
223
224 /**
225 * @return {!WebInspector.CSSLength}
226 */
227 spreadRadius() {
228 return this._spreadRadius;
229 }
230
231 /**
232 * @return {!WebInspector.Color}
233 */
234 color() {
235 return this._color;
236 }
237
238 /**
239 * @return {string}
240 */
241 asCSSText() {
242 var parts = [];
243 for (var i = 0; i < this._format.length; i++) {
244 var part = this._format[i];
245 if (part === WebInspector.CSSShadowModel._Part.Inset && this._inset)
246 parts.push('inset');
247 else if (part === WebInspector.CSSShadowModel._Part.OffsetX)
248 parts.push(this._offsetX.asCSSText());
249 else if (part === WebInspector.CSSShadowModel._Part.OffsetY)
250 parts.push(this._offsetY.asCSSText());
251 else if (part === WebInspector.CSSShadowModel._Part.BlurRadius)
252 parts.push(this._blurRadius.asCSSText());
253 else if (part === WebInspector.CSSShadowModel._Part.SpreadRadius)
254 parts.push(this._spreadRadius.asCSSText());
255 else if (part === WebInspector.CSSShadowModel._Part.Color)
256 parts.push(this._color.asString(this._color.format()));
257 }
258 return parts.join(' ');
259 }
290 }; 260 };
291 261
292 /** 262 /**
293 * @constructor 263 * @enum {string}
294 * @param {number} amount
295 * @param {string} unit
296 */ 264 */
297 WebInspector.CSSLength = function(amount, unit) 265 WebInspector.CSSShadowModel._Part = {
298 { 266 Inset: 'I',
267 OffsetX: 'X',
268 OffsetY: 'Y',
269 BlurRadius: 'B',
270 SpreadRadius: 'S',
271 Color: 'C'
272 };
273
274
275 /**
276 * @unrestricted
277 */
278 WebInspector.CSSLength = class {
279 /**
280 * @param {number} amount
281 * @param {string} unit
282 */
283 constructor(amount, unit) {
299 this.amount = amount; 284 this.amount = amount;
300 this.unit = unit; 285 this.unit = unit;
301 }; 286 }
302 287
303 /** @type {!RegExp} */ 288 /**
304 WebInspector.CSSLength.Regex = (function() 289 * @param {string} text
305 { 290 * @return {?WebInspector.CSSLength}
306 var number = "([+-]?(?:[0-9]*[.])?[0-9]+(?:[eE][+-]?[0-9]+)?)"; 291 */
307 var unit = "(ch|cm|em|ex|in|mm|pc|pt|px|rem|vh|vmax|vmin|vw)"; 292 static parse(text) {
308 var zero = "[+-]?(?:0*[.])?0+(?:[eE][+-]?[0-9]+)?"; 293 var lengthRegex = new RegExp('^(?:' + WebInspector.CSSLength.Regex.source + ')$', 'i');
309 return new RegExp(number + unit + "|" + zero, "gi");
310 })();
311
312 /**
313 * @param {string} text
314 * @return {?WebInspector.CSSLength}
315 */
316 WebInspector.CSSLength.parse = function(text)
317 {
318 var lengthRegex = new RegExp("^(?:" + WebInspector.CSSLength.Regex.source + ")$", "i");
319 var match = text.match(lengthRegex); 294 var match = text.match(lengthRegex);
320 if (!match) 295 if (!match)
321 return null; 296 return null;
322 if (match.length > 2 && match[2]) 297 if (match.length > 2 && match[2])
323 return new WebInspector.CSSLength(parseFloat(match[1]), match[2]); 298 return new WebInspector.CSSLength(parseFloat(match[1]), match[2]);
324 return WebInspector.CSSLength.zero(); 299 return WebInspector.CSSLength.zero();
300 }
301
302 /**
303 * @return {!WebInspector.CSSLength}
304 */
305 static zero() {
306 return new WebInspector.CSSLength(0, '');
307 }
308
309 /**
310 * @return {string}
311 */
312 asCSSText() {
313 return this.amount + this.unit;
314 }
325 }; 315 };
326 316
327 /** 317 /** @type {!RegExp} */
328 * @return {!WebInspector.CSSLength} 318 WebInspector.CSSLength.Regex = (function() {
329 */ 319 var number = '([+-]?(?:[0-9]*[.])?[0-9]+(?:[eE][+-]?[0-9]+)?)';
330 WebInspector.CSSLength.zero = function() 320 var unit = '(ch|cm|em|ex|in|mm|pc|pt|px|rem|vh|vmax|vmin|vw)';
331 { 321 var zero = '[+-]?(?:0*[.])?0+(?:[eE][+-]?[0-9]+)?';
332 return new WebInspector.CSSLength(0, ""); 322 return new RegExp(number + unit + '|' + zero, 'gi');
333 }; 323 })();
334 324
335 WebInspector.CSSLength.prototype = { 325
336 /**
337 * @return {string}
338 */
339 asCSSText: function()
340 {
341 return this.amount + this.unit;
342 }
343 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698