-
Notifications
You must be signed in to change notification settings - Fork 0
/
quack.js
153 lines (141 loc) · 6.17 KB
/
quack.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
//Start a map for already parsed lines of quack; ensures whenever a group
//is pushed to mapToCss it doesn't needlessly reparse/analyze already parsed lines
let checked = new Map();
class QuackParser {
constructor() {
this.patterns = [];
this.quackMap = new Map();
this.shortHand = new Map();
this.setPatterns();
this.setRules();
}
setPatterns() {
this.patterns.push(/^.+?\s*=\s*.+$/); //<property> = <value> (whitespace not included)
this.patterns.push(/\w+;;/); //<shorthand>;;
}
//Map shortened syntax to its JS equivalent
setRules() {
this.quackMap.set('bg', 'background');
this.quackMap.set('bgAttachment', 'backgroundAttachment');
this.quackMap.set('bgBlend', 'backgroundBlendMode');
this.quackMap.set('bgClip', 'backgroundClip');
this.quackMap.set('bgColor', 'backgroundColor');
this.quackMap.set('bgImage', 'backgroundImage');
this.quackMap.set('bgOrigin', 'backgroundOrigin');
this.quackMap.set('bgPosition', 'backgroundPosition');
this.quackMap.set('bgRepeat', 'backgroundRepeat');
this.quackMap.set('bgSize', 'backgroundSize');
this.quackMap.set('borderBtm', 'borderBottom');
this.quackMap.set('borderBtmColor', 'borderBottomColor');
this.quackMap.set('borderBtmLeftRadius', 'borderBottomLeftRadius');
this.quackMap.set('borderBtmRightRadius', 'borderBottomRightRadius');
this.quackMap.set('borderBtmStyle', 'borderBottomStyle');
this.quackMap.set('borderBtmWidth', 'borderBottomWidth');
this.quackMap.set('borderImg', 'borderImage');
this.quackMap.set('borderImgOutset', 'borderImageOutset');
this.quackMap.set('borderImgRepeat', 'borderImageRepeat');
this.quackMap.set('borderImgSlice', 'borderImageSlice');
this.quackMap.set('borderImgSource', 'borderImageSource');
this.quackMap.set('borderImgWidth', 'borderImageWidth');
this.quackMap.set('btm', 'bottom');
this.quackMap.set('listStyleImg', 'listStyleImage');
this.quackMap.set('marginBtm', 'marginBottom');
this.quackMap.set('paddingBtm', 'paddingBottom');
this.quackMap.set('zindex', 'zIndex');
//Map combinations off CSS to a shorthand 1 line version
this.shortHand.set('centerItems', [
'display,flex',
'justifyContent,center',
'alignItems,center'
]);
this.shortHand.set('fullSize', [
'width,100vw',
'height,100vh'
]);
this.shortHand.set('centered', [
'position,absolute',
'top,50%',
'left,50%',
'transform,translate(-50%, -50%)'
]);
}
//Match each line provided (other than comments or blank ones) against patterns
//Determine if its valid syntax
quack(inputString) {
const lines = inputString.split('\r\n');
let group = null; //Holds groups of quack statements
let startPattern = /^[.#]\w+:+$/;
lines.forEach(line => {
if (!line.startsWith('//') && line.length !== 0) {
//Start a new group if the line matches the selector rule property
//e.g., .class:
if (startPattern.test(line)) {
group = [line];
} else {
//Otherwise if it matches a property/value pattern, push it to the
//existing group
let matchFound = false;
this.patterns.forEach(pattern => {
if (pattern.test(line.trim()) && group !== null) {
matchFound = true;
group.push(line);
this.updateDOM(group);
}
});
}
}
});
}
updateDOM(group) {
//Add a new entry to the map for the element of the group
if (!checked.has(group[0])) {
checked.set(group[0], []);
}
let querySelector = group[0]; //Element idenfifier (e.g., .class/#id)
querySelector = querySelector.split(':');
querySelector = querySelector[0].trim();
const targetElement = document.querySelectorAll(querySelector);
//Parse the line of quack if it has not already been done so
for (let i = 1; i < group.length; i++) {
if (!checked.get(group[0]).includes(group[i])) {
if (!this.patterns[1].test(group[i])) {
let property = group[i].split('=');
let value = property[1].trim();
property = property[0].trim();
targetElement.forEach(element => {
this.mapToCss(element, property, value);
});
} else {
if (group[i] !== undefined) {
let shorthand = group[i].split(";;");
shorthand = shorthand[0];
if (this.shortHand.has(shorthand)) {
this.shortHand.get(shorthand).forEach(s => {
let property = s.split(",");
let value = property[1];
property = property[0];
targetElement.forEach(element => {
this.mapToCss(element, property, value);
});
})
}
}
}
checked.get(group[0]).push(group[i]); //Add the line so it doesn't get checked again
} else {
}
}
}
mapToCss(target, property, value) {
//True represents a shortened version of the JS syntax being used
if (this.quackMap.has(property)) {
let _css = this.quackMap.get(property);
target.style[_css] = value;
} else {
//If it doesn't, assume a JS syntax is attempted to be used and set
//that style
target.style[property] = value;
}
}
}
export default QuackParser;