Skip to content

feat: support unicode range node #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ whether the first character in the argument is a quotation mark.
] }
```

### unicode-range

The unicode-range CSS descriptor sets the specific range of characters to be
used from a font defined by @font-face and made available
for use on the current page (`unicode-range: U+0025-00FF`).

Node-specific properties:

- **value**: The "unicode-range" itself.

## API

```
Expand Down
14 changes: 14 additions & 0 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ var slash = "/".charCodeAt(0);
var comma = ",".charCodeAt(0);
var colon = ":".charCodeAt(0);
var star = "*".charCodeAt(0);
var uLower = "u".charCodeAt(0);
var uUpper = "U".charCodeAt(0);
var plus = "+".charCodeAt(0);
var isUnicodeRange = /^[a-f0-9?-]+$/i;

module.exports = function(input) {
var tokens = [];
Expand Down Expand Up @@ -267,6 +271,16 @@ module.exports = function(input) {

if (openParentheses === code) {
name = token;
} else if (
(uLower === token.charCodeAt(0) || uUpper === token.charCodeAt(0)) &&
plus === token.charCodeAt(1) &&
isUnicodeRange.test(token.slice(2))
) {
tokens.push({
type: "unicode-range",
sourceIndex: pos,
value: token
});
} else {
tokens.push({
type: "word",
Expand Down
54 changes: 54 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,60 @@ var tests = [
{ type: "space", sourceIndex: 10, value: " " },
{ type: "word", sourceIndex: 11, value: "-0" }
]
},
{
message: "should parse unicode-range (single codepoint)",
fixture: "U+26",
expected: [{ type: "unicode-range", sourceIndex: 0, value: "U+26" }]
},
{
message: "should parse unicode-range (single codepoint) 2",
fixture: "U+0-7F",
expected: [{ type: "unicode-range", sourceIndex: 0, value: "U+0-7F" }]
},
{
message: "should parse unicode-range (single codepoint) 3",
fixture: "U+0-7f",
expected: [{ type: "unicode-range", sourceIndex: 0, value: "U+0-7f" }]
},
{
message: "should parse unicode-range (single codepoint) (lowercase)",
fixture: "u+26",
expected: [{ type: "unicode-range", sourceIndex: 0, value: "u+26" }]
},
{
message: "should parse unicode-range (codepoint range)",
fixture: "U+0025-00FF",
expected: [{ type: "unicode-range", sourceIndex: 0, value: "U+0025-00FF" }]
},
{
message: "should parse unicode-range (wildcard range)",
fixture: "U+4??",
expected: [{ type: "unicode-range", sourceIndex: 0, value: "U+4??" }]
},
{
message: "should parse unicode-range (multiple values)",
fixture: "U+0025-00FF, U+4??",
expected: [
{ type: "unicode-range", sourceIndex: 0, value: "U+0025-00FF" },
{ type: "div", sourceIndex: 11, value: ",", before: "", after: " " },
{ type: "unicode-range", sourceIndex: 13, value: "U+4??" }
]
},
{
message: "should parse invalid unicode-range as word",
fixture: "U+4??Z",
expected: [{ type: "word", sourceIndex: 0, value: "U+4??Z" }]
},
{
message: "should parse invalid unicode-range as word 2",
fixture: "U+",
expected: [{ type: "word", sourceIndex: 0, value: "U+" }]
},
{
message: "should parse invalid unicode-range as word 2",
fixture: "U+Z",
expected: [{ type: "word", sourceIndex: 0, value: "U+Z" }]
}
];

Expand Down
16 changes: 16 additions & 0 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ var tests = [
{
message: "Should correctly process empty url whitespace characters",
fixture: "url( \n \t \n )"
},
{
message: "Should correctly process unicode-range (single codepoint)",
fixture: "U+26"
},
{
message: "Should correctly process unicode-range (codepoint range)",
fixture: "U+0025-00FF"
},
{
message: "Should correctly process unicode-range (wildcard range)",
fixture: "U+4??"
},
{
message: "Should correctly process unicode-range (multiple values)",
fixture: "U+0025-00FF, U+4??"
}
];

Expand Down