From da27c07d0df25a38699344c13b6614c53a469fd9 Mon Sep 17 00:00:00 2001 From: Mark Dalgleish Date: Fri, 2 Sep 2016 23:25:35 +1000 Subject: [PATCH] Prefix hashes with underscores based on CSS spec According to the CSS spec, identifiers cannot start with a digit, two hyphens, or a hyphen followed by a digit. --- lib/getLocalIdent.js | 2 +- test/localTest.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/getLocalIdent.js b/lib/getLocalIdent.js index cc927c0d..d671fb64 100644 --- a/lib/getLocalIdent.js +++ b/lib/getLocalIdent.js @@ -12,5 +12,5 @@ module.exports = function getLocalIdent(loaderContext, localIdentName, localName options.content = options.hashPrefix + request + "+" + localName; localIdentName = localIdentName.replace(/\[local\]/gi, localName); var hash = loaderUtils.interpolateName(loaderContext, localIdentName, options); - return hash.replace(new RegExp("[^a-zA-Z0-9\\-_\u00A0-\uFFFF]", "g"), "-").replace(/^([^a-zA-Z_])/, "_$1"); + return hash.replace(new RegExp("[^a-zA-Z0-9\\-_\u00A0-\uFFFF]", "g"), "-").replace(/^((-?[0-9])|--)/, "_$1"); }; diff --git a/test/localTest.js b/test/localTest.js index ac0f3db7..e2146bdf 100644 --- a/test/localTest.js +++ b/test/localTest.js @@ -202,4 +202,19 @@ describe("local", function() { ], { "bar": "bar--58a3b08b9195a6af0de7431eaf3427c7" }, "?modules&localIdentName=[local]--[hash]&hashPrefix=x"); + testLocal("prefixes leading digit with underscore", ":local(.test) { background: red; }", [ + [1, "._1test { background: red; }", ""] + ], { + test: "_1test" + }, "?localIdentName=1[local]"); + testLocal("prefixes leading hyphen + digit with underscore", ":local(.test) { background: red; }", [ + [1, "._-1test { background: red; }", ""] + ], { + test: "_-1test" + }, "?localIdentName=-1[local]"); + testLocal("prefixes two leading hyphens with underscore", ":local(.test) { background: red; }", [ + [1, "._--test { background: red; }", ""] + ], { + test: "_--test" + }, "?localIdentName=--[local]"); });