From 5fdf2454d65795dc259978a67861daa3c267e981 Mon Sep 17 00:00:00 2001 From: Kyo Nagashima Date: Wed, 16 Mar 2016 10:12:50 +0900 Subject: [PATCH] Ignore var() function on unquoting The `var()` function of a custom properties should be untouched. This fixes #70. --- index.js | 5 +++++ lib/regexp.js | 3 +++ test/expected/issue70.css | 1 + test/fixtures/issue70.css | 15 +++++++++++++++ 4 files changed, 24 insertions(+) create mode 100644 test/expected/issue70.css create mode 100644 test/fixtures/issue70.css diff --git a/index.js b/index.js index f76b71b..5f1e7df 100644 --- a/index.js +++ b/index.js @@ -57,6 +57,11 @@ var canUnquote = function (str) { // Unquote font family name if possible var unquoteFontFamily = function (family) { var quote; + + if (family.match(re.varFunction)) { + return family; + } + family = family.replace(re.quotedString, "$2"); quote = setQuote(RegExp.$1); diff --git a/lib/regexp.js b/lib/regexp.js index d4cb8eb..168ffde 100644 --- a/lib/regexp.js +++ b/lib/regexp.js @@ -77,6 +77,9 @@ re.urlNeedQuote = /[\s()"']/, // --valid_prop-name re.validProp = /^-{0,2}[^!-,./:-@[-^`{-~]+$/i, +// var(--custom-prop-name) +re.varFunction = /^var\([\w-]+\)$/i, + // , \t, \r, \n re.whiteSpaces = /\s+/g, re.whiteSpacesAfterSymbol = /([(,:])\s/g, diff --git a/test/expected/issue70.css b/test/expected/issue70.css new file mode 100644 index 0000000..67a3a4b --- /dev/null +++ b/test/expected/issue70.css @@ -0,0 +1 @@ +:root{--foo:"Foo"}.foo{font-family:var(--foo)}.bar{font-family:Bar,var(--foo),serif}.baz{font-family:"var(--foo)"} diff --git a/test/fixtures/issue70.css b/test/fixtures/issue70.css new file mode 100644 index 0000000..59dc5fa --- /dev/null +++ b/test/fixtures/issue70.css @@ -0,0 +1,15 @@ +:root { + --foo: "Foo"; +} + +.foo { + font-family: var(--foo); +} + +.bar { + font-family: Bar, var(--foo), serif; +} + +.baz { + font-family: "var(--foo)"; +}