-
Notifications
You must be signed in to change notification settings - Fork 1
/
csparse.js
executable file
·37 lines (31 loc) · 1.15 KB
/
csparse.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
var cs = {};
cs.parse = function(cssString) {
var separateEachSelector = cssString.replace(/\n/g, '').split('}')
separateEachSelector.pop(); //Last element will be //'' or \n
var cssObj = {}
for (var i = 0; i < separateEachSelector.length; i += 1) {
var sepSelectorAndData = separateEachSelector[i].split('{');
var selector = sepSelectorAndData[0].trim();
var fields = sepSelectorAndData[1].trim()
var individualFields = fields.split(';');
individualFields.pop();
cssObj[selector] = {};
for (var j = 0; j < individualFields.length; j += 1) {
var sepPropAndValue = individualFields[j].split(':');
cssObj[selector][sepPropAndValue[0]] = sepPropAndValue[1];
}
}
return cssObj
}
cs.stringify = function(cssObject) {
var cssString = '';
for (var selector in cssObject) {
var mainObject = cssObject[selector]
cssString += selector + ' {\n';
for (var field in mainObject) {
field + ':' + mainObject[field] + '\n;';
}
cssString += '}\n';
}
return cssString;
}