Skip to content

fix: quote empty and lone-hyphen values with smart quote marks - #365

Open
bardrop wants to merge 1 commit into
postcss:mainfrom
bardrop-oss:fix-smart-quote-empty-value
Open

bardrop wants to merge 1 commit into
postcss:mainfrom
bardrop-oss:fix-smart-quote-empty-value

Conversation

@bardrop

@bardrop bardrop commented Nov 16, 2026

Copy link
Copy Markdown

Attribute#smartQuoteMark() decides that a value with no quotes in it is an identifier when cssesc(v, { isIdentifier: true }) returns it unchanged. Two values pass that check without being identifiers:

const root = parser().astSync("[data-foo=bar]");
const attr = root.first.first;

attr.setValue("", { smart: true });
root.toString(); // "[data-foo=]"  -> parser().astSync() throws "Expected an attribute value." (since #352)

attr.setValue("-", { smart: true });
root.toString(); // "[data-foo=-]" -> document.querySelector() throws, `-` alone is a delim token

Both now get a quote mark, picked the same way as any other value that needs quotes (the quoteMark option, then the current quote mark, then "). getQuotedValue({ smart: true }) follows, since it uses the same method. Real identifiers (a, -a, --, _, é) still stay unquoted.

I kept the change in smartQuoteMark() and did not touch the vendored cssesc, since its tests pin it to cssesc 3.0.0 output.

Tests: 7 new cases in attributes.mjs (empty and - with each quoteMark option, plus preferCurrentQuoteMark) fail before the change and pass after; each one also checks that the output parses back to the same value. One control test checks identifiers stay unquoted. npm test passes (1,092 tests, lint, typecheck, coverage). oxfmt --check is clean for the changed files.

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 postcss#352, and "-" wrote
[data-foo=-], which browsers reject. Both now get a quote mark.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant