Skip to content

Commit c9c6930

Browse files
committed
test: add more :global test
1 parent f6ce4b3 commit c9c6930

6 files changed

+292
-101
lines changed

.commithash

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

dist/cssobj-plugin-localize.amd.js

Lines changed: 85 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,101 @@
11
define('cssobj_plugin_localize', function () { 'use strict';
22

3-
// random string, should used across all cssobj plugins
4-
var random = (function () {
5-
var count = 0
6-
return function () {
7-
count++
8-
return '_' + Math.floor(Math.random() * Math.pow(2, 32)).toString(36) + count + '_'
9-
}
10-
})()
3+
// helper functions for cssobj
114

12-
var reClass = /:global\s*\(((?:\s*\.[A-Za-z0-9_-]+\s*)+)\)|(\.)([!A-Za-z0-9_-]+)/g
5+
// check n is numeric, or string of numeric
136

14-
function cssobj_plugin_selector_localize(prefix, localNames) {
157

16-
prefix = prefix!=='' ? prefix || random() : ''
8+
function own(o, k) {
9+
return {}.hasOwnProperty.call(o, k)
10+
}
1711

18-
localNames = localNames || {}
12+
// set default option (not deeply)
1913

20-
var replacer = function (match, global, dot, name) {
21-
if (global) {
22-
return global
23-
}
24-
if (name[0] === '!') {
25-
return dot + name.substr(1)
26-
}
2714

28-
return dot + (name in localNames
29-
? localNames[name]
30-
: prefix + name)
31-
}
15+
// convert js prop into css prop (dashified)
3216

33-
var mapSel = function(str, isClassList) {
34-
return str.replace(reClass, replacer)
35-
}
3617

37-
var mapClass = function(str) {
38-
return mapSel((' '+str).replace(/\s+\.?/g, '.')).replace(/\./g, ' ')
18+
// capitalize str
19+
20+
21+
// repeat str for num times
22+
23+
24+
// don't use String.prototype.trim in cssobj, using below instead
25+
26+
27+
// random string, should used across all cssobj plugins
28+
var random = (function () {
29+
var count = 0;
30+
return function () {
31+
count++;
32+
return '_' + Math.floor(Math.random() * Math.pow(2, 32)).toString(36) + count + '_'
33+
}
34+
})();
35+
36+
// extend obj from source, if it's no key in obj, create one
37+
38+
39+
// ensure obj[k] as array, then push v into it
40+
function arrayKV (obj, k, v, reverse, unique) {
41+
obj[k] = k in obj ? [].concat(obj[k]) : [];
42+
if(unique && obj[k].indexOf(v)>-1) return
43+
reverse ? obj[k].unshift(v) : obj[k].push(v);
44+
}
45+
46+
// replace find in str, with rep function result
47+
48+
49+
// get parents array from node (when it's passed the test)
50+
51+
52+
// split selector etc. aware of css attributes
53+
54+
55+
// checking for valid css value
56+
57+
// cssobj plugin
58+
59+
var reClass = /:global\s*\(((?:\s*\.[A-Za-z0-9_-]+\s*)+)\)|(\.)([!A-Za-z0-9_-]+)/g;
60+
61+
function cssobj_plugin_selector_localize(prefix, localNames) {
62+
63+
prefix = prefix!=='' ? prefix || random() : '';
64+
65+
localNames = localNames || {};
66+
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)
3973
}
4074

41-
return {
42-
selector: function localizeName (sel, node, result) {
43-
// don't touch at rule's selText
44-
// it's copied from parent, which already localized
45-
if(node.at) return sel
46-
if(!result.mapSel) result.mapSel = mapSel, result.mapClass = mapClass
47-
return mapSel(sel)
48-
}
75+
return dot + (name in localNames
76+
? localNames[name]
77+
: prefix + name)
78+
};
79+
80+
var mapSel = function(str, isClassList) {
81+
return str.replace(reClass, replacer)
82+
};
83+
84+
var mapClass = function(str) {
85+
return mapSel((' '+str).replace(/\s+\.?/g, '.')).replace(/\./g, ' ')
86+
};
87+
88+
return {
89+
selector: function localizeName (sel, node, result) {
90+
// don't touch at rule's selText
91+
// it's copied from parent, which already localized
92+
if(node.at) return sel
93+
if(!result.mapSel) result.mapSel = mapSel, result.mapClass = mapClass;
94+
return mapSel(sel)
4995
}
5096
}
97+
}
5198

52-
return cssobj_plugin_selector_localize;
99+
return cssobj_plugin_selector_localize;
53100

54-
});
101+
});

dist/cssobj-plugin-localize.cjs.js

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,68 @@
11
'use strict';
22

3+
// helper functions for cssobj
4+
5+
// check n is numeric, or string of numeric
6+
7+
8+
function own(o, k) {
9+
return {}.hasOwnProperty.call(o, k)
10+
}
11+
12+
// set default option (not deeply)
13+
14+
15+
// convert js prop into css prop (dashified)
16+
17+
18+
// capitalize str
19+
20+
21+
// repeat str for num times
22+
23+
24+
// don't use String.prototype.trim in cssobj, using below instead
25+
26+
327
// random string, should used across all cssobj plugins
428
var random = (function () {
5-
var count = 0
29+
var count = 0;
630
return function () {
7-
count++
31+
count++;
832
return '_' + Math.floor(Math.random() * Math.pow(2, 32)).toString(36) + count + '_'
933
}
10-
})()
34+
})();
1135

12-
var reClass = /:global\s*\(((?:\s*\.[A-Za-z0-9_-]+\s*)+)\)|(\.)([!A-Za-z0-9_-]+)/g
36+
// extend obj from source, if it's no key in obj, create one
37+
38+
39+
// ensure obj[k] as array, then push v into it
40+
function arrayKV (obj, k, v, reverse, unique) {
41+
obj[k] = k in obj ? [].concat(obj[k]) : [];
42+
if(unique && obj[k].indexOf(v)>-1) return
43+
reverse ? obj[k].unshift(v) : obj[k].push(v);
44+
}
45+
46+
// replace find in str, with rep function result
47+
48+
49+
// get parents array from node (when it's passed the test)
50+
51+
52+
// split selector etc. aware of css attributes
53+
54+
55+
// checking for valid css value
56+
57+
// cssobj plugin
58+
59+
var reClass = /:global\s*\(((?:\s*\.[A-Za-z0-9_-]+\s*)+)\)|(\.)([!A-Za-z0-9_-]+)/g;
1360

1461
function cssobj_plugin_selector_localize(prefix, localNames) {
1562

16-
prefix = prefix!=='' ? prefix || random() : ''
63+
prefix = prefix!=='' ? prefix || random() : '';
1764

18-
localNames = localNames || {}
65+
localNames = localNames || {};
1966

2067
var replacer = function (match, global, dot, name) {
2168
if (global) {
@@ -28,25 +75,25 @@ function cssobj_plugin_selector_localize(prefix, localNames) {
2875
return dot + (name in localNames
2976
? localNames[name]
3077
: prefix + name)
31-
}
78+
};
3279

3380
var mapSel = function(str, isClassList) {
3481
return str.replace(reClass, replacer)
35-
}
82+
};
3683

3784
var mapClass = function(str) {
3885
return mapSel((' '+str).replace(/\s+\.?/g, '.')).replace(/\./g, ' ')
39-
}
86+
};
4087

4188
return {
4289
selector: function localizeName (sel, node, result) {
4390
// don't touch at rule's selText
4491
// it's copied from parent, which already localized
4592
if(node.at) return sel
46-
if(!result.mapSel) result.mapSel = mapSel, result.mapClass = mapClass
93+
if(!result.mapSel) result.mapSel = mapSel, result.mapClass = mapClass;
4794
return mapSel(sel)
4895
}
4996
}
5097
}
5198

52-
module.exports = cssobj_plugin_selector_localize;
99+
module.exports = cssobj_plugin_selector_localize;

dist/cssobj-plugin-localize.es.js

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,66 @@
1+
// helper functions for cssobj
2+
3+
// check n is numeric, or string of numeric
4+
5+
6+
function own(o, k) {
7+
return {}.hasOwnProperty.call(o, k)
8+
}
9+
10+
// set default option (not deeply)
11+
12+
13+
// convert js prop into css prop (dashified)
14+
15+
16+
// capitalize str
17+
18+
19+
// repeat str for num times
20+
21+
22+
// don't use String.prototype.trim in cssobj, using below instead
23+
24+
125
// random string, should used across all cssobj plugins
226
var random = (function () {
3-
var count = 0
27+
var count = 0;
428
return function () {
5-
count++
29+
count++;
630
return '_' + Math.floor(Math.random() * Math.pow(2, 32)).toString(36) + count + '_'
731
}
8-
})()
32+
})();
933

10-
var reClass = /:global\s*\(((?:\s*\.[A-Za-z0-9_-]+\s*)+)\)|(\.)([!A-Za-z0-9_-]+)/g
34+
// extend obj from source, if it's no key in obj, create one
35+
36+
37+
// ensure obj[k] as array, then push v into it
38+
function arrayKV (obj, k, v, reverse, unique) {
39+
obj[k] = k in obj ? [].concat(obj[k]) : [];
40+
if(unique && obj[k].indexOf(v)>-1) return
41+
reverse ? obj[k].unshift(v) : obj[k].push(v);
42+
}
43+
44+
// replace find in str, with rep function result
45+
46+
47+
// get parents array from node (when it's passed the test)
48+
49+
50+
// split selector etc. aware of css attributes
51+
52+
53+
// checking for valid css value
54+
55+
// cssobj plugin
56+
57+
var reClass = /:global\s*\(((?:\s*\.[A-Za-z0-9_-]+\s*)+)\)|(\.)([!A-Za-z0-9_-]+)/g;
1158

1259
function cssobj_plugin_selector_localize(prefix, localNames) {
1360

14-
prefix = prefix!=='' ? prefix || random() : ''
61+
prefix = prefix!=='' ? prefix || random() : '';
1562

16-
localNames = localNames || {}
63+
localNames = localNames || {};
1764

1865
var replacer = function (match, global, dot, name) {
1966
if (global) {
@@ -26,25 +73,25 @@ function cssobj_plugin_selector_localize(prefix, localNames) {
2673
return dot + (name in localNames
2774
? localNames[name]
2875
: prefix + name)
29-
}
76+
};
3077

3178
var mapSel = function(str, isClassList) {
3279
return str.replace(reClass, replacer)
33-
}
80+
};
3481

3582
var mapClass = function(str) {
3683
return mapSel((' '+str).replace(/\s+\.?/g, '.')).replace(/\./g, ' ')
37-
}
84+
};
3885

3986
return {
4087
selector: function localizeName (sel, node, result) {
4188
// don't touch at rule's selText
4289
// it's copied from parent, which already localized
4390
if(node.at) return sel
44-
if(!result.mapSel) result.mapSel = mapSel, result.mapClass = mapClass
91+
if(!result.mapSel) result.mapSel = mapSel, result.mapClass = mapClass;
4592
return mapSel(sel)
4693
}
4794
}
4895
}
4996

50-
export default cssobj_plugin_selector_localize;
97+
export default cssobj_plugin_selector_localize;

0 commit comments

Comments
 (0)