Skip to content

Commit a4ffe56

Browse files
committed
fix: unicode selector bug
1 parent ce82066 commit a4ffe56

8 files changed

+93
-73
lines changed

.commithash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3fd95903b3b422f1a7dd7530dc938d1dd8ded836
1+
b9d89ec66d5c1768d6c5fc356878faee491591ba

dist/cssobj-plugin-localize.amd.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ function cssobj_plugin_selector_localize(prefix, localNames) {
6363
localNames = localNames || {};
6464

6565
var parser = function(str) {
66-
var store=[], ast=[], lastAst, name, match;
67-
for(var c, i=0, len=str.length; i<len; i++) {
66+
var store=[], ast=[], lastAst, match;
67+
for(var c, n, i=0, len=str.length; i<len; i++) {
6868
c=str[i];
6969
lastAst = ast[0];
7070
if(lastAst!=='\'' && lastAst!=='"') {
7171
// not in string
72-
if(c===':' && str.substr(i+1, 7)==='global(') {
72+
if(!lastAst && c===':' && str.substr(i+1, 7)==='global(') {
7373
ast.unshift('g');
7474
i+=7;
7575
continue
@@ -79,18 +79,20 @@ function cssobj_plugin_selector_localize(prefix, localNames) {
7979
if(c==')' && lastAst=='g') c='';
8080
ast.shift(c);
8181
}
82-
if(c==='.' && !lastAst) {
83-
if(str[i+1]=='!') {
84-
i++;
85-
} else {
86-
match = /[a-z0-9_-]+/i.exec(str.slice(i+1));
87-
if(match) {
88-
name = match[0];
89-
c += name in localNames
90-
? localNames[name]
91-
: prefix + name;
92-
i += name.length;
82+
if(!lastAst && c==='.') {
83+
i++;
84+
if(str[i]!=='!') {
85+
match = [];
86+
while( (n=str[i]) &&
87+
(n>='0'&&n<='9'||n>='a'&&n<='z'||n>='A'&&n<='Z'||n=='-'||n=='_'||n>='\u00a0'))
88+
match.push(str[i++]);
89+
if(match.length) {
90+
n = match.join('');
91+
c += n in localNames
92+
? localNames[n]
93+
: prefix + n;
9394
}
95+
i--;
9496
}
9597
}
9698
} else {

dist/cssobj-plugin-localize.cjs.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ function cssobj_plugin_selector_localize(prefix, localNames) {
6363
localNames = localNames || {};
6464

6565
var parser = function(str) {
66-
var store=[], ast=[], lastAst, name, match;
67-
for(var c, i=0, len=str.length; i<len; i++) {
66+
var store=[], ast=[], lastAst, match;
67+
for(var c, n, i=0, len=str.length; i<len; i++) {
6868
c=str[i];
6969
lastAst = ast[0];
7070
if(lastAst!=='\'' && lastAst!=='"') {
7171
// not in string
72-
if(c===':' && str.substr(i+1, 7)==='global(') {
72+
if(!lastAst && c===':' && str.substr(i+1, 7)==='global(') {
7373
ast.unshift('g');
7474
i+=7;
7575
continue
@@ -79,18 +79,20 @@ function cssobj_plugin_selector_localize(prefix, localNames) {
7979
if(c==')' && lastAst=='g') c='';
8080
ast.shift(c);
8181
}
82-
if(c==='.' && !lastAst) {
83-
if(str[i+1]=='!') {
84-
i++;
85-
} else {
86-
match = /[a-z0-9_-]+/i.exec(str.slice(i+1));
87-
if(match) {
88-
name = match[0];
89-
c += name in localNames
90-
? localNames[name]
91-
: prefix + name;
92-
i += name.length;
82+
if(!lastAst && c==='.') {
83+
i++;
84+
if(str[i]!=='!') {
85+
match = [];
86+
while( (n=str[i]) &&
87+
(n>='0'&&n<='9'||n>='a'&&n<='z'||n>='A'&&n<='Z'||n=='-'||n=='_'||n>='\u00a0'))
88+
match.push(str[i++]);
89+
if(match.length) {
90+
n = match.join('');
91+
c += n in localNames
92+
? localNames[n]
93+
: prefix + n;
9394
}
95+
i--;
9496
}
9597
}
9698
} else {

dist/cssobj-plugin-localize.es.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ function cssobj_plugin_selector_localize(prefix, localNames) {
6161
localNames = localNames || {};
6262

6363
var parser = function(str) {
64-
var store=[], ast=[], lastAst, name, match;
65-
for(var c, i=0, len=str.length; i<len; i++) {
64+
var store=[], ast=[], lastAst, match;
65+
for(var c, n, i=0, len=str.length; i<len; i++) {
6666
c=str[i];
6767
lastAst = ast[0];
6868
if(lastAst!=='\'' && lastAst!=='"') {
6969
// not in string
70-
if(c===':' && str.substr(i+1, 7)==='global(') {
70+
if(!lastAst && c===':' && str.substr(i+1, 7)==='global(') {
7171
ast.unshift('g');
7272
i+=7;
7373
continue
@@ -77,18 +77,20 @@ function cssobj_plugin_selector_localize(prefix, localNames) {
7777
if(c==')' && lastAst=='g') c='';
7878
ast.shift(c);
7979
}
80-
if(c==='.' && !lastAst) {
81-
if(str[i+1]=='!') {
82-
i++;
83-
} else {
84-
match = /[a-z0-9_-]+/i.exec(str.slice(i+1));
85-
if(match) {
86-
name = match[0];
87-
c += name in localNames
88-
? localNames[name]
89-
: prefix + name;
90-
i += name.length;
80+
if(!lastAst && c==='.') {
81+
i++;
82+
if(str[i]!=='!') {
83+
match = [];
84+
while( (n=str[i]) &&
85+
(n>='0'&&n<='9'||n>='a'&&n<='z'||n>='A'&&n<='Z'||n=='-'||n=='_'||n>='\u00a0'))
86+
match.push(str[i++]);
87+
if(match.length) {
88+
n = match.join('');
89+
c += n in localNames
90+
? localNames[n]
91+
: prefix + n;
9192
}
93+
i--;
9294
}
9395
}
9496
} else {

dist/cssobj-plugin-localize.iife.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ function cssobj_plugin_selector_localize(prefix, localNames) {
6464
localNames = localNames || {};
6565

6666
var parser = function(str) {
67-
var store=[], ast=[], lastAst, name, match;
68-
for(var c, i=0, len=str.length; i<len; i++) {
67+
var store=[], ast=[], lastAst, match;
68+
for(var c, n, i=0, len=str.length; i<len; i++) {
6969
c=str[i];
7070
lastAst = ast[0];
7171
if(lastAst!=='\'' && lastAst!=='"') {
7272
// not in string
73-
if(c===':' && str.substr(i+1, 7)==='global(') {
73+
if(!lastAst && c===':' && str.substr(i+1, 7)==='global(') {
7474
ast.unshift('g');
7575
i+=7;
7676
continue
@@ -80,18 +80,20 @@ function cssobj_plugin_selector_localize(prefix, localNames) {
8080
if(c==')' && lastAst=='g') c='';
8181
ast.shift(c);
8282
}
83-
if(c==='.' && !lastAst) {
84-
if(str[i+1]=='!') {
85-
i++;
86-
} else {
87-
match = /[a-z0-9_-]+/i.exec(str.slice(i+1));
88-
if(match) {
89-
name = match[0];
90-
c += name in localNames
91-
? localNames[name]
92-
: prefix + name;
93-
i += name.length;
83+
if(!lastAst && c==='.') {
84+
i++;
85+
if(str[i]!=='!') {
86+
match = [];
87+
while( (n=str[i]) &&
88+
(n>='0'&&n<='9'||n>='a'&&n<='z'||n>='A'&&n<='Z'||n=='-'||n=='_'||n>='\u00a0'))
89+
match.push(str[i++]);
90+
if(match.length) {
91+
n = match.join('');
92+
c += n in localNames
93+
? localNames[n]
94+
: prefix + n;
9495
}
96+
i--;
9597
}
9698
}
9799
} else {

dist/cssobj-plugin-localize.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cssobj-plugin-localize.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ export default function cssobj_plugin_selector_localize(prefix, localNames) {
99
localNames = localNames || {}
1010

1111
var parser = function(str) {
12-
var store=[], ast=[], lastAst, name, match
13-
for(var c, i=0, len=str.length; i<len; i++) {
12+
var store=[], ast=[], lastAst, match
13+
for(var c, n, i=0, len=str.length; i<len; i++) {
1414
c=str[i]
1515
lastAst = ast[0]
1616
if(lastAst!=='\'' && lastAst!=='"') {
1717
// not in string
18-
if(c===':' && str.substr(i+1, 7)==='global(') {
18+
if(!lastAst && c===':' && str.substr(i+1, 7)==='global(') {
1919
ast.unshift('g')
2020
i+=7
2121
continue
@@ -25,18 +25,20 @@ export default function cssobj_plugin_selector_localize(prefix, localNames) {
2525
if(c==')' && lastAst=='g') c=''
2626
ast.shift(c)
2727
}
28-
if(c==='.' && !lastAst) {
29-
if(str[i+1]=='!') {
30-
i++
31-
} else {
32-
match = /[a-z0-9_-]+/i.exec(str.slice(i+1))
33-
if(match) {
34-
name = match[0]
35-
c += name in localNames
36-
? localNames[name]
37-
: prefix + name
38-
i += name.length
28+
if(!lastAst && c==='.') {
29+
i++
30+
if(str[i]!=='!') {
31+
match = []
32+
while( (n=str[i]) &&
33+
(n>='0'&&n<='9'||n>='a'&&n<='z'||n>='A'&&n<='Z'||n=='-'||n=='_'||n>='\u00a0'))
34+
match.push(str[i++])
35+
if(match.length) {
36+
n = match.join('')
37+
c += n in localNames
38+
? localNames[n]
39+
: prefix + n
3940
}
41+
i--
4042
}
4143
}
4244
} else {

test/test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,22 @@ describe('Test plugin selector localize', function() {
136136
expect(result.mapSel('.nav.!item')).equal('._prefix_nav.item')
137137
})
138138

139-
it('should work right with string', function() {
139+
it('should work right with unicode selector', function() {
140+
var loc = lib('_prefix_').selector
141+
var result = {}
142+
143+
var ret = loc('.选择器1 .选择器2 .!选择器3', {}, result)
144+
expect(ret).equal('._prefix_选择器1 ._prefix_选择器2 .选择器3')
145+
})
146+
147+
it('should work right with string and bracket', function() {
140148
var loc = lib('_prefix_').selector
141149
var result = {}
142150

143151
var ret = loc('.nav a[title=".sdf].abc:global(.def)"]', {}, result)
144152
expect(ret).equal('._prefix_nav a[title=".sdf].abc:global(.def)"]')
153+
154+
expect(result.mapSel('.item[.!xyz.abc:global(.def)]')).equal('._prefix_item[.!xyz.abc:global(.def)]')
145155
})
146156

147157
it('should work right with ! symbol and localNames', function() {

0 commit comments

Comments
 (0)