forked from flackr/scroll-timeline
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
80 lines (71 loc) · 2.18 KB
/
utils.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
const canonicalUnits = new Set(["px", "deg", "s", "hz", "dppx", "number", "fr"]);
export function isCanonical(unit) {
return canonicalUnits.has(unit.toLowerCase());
}
export function normalizeAxis(axis, computedStyle) {
if (['x','y'].includes(axis)) return axis;
if (!computedStyle) {
throw new Error('To determine the normalized axis the computedStyle of the source is required.');
}
const horizontalWritingMode = computedStyle.writingMode == 'horizontal-tb';
if (axis === "block") {
axis = horizontalWritingMode ? "y" : "x";
} else if (axis === "inline") {
axis = horizontalWritingMode ? "x" : "y";
} else {
throw new TypeError(`Invalid axis “${axis}”`);
}
return axis;
}
/**
* Split an input string into a list of individual component value strings,
* so that each can be handled as a keyword or parsed with `CSSNumericValue.parse()`;
*
* Examples:
* splitIntoComponentValues('cover'); // ['cover']
* splitIntoComponentValues('auto 0%'); // ['auto', '100%']
* splitIntoComponentValues('calc(0% + 50px) calc(100% - 50px)'); // ['calc(0% + 50px)', 'calc(100% - 50px)']
* splitIntoComponentValues('1px 2px').map(val => CSSNumericValue.parse(val)) // [new CSSUnitValue(1, 'px'), new CSSUnitValue(2, 'px')]
*
* @param {string} input
* @return {string[]}
*/
export function splitIntoComponentValues(input) {
const res = [];
let i = 0;
function consumeComponentValue() {
let level = 0;
const startIndex = i;
while (i < input.length) {
const nextChar = input.slice(i, i + 1);
if (/\s/.test(nextChar) && level === 0) {
break;
} else if (nextChar === '(') {
level += 1;
} else if (nextChar === ')') {
level -= 1;
if (level === 0) {
// Consume the next character and break
i++;
break;
}
}
i++;
}
return input.slice(startIndex, i);
}
function consumeWhitespace() {
while (/\s/.test(input.slice(i, i + 1))) {
i++;
}
}
while(i < input.length) {
const nextChar = input.slice(i, i + 1);
if (/\s/.test(nextChar)) {
consumeWhitespace();
} else {
res.push(consumeComponentValue());
}
}
return res;
}