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

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/ui/ShadowEditor.js

Issue 2230183004: DevTools: Add shadow-editor swatch/icon before box-shadows and text-shadows (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update model Created 4 years, 4 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
(Empty)
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
3 // found in the LICENSE file.
4
5 /**
6 * @constructor
7 */
8 WebInspector.ShadowEditor = function() {}
9
10 /**
11 * @constructor
12 * @param {boolean=} inset
lushnikov 2016/08/19 16:24:35 let's make non-optional
flandy 2016/08/19 21:50:39 Done.
13 * @param {!WebInspector.CSSLength=} x
14 * @param {!WebInspector.CSSLength=} y
15 * @param {!WebInspector.CSSLength=} blurRadius
16 * @param {!WebInspector.CSSLength=} spreadRadius
17 * @param {!WebInspector.Color=} color
18 */
19 WebInspector.CSSShadowModel = function(inset, x, y, blurRadius, spreadRadius, co lor)
lushnikov 2016/08/19 16:24:35 This model is a good patch by itself. Let's extra
flandy 2016/08/19 21:50:40 Done.
20 {
21 this._inset = !!inset;
22 this._x = x || new WebInspector.CSSLength(0, WebInspector.CSSLengthUnit.None );
lushnikov 2016/08/19 16:24:35 let's introduce a convenience method: WebInspector
flandy 2016/08/19 21:50:39 Done.
23 this._y = y || new WebInspector.CSSLength(0, WebInspector.CSSLengthUnit.None );
lushnikov 2016/08/19 16:24:35 let's name these and all related local variables a
flandy 2016/08/19 21:50:39 Done.
24 this._blurRadius = blurRadius || new WebInspector.CSSLength(0, WebInspector. CSSLengthUnit.None);
25 this._spreadRadius = spreadRadius || new WebInspector.CSSLength(0, WebInspec tor.CSSLengthUnit.None);
26 this._color = color;
27 this._format = "";
lushnikov 2016/08/19 16:24:35 there should be some default format - otherwise, t
flandy 2016/08/19 21:50:39 Done.
28 }
29
30 /**
31 * @enum {string}
32 */
33 WebInspector.CSSShadowModel.FormatParts = {
34 Inset: "I",
35 X: "X",
36 Y: "Y",
37 BlurRadius: "B",
38 SpreadRadius: "S",
39 Color: "C"
40 }
41
42 /**
43 * @param {string} text
44 * @return {?Array<!WebInspector.CSSShadowModel>}
lushnikov 2016/08/19 16:24:35 let's return !Array<..>, which would be empty in c
flandy 2016/08/19 21:50:39 Done.
45 */
46 WebInspector.CSSShadowModel.parseTextShadow = function(text)
47 {
48 return WebInspector.CSSShadowModel._parseShadow(text, false);
49 }
50
51 /**
52 * @param {string} text
53 * @return {?Array<!WebInspector.CSSShadowModel>}
54 */
55 WebInspector.CSSShadowModel.parseBoxShadow = function(text)
56 {
57 return WebInspector.CSSShadowModel._parseShadow(text, true);
58 }
59
60 WebInspector.CSSShadowModel.prototype = {
61 /**
62 * @param {boolean} inset
63 */
64 setInset: function(inset)
65 {
66 this._inset = inset;
67 },
68
69 /**
70 * @param {!WebInspector.CSSLength} x
71 * @param {!WebInspector.CSSLength} y
72 */
73 setOffset: function(x, y)
74 {
75 this._x = x;
76 this._y = y;
77 },
78
79 /**
80 * @param {!WebInspector.CSSLength} blurRadius
81 */
82 setBlurRadius: function(blurRadius)
83 {
84 this._blurRadius = blurRadius;
85 },
86
87 /**
88 * @param {!WebInspector.CSSLength} spreadRadius
89 */
90 setSpreadRadius: function(spreadRadius)
91 {
92 this._spreadRadius = spreadRadius;
93 },
94
95 /**
96 * @param {!WebInspector.Color} color
97 */
98 setColor: function(color)
99 {
100 this._color = color;
101 },
102
103 /**
104 * @param {string} format
105 */
106 setFormat: function(format)
107 {
108 this._format = format;
109 },
110
111 /**
112 * @return {!WebInspector.CSSLength}
113 */
114 xOffset: function()
115 {
116 return this._x;
117 },
118
119 /**
120 * @return {!WebInspector.CSSLength}
121 */
122 yOffset: function()
123 {
124 return this._y;
125 },
126
127 /**
128 * @return {string}
129 */
130 asCSSText: function()
131 {
132 var parts = [];
133 for (var i = 0; i < this._format.length; i++) {
134 var part = this._format.charAt(i);
135 if (part === WebInspector.CSSShadowModel.FormatParts.Inset && this._ inset)
136 parts.push("inset");
137 else if (part === WebInspector.CSSShadowModel.FormatParts.X)
138 parts.push(this._x.asCSSText());
139 else if (part === WebInspector.CSSShadowModel.FormatParts.Y)
140 parts.push(this._y.asCSSText());
141 else if (part === WebInspector.CSSShadowModel.FormatParts.BlurRadius )
142 parts.push(this._blurRadius.asCSSText());
143 else if (part === WebInspector.CSSShadowModel.FormatParts.SpreadRadi us)
144 parts.push(this._spreadRadius.asCSSText());
145 else if (part === WebInspector.CSSShadowModel.FormatParts.Color && t his._color)
146 parts.push(this._color.asString(this._color.format()));
147 }
148 return parts.join(" ");
149 }
150 }
151
152 /**
153 * @param {string} text
154 * @param {boolean} isBoxShadow
155 * @return {?Array<!WebInspector.CSSShadowModel>}
156 */
157 WebInspector.CSSShadowModel._parseShadow = function(text, isBoxShadow)
158 {
159 var shadowTexts = [];
160 // Split by commas that aren't inside of color values to get the individual shadow values.
161 var splits = WebInspector.TextUtils.splitStringByRegexes(text, [WebInspector .Color.Regex, /,/g]);
162 var currentIndex = 0;
163 for (var i = 0; i < splits.length; i++) {
164 if (splits[i].regexIndex === 1) {
165 var comma = splits[i];
166 shadowTexts.push(text.substring(currentIndex, comma.position));
167 currentIndex = comma.position + 1;
168 }
169 }
170 shadowTexts.push(text.substring(currentIndex, text.length));
171
172 var shadows = [];
173 for (var i = 0; i < shadowTexts.length; i++) {
174 var shadow = new WebInspector.CSSShadowModel();
175 var format = "";
176 var nextPartAllowed = true;
177 var regexes = [/inset/gi, WebInspector.Color.Regex, WebInspector.CSSLeng th.Regex];
178 var results = WebInspector.TextUtils.splitStringByRegexes(shadowTexts[i] , regexes);
179 for (var j = 0; j < results.length; j++) {
180 var result = results[j];
181 if (result.regexIndex === -1) {
182 // Don't allow anything other than inset, color, length values, and whitespace.
183 if (/\S/.test(result.value))
184 return null;
185 // All parts must be separated by whitespace.
186 nextPartAllowed = true;
187 } else {
188 if (!nextPartAllowed)
189 return null;
190 nextPartAllowed = false;
191
192 if (result.regexIndex === 0) {
193 shadow.setInset(true);
194 format += WebInspector.CSSShadowModel.FormatParts.Inset;
195 } else if (result.regexIndex === 1) {
196 var color = WebInspector.Color.parse(result.value);
197 if (!color)
198 return null;
199 shadow.setColor(color);
200 format += WebInspector.CSSShadowModel.FormatParts.Color;
201 } else if (result.regexIndex === 2) {
202 var length = WebInspector.CSSLength.parse(result.value);
203 if (!length)
204 return null;
205 var previousPart = format.length > 0 ? format.charAt(format. length - 1) : "";
206 if (previousPart === WebInspector.CSSShadowModel.FormatParts .X) {
207 shadow.setOffset(shadow.xOffset(), length);
208 format += WebInspector.CSSShadowModel.FormatParts.Y;
209 } else if (previousPart === WebInspector.CSSShadowModel.Form atParts.Y) {
210 shadow.setBlurRadius(length);
211 format += WebInspector.CSSShadowModel.FormatParts.BlurRa dius;
212 } else if (previousPart === WebInspector.CSSShadowModel.Form atParts.BlurRadius) {
213 shadow.setSpreadRadius(length);
214 format += WebInspector.CSSShadowModel.FormatParts.Spread Radius;
215 } else {
216 shadow.setOffset(length, shadow.yOffset());
217 format += WebInspector.CSSShadowModel.FormatParts.X;
218 }
219 }
220 }
221 }
222 if (invalidCount(WebInspector.CSSShadowModel.FormatParts.X, 1, 1)
223 || invalidCount(WebInspector.CSSShadowModel.FormatParts.Y, 1, 1)
224 || invalidCount(WebInspector.CSSShadowModel.FormatParts.Color, 0 , 1)
225 || invalidCount(WebInspector.CSSShadowModel.FormatParts.BlurRadi us, 0, 1)
226 || invalidCount(WebInspector.CSSShadowModel.FormatParts.Inset, 0 , isBoxShadow ? 1 : 0)
227 || invalidCount(WebInspector.CSSShadowModel.FormatParts.SpreadRa dius, 0, isBoxShadow ? 1 : 0))
228 return null;
229 shadow.setFormat(format);
230 shadows.push(shadow);
231 }
232 return shadows;
233
234 /**
235 * @param {string} character
236 * @param {number} min
237 * @param {number} max
238 * @return {boolean}
239 */
240 function invalidCount(character, min, max)
241 {
242 var count = 0;
243 for (var i = 0; i < format.length; i++) {
244 if (format.charAt(i) === character)
245 count++;
246 }
247 return count < min || count > max;
248 }
249 }
250
251 /**
252 * @constructor
253 * @param {number} amount
254 * @param {string} unit
lushnikov 2016/08/19 16:24:35 WebInspector.CSSLengthUnit
flandy 2016/08/19 21:50:39 Done.
255 */
256 WebInspector.CSSLength = function(amount, unit)
257 {
258 this.amount = amount;
259 this.unit = unit;
260 }
261
262 /** @type {!RegExp} */
263 WebInspector.CSSLength.Regex = /([+-]?(?:[0-9]*[.])?[0-9]+(?:[eE][+-]?[0-9]+)?)( [a-zA-Z]{2,4})|[+-]?(?:0*[.])?0+(?:[eE][+-]?[0-9]+)?/g;
lushnikov 2016/08/19 16:24:35 can we build this complex regex out of composing p
flandy 2016/08/19 21:50:39 Done.
264
265 /**
266 * @enum {string}
267 */
268 WebInspector.CSSLengthUnit = {
269 Ch: "ch",
270 Cm: "cm",
271 Em: "em",
272 Ex: "ex",
273 In: "in",
274 Mm: "mm",
275 Pc: "pc",
276 Pt: "pt",
277 Px: "px",
278 Rem: "rem",
279 Vh: "vh",
280 Vmax: "vmax",
281 Vmin: "vmin",
282 Vw: "vw",
283 None: ""
284 }
285
286 /**
287 * @param {string} text
288 * @return {?WebInspector.CSSLength}
289 */
290 WebInspector.CSSLength.parse = function(text)
291 {
292 var lengthRegex = new RegExp("^(" + WebInspector.CSSLength.Regex.source + ") $");
293 var match = text.match(lengthRegex) || [];
294 if (match.length > 3 && match[3]) {
295 if (validUnit(match[3]))
296 return new WebInspector.CSSLength(parseFloat(match[2]), match[3]);
297 } else if (match.length > 1) {
298 return new WebInspector.CSSLength(0, WebInspector.CSSLengthUnit.None);
299 }
300 return null;
301
302 /**
303 * @param {string} unit
304 * @return {boolean}
305 */
306 function validUnit(unit)
lushnikov 2016/08/19 16:24:35 this can go away if the CSSLengthUnit regex would
flandy 2016/08/19 21:50:40 Done.
307 {
308 unit = unit.toLowerCase();
309 var keys = Object.keys(WebInspector.CSSLengthUnit);
310 for (var i = 0; i < keys.length; i++) {
311 if (WebInspector.CSSLengthUnit[keys[i]] === unit)
312 return true;
313 }
314 return false;
315 }
316 }
317
318 WebInspector.CSSLength.prototype = {
319 /**
320 * @return {string}
321 */
322 asCSSText: function()
323 {
324 return this.amount + this.unit;
325 }
326 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698