1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
/*
* Copyright (C) 2014 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
WebInspector.Gradient = class Gradient
{
constructor(type, stops)
{
this.type = type;
this.stops = stops;
}
// Static
static fromString(cssString)
{
var type;
var openingParenthesisIndex = cssString.indexOf("(");
var typeString = cssString.substring(0, openingParenthesisIndex);
if (typeString.indexOf(WebInspector.Gradient.Types.Linear) !== -1)
type = WebInspector.Gradient.Types.Linear;
else if (typeString.indexOf(WebInspector.Gradient.Types.Radial) !== -1)
type = WebInspector.Gradient.Types.Radial;
else
return null;
var components = [];
var currentParams = [];
var currentParam = "";
var openParentheses = 0;
var ch = openingParenthesisIndex + 1;
var c = null;
while (c = cssString[ch]) {
if (c === "(")
openParentheses++;
if (c === ")")
openParentheses--;
var isComma = c === ",";
var isSpace = /\s/.test(c);
if (openParentheses === 0) {
if (isSpace) {
if (currentParam !== "")
currentParams.push(currentParam);
currentParam = "";
} else if (isComma) {
currentParams.push(currentParam);
components.push(currentParams);
currentParams = [];
currentParam = "";
}
}
if (openParentheses === -1) {
currentParams.push(currentParam);
components.push(currentParams);
break;
}
if (openParentheses > 0 || (!isComma && !isSpace))
currentParam += c;
ch++;
}
var gradient;
if (type === WebInspector.Gradient.Types.Linear)
gradient = WebInspector.LinearGradient.fromComponents(components);
else
gradient = WebInspector.RadialGradient.fromComponents(components);
if (gradient)
gradient.repeats = typeString.startsWith("repeating");
return gradient;
}
static stopsWithComponents(components)
{
// FIXME: handle lengths.
var stops = components.map(function(component) {
while (component.length) {
var color = WebInspector.Color.fromString(component.shift());
if (!color)
continue;
var stop = {color};
if (component.length && component[0].substr(-1) === "%")
stop.offset = parseFloat(component.shift()) / 100;
return stop;
}
});
if (!stops.length)
return null;
for (var i = 0, count = stops.length; i < count; ++i) {
var stop = stops[i];
// If one of the stops failed to parse, then this is not a valid
// set of components for a gradient. So the whole thing is invalid.
if (!stop)
return null;
if (!stop.offset)
stop.offset = i / (count - 1);
}
return stops;
}
// Public
stringFromStops(stops)
{
var count = stops.length - 1;
return stops.map(function(stop, index) {
var str = stop.color;
if (stop.offset !== index / count)
str += " " + Math.round(stop.offset * 10000) / 100 + "%";
return str;
}).join(", ");
}
// Public
copy()
{
// Implemented by subclasses.
}
toString()
{
// Implemented by subclasses.
}
};
WebInspector.Gradient.Types = {
Linear: "linear-gradient",
Radial: "radial-gradient"
};
WebInspector.LinearGradient = class LinearGradient extends WebInspector.Gradient
{
constructor(angle, stops)
{
super(WebInspector.Gradient.Types.Linear, stops);
this.angle = angle;
}
// Static
static fromComponents(components)
{
var angle = 180;
if (components[0].length === 1 && components[0][0].substr(-3) === "deg") {
angle = (parseFloat(components[0][0]) % 360 + 360) % 360;
components.shift();
} else if (components[0][0] === "to") {
components[0].shift();
switch (components[0].sort().join(" ")) {
case "top":
angle = 0;
break;
case "right top":
angle = 45;
break;
case "right":
angle = 90;
break;
case "bottom right":
angle = 135;
break;
case "bottom":
angle = 180;
break;
case "bottom left":
angle = 225;
break;
case "left":
angle = 270;
break;
case "left top":
angle = 315;
break;
default:
return null;
}
components.shift();
} else if (components[0].length !== 1 && !WebInspector.Color.fromString(components[0][0])) {
// If the first component is not a color, then we're dealing with a
// legacy linear gradient format that we don't support.
return null;
}
var stops = WebInspector.Gradient.stopsWithComponents(components);
if (!stops)
return null;
return new WebInspector.LinearGradient(angle, stops);
}
// Public
copy()
{
return new WebInspector.LinearGradient(this.angle, this.stops.concat());
}
toString()
{
var str = "";
if (this.angle === 0)
str += "to top";
else if (this.angle === 45)
str += "to top right";
else if (this.angle === 90)
str += "to right";
else if (this.angle === 135)
str += "to bottom right";
else if (this.angle === 225)
str += "to bottom left";
else if (this.angle === 270)
str += "to left";
else if (this.angle === 315)
str += "to top left";
else if (this.angle !== 180)
str += this.angle + "deg";
if (str !== "")
str += ", ";
str += this.stringFromStops(this.stops);
return (this.repeats ? "repeating-" : "") + this.type + "(" + str + ")";
}
};
WebInspector.RadialGradient = class RadialGradient extends WebInspector.Gradient
{
constructor(sizing, stops)
{
super(WebInspector.Gradient.Types.Radial, stops);
this.sizing = sizing;
}
// Static
static fromComponents(components)
{
var sizing = !WebInspector.Color.fromString(components[0].join(" ")) ? components.shift().join(" ") : "";
var stops = WebInspector.Gradient.stopsWithComponents(components);
if (!stops)
return null;
return new WebInspector.RadialGradient(sizing, stops);
}
// Public
copy()
{
return new WebInspector.RadialGradient(this.sizing, this.stops.concat());
}
toString()
{
var str = this.sizing;
if (str !== "")
str += ", ";
str += this.stringFromStops(this.stops);
return (this.repeats ? "repeating-" : "") + this.type + "(" + str + ")";
}
};
|