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
4 changes: 2 additions & 2 deletions src/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { CompiledQuery, InternalOptions } from "./types.js";
* https://github.com/slevithan/xregexp/blob/95eeebeb8fac8754d54eafe2b4743661ac1cf028/src/xregexp.js#L794
*/
const reChars = /[-[\]{}()*+?.,\\^$|#\s]/g;
const whitespaceRe = /\s/;
const whitespaceRe = /[\t\n\f\r ]/;
function escapeRegex(value: string): string {
return value.replace(reChars, "\\$&");
}
Expand Down Expand Up @@ -151,7 +151,7 @@ export const attributeRules: Record<
}

const regex = new RegExp(
`(?:^|\\s)${escapeRegex(value)}(?:$|\\s)`,
`(?:^|[\\t\\n\\f\\r ])${escapeRegex(value)}(?:$|[\\t\\n\\f\\r ])`,
shouldIgnoreCase(data, options) ? "i" : "",
);

Expand Down
22 changes: 22 additions & 0 deletions test/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ const dom = parseDocument(
const domChilds = dom.children as Element[];

describe("Attributes", () => {
describe("whitespace-separated values", () => {
it("should keep non-CSS whitespace inside a token", () => {
const token = "foo\u{A0}bar";
const document = parseDocument(`<i class="${token}"></i>`);
expect(CSSselect.selectAll("[class~=foo]", document)).toHaveLength(
0,
);
expect(CSSselect.selectAll("[class~=bar]", document)).toHaveLength(
0,
);
expect(
CSSselect.selectAll(`[class~="${token}"]`, document),
).toStrictEqual(document.children);
expect(
CSSselect.selectAll(
`[class~="${token.toUpperCase()}" i]`,
document,
),
).toStrictEqual(document.children);
});
Comment on lines +14 to +32

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This test now exercises only NBSP, so a regression that treats TAB, LF, FF, CR, or SPACE as token content would pass the suite. Retain a parameterized assertion for all five CSS separators.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At test/attributes.ts, line 14:

<comment>This test now exercises only NBSP, so a regression that treats TAB, LF, FF, CR, or SPACE as token content would pass the suite. Retain a parameterized assertion for all five CSS separators.</comment>

<file context>
@@ -11,46 +11,25 @@ const domChilds = dom.children as Element[];
-                ).toBe(boolbase.falseFunc);
-            },
-        );
+        it("should keep non-CSS whitespace inside a token", () => {
+            const token = "foo\u{A0}bar";
+            const document = parseDocument(`<i class="${token}"></i>`);
</file context>
Suggested change
it("should keep non-CSS whitespace inside a token", () => {
const token = "foo\u{A0}bar";
const document = parseDocument(`<i class="${token}"></i>`);
expect(CSSselect.selectAll("[class~=foo]", document)).toHaveLength(
0,
);
expect(CSSselect.selectAll("[class~=bar]", document)).toHaveLength(
0,
);
expect(
CSSselect.selectAll(`[class~="${token}"]`, document),
).toStrictEqual(document.children);
expect(
CSSselect.selectAll(
`[class~="${token.toUpperCase()}" i]`,
document,
),
).toStrictEqual(document.children);
});
it("should keep non-CSS whitespace inside a token", () => {
const token = "foo\u{A0}bar";
const document = parseDocument(`<i class="${token}"></i>`);
expect(CSSselect.selectAll("[class~=foo]", document)).toHaveLength(
0,
);
expect(CSSselect.selectAll("[class~=bar]", document)).toHaveLength(
0,
);
expect(
CSSselect.selectAll(`[class~="${token}"]`, document),
).toStrictEqual(document.children);
expect(
CSSselect.selectAll(
`[class~="${token.toUpperCase()}" i]`,
document,
),
).toStrictEqual(document.children);
});
it.each(["\t", "\n", "\f", "\r", " "])(
"should split tokens on CSS whitespace %j",
(space) => {
const document = parseDocument(
`<i class="foo${space}bar"></i>`,
);
expect(
CSSselect.selectAll("[class~=foo]", document),
).toStrictEqual(document.children);
expect(
CSSselect.selectAll("[class~=bar]", document),
).toStrictEqual(document.children);
expect(
CSSselect._compileUnsafe(`[class~="foo${space}bar"]`),
).toBe(boolbase.falseFunc);
},
);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentional: the five-separator rows all pass on master (\s already splits on them), so they don't test this change; splitting on spaces stays covered by the existing ~= tests ([data-foo~=IT i], the nwmatcher [class~=…] cases). The remaining NBSP row is the one that fails without the fix.

🤖 Generated with Claude Code, using Opus 5.5.

});

describe("ignore case", () => {
it("should for =", () => {
let matches = CSSselect.selectAll(
Expand Down
Loading