forked from cindyputri/GreenCodeEvaluator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
39 lines (31 loc) · 1.04 KB
/
Copy pathscript.js
File metadata and controls
39 lines (31 loc) · 1.04 KB
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
let input = document.querySelector('input');
let textarea = document.querySelector('textarea');
var colors = [];
input.addEventListener('change', () => {
let files = input.files;
if (files.length == 0) return;
const file = files[0];
let reader = new FileReader();
reader.onload = (e) => {
const file = e.target.result;
const lines = file.split(/\r\n|\n/);
textarea.value = lines.join('\n');
const selectorExp = /\s*([^{]+?)\s*\{\s*([^}]+?)\s*\}/g;
const colorExp = /(^|[\s;])color\s*:\s*([^;]+)/i;
let selectorMatch;
while (selectorMatch = selectorExp.exec(lines)) {
const [match, selectors, rules] = selectorMatch;
const colorMatch = colorExp.exec(rules);
if (colorMatch) {
colors = colors.concat(selectors.split(/\s*,\s*/g).map(s => ({
// selector: s,
color: colorMatch[2]
})));
}
}
textarea.value += colors.toString();
console.log(colors.toString());
};
reader.onerror = (e) => alert(e.target.error.name);
reader.readAsText(file);
});