Skip to content

Commit 3696841

Browse files
feat: support unicode range node (#45)
1 parent a86f64b commit 3696841

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,16 @@ whether the first character in the argument is a quotation mark.
183183
] }
184184
```
185185

186+
### unicode-range
187+
188+
The unicode-range CSS descriptor sets the specific range of characters to be
189+
used from a font defined by @font-face and made available
190+
for use on the current page (`unicode-range: U+0025-00FF`).
191+
192+
Node-specific properties:
193+
194+
- **value**: The "unicode-range" itself.
195+
186196
## API
187197

188198
```

lib/parse.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ var slash = "/".charCodeAt(0);
77
var comma = ",".charCodeAt(0);
88
var colon = ":".charCodeAt(0);
99
var star = "*".charCodeAt(0);
10+
var uLower = "u".charCodeAt(0);
11+
var uUpper = "U".charCodeAt(0);
12+
var plus = "+".charCodeAt(0);
13+
var isUnicodeRange = /^[a-f0-9?-]+$/i;
1014

1115
module.exports = function(input) {
1216
var tokens = [];
@@ -267,6 +271,16 @@ module.exports = function(input) {
267271

268272
if (openParentheses === code) {
269273
name = token;
274+
} else if (
275+
(uLower === token.charCodeAt(0) || uUpper === token.charCodeAt(0)) &&
276+
plus === token.charCodeAt(1) &&
277+
isUnicodeRange.test(token.slice(2))
278+
) {
279+
tokens.push({
280+
type: "unicode-range",
281+
sourceIndex: pos,
282+
value: token
283+
});
270284
} else {
271285
tokens.push({
272286
type: "word",

test/parse.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,60 @@ var tests = [
11481148
{ type: "space", sourceIndex: 10, value: " " },
11491149
{ type: "word", sourceIndex: 11, value: "-0" }
11501150
]
1151+
},
1152+
{
1153+
message: "should parse unicode-range (single codepoint)",
1154+
fixture: "U+26",
1155+
expected: [{ type: "unicode-range", sourceIndex: 0, value: "U+26" }]
1156+
},
1157+
{
1158+
message: "should parse unicode-range (single codepoint) 2",
1159+
fixture: "U+0-7F",
1160+
expected: [{ type: "unicode-range", sourceIndex: 0, value: "U+0-7F" }]
1161+
},
1162+
{
1163+
message: "should parse unicode-range (single codepoint) 3",
1164+
fixture: "U+0-7f",
1165+
expected: [{ type: "unicode-range", sourceIndex: 0, value: "U+0-7f" }]
1166+
},
1167+
{
1168+
message: "should parse unicode-range (single codepoint) (lowercase)",
1169+
fixture: "u+26",
1170+
expected: [{ type: "unicode-range", sourceIndex: 0, value: "u+26" }]
1171+
},
1172+
{
1173+
message: "should parse unicode-range (codepoint range)",
1174+
fixture: "U+0025-00FF",
1175+
expected: [{ type: "unicode-range", sourceIndex: 0, value: "U+0025-00FF" }]
1176+
},
1177+
{
1178+
message: "should parse unicode-range (wildcard range)",
1179+
fixture: "U+4??",
1180+
expected: [{ type: "unicode-range", sourceIndex: 0, value: "U+4??" }]
1181+
},
1182+
{
1183+
message: "should parse unicode-range (multiple values)",
1184+
fixture: "U+0025-00FF, U+4??",
1185+
expected: [
1186+
{ type: "unicode-range", sourceIndex: 0, value: "U+0025-00FF" },
1187+
{ type: "div", sourceIndex: 11, value: ",", before: "", after: " " },
1188+
{ type: "unicode-range", sourceIndex: 13, value: "U+4??" }
1189+
]
1190+
},
1191+
{
1192+
message: "should parse invalid unicode-range as word",
1193+
fixture: "U+4??Z",
1194+
expected: [{ type: "word", sourceIndex: 0, value: "U+4??Z" }]
1195+
},
1196+
{
1197+
message: "should parse invalid unicode-range as word 2",
1198+
fixture: "U+",
1199+
expected: [{ type: "word", sourceIndex: 0, value: "U+" }]
1200+
},
1201+
{
1202+
message: "should parse invalid unicode-range as word 2",
1203+
fixture: "U+Z",
1204+
expected: [{ type: "word", sourceIndex: 0, value: "U+Z" }]
11511205
}
11521206
];
11531207

test/stringify.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ var tests = [
6969
{
7070
message: "Should correctly process empty url whitespace characters",
7171
fixture: "url( \n \t \n )"
72+
},
73+
{
74+
message: "Should correctly process unicode-range (single codepoint)",
75+
fixture: "U+26"
76+
},
77+
{
78+
message: "Should correctly process unicode-range (codepoint range)",
79+
fixture: "U+0025-00FF"
80+
},
81+
{
82+
message: "Should correctly process unicode-range (wildcard range)",
83+
fixture: "U+4??"
84+
},
85+
{
86+
message: "Should correctly process unicode-range (multiple values)",
87+
fixture: "U+0025-00FF, U+4??"
7288
}
7389
];
7490

0 commit comments

Comments
 (0)