Skip to content

Commit

Permalink
Prefix hashes with underscores based on CSS spec
Browse files Browse the repository at this point in the history
According to the CSS spec, identifiers cannot
start with a digit, two hyphens, or a hyphen
followed by a digit.
  • Loading branch information
markdalgleish committed Sep 2, 2016
1 parent 278d6cc commit da27c07
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/getLocalIdent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
};
15 changes: 15 additions & 0 deletions test/localTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]");
});

0 comments on commit da27c07

Please sign in to comment.