Skip to content

Commit 4bf579e

Browse files
committed
add parser function to parse selector
1 parent c9c6930 commit 4bf579e

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

.commithash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
f6ce4b30435764758f6339085bc9932e798eb573
1+
c9c6930e819434481c8e49bb7f9a09909b11dd7b

src/cssobj-plugin-localize.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,32 @@ export default function cssobj_plugin_selector_localize(prefix, localNames) {
2323
: prefix + name)
2424
}
2525

26+
var parser = function(str) {
27+
var store=[], ast=[], lastAst
28+
for(var c, i=0, len=str.length; i<len; i++) {
29+
c=str[i]
30+
lastAst = ast[0]
31+
if(lastAst!=='\'' && lastAst!=='"') {
32+
// not in string
33+
if(c===':' && str.substr(i+1, 7)==='global(') {
34+
ast.unshift('g')
35+
i+=7
36+
continue
37+
}
38+
if(~ '[(\'"'.indexOf(c)) ast.unshift(c)
39+
if(~ '])'.indexOf(c)) {
40+
if(c==')' && lastAst=='g') c=''
41+
ast.shift(c)
42+
}
43+
if(c==='.' && !lastAst) c='.__'
44+
} else {
45+
if(c===lastAst) ast.shift()
46+
}
47+
store.push(c)
48+
}
49+
return store
50+
}
51+
2652
var mapSel = function(str, isClassList) {
2753
return str.replace(reClass, replacer)
2854
}

test/test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ describe('Test plugin selector localize', function() {
4747
expect(ret).match(/body ._prefix_nav ._prefix_item/)
4848

4949
expect(result.mapSel('.nav')).equal('._prefix_nav')
50+
expect(result.mapSel('.nav a[href=abc.html]')).equal('._prefix_nav a[href=abc.html]')
5051

5152
expect(result.mapSel('.item')).equal('._prefix_item')
5253

0 commit comments

Comments
 (0)