Skip to content

Commit

Permalink
fix(attributes): Fix != ignoring elements without attribute (#402)
Browse files Browse the repository at this point in the history
When `ignoreCase` is `true`.

Necessary for #390
  • Loading branch information
fb55 authored Mar 23, 2021
1 parent aff07d1 commit 4484259
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 33 deletions.
48 changes: 35 additions & 13 deletions src/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ export const attributeRules: Record<
if (data.ignoreCase) {
value = value.toLowerCase();

return (elem) =>
adapter.getAttributeValue(elem, name)?.toLowerCase() ===
value && next(elem);
return (elem) => {
const attr = adapter.getAttributeValue(elem, name);
return (
attr != null &&
attr.length === value.length &&
attr.toLowerCase() === value &&
next(elem)
);
};
}

return (elem) =>
Expand All @@ -51,6 +57,7 @@ export const attributeRules: Record<
const attr = adapter.getAttributeValue(elem, name);
return (
attr != null &&
attr.length >= len &&
(attr.length === len || attr.charAt(len) === "-") &&
attr.substr(0, len).toLowerCase() === value &&
next(elem)
Expand All @@ -62,6 +69,7 @@ export const attributeRules: Record<
const attr = adapter.getAttributeValue(elem, name);
return (
attr != null &&
attr.length >= len &&
attr.substr(0, len) === value &&
(attr.length === len || attr.charAt(len) === "-") &&
next(elem)
Expand All @@ -80,7 +88,12 @@ export const attributeRules: Record<

return function element(elem) {
const attr = adapter.getAttributeValue(elem, name);
return attr != null && regex.test(attr) && next(elem);
return (
attr != null &&
attr.length >= value.length &&
regex.test(attr) &&
next(elem)
);
};
},
exists(next, { name }, { adapter }) {
Expand All @@ -98,11 +111,15 @@ export const attributeRules: Record<
if (data.ignoreCase) {
value = value.toLowerCase();

return (elem) =>
adapter
.getAttributeValue(elem, name)
?.substr(0, len)
.toLowerCase() === value && next(elem);
return (elem) => {
const attr = adapter.getAttributeValue(elem, name);
return (
attr != null &&
attr.length >= len &&
attr.substr(0, len).toLowerCase() === value &&
next(elem)
);
};
}

return (elem) =>
Expand Down Expand Up @@ -144,7 +161,12 @@ export const attributeRules: Record<

return function anyIC(elem) {
const attr = adapter.getAttributeValue(elem, name);
return attr != null && regex.test(attr) && next(elem);
return (
attr != null &&
attr.length >= value.length &&
regex.test(attr) &&
next(elem)
);
};
}

Expand All @@ -164,10 +186,10 @@ export const attributeRules: Record<

return (elem) => {
const attr = adapter.getAttributeValue(elem, name);

return (
attr != null &&
attr.toLocaleLowerCase() !== value &&
(attr == null ||
attr.length !== value.length ||
attr.toLowerCase() !== value) &&
next(elem)
);
};
Expand Down
41 changes: 21 additions & 20 deletions test/attributes.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as CSSselect from "../src";
import { parseDOM } from "htmlparser2";
import { parseDocument } from "htmlparser2";
import { falseFunc } from "boolbase";
import type { Element } from "domhandler";

const dom = parseDOM(
'<div><div data-foo="In the end, it doesn\'t really matter."></div><div data-foo="Indeed-that\'s a delicate matter.">'
) as Element[];
const dom = parseDocument(
'<div data-foo="In the end, it doesn\'t really matter."></div><div data-foo="Indeed-that\'s a delicate matter.">'
);
const domChilds = dom.children as Element[];

describe("Attributes", () => {
describe("ignore case", () => {
Expand All @@ -15,32 +16,32 @@ describe("Attributes", () => {
dom
);
expect(matches).toHaveLength(1);
expect(matches).toStrictEqual([dom[0].children[1] as Element]);
expect(matches).toStrictEqual([domChilds[1]]);
matches = CSSselect.selectAll(
'[data-foo="inDeeD-THAT\'s a DELICATE matteR." i]',
dom
);
expect(matches).toStrictEqual([dom[0].children[1] as Element]);
expect(matches).toStrictEqual([domChilds[1]]);
});

it("should for ^=", () => {
let matches = CSSselect.selectAll("[data-foo^=IN i]", dom);
expect(matches).toHaveLength(2);
expect(matches).toStrictEqual(dom[0].children as Element[]);
expect(matches).toStrictEqual(domChilds);
matches = CSSselect.selectAll("[data-foo^=in i]", dom);
expect(matches).toStrictEqual(dom[0].children as Element[]);
expect(matches).toStrictEqual(domChilds);
matches = CSSselect.selectAll("[data-foo^=iN i]", dom);
expect(matches).toStrictEqual(dom[0].children as Element[]);
expect(matches).toStrictEqual(domChilds);
});

it("should for $=", () => {
let matches = CSSselect.selectAll('[data-foo$="MATTER." i]', dom);
expect(matches).toHaveLength(2);
expect(matches).toStrictEqual(dom[0].children as Element[]);
expect(matches).toStrictEqual(domChilds);
matches = CSSselect.selectAll('[data-foo$="matter." i]', dom);
expect(matches).toStrictEqual(dom[0].children as Element[]);
expect(matches).toStrictEqual(domChilds);
matches = CSSselect.selectAll('[data-foo$="MaTtEr." i]', dom);
expect(matches).toStrictEqual(dom[0].children as Element[]);
expect(matches).toStrictEqual(domChilds);
});

it("should for !=", () => {
Expand All @@ -49,36 +50,36 @@ describe("Attributes", () => {
dom
);
expect(matches).toHaveLength(1);
expect(matches).toStrictEqual([dom[0].children[0] as Element]);
expect(matches).toStrictEqual([domChilds[0]]);
matches = CSSselect.selectAll(
'[data-foo!="inDeeD-THAT\'s a DELICATE matteR." i]',
dom
);
expect(matches).toStrictEqual([dom[0].children[0] as Element]);
expect(matches).toStrictEqual([domChilds[0]]);
});

it("should for *=", () => {
let matches = CSSselect.selectAll("[data-foo*=IT i]", dom);
expect(matches).toHaveLength(1);
expect(matches).toStrictEqual([dom[0].children[0] as Element]);
expect(matches).toStrictEqual([domChilds[0]]);
matches = CSSselect.selectAll("[data-foo*=tH i]", dom);
expect(matches).toStrictEqual(dom[0].children as Element[]);
expect(matches).toStrictEqual(domChilds);
});

it("should for |=", () => {
let matches = CSSselect.selectAll("[data-foo|=indeed i]", dom);
expect(matches).toHaveLength(1);
expect(matches).toStrictEqual([dom[0].children[1] as Element]);
expect(matches).toStrictEqual([domChilds[1]]);
matches = CSSselect.selectAll("[data-foo|=inDeeD i]", dom);
expect(matches).toStrictEqual([dom[0].children[1] as Element]);
expect(matches).toStrictEqual([domChilds[1]]);
});

it("should for ~=", () => {
let matches = CSSselect.selectAll("[data-foo~=IT i]", dom);
expect(matches).toHaveLength(1);
expect(matches).toStrictEqual([dom[0].children[0] as Element]);
expect(matches).toStrictEqual([domChilds[0]]);
matches = CSSselect.selectAll("[data-foo~=dElIcAtE i]", dom);
expect(matches).toStrictEqual([dom[0].children[1] as Element]);
expect(matches).toStrictEqual([domChilds[1]]);
});
});

Expand Down

0 comments on commit 4484259

Please sign in to comment.