Skip to content

Commit

Permalink
Add length checks to attribute checks
Browse files Browse the repository at this point in the history
Should improve perf
  • Loading branch information
fb55 committed Mar 23, 2021
1 parent a62525f commit 16dae6a
Showing 1 changed file with 41 additions and 13 deletions.
54 changes: 41 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 @@ -162,9 +184,15 @@ export const attributeRules: Record<
} else 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 Down

0 comments on commit 16dae6a

Please sign in to comment.