Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/__tests__/attributes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '"';
Expand Down
7 changes: 5 additions & 2 deletions src/selectors/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
}
Expand Down
Loading