File size: 1,106 Bytes
4e1096a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | export const createRejectFilter = ({
tags = [],
classes = [],
attributes = [],
contents = [],
}: {
tags?: string[];
classes?: string[];
attributes?: string[];
contents?: { tag: string; content: RegExp }[];
}) => {
return (node: Node): number => {
if (node.nodeType === Node.ELEMENT_NODE) {
const name = (node as Element).tagName.toLowerCase();
if (name === 'script' || name === 'style') {
return NodeFilter.FILTER_REJECT;
}
if (tags.includes(name)) {
return NodeFilter.FILTER_REJECT;
}
if (classes.some((cls) => (node as Element).classList.contains(cls))) {
return NodeFilter.FILTER_REJECT;
}
if (attributes.some((attr) => (node as Element).hasAttribute(attr))) {
return NodeFilter.FILTER_REJECT;
}
if (
contents.some(({ tag, content }) => {
return name === tag && content.test((node as Element).textContent || '');
})
) {
return NodeFilter.FILTER_REJECT;
}
return NodeFilter.FILTER_SKIP;
}
return NodeFilter.FILTER_ACCEPT;
};
};
|