forked from subtleGradient/Sheet.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSheetParser.CSS.js
194 lines (155 loc) · 4.65 KB
/
SheetParser.CSS.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
---
name : SheetParser.CSS
authors : Thomas Aylott
copyright : © 2010 Thomas Aylott
license : MIT
provides : SheetParser.CSS
requires : combineRegExp
...
*/
;(function(exports){
/*<depend>*/
var UNDEF = {undefined:1}
if (!exports.SheetParser) exports.SheetParser = {}
/*<CommonJS>*/
var combineRegExp = (UNDEF[typeof module] || !module.exports)
? exports.combineRegExp
: require('./sg-regex-tools').combineRegExp
var SheetParser = exports.SheetParser
/*</CommonJS>*/
/*<debug>*/;if (UNDEF[typeof combineRegExp]) throw new Error('Missing required function: "combineRegExp"');/*</debug>*/
/*</depend>*/
var CSS = SheetParser.CSS = {version: '1.0.2 dev'}
CSS.trim = trim
function trim(str){
// http://blog.stevenlevithan.com/archives/faster-trim-javascript
var str = (''+str).replace(/^\s\s*/, ''),
ws = /\s/,
i = str.length;
while (ws.test(str.charAt(--i)));
return str.slice(0, i + 1);
}
CSS.camelCase = function(string){
return ('' + string).replace(camelCaseSearch, camelCaseReplace)
}
var camelCaseSearch = /-\D/g
function camelCaseReplace(match){
return match.charAt(1).toUpperCase()
}
CSS.parse = function(cssText){
var found
, rule
, rules = {length:0}
, keyIndex = -1
, regex = this.parser
, names = CSS.parser.names
, i,r,l
, ruleCount
rules.cssText = cssText = trim(cssText)
// strip comments
cssText = cssText.replace(CSS.comment, '');
regex.lastIndex = 0
while ((found = regex.exec(cssText))){
// avoid an infinite loop on zero-length keys
if (regex.lastIndex == found.index) ++ regex.lastIndex
// key:value
if (found[names._key]){
rules[rules.length ++] = found[names._key]
rules[found[names._key]] = found[names._value]
rules[CSS.camelCase(found[names._key])] = found[names._value]
continue
}
rules[rules.length++] = rule = {}
for (i = 0, l = names.length; i < l; ++i){
if (!(names[i-1] && found[i])) continue
rule[names[i-1]] = trim(found[i])
}
}
var atKey, atRule, atList, atI
for (i = 0, l = rules.length; i < l; ++i){
if (!rules[i]) continue
if (rules[i]._style_cssText){
rules[i].style = CSS.parse(rules[i]._style_cssText)
delete rules[i]._style_cssText
}
// _atKey/_atValue
if (atKey = rules[i]._atKey){
atKey = CSS.camelCase(atKey)
atRule = {length:0}
rules[i][atKey] = atRule
atRule["_source"] =
atRule[atKey + "Text"] = rules[i]._atValue
atList = ('' + rules[i]._atValue).split(/,\s*/)
for (atI = 0; atI < atList.length; ++atI){
atRule[atRule.length ++] = atList[atI]
}
rules[i].length = 1
rules[i][0] = atKey
delete rules[i]._atKey
delete rules[i]._atValue
}
if (rules[i].style)
for (ruleCount = -1, r = -1, rule; rule = rules[i].style[++r];){
if (typeof rule == 'string') continue
rules[i][r] = (rules[i].cssRules || (rules[i].cssRules = {}))[++ ruleCount] = rule
rules[i].cssRules.length = ruleCount + 1
rules[i].rules = rules[i].cssRules
}
}
return rules
}
var x = combineRegExp
var OR = '|'
;(CSS.at = x(/\s*@([-a-zA-Z0-9]+)\s+(([\w-]+)?[^;{]*)/))
.names=[ '_atKey', '_atValue', 'name']
CSS.atRule = x([CSS.at, ';'])
;(CSS.keyValue_key = x(/([-a-zA-Z0-9]+)/))
.names=[ '_key']
;(CSS.keyValue_value_end = x(/(?:;|(?=\})|$)/))
;(CSS.notString = x(/[^"']+/))
;(CSS.stringSingle = x(/"(?:[^"]|\\")*"/))
;(CSS.stringDouble = x(/'(?:[^']|\\')*'/))
;(CSS.string = x(['(?:',CSS.stringSingle ,OR, CSS.stringDouble,')']))
;(CSS.propertyValue = x([/[^;}]+/, CSS.keyValue_value_end]))
var rRound = "(?:[^()]|\\((?:[^()]|\\((?:[^()]|\\((?:[^()]|\\([^()]*\\))*\\))*\\))*\\))"
;(CSS.keyValue_value = x(
[
x(['((?:'
, CSS.stringSingle
, OR
, CSS.stringDouble
, OR
, "\\("+rRound+"*\\)"
, OR
, /[^;}()]/ // not a keyValue_value terminator
, ')*)'
])
, CSS.keyValue_value_end
])).names = ['_value']
;(CSS.keyValue = x([CSS.keyValue_key ,/\s*:\s*/, CSS.keyValue_value]))
;(CSS.comment = x(/\/\*\s*((?:[^*]|\*(?!\/))*)\s*\*\//))
.names=[ 'comment']
;(CSS.selector = x(['(',/\s*(\d+%)\s*/,OR,'(?:',/[^{}'"()]|\([^)]*\)|\[[^\]]*\]/,')+',')']))
.names=[ 'selectorText','keyText']
var rCurly = "(?:[^{}]|\\{(?:[^{}]|\\{(?:[^{}]|\\{(?:[^{}]|\\{[^{}]*\\})*\\})*\\})*\\})"
var rCurlyRound = "(?:[^{}()]+|\\{(?:[^{}()]+|\\{(?:[^{}()]+|\\{(?:[^{}()]+|\\{[^{}()]*\\})*\\})*\\})*\\})"
;(CSS.block = x("\\{\\s*((?:"+"\\("+rRound+"*\\)|"+rCurly+")*)\\s*\\}"))
.names=[ '_style_cssText']
CSS.selectorBlock = x([CSS.selector, CSS.block])
CSS.atBlock = x([CSS.at, CSS.block])
CSS.parser = x
(
[ x(CSS.comment)
, OR
, x(CSS.atBlock)
, OR
, x(CSS.atRule)
, OR
, x(CSS.selectorBlock)
, OR
, x(CSS.keyValue)
]
, 'cssText'
);
})(typeof exports != 'undefined' ? exports : this);