-
Notifications
You must be signed in to change notification settings - Fork 84
/
proxy-cssom.js
359 lines (321 loc) · 9.18 KB
/
proxy-cssom.js
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { createAType, invertType, multiplyTypes, to, toSum } from "./numeric-values";
import { simplifyCalculation } from "./simplify-calculation";
export function installCSSOM() {
// Object for storing details associated with an object which are to be kept
// private. This approach allows the constructed objects to more closely
// resemble their native counterparts when inspected.
let privateDetails = new WeakMap();
function displayUnit(unit) {
switch(unit) {
case 'percent':
return '%';
case 'number':
return '';
default:
return unit.toLowerCase();
}
}
function toCssUnitValue(v) {
if (typeof v === 'number')
return new CSSUnitValue(v, 'number');
return v;
}
function toCssNumericArray(values) {
const result = [];
for (let i = 0; i < values.length; i++) {
result[i] = toCssUnitValue(values[i]);
}
return result;
}
class MathOperation {
constructor(values, operator, opt_name, opt_delimiter) {
privateDetails.set(this, {
values: toCssNumericArray(values),
operator: operator,
name: opt_name || operator,
delimiter: opt_delimiter || ', '
});
}
get operator() {
return privateDetails.get(this).operator;
}
get values() {
return privateDetails.get(this).values;
}
toString() {
const details = privateDetails.get(this);
return `${details.name}(${details.values.join(details.delimiter)})`;
}
}
/**
* Parse a CSSUnitValue from the passed string
* @param {string} str
* @return {CSSUnitValue}
*/
function parseCSSUnitValue(str) {
const UNIT_VALUE_REGEXP = /^(-?\d*[.]?\d+)(r?em|r?ex|r?cap|r?ch|r?ic|r?lh|[sld]?v(w|h|i|b|min|max)|cm|mm|Q|in|pt|pc|px|%)?$/;
const match = str.match(UNIT_VALUE_REGEXP);
if (match) {
let [_, v, unit] = match;
if (typeof unit === 'undefined') {
unit = 'number';
} else if (unit === '%') {
unit = 'percent';
}
return new CSSUnitValue(parseFloat(v), unit);
} else {
throw new SyntaxError(`Unsupported syntax ${str}`);
}
}
/**
* Parse the string as a CSSMathProduct
* @param {string} str
* @return {CSSMathProduct}
*/
function parseCSSMultiplication(str) {
let values = [];
const tokens = str.split(/(?<!\([^\)]*)([*])(?![^\(]*\))/);
values.push(parseCSSDivision(tokens.shift()));
while (tokens.length) {
tokens.shift(); // Consume operator '*'
values.push(parseCSSDivision(tokens.shift()));
}
return new CSSMathProduct(...values);
}
/**
* Parse the string as a CSSMathProduct
* @param {string} str
* @return {CSSMathProduct}
*/
function parseCSSDivision(str) {
let values = [];
const tokens = str.split(/(?<!\([^\)]*)([/])(?![^\(]*\))/);
values.push(parseCSSNumericValue(tokens.shift()));
while (tokens.length) {
tokens.shift(); // Consume operator '/'
values.push(new CSSMathInvert(parseCSSNumericValue(tokens.shift())));
}
return new CSSMathProduct(...values);
}
/**
* Parse the string as a CSSMathSum
* @param {string} str
* @return {CSSMathSum}
*/
function parseCSSMathSum(str) {
let values = [];
const tokens = str.split(/(?<!\([^\)]*)(\s[+-]\s)(?![^\(]*\))/);
values.push(parseCSSMultiplication(tokens.shift()));
while (tokens.length) {
let op = tokens.shift();
let val = tokens.shift();
if (op.trim() === '+') {
values.push(parseCSSMultiplication(val));
} else if (op.trim() === '-') {
values.push(new CSSMathNegate(parseCSSMultiplication(val)));
}
}
return new CSSMathSum(...values);
}
/**
* Parse math function form the passed string and return a matching CSSMathValue
* @param {string} str
* @return {CSSMathValue}
*/
function parseMathFunction(str) {
const MATH_VALUE_REGEXP = /^(calc|min|max)?\((.*)\)$/;
const match = str.match(MATH_VALUE_REGEXP);
if (match) {
let [_, operation = 'parens', value] = match;
switch (operation) {
case 'calc':
case 'parens':
return parseCSSMathSum(value);
case 'min':
return new CSSMathMin(...value.split(',').map(parseCSSNumericValue));
case 'max':
return new CSSMathMax(...value.split(',').map(parseCSSNumericValue));
}
} else {
throw new SyntaxError(`Unsupported syntax ${str}`);
}
}
/**
* A naive parsing function parsing the input string and returning a CSSNumericValue.
* It supports simple expressions as 'calc(10em + 10px)'
*
* @param {string} value
* @return {CSSNumericValue}
*/
function parseCSSNumericValue(value) {
value = value.trim();
if (value.match(/^[a-z(]/i)) {
return parseMathFunction(value);
} else {
return parseCSSUnitValue(value);
}
}
const cssOMTypes = {
'CSSNumericValue': class {
static parse(value) {
return simplifyCalculation(parseCSSNumericValue(value), {});
}
},
'CSSUnitValue': class {
constructor(value, unit) {
privateDetails.set(this, {
value: value,
unit: unit
});
}
get value() {
return privateDetails.get(this).value;
}
set value(value) {
privateDetails.get(this).value = value;
}
get unit() {
return privateDetails.get(this).unit;
}
to(unit) {
return to(this, unit)
}
toSum(...units) {
return toSum(this, ...units)
}
type() {
const details = privateDetails.get(this)
// The type of a CSSUnitValue is the result of creating a type from its unit internal slot.
return createAType(details.unit)
}
toString() {
const details = privateDetails.get(this);
return `${details.value}${displayUnit(details.unit)}`;
}
},
'CSSKeywordValue': class {
constructor(value) {
this.value = value;
}
toString() {
return this.value.toString();
}
},
'CSSMathSum': class extends MathOperation {
constructor(values) {
super(arguments, 'sum', 'calc', ' + ');
}
},
'CSSMathProduct': class extends MathOperation {
constructor(values) {
super(arguments, 'product', 'calc', ' * ');
}
toSum(...units) {
return toSum(this, ...units)
}
type() {
const values = privateDetails.get(this).values;
// The type is the result of multiplying the types of each of the items in its values internal slot.
return values.map(v => v.type()).reduce(multiplyTypes)
}
},
'CSSMathNegate': class extends MathOperation {
constructor(values) {
super([arguments[0]], 'negate', '-');
}
get value() {
return privateDetails.get(this).values[0];
}
},
'CSSMathInvert': class extends MathOperation {
constructor(values) {
super([1, arguments[0]], 'invert', 'calc', ' / ');
}
get value() {
return privateDetails.get(this).values[1];
}
type() {
const details = privateDetails.get(this)
// The type of a CSSUnitValue is the result of creating a type from its unit internal slot.
return invertType(details.values[1].type())
}
},
'CSSMathMax': class extends MathOperation {
constructor() {
super(arguments, 'max');
}
},
'CSSMathMin': class extends MathOperation {
constructor() {
super(arguments, 'min');
}
}
};
if (!window.CSS) {
if (!Reflect.defineProperty(window, 'CSS', { value: {} }))
throw Error(`Error installing CSSOM support`);
}
if (!window.CSSUnitValue) {
[
'number',
'percent',
// Length units
'em',
'ex',
'px',
'cm',
'mm',
'in',
'pt',
'pc', // Picas
'Q', // Quarter millimeter
'vw',
'vh',
'vmin',
'vmax',
'rems',
"ch",
// Angle units
'deg',
'rad',
'grad',
'turn',
// Time units
'ms',
's',
'Hz',
'kHz',
// Resolution
'dppx',
'dpi',
'dpcm',
// Other units
"fr"
].forEach((name) => {
const fn = (value) => {
return new CSSUnitValue(value, name);
};
if (!Reflect.defineProperty(CSS, name, { value: fn }))
throw Error(`Error installing CSS.${name}`);
});
}
for (let type in cssOMTypes) {
if (type in window)
continue;
if (!Reflect.defineProperty(window, type, { value: cssOMTypes[type] }))
throw Error(`Error installing CSSOM support for ${type}`);
}
}