This repository was archived by the owner on Jun 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmedia-ast-from-string.js
134 lines (105 loc) · 3.04 KB
/
media-ast-from-string.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
function parse(string, splitByAnd) {
const array = [];
let buffer = '';
let split = false;
let func = 0;
let i = -1;
while (++i < string.length) {
const char = string[i];
if (char === '(') {
func += 1;
} else if (char === ')') {
if (func > 0) {
func -= 1;
}
} else if (func === 0) {
if (splitByAnd && andRegExp.test(buffer + char)) {
split = true;
} else if (!splitByAnd && char === ',') {
split = true;
}
}
if (split) {
array.push(splitByAnd ? new MediaExpression(buffer + char) : new MediaQuery(buffer));
buffer = '';
split = false;
} else {
buffer += char
}
}
if (buffer !== '') {
array.push(splitByAnd ? new MediaExpression(buffer) : new MediaQuery(buffer));
}
return array;
}
class MediaQueryList {
constructor(string) {
this.nodes = parse(string);
}
invert() {
this.nodes.forEach(node => {
node.invert();
})
return this;
}
clone() {
return new MediaQueryList(String(this));
}
toString() {
return this.nodes.join(',');
}
}
class MediaQuery {
constructor(string) {
const [, before, media, after ] = string.match(spaceWrapRegExp);
const [, modifier = '', afterModifier = ' ', type = '', beforeAnd = '', and = '', beforeExpression = '', expression1 = '', expression2 = ''] = media.match(mediaRegExp) || [];
const raws = { before, after, afterModifier, originalModifier: modifier || '', beforeAnd, and, beforeExpression };
const nodes = parse(expression1 || expression2, true);
Object.assign(this, {
modifier,
type,
raws,
nodes
});
}
clone(overrides) {
const instance = new MediaQuery(String(this));
Object.assign(instance, overrides);
return instance;
}
invert() {
this.modifier = this.modifier ? '' : this.raws.originalModifier;
return this;
}
toString() {
const { raws } = this;
return `${raws.before}${this.modifier}${this.modifier ? `${raws.afterModifier}` : ''}${this.type}${raws.beforeAnd}${raws.and}${raws.beforeExpression}${this.nodes.join('')}${this.raws.after}`;
}
}
class MediaExpression {
constructor(string) {
const [, value, after = '', and = '', afterAnd = '' ] = string.match(andRegExp) || [null, string];
const raws = { after, and, afterAnd };
Object.assign(this, { value, raws });
}
clone(overrides) {
const instance = new MediaExpression(String(this));
Object.assign(instance, overrides);
return instance;
}
toString() {
const { raws } = this;
return `${this.value}${raws.after}${raws.and}${raws.afterAnd}`;
}
}
const modifierRE = '(not|only)';
const typeRE = '(all|print|screen|speech)';
const noExpressionRE = '([\\W\\w]*)';
const expressionRE = '([\\W\\w]+)';
const noSpaceRE = '(\\s*)';
const spaceRE = '(\\s+)';
const andRE = '(?:(\\s+)(and))';
const andRegExp = new RegExp(`^${expressionRE}(?:${andRE}${spaceRE})$`, 'i');
const spaceWrapRegExp = new RegExp(`^${noSpaceRE}${noExpressionRE}${noSpaceRE}$`);
const mediaRegExp = new RegExp(`^(?:${modifierRE}${spaceRE})?(?:${typeRE}(?:${andRE}${spaceRE}${expressionRE})?|${expressionRE})$`, 'i');
export default string => new MediaQueryList(string);