-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcss-convertToArray.js
More file actions
149 lines (133 loc) · 5.28 KB
/
css-convertToArray.js
File metadata and controls
149 lines (133 loc) · 5.28 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
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
/*jslint browser: true, vars: true, plusplus:true */
/*global console */
/*
convertToArray.js
© 2014 Brian Weiser. All rights reserved.
*/
var convertToArray;
(function () {
var array;
/*
@variabe text the text to be searched within
@variable pos the starting position for the search
*/
function findClosingBracket(text, pos) {
var open, close, depth = 0;
while (true) {
open = text.indexOf('{', pos + 1);
close = text.indexOf('}', pos + 1);
if (open === -1 || close === -1) {
break;
} else if (open < close) {
depth++;
pos = close;
} else {
depth--;
}
if (depth <= 0) {
break;
}
}
return close; //closing barcket position
}
function extractMediaQuery(text, position) {
var media, inner = '';
var start = text.indexOf('{');
media = text.substring(0, start).trim();
var close = findClosingBracket(text, start);
inner = text.substring(start + 1, close).trim();
return {"media": media, innerCSSText: inner, beginning: position, 'start': start, 'end': close};
}
function seperateProperties(properties) {
var p;
var prop = properties.split(';');
var result = [];
for (p = 0; p < prop.length; p++) {
if (prop[p].length !== 0) {
var colon = prop[p].indexOf(':');
var property = prop[p].substring(0, colon);
var value = prop[p].substring(colon + 1, prop[p].length);
var important = false;
if (value.indexOf("!important") !== -1) {
value = (value.substring(0, value.indexOf("!important"))).trim();
important = true;
}
result.push({
property: property,
value: value,
isImportant: important
});
}
}
return result;
}
function extractInnerCSS(innerCSSText) {
var selectorArray = [], location, prevClose = 0;
location = innerCSSText.indexOf('{');
while (location !== -1) {
var close = findClosingBracket(innerCSSText, location);
var obj = {
selector: innerCSSText.substring(prevClose, location).trim(),
innerCSS: innerCSSText.substring(location + 1, close).trim()
};
obj.innerCSS = seperateProperties(obj.innerCSS);
prevClose = close + 1;
selectorArray.push(obj);
location = innerCSSText.indexOf('{', location + 1);
}
return selectorArray;
}
convertToArray = function (text) {
array = [];
var location = 0, count = 0, /*newLocation, */media, innerCSSText, closingBracket, openingBracket;
var string = String(text);
var prevLocation = 0, prevClose = 0;
location = string.toLowerCase().indexOf('@media');
while (location !== -1) {
// push everything between either the two media queries or from the start
var textBetweenMedias = string.substring((prevClose === 0) ? prevClose : prevClose + 1, location);
textBetweenMedias = textBetweenMedias.trim();
if (textBetweenMedias.length > 0) {
array.push({
innerCSSText : textBetweenMedias,
beginning: (prevClose === 0) ? prevClose : prevClose + 1,
end: (location - 1),
innerCSS: extractInnerCSS(textBetweenMedias)
});
}
openingBracket = string.indexOf('{', location);
closingBracket = findClosingBracket(string, openingBracket);
//adding the next media query
innerCSSText = string.substring((openingBracket === 0) ? openingBracket : openingBracket + 1, closingBracket);
innerCSSText = innerCSSText.trim();
if (innerCSSText.length > 0) {
media = {
"media": string.substring(location, openingBracket).trim(),
"innerCSSText": innerCSSText,
"beginning": location,
"openingBracket": openingBracket,
"end": closingBracket,
"innerCSS": extractInnerCSS(innerCSSText)
};
array.push(media);
}
prevLocation = location;
prevClose= closingBracket;
//keeps count of number of media queries
count++;
location = string.toLowerCase().indexOf('@media', location + 1);
}
//adds the last part of the css file
innerCSSText = string.substring(closingBracket + 1);
innerCSSText = innerCSSText.trim();
if (innerCSSText.length > 0) {
array.push({
"innerCSSText": innerCSSText,
"beginning": closingBracket + 1,
"end": string.length,
"innerCSS": extractInnerCSS(innerCSSText)
});
}
return array;
};
}());