Skip to content

Commit da27c07

Browse files
committed
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.
1 parent 278d6cc commit da27c07

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/getLocalIdent.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ module.exports = function getLocalIdent(loaderContext, localIdentName, localName
1212
options.content = options.hashPrefix + request + "+" + localName;
1313
localIdentName = localIdentName.replace(/\[local\]/gi, localName);
1414
var hash = loaderUtils.interpolateName(loaderContext, localIdentName, options);
15-
return hash.replace(new RegExp("[^a-zA-Z0-9\\-_\u00A0-\uFFFF]", "g"), "-").replace(/^([^a-zA-Z_])/, "_$1");
15+
return hash.replace(new RegExp("[^a-zA-Z0-9\\-_\u00A0-\uFFFF]", "g"), "-").replace(/^((-?[0-9])|--)/, "_$1");
1616
};

test/localTest.js

+15
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,19 @@ describe("local", function() {
202202
], {
203203
"bar": "bar--58a3b08b9195a6af0de7431eaf3427c7"
204204
}, "?modules&localIdentName=[local]--[hash]&hashPrefix=x");
205+
testLocal("prefixes leading digit with underscore", ":local(.test) { background: red; }", [
206+
[1, "._1test { background: red; }", ""]
207+
], {
208+
test: "_1test"
209+
}, "?localIdentName=1[local]");
210+
testLocal("prefixes leading hyphen + digit with underscore", ":local(.test) { background: red; }", [
211+
[1, "._-1test { background: red; }", ""]
212+
], {
213+
test: "_-1test"
214+
}, "?localIdentName=-1[local]");
215+
testLocal("prefixes two leading hyphens with underscore", ":local(.test) { background: red; }", [
216+
[1, "._--test { background: red; }", ""]
217+
], {
218+
test: "_--test"
219+
}, "?localIdentName=--[local]");
205220
});

0 commit comments

Comments
 (0)