Skip to content

Commit 4a43902

Browse files
committed
update CSSExpression.js
1 parent 8648369 commit 4a43902

File tree

4 files changed

+131
-79
lines changed

4 files changed

+131
-79
lines changed

lib/CSSValueExpression.js

Lines changed: 78 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ CSSOM.CSSValueExpression.prototype.parse = function() {
4343
STRING: 1,
4444
REGEXP: 2
4545
},
46-
state = STATES.LITERAL;
46+
state = STATES.LITERAL,
4747
string_sep = '',
4848
expression = '',
4949
error = '',
@@ -158,10 +158,10 @@ CSSOM.CSSValueExpression.prototype.parse = function() {
158158
* parse regexp in css expression
159159
*
160160
* @return {Object|false}
161-
* - idx:
162-
* - regExp:
163-
* or
164-
* false
161+
* - idx:
162+
* - regExp:
163+
* or
164+
* false
165165
*/
166166

167167
/*
@@ -204,83 +204,83 @@ all legal RegExp
204204
=/a/
205205
,/a/
206206
207-
delete /a/
208-
in /a/
207+
delete /a/
208+
in /a/
209209
instanceof /a/
210-
new /a/
211-
typeof /a/
212-
void /a/
210+
new /a/
211+
typeof /a/
212+
void /a/
213213
214214
*/
215215
function parseJSRexExp(token, idx) {
216-
var before = token.substring(0, idx).trimRight(),
217-
legalRegx = [
218-
/^$/,
219-
/\($/,
220-
/\[$/,
221-
/\!$/,
222-
/\+$/,
223-
/\-$/,
224-
/\*$/,
225-
/\/\s+/,
226-
/\%$/,
227-
/\=$/,
228-
/\>$/,
229-
/\<$/,
230-
/\&$/,
231-
/\|$/,
232-
/\^$/,
233-
/\~$/,
234-
/\?$/,
235-
/\,$/,
236-
/delete$/,
237-
/in$/,
238-
/instanceof$/,
239-
/new$/,
240-
/typeof$/,
241-
/void$/,
242-
];
243-
244-
var isLegal = legalRegx.some(function(reg) {
245-
return reg.test(before);
246-
});
247-
248-
if (!isLegal) {
249-
return false;
250-
} else {
251-
var startIdx = idx,
252-
regexp;
253-
254-
while(true) {
255-
var regEndIdx = token.indexOf('/', startIdx + 1);
256-
257-
if (regEndIdx === -1) {
258-
return false;
259-
} else {
260-
regexp = token.substring(idx + 1, regEndIdx);
261-
var matched = regexp.match(/\\+$/);
262-
if (!matched || matched[0] % 2 == 0) {
263-
regexp = '/' + regexp + '/';
264-
break;
265-
} else {
266-
startIdx = regEndIdx;
267-
}
268-
}
269-
}
270-
}
271-
272-
// validate
273-
// RegExp boundary(/) must be in the same line
274-
var nextNewLineIdx = token.indexOf('\n', idx + 1);
275-
if (nextNewLineIdx < regEndIdx) {
276-
return false;
277-
}
278-
279-
280-
return {
281-
idx: regEndIdx,
282-
regExp: regexp
283-
};
216+
var before = token.substring(0, idx).trimRight(),
217+
legalRegx = [
218+
/^$/,
219+
/\($/,
220+
/\[$/,
221+
/\!$/,
222+
/\+$/,
223+
/\-$/,
224+
/\*$/,
225+
/\/\s+/,
226+
/\%$/,
227+
/\=$/,
228+
/\>$/,
229+
/\<$/,
230+
/\&$/,
231+
/\|$/,
232+
/\^$/,
233+
/\~$/,
234+
/\?$/,
235+
/\,$/,
236+
/delete$/,
237+
/in$/,
238+
/instanceof$/,
239+
/new$/,
240+
/typeof$/,
241+
/void$/,
242+
];
243+
244+
var isLegal = legalRegx.some(function(reg) {
245+
return reg.test(before);
246+
});
247+
248+
if (!isLegal) {
249+
return false;
250+
} else {
251+
var startIdx = idx,
252+
regexp;
253+
254+
while(true) {
255+
var regEndIdx = token.indexOf('/', startIdx + 1);
256+
257+
if (regEndIdx === -1) {
258+
return false;
259+
} else {
260+
regexp = token.substring(idx + 1, regEndIdx);
261+
var matched = regexp.match(/\\+$/);
262+
if (!matched || matched[0] % 2 == 0) {
263+
regexp = '/' + regexp + '/';
264+
break;
265+
} else {
266+
startIdx = regEndIdx;
267+
}
268+
}
269+
}
270+
}
271+
272+
// validate
273+
// RegExp boundary(/) must be in the same line
274+
var nextNewLineIdx = token.indexOf('\n', idx + 1);
275+
if (nextNewLineIdx < regEndIdx) {
276+
return false;
277+
}
278+
279+
280+
return {
281+
idx: regEndIdx,
282+
regExp: regexp
283+
};
284284
}
285285

286286

lib/parse.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ CSSOM.parse = function parse(token) {
222222
if (state === 'value') {
223223
// ie css expression mode
224224
if (buffer.trim() == 'expression') {
225-
var info = new CSSOM.CSSValueExpression(token, i).parse();
225+
var info = (new CSSOM.CSSValueExpression(token, i)).parse();
226226

227227
if (info.error) {
228228
parseError(info.error);

spec/CSSValueExpression.spec.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
describe('CSSOM', function() {
2+
describe('CSSValueExpression', function() {
3+
4+
var END = '__EOL__';
5+
6+
7+
var cssExpressionValue = [
8+
"(function(hash){",
9+
" if (!hash.match(/#[0-9a-f]{3,6}/g)) {",
10+
" hash = hash.substr(1);",
11+
" if (!hash) {",
12+
" hash = '#ccc';",
13+
" }",
14+
" }",
15+
16+
" var n1 = 4/5;",
17+
18+
" // hello line comment",
19+
20+
" var n2 = 5/6;",
21+
22+
" var r1 = /hello ( /img;",
23+
24+
" // hello line comment",
25+
26+
" /* hello block comment */",
27+
28+
" return hash;",
29+
"}(location.hash))",
30+
31+
END
32+
33+
].join('\n');
34+
35+
36+
37+
given(cssExpressionValue, function(token) {
38+
var i = 0;
39+
40+
var info = (new CSSOM.CSSValueExpression(token, i)).parse();
41+
42+
expect(info.idx).toBeDefined();
43+
44+
var end = token.substr(info.idx + 1);
45+
end = end.trim();
46+
expect(end).toBe(END);
47+
});
48+
49+
50+
});
51+
});

spec/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<script src="CSSStyleRule.spec.js"></script>
2222
<script src="CSSStyleDeclaration.spec.js"></script>
2323
<script src="CSSStyleSheet.spec.js"></script>
24+
<script src="CSSValueExpression.spec.js"></script>
2425
<script>
2526
var htmlReporter = new jasmine.HtmlReporter();
2627
var jasmineEnv = jasmine.getEnv();

0 commit comments

Comments
 (0)