From 796d3b55e08c27981900c2c2a71506fb00f3bbe1 Mon Sep 17 00:00:00 2001 From: Mradul Singh Date: Mon, 16 Nov 2026 10:09:51 +0530 Subject: [PATCH] fix: quote empty and lone-hyphen values with smart quote marks smartQuoteMark() treated a value as an identifier when cssesc returned it unchanged. That holds for "" and "-" too, so setValue("", { smart: true }) wrote [data-foo=], which this parser rejects since #352, and "-" wrote [data-foo=-], which browsers reject. Both now get a quote mark. --- src/__tests__/attributes.mjs | 43 ++++++++++++++++++++++++++++++++++++ src/selectors/attribute.js | 7 ++++-- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/__tests__/attributes.mjs b/src/__tests__/attributes.mjs index ba9f89a..9dfb271 100644 --- a/src/__tests__/attributes.mjs +++ b/src/__tests__/attributes.mjs @@ -661,6 +661,49 @@ testDeprecation("smart quotes", "[data-foo=bar]", (t, tree) => { t.deepEqual(attr.toString(), "[data-foo='smart with \"double quotes\"']"); }); +// An empty string and a lone hyphen escape to themselves as identifiers, but +// neither is a valid identifier, so leaving them unquoted wrote `[data-foo=]` +// (a syntax error for this parser) and `[data-foo=-]` (rejected by browsers). +for (const [value, quoteMark, expected] of [ + ["", undefined, '[data-foo=""]'], + ["", "'", "[data-foo='']"], + ["", null, '[data-foo=""]'], + ["-", undefined, '[data-foo="-"]'], + ["-", "'", "[data-foo='-']"], + ["-", null, '[data-foo="-"]'], +]) { + test( + `smart quotes quote ${JSON.stringify(value)} with quoteMark ${quoteMark}`, + "[data-foo=bar]", + (t, tree) => { + let attr = tree.nodes[0].nodes[0]; + attr.setValue(value, { smart: true, quoteMark }); + t.deepEqual(attr.toString(), expected); + t.deepEqual(attr.quoteMark, expected.charAt(10)); + t.deepEqual(attr.getQuotedValue({ smart: true, quoteMark }), expected.slice(10, -1)); + t.deepEqual(parser().astSync(tree.toString()).nodes[0].nodes[0].value, value); + }, + ); +} + +test( + "smart quotes keep the current quote mark of an empty value", + "[data-foo='bar']", + (t, tree) => { + let attr = tree.nodes[0].nodes[0]; + attr.setValue("", { smart: true, preferCurrentQuoteMark: true, quoteMark: null }); + t.deepEqual(attr.toString(), "[data-foo='']"); + }, +); + +test("smart quotes still leave identifiers unquoted", "[data-foo='bar']", (t, tree) => { + let attr = tree.nodes[0].nodes[0]; + for (const value of ["a", "-a", "--", "_", "é"]) { + attr.setValue(value, { smart: true }); + t.deepEqual(attr.quoteMark, null, value); + } +}); + testDeprecation("set Attribute#quoteMark", "[data-foo=bar]", (t, tree) => { let attr = tree.nodes[0].nodes[0]; attr.quoteMark = '"'; diff --git a/src/selectors/attribute.js b/src/selectors/attribute.js index ef991c1..58ca9ed 100644 --- a/src/selectors/attribute.js +++ b/src/selectors/attribute.js @@ -145,7 +145,10 @@ export default class Attribute extends Namespace { let numDoubleQuotes = v.replace(/[^"]/g, "").length; if (numSingleQuotes + numDoubleQuotes === 0) { let escaped = cssesc(v, { isIdentifier: true }); - if (escaped === v) { + // An empty string and a lone hyphen come back from cssesc unchanged, but + // neither is an identifier, so they can only be written as a string. + let needsString = v === "" || v === "-"; + if (escaped === v && !needsString) { return Attribute.NO_QUOTE; } else { let pref = this.preferredQuoteMark(options); @@ -154,7 +157,7 @@ export default class Attribute extends Namespace { let quote = this.quoteMark || options.quoteMark || Attribute.DOUBLE_QUOTE; let opts = CSSESC_QUOTE_OPTIONS[quote]; let quoteValue = cssesc(v, opts); - if (quoteValue.length < escaped.length) { + if (needsString || quoteValue.length < escaped.length) { return quote; } }