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