Skip to content

Commit ce82066

Browse files
committed
refactor: replacer for sel with bug fixed
1 parent 4bf579e commit ce82066

9 files changed

+191
-86
lines changed

.commithash

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

dist/cssobj-plugin-localize.amd.js

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,33 +56,57 @@ function arrayKV (obj, k, v, reverse, unique) {
5656

5757
// cssobj plugin
5858

59-
var reClass = /:global\s*\(((?:\s*\.[A-Za-z0-9_-]+\s*)+)\)|(\.)([!A-Za-z0-9_-]+)/g;
60-
6159
function cssobj_plugin_selector_localize(prefix, localNames) {
6260

6361
prefix = prefix!=='' ? prefix || random() : '';
6462

6563
localNames = localNames || {};
6664

67-
var replacer = function (match, global, dot, name) {
68-
if (global) {
69-
return global
70-
}
71-
if (name[0] === '!') {
72-
return dot + name.substr(1)
65+
var parser = function(str) {
66+
var store=[], ast=[], lastAst, name, match;
67+
for(var c, i=0, len=str.length; i<len; i++) {
68+
c=str[i];
69+
lastAst = ast[0];
70+
if(lastAst!=='\'' && lastAst!=='"') {
71+
// not in string
72+
if(c===':' && str.substr(i+1, 7)==='global(') {
73+
ast.unshift('g');
74+
i+=7;
75+
continue
76+
}
77+
if(~ '[(\'"'.indexOf(c)) ast.unshift(c);
78+
if(~ '])'.indexOf(c)) {
79+
if(c==')' && lastAst=='g') c='';
80+
ast.shift(c);
81+
}
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;
93+
}
94+
}
95+
}
96+
} else {
97+
if(c===lastAst) ast.shift();
98+
}
99+
store.push(c);
73100
}
74-
75-
return dot + (name in localNames
76-
? localNames[name]
77-
: prefix + name)
101+
return store.join('')
78102
};
79103

80-
var mapSel = function(str, isClassList) {
81-
return str.replace(reClass, replacer)
104+
var mapSel = function(str) {
105+
return parser(str)
82106
};
83107

84108
var mapClass = function(str) {
85-
return mapSel((' '+str).replace(/\s+\.?/g, '.')).replace(/\./g, ' ')
109+
return mapSel(str.replace(/\s+\.?/g, '.').replace(/^([^:\s.])/i, '.$1')).replace(/\./g, ' ')
86110
};
87111

88112
return {

dist/cssobj-plugin-localize.cjs.js

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,33 +56,57 @@ function arrayKV (obj, k, v, reverse, unique) {
5656

5757
// cssobj plugin
5858

59-
var reClass = /:global\s*\(((?:\s*\.[A-Za-z0-9_-]+\s*)+)\)|(\.)([!A-Za-z0-9_-]+)/g;
60-
6159
function cssobj_plugin_selector_localize(prefix, localNames) {
6260

6361
prefix = prefix!=='' ? prefix || random() : '';
6462

6563
localNames = localNames || {};
6664

67-
var replacer = function (match, global, dot, name) {
68-
if (global) {
69-
return global
70-
}
71-
if (name[0] === '!') {
72-
return dot + name.substr(1)
65+
var parser = function(str) {
66+
var store=[], ast=[], lastAst, name, match;
67+
for(var c, i=0, len=str.length; i<len; i++) {
68+
c=str[i];
69+
lastAst = ast[0];
70+
if(lastAst!=='\'' && lastAst!=='"') {
71+
// not in string
72+
if(c===':' && str.substr(i+1, 7)==='global(') {
73+
ast.unshift('g');
74+
i+=7;
75+
continue
76+
}
77+
if(~ '[(\'"'.indexOf(c)) ast.unshift(c);
78+
if(~ '])'.indexOf(c)) {
79+
if(c==')' && lastAst=='g') c='';
80+
ast.shift(c);
81+
}
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;
93+
}
94+
}
95+
}
96+
} else {
97+
if(c===lastAst) ast.shift();
98+
}
99+
store.push(c);
73100
}
74-
75-
return dot + (name in localNames
76-
? localNames[name]
77-
: prefix + name)
101+
return store.join('')
78102
};
79103

80-
var mapSel = function(str, isClassList) {
81-
return str.replace(reClass, replacer)
104+
var mapSel = function(str) {
105+
return parser(str)
82106
};
83107

84108
var mapClass = function(str) {
85-
return mapSel((' '+str).replace(/\s+\.?/g, '.')).replace(/\./g, ' ')
109+
return mapSel(str.replace(/\s+\.?/g, '.').replace(/^([^:\s.])/i, '.$1')).replace(/\./g, ' ')
86110
};
87111

88112
return {

dist/cssobj-plugin-localize.es.js

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,33 +54,57 @@ function arrayKV (obj, k, v, reverse, unique) {
5454

5555
// cssobj plugin
5656

57-
var reClass = /:global\s*\(((?:\s*\.[A-Za-z0-9_-]+\s*)+)\)|(\.)([!A-Za-z0-9_-]+)/g;
58-
5957
function cssobj_plugin_selector_localize(prefix, localNames) {
6058

6159
prefix = prefix!=='' ? prefix || random() : '';
6260

6361
localNames = localNames || {};
6462

65-
var replacer = function (match, global, dot, name) {
66-
if (global) {
67-
return global
68-
}
69-
if (name[0] === '!') {
70-
return dot + name.substr(1)
63+
var parser = function(str) {
64+
var store=[], ast=[], lastAst, name, match;
65+
for(var c, i=0, len=str.length; i<len; i++) {
66+
c=str[i];
67+
lastAst = ast[0];
68+
if(lastAst!=='\'' && lastAst!=='"') {
69+
// not in string
70+
if(c===':' && str.substr(i+1, 7)==='global(') {
71+
ast.unshift('g');
72+
i+=7;
73+
continue
74+
}
75+
if(~ '[(\'"'.indexOf(c)) ast.unshift(c);
76+
if(~ '])'.indexOf(c)) {
77+
if(c==')' && lastAst=='g') c='';
78+
ast.shift(c);
79+
}
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;
91+
}
92+
}
93+
}
94+
} else {
95+
if(c===lastAst) ast.shift();
96+
}
97+
store.push(c);
7198
}
72-
73-
return dot + (name in localNames
74-
? localNames[name]
75-
: prefix + name)
99+
return store.join('')
76100
};
77101

78-
var mapSel = function(str, isClassList) {
79-
return str.replace(reClass, replacer)
102+
var mapSel = function(str) {
103+
return parser(str)
80104
};
81105

82106
var mapClass = function(str) {
83-
return mapSel((' '+str).replace(/\s+\.?/g, '.')).replace(/\./g, ' ')
107+
return mapSel(str.replace(/\s+\.?/g, '.').replace(/^([^:\s.])/i, '.$1')).replace(/\./g, ' ')
84108
};
85109

86110
return {

dist/cssobj-plugin-localize.iife.js

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,33 +57,57 @@ function arrayKV (obj, k, v, reverse, unique) {
5757

5858
// cssobj plugin
5959

60-
var reClass = /:global\s*\(((?:\s*\.[A-Za-z0-9_-]+\s*)+)\)|(\.)([!A-Za-z0-9_-]+)/g;
61-
6260
function cssobj_plugin_selector_localize(prefix, localNames) {
6361

6462
prefix = prefix!=='' ? prefix || random() : '';
6563

6664
localNames = localNames || {};
6765

68-
var replacer = function (match, global, dot, name) {
69-
if (global) {
70-
return global
71-
}
72-
if (name[0] === '!') {
73-
return dot + name.substr(1)
66+
var parser = function(str) {
67+
var store=[], ast=[], lastAst, name, match;
68+
for(var c, i=0, len=str.length; i<len; i++) {
69+
c=str[i];
70+
lastAst = ast[0];
71+
if(lastAst!=='\'' && lastAst!=='"') {
72+
// not in string
73+
if(c===':' && str.substr(i+1, 7)==='global(') {
74+
ast.unshift('g');
75+
i+=7;
76+
continue
77+
}
78+
if(~ '[(\'"'.indexOf(c)) ast.unshift(c);
79+
if(~ '])'.indexOf(c)) {
80+
if(c==')' && lastAst=='g') c='';
81+
ast.shift(c);
82+
}
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;
94+
}
95+
}
96+
}
97+
} else {
98+
if(c===lastAst) ast.shift();
99+
}
100+
store.push(c);
74101
}
75-
76-
return dot + (name in localNames
77-
? localNames[name]
78-
: prefix + name)
102+
return store.join('')
79103
};
80104

81-
var mapSel = function(str, isClassList) {
82-
return str.replace(reClass, replacer)
105+
var mapSel = function(str) {
106+
return parser(str)
83107
};
84108

85109
var mapClass = function(str) {
86-
return mapSel((' '+str).replace(/\s+\.?/g, '.')).replace(/\./g, ' ')
110+
return mapSel(str.replace(/\s+\.?/g, '.').replace(/^([^:\s.])/i, '.$1')).replace(/\./g, ' ')
87111
};
88112

89113
return {

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.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cssobj-plugin-localize",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "Localize class name for cssobj",
55
"main": "dist/cssobj-plugin-localize.cjs.js",
66
"jsnext:main": "dist/cssobj-plugin-localize.es.js",

src/cssobj-plugin-localize.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,14 @@
22

33
import {random} from '../../cssobj-helper/lib/cssobj-helper.js'
44

5-
var reClass = /:global\s*\(((?:\s*\.[A-Za-z0-9_-]+\s*)+)\)|(\.)([!A-Za-z0-9_-]+)/g
6-
75
export default function cssobj_plugin_selector_localize(prefix, localNames) {
86

97
prefix = prefix!=='' ? prefix || random() : ''
108

119
localNames = localNames || {}
1210

13-
var replacer = function (match, global, dot, name) {
14-
if (global) {
15-
return global
16-
}
17-
if (name[0] === '!') {
18-
return dot + name.substr(1)
19-
}
20-
21-
return dot + (name in localNames
22-
? localNames[name]
23-
: prefix + name)
24-
}
25-
2611
var parser = function(str) {
27-
var store=[], ast=[], lastAst
12+
var store=[], ast=[], lastAst, name, match
2813
for(var c, i=0, len=str.length; i<len; i++) {
2914
c=str[i]
3015
lastAst = ast[0]
@@ -40,21 +25,34 @@ export default function cssobj_plugin_selector_localize(prefix, localNames) {
4025
if(c==')' && lastAst=='g') c=''
4126
ast.shift(c)
4227
}
43-
if(c==='.' && !lastAst) c='.__'
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
39+
}
40+
}
41+
}
4442
} else {
4543
if(c===lastAst) ast.shift()
4644
}
4745
store.push(c)
4846
}
49-
return store
47+
return store.join('')
5048
}
5149

52-
var mapSel = function(str, isClassList) {
53-
return str.replace(reClass, replacer)
50+
var mapSel = function(str) {
51+
return parser(str)
5452
}
5553

5654
var mapClass = function(str) {
57-
return mapSel((' '+str).replace(/\s+\.?/g, '.')).replace(/\./g, ' ')
55+
return mapSel(str.replace(/\s+\.?/g, '.').replace(/^([^:\s.])/i, '.$1')).replace(/\./g, ' ')
5856
}
5957

6058
return {

0 commit comments

Comments
 (0)