Skip to content

Commit 1cc0156

Browse files
committed
revert: escape class values supplied to the constructor (#349)
This reverts commit b74fdd4. The constructor had written `value` verbatim since 2018, and callers rely on it: postcss-modules-scope passes the name css-loader has already escaped, which 7.2.0 escaped a second time, so CSS Modules selectors stopped matching (#362). The change moves to the next major (#357).
1 parent ab72383 commit 1cc0156

3 files changed

Lines changed: 0 additions & 87 deletions

File tree

‎src/__tests__/classes.mjs‎

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { test } from "./util/helpers.mjs";
2-
import apiTest from "./util/runner.mjs";
3-
import parser from "../../dist/index.js";
42

53
test("class name", ".one", (t, tree) => {
64
t.deepEqual(tree.nodes[0].nodes[0].value, "one");
@@ -276,79 +274,3 @@ test("class selector with escaping (36)", ".not-pseudo\\:\\:focus", (t, tree) =>
276274
t.deepEqual(tree.nodes[0].nodes[0].type, "class");
277275
t.deepEqual(tree.nodes[0].nodes[0].raws.value, "not-pseudo\\:\\:focus");
278276
});
279-
280-
for (const [value, escaped] of [
281-
["hover:bg-blue", "hover\\:bg-blue"],
282-
["1abc", "\\31 abc"],
283-
["foo.bar", "foo\\.bar"],
284-
["foo\\bar", "foo\\\\bar"],
285-
["♥", "\\2665"],
286-
]) {
287-
apiTest(`ClassName constructor escapes ${value}`, (t) => {
288-
const className = parser.className({ value });
289-
t.is(className.value, value);
290-
t.is(className.toString(), `.${escaped}`, "constructor must escape the semantic value");
291-
t.is(parser().astSync(className.toString()).first.first.value, value);
292-
});
293-
}
294-
295-
apiTest("ClassName constructor preserves explicit raw spelling in either option order", (t) => {
296-
for (const opts of [
297-
{ value: "hover:bg-blue", raws: { value: "hover\\3A bg-blue" } },
298-
{ raws: { value: "hover\\3A bg-blue" }, value: "hover:bg-blue" },
299-
]) {
300-
const className = parser.className(opts);
301-
t.is(className.value, "hover:bg-blue");
302-
t.is(className.toString(), ".hover\\3A bg-blue");
303-
}
304-
});
305-
306-
apiTest("ClassName constructor without a value still supports later assignment", (t) => {
307-
const className = parser.className();
308-
t.is(className.value, undefined);
309-
className.value = "hover:bg-blue";
310-
t.is(className.toString(), ".hover\\:bg-blue");
311-
});
312-
313-
apiTest("ClassName constructor leaves a plain identifier and other raw properties intact", (t) => {
314-
const plain = parser.className({ value: "button" });
315-
t.is(plain.toString(), ".button");
316-
t.is(plain.raws, undefined);
317-
const className = parser.className({
318-
value: "hover:bg-blue",
319-
raws: { spaces: { before: "/* before */" } },
320-
});
321-
t.is(
322-
className.toString(),
323-
"/* before */.hover\\:bg-blue",
324-
"other raw fields must not suppress escaping",
325-
);
326-
});
327-
328-
apiTest(
329-
"ClassName clones preserve raw spelling and escape replacement values independently",
330-
(t) => {
331-
const original = parser.className({
332-
value: "hover:bg-blue",
333-
raws: { value: "hover\\3A bg-blue" },
334-
});
335-
const clone = original.clone();
336-
t.is(clone.toString(), original.toString());
337-
t.not(clone.raws, original.raws);
338-
const replaced = original.clone({ value: "focus:bg-red" });
339-
t.is(replaced.toString(), ".focus\\:bg-red");
340-
t.is(original.toString(), ".hover\\3A bg-blue");
341-
},
342-
);
343-
344-
apiTest("Parsed classes keep their original raw state when cloned", (t) => {
345-
for (const input of [".button", ".♥", ".foo@{bar}", ".hover\\3A bg-blue"]) {
346-
const original = parser().astSync(input).first.first;
347-
const clone = original.clone();
348-
t.is(clone.toString(), input);
349-
t.deepEqual(clone.raws, original.raws);
350-
if (!input.includes("\\")) {
351-
t.is(original.raws, undefined);
352-
}
353-
}
354-
});

‎src/parser.js‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,11 +1144,6 @@ export default class Parser {
11441144
sourceIndex,
11451145
};
11461146
node = new ClassName(unescapeProp(classNameOpts, "value"));
1147-
// Preserve the parsed spelling, including non-standard syntax, rather
1148-
// than the automatic escaping used for API-created class names.
1149-
if (!classNameOpts.raws) {
1150-
delete node.raws;
1151-
}
11521147
} else if (idIndexes.has(ind)) {
11531148
let idOpts = {
11541149
value: value.slice(1),

‎src/selectors/className.js‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ export default class ClassName extends Node {
88
super(opts);
99
this.type = CLASS;
1010
this._constructed = true;
11-
const value = this.value;
12-
if (typeof value === "string" && (!this.raws || this.raws.value === undefined)) {
13-
this.value = value;
14-
}
1511
}
1612

1713
set value(v) {

0 commit comments

Comments
 (0)