From e4203d17ed72c391fdaf303299d73c2548c6ecdd Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Mon, 10 Dec 2018 19:13:34 +0300 Subject: [PATCH] feat: support unicode range node --- README.md | 10 +++++++++ lib/parse.js | 14 ++++++++++++ test/parse.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++ test/stringify.js | 16 ++++++++++++++ 4 files changed, 94 insertions(+) diff --git a/README.md b/README.md index 0f3be47..3bd6a0d 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/lib/parse.js b/lib/parse.js index cc80bf9..d9d995f 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -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 = []; @@ -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", diff --git a/test/parse.js b/test/parse.js index 4479d37..5cd41e0 100644 --- a/test/parse.js +++ b/test/parse.js @@ -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" }] } ]; diff --git a/test/stringify.js b/test/stringify.js index 737a043..fb695a5 100644 --- a/test/stringify.js +++ b/test/stringify.js @@ -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??" } ];