postcss-normalize-unicode 5.0.2 → 5.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -8
- package/src/index.js +132 -0
- package/types/index.d.ts +9 -0
- package/dist/index.js +0 -114
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postcss-normalize-unicode",
|
|
3
|
-
"version": "5.0
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"description": "Normalize unicode-range descriptors, and can convert to wildcard ranges.",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"types": "types/index.d.ts",
|
|
6
7
|
"files": [
|
|
7
|
-
"
|
|
8
|
-
"LICENSE-MIT"
|
|
8
|
+
"src",
|
|
9
|
+
"LICENSE-MIT",
|
|
10
|
+
"types"
|
|
9
11
|
],
|
|
10
12
|
"keywords": [
|
|
11
13
|
"css",
|
|
@@ -36,9 +38,5 @@
|
|
|
36
38
|
"peerDependencies": {
|
|
37
39
|
"postcss": "^8.2.15"
|
|
38
40
|
},
|
|
39
|
-
"scripts": {
|
|
40
|
-
"prebuild": "rimraf dist",
|
|
41
|
-
"build": "babel src --config-file ../../babel.config.json --out-dir dist --ignore \"**/__tests__/\""
|
|
42
|
-
},
|
|
43
41
|
"readme": "# [postcss][postcss]-normalize-unicode\n\n> Normalize unicode with PostCSS.\n\n## Install\n\nWith [npm](https://npmjs.org/package/postcss-normalize-unicode) do:\n\n```\nnpm install postcss-normalize-unicode --save\n```\n\n## Example\n\n### Input\n\n```css\n@font-face{\n font-family: test;\n unicode-range: u+2b00-2bff\n}\n```\n\n### Output\n\n```css\n@font-face{\n font-family: test;\n unicode-range: u+2b??\n}\n``` \n\n## Usage\n\nSee the [PostCSS documentation](https://github.com/postcss/postcss#usage) for\nexamples for your environment.\n\n## Contributors\n\nSee [CONTRIBUTORS.md](https://github.com/cssnano/cssnano/blob/master/CONTRIBUTORS.md).\n\n## License\n\nMIT © [Ben Briggs](http://beneb.info)\n\n[postcss]: https://github.com/postcss/postcss\n"
|
|
44
42
|
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const browserslist = require('browserslist');
|
|
3
|
+
const valueParser = require('postcss-value-parser');
|
|
4
|
+
|
|
5
|
+
const regexLowerCaseUPrefix = /^u(?=\+)/;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @param {string} range
|
|
9
|
+
* @return {string}
|
|
10
|
+
*/
|
|
11
|
+
function unicode(range) {
|
|
12
|
+
const values = range.slice(2).split('-');
|
|
13
|
+
|
|
14
|
+
if (values.length < 2) {
|
|
15
|
+
return range;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const left = values[0].split('');
|
|
19
|
+
const right = values[1].split('');
|
|
20
|
+
|
|
21
|
+
if (left.length !== right.length) {
|
|
22
|
+
return range;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const merged = mergeRangeBounds(left, right);
|
|
26
|
+
|
|
27
|
+
if (merged) {
|
|
28
|
+
return merged;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return range;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* @param {string[]} left
|
|
35
|
+
* @param {string[]} right
|
|
36
|
+
* @return {false|string}
|
|
37
|
+
*/
|
|
38
|
+
function mergeRangeBounds(left, right) {
|
|
39
|
+
let questionCounter = 0;
|
|
40
|
+
let group = 'u+';
|
|
41
|
+
for (const [index, value] of left.entries()) {
|
|
42
|
+
if (value === right[index] && questionCounter === 0) {
|
|
43
|
+
group = group + value;
|
|
44
|
+
} else if (value === '0' && right[index] === 'f') {
|
|
45
|
+
questionCounter++;
|
|
46
|
+
group = group + '?';
|
|
47
|
+
} else {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
// The maximum number of wildcard characters (?) for ranges is 5.
|
|
52
|
+
if (questionCounter < 6) {
|
|
53
|
+
return group;
|
|
54
|
+
} else {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* IE and Edge before 16 version ignore the unicode-range if the 'U' is lowercase
|
|
61
|
+
*
|
|
62
|
+
* https://caniuse.com/#search=unicode-range
|
|
63
|
+
*
|
|
64
|
+
* @param {string} browser
|
|
65
|
+
* @return {boolean}
|
|
66
|
+
*/
|
|
67
|
+
function hasLowerCaseUPrefixBug(browser) {
|
|
68
|
+
return browserslist('ie <=11, edge <= 15').includes(browser);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @param {string} value
|
|
73
|
+
* @return {string}
|
|
74
|
+
*/
|
|
75
|
+
function transform(value, isLegacy = false) {
|
|
76
|
+
return valueParser(value)
|
|
77
|
+
.walk((child) => {
|
|
78
|
+
if (child.type === 'unicode-range') {
|
|
79
|
+
const transformed = unicode(child.value.toLowerCase());
|
|
80
|
+
|
|
81
|
+
child.value = isLegacy
|
|
82
|
+
? transformed.replace(regexLowerCaseUPrefix, 'U')
|
|
83
|
+
: transformed;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return false;
|
|
87
|
+
})
|
|
88
|
+
.toString();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* @type {import('postcss').PluginCreator<void>}
|
|
93
|
+
* @return {import('postcss').Plugin}
|
|
94
|
+
*/
|
|
95
|
+
function pluginCreator() {
|
|
96
|
+
return {
|
|
97
|
+
postcssPlugin: 'postcss-normalize-unicode',
|
|
98
|
+
/** @param {import('postcss').Result & {opts: browserslist.Options}} result*/
|
|
99
|
+
prepare(result) {
|
|
100
|
+
const cache = new Map();
|
|
101
|
+
const resultOpts = result.opts || {};
|
|
102
|
+
const browsers = browserslist(null, {
|
|
103
|
+
stats: resultOpts.stats,
|
|
104
|
+
path: __dirname,
|
|
105
|
+
env: resultOpts.env,
|
|
106
|
+
});
|
|
107
|
+
const isLegacy = browsers.some(hasLowerCaseUPrefixBug);
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
OnceExit(css) {
|
|
111
|
+
css.walkDecls(/^unicode-range$/i, (decl) => {
|
|
112
|
+
const value = decl.value;
|
|
113
|
+
|
|
114
|
+
if (cache.has(value)) {
|
|
115
|
+
decl.value = cache.get(value);
|
|
116
|
+
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const newValue = transform(value, isLegacy);
|
|
121
|
+
|
|
122
|
+
decl.value = newValue;
|
|
123
|
+
cache.set(value, newValue);
|
|
124
|
+
});
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
pluginCreator.postcss = true;
|
|
132
|
+
module.exports = pluginCreator;
|
package/types/index.d.ts
ADDED
package/dist/index.js
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
var _browserslist = _interopRequireDefault(require("browserslist"));
|
|
9
|
-
|
|
10
|
-
var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
|
|
11
|
-
|
|
12
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
|
-
|
|
14
|
-
const regexLowerCaseUPrefix = /^u(?=\+)/;
|
|
15
|
-
|
|
16
|
-
function unicode(range) {
|
|
17
|
-
const values = range.slice(2).split('-');
|
|
18
|
-
|
|
19
|
-
if (values.length < 2) {
|
|
20
|
-
return range;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const left = values[0].split('');
|
|
24
|
-
const right = values[1].split('');
|
|
25
|
-
|
|
26
|
-
if (left.length !== right.length) {
|
|
27
|
-
return range;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
let questionCounter = 0;
|
|
31
|
-
const merged = left.reduce((group, value, index) => {
|
|
32
|
-
if (group === false) {
|
|
33
|
-
return false;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
if (value === right[index] && !questionCounter) {
|
|
37
|
-
return group + value;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (value === '0' && right[index] === 'f') {
|
|
41
|
-
questionCounter++;
|
|
42
|
-
return group + '?';
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return false;
|
|
46
|
-
}, 'u+'); // The maximum number of wildcard characters (?) for ranges is 5.
|
|
47
|
-
|
|
48
|
-
if (merged && questionCounter < 6) {
|
|
49
|
-
return merged;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return range;
|
|
53
|
-
}
|
|
54
|
-
/*
|
|
55
|
-
* IE and Edge before 16 version ignore the unicode-range if the 'U' is lowercase
|
|
56
|
-
*
|
|
57
|
-
* https://caniuse.com/#search=unicode-range
|
|
58
|
-
*/
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
function hasLowerCaseUPrefixBug(browser) {
|
|
62
|
-
return ~(0, _browserslist.default)('ie <=11, edge <= 15').indexOf(browser);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function transform(value, isLegacy = false) {
|
|
66
|
-
return (0, _postcssValueParser.default)(value).walk(child => {
|
|
67
|
-
if (child.type === 'unicode-range') {
|
|
68
|
-
const transformed = unicode(child.value.toLowerCase());
|
|
69
|
-
child.value = isLegacy ? transformed.replace(regexLowerCaseUPrefix, 'U') : transformed;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return false;
|
|
73
|
-
}).toString();
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function pluginCreator() {
|
|
77
|
-
return {
|
|
78
|
-
postcssPlugin: 'postcss-normalize-unicode',
|
|
79
|
-
|
|
80
|
-
prepare(result) {
|
|
81
|
-
const cache = new Map();
|
|
82
|
-
const resultOpts = result.opts || {};
|
|
83
|
-
const browsers = (0, _browserslist.default)(null, {
|
|
84
|
-
stats: resultOpts.stats,
|
|
85
|
-
path: __dirname,
|
|
86
|
-
env: resultOpts.env
|
|
87
|
-
});
|
|
88
|
-
const isLegacy = browsers.some(hasLowerCaseUPrefixBug);
|
|
89
|
-
return {
|
|
90
|
-
OnceExit(css) {
|
|
91
|
-
css.walkDecls(/^unicode-range$/i, decl => {
|
|
92
|
-
const value = decl.value;
|
|
93
|
-
|
|
94
|
-
if (cache.has(value)) {
|
|
95
|
-
decl.value = cache.get(value);
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const newValue = transform(value, isLegacy);
|
|
100
|
-
decl.value = newValue;
|
|
101
|
-
cache.set(value, newValue);
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
pluginCreator.postcss = true;
|
|
112
|
-
var _default = pluginCreator;
|
|
113
|
-
exports.default = _default;
|
|
114
|
-
module.exports = exports.default;
|