fix: don't match anything for empty ~= attribute value

NE
4d ago
+21-1

Fixes #1910

Per the CSS spec, [a~=""] matches nothing. css-select matched elements with an empty value and values with leading, trailing or doubled spaces, because an empty value produced a regex that matches the empty string.

This change makes the ~= rule return falseFunc for an empty value, the same way it already does for values containing whitespace.

Tests added to test/attributes.ts:

[foo~=''] compiles to falseFunc
[a~=""] matches none of the four elements from the issue
[a~="x"] still matches whole words

I confirmed the new tests fail without the fix. The full test suite and lint pass locally.

Summary by CodeRabbit

  • Bug Fixes
    • Attribute selectors using ~= with an empty value now correctly match no elements. Existing selectors with non-empty values continue to work.