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.ShadowEditor = function() {} | |
| 9 | |
| 10 /** | |
| 11 * @constructor | |
| 12 */ | |
| 13 WebInspector.ShadowEditor.Shadow = function(type) | |
|
lushnikov
2016/08/16 22:53:20
Let's make this a real css shadow model:
WebInspe
flandy
2016/08/18 22:00:41
Done. I've opted to use more options for the forma
| |
| 14 { | |
| 15 this.type = type; | |
| 16 this.order = []; | |
| 17 } | |
| 18 | |
| 19 /** | |
| 20 * @enum {string} | |
| 21 */ | |
| 22 WebInspector.ShadowEditor.Shadow.Parts = { | |
| 23 Inset: "inset", | |
| 24 Length: "length", | |
| 25 Color: "color" | |
| 26 }; | |
| 27 | |
| 28 /** | |
| 29 * @param {string} text | |
| 30 * @param {string} type | |
| 31 * @return {?Array<!WebInspector.ShadowEditor.Shadow>} | |
| 32 */ | |
| 33 WebInspector.ShadowEditor.Shadow.parse = function(text, type) | |
|
lushnikov
2016/08/16 22:53:20
WI.CSSShadowModel.parseBoxShadow = function(text)
flandy
2016/08/18 22:00:41
Done.
| |
| 34 { | |
| 35 var shadowTexts = []; | |
| 36 // Split by commas that aren't inside of color values to get the individual shadow values. | |
| 37 var splits = WebInspector.TextUtils.splitStringByRegexes(text, [WebInspector .Color.Regex, /,/g]); | |
| 38 var currentIndex = 0; | |
| 39 for (var i = 0; i < splits.length; i++) { | |
| 40 if (splits[i].regexIndex === 1) { | |
| 41 var comma = splits[i]; | |
| 42 shadowTexts.push(text.substring(currentIndex, comma.position)); | |
| 43 currentIndex = comma.position + 1; | |
| 44 } | |
| 45 } | |
| 46 shadowTexts.push(text.substring(currentIndex, text.length)); | |
| 47 | |
| 48 var shadows = []; | |
| 49 for (var i = 0; i < shadowTexts.length; i++) { | |
| 50 var shadow = new WebInspector.ShadowEditor.Shadow(type); | |
| 51 var regexes = [/inset/g, WebInspector.Color.Regex, WebInspector.ShadowEd itor.Length.Regex]; | |
| 52 var results = WebInspector.TextUtils.splitStringByRegexes(shadowTexts[i] , regexes); | |
| 53 for (var j = 0; j < results.length; j++) { | |
| 54 var result = results[j]; | |
| 55 if (result.regexIndex === 0) { | |
| 56 shadow.setInset(result.value); | |
| 57 } else if (result.regexIndex === 1) { | |
| 58 var color = WebInspector.Color.parse(result.value); | |
| 59 if (!color) | |
| 60 return null; | |
| 61 shadow.setColor(color); | |
| 62 } else if (result.regexIndex === 2) { | |
| 63 var length = WebInspector.ShadowEditor.Length.parse(result.value ); | |
| 64 if (!length) | |
| 65 return null; | |
| 66 shadow.addLength(length); | |
| 67 } | |
| 68 } | |
| 69 shadows.push(shadow); | |
| 70 } | |
| 71 return shadows; | |
| 72 } | |
| 73 | |
| 74 WebInspector.ShadowEditor.Shadow.prototype = { | |
| 75 /** | |
| 76 * @param {string} inset | |
| 77 */ | |
| 78 setInset: function(inset) | |
| 79 { | |
| 80 if (!this.inset) | |
| 81 this.order.push(WebInspector.ShadowEditor.Shadow.Parts.Inset); | |
| 82 this.inset = inset; | |
| 83 }, | |
| 84 | |
| 85 /** | |
| 86 * @param {!WebInspector.Color} color | |
| 87 */ | |
| 88 setColor: function(color) | |
| 89 { | |
| 90 if (!this.color) | |
| 91 this.order.push(WebInspector.ShadowEditor.Shadow.Parts.Color); | |
| 92 this.color = color; | |
| 93 }, | |
| 94 | |
| 95 /** | |
| 96 * @param {!WebInspector.ShadowEditor.Length} length | |
| 97 */ | |
| 98 addLength: function(length) | |
|
lushnikov
2016/08/16 22:53:20
this method should not belong to model; it's a par
flandy
2016/08/18 22:00:41
Done.
| |
| 99 { | |
| 100 if (!this.xOffset) { | |
| 101 this.order.push(WebInspector.ShadowEditor.Shadow.Parts.Length); | |
| 102 this.xOffset = length; | |
| 103 } else if (!this.yOffset) { | |
| 104 this.yOffset = length; | |
| 105 } else if (!this.blur) { | |
| 106 this.blur = length; | |
| 107 } else if (!this.spread) { | |
| 108 this.spread = length; | |
| 109 } | |
| 110 }, | |
| 111 | |
| 112 /** | |
| 113 * @return {!Array<!WebInspector.ShadowEditor.Length>} | |
| 114 */ | |
| 115 lengths: function() | |
| 116 { | |
| 117 var lengthValues = []; | |
| 118 if (this.xOffset) | |
| 119 lengthValues.push(this.xOffset); | |
| 120 if (this.yOffset) | |
| 121 lengthValues.push(this.yOffset); | |
| 122 if (this.blur) | |
| 123 lengthValues.push(this.blur); | |
| 124 if (this.spread) | |
| 125 lengthValues.push(this.spread); | |
| 126 return lengthValues; | |
| 127 }, | |
| 128 | |
| 129 /** | |
| 130 * @return {!Array<{type: string, text: string}>} | |
| 131 */ | |
| 132 textParts: function() | |
| 133 { | |
| 134 var parts = []; | |
| 135 for (var i = 0; i < this.order.length; i++) { | |
| 136 var currentPart = this.order[i]; | |
| 137 var textValue; | |
| 138 if (currentPart === WebInspector.ShadowEditor.Shadow.Parts.Inset) | |
| 139 textValue = this.inset; | |
| 140 else if (currentPart === WebInspector.ShadowEditor.Shadow.Parts.Leng th) | |
| 141 textValue = this.lengths().join(" "); | |
| 142 else if (currentPart === WebInspector.ShadowEditor.Shadow.Parts.Colo r) | |
| 143 textValue = this.color.asString(this.color.format()); | |
| 144 parts.push({ | |
| 145 type: currentPart, | |
| 146 text: textValue | |
| 147 }); | |
| 148 } | |
| 149 return parts; | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 | |
| 154 /** | |
| 155 * @constructor | |
| 156 * @param {number} amount | |
| 157 * @param {string} unit | |
| 158 */ | |
| 159 WebInspector.ShadowEditor.Length = function(amount, unit) | |
|
lushnikov
2016/08/16 22:53:20
WI.CSSLength = function(...)
flandy
2016/08/18 22:00:41
Done.
| |
| 160 { | |
| 161 this.amount = amount; | |
| 162 this.unit = unit; | |
| 163 } | |
| 164 | |
| 165 /** @type {!RegExp} */ | |
| 166 WebInspector.ShadowEditor.Length.Regex = /([0-9]+)([a-zA-Z]{2,4})|0/g; | |
|
lushnikov
2016/08/16 22:53:20
could it be negative/fractional?
flandy
2016/08/18 22:00:41
Yes, and also scientific notation. Fixed.
| |
| 167 | |
| 168 /** @type {!Set<string>} */ | |
| 169 WebInspector.ShadowEditor.Length._units = new Set([ | |
|
lushnikov
2016/08/16 22:53:20
WI.CSSLengthUnit = {
Ch: "ch",
Ex: "ex",
flandy
2016/08/18 22:00:41
Done.
| |
| 170 "ch", "cm", "em", "ex", "in", "mm", "pc", "pt", "px", "rem", "vh", "vmax", " vmin", "vw" | |
| 171 ]); | |
| 172 | |
| 173 /** | |
| 174 * @param {string} text | |
| 175 * @return {?WebInspector.ShadowEditor.Length} | |
| 176 */ | |
| 177 WebInspector.ShadowEditor.Length.parse = function(text) | |
| 178 { | |
| 179 var lengthRegex = new RegExp("^(" + WebInspector.ShadowEditor.Length.Regex.s ource + ")$"); | |
| 180 var match = text.match(lengthRegex) || []; | |
| 181 if (match.length > 3 && match[3]) { | |
| 182 if (WebInspector.ShadowEditor.Length._units.has(match[3].toLowerCase())) | |
| 183 return new WebInspector.ShadowEditor.Length(parseFloat(match[2]), ma tch[3]); | |
| 184 } else if (match.length > 1) { | |
| 185 return new WebInspector.ShadowEditor.Length(0, ""); | |
| 186 } | |
| 187 return null; | |
| 188 } | |
| 189 | |
| 190 WebInspector.ShadowEditor.Length.prototype = { | |
| 191 /** | |
| 192 * @override | |
| 193 * @return {string} | |
| 194 */ | |
| 195 toString: function() | |
|
lushnikov
2016/08/16 22:53:20
let's do toCSSText()
flandy
2016/08/18 22:00:41
Renamed to asCSSText() to keep consistent with WI.
| |
| 196 { | |
| 197 return this.amount + this.unit; | |
| 198 } | |
| 199 } | |
| OLD | NEW |