Skip to content

Commit

Permalink
fix: bad xpath expression breaks matching for everyone
Browse files Browse the repository at this point in the history
  • Loading branch information
JalilArfaoui authored and lutangar committed Aug 31, 2021
1 parent 70354ab commit 0a196c6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
18 changes: 13 additions & 5 deletions src/libs/domain/matchingContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,15 @@ export const filterContextsMatchingUrl = (
urlMatchesContext(url, context)
);

export const doesTabContentMatchExpression = async (tab: Tab, xpath?: string) =>
export const doesTabContentMatchExpression = async (
tab: Tab,
xpath: string,
matchingContextId: MatchingContext['id']
) =>
xpath
? sendContentScriptRequest<boolean>(tab, 'doesDocumentMatchExpression', [
xpath
xpath,
matchingContextId
])
: Promise.resolve(true);

Expand All @@ -57,9 +62,12 @@ export const filterContextsMatchingTabContent = async (
matchingContexts: MatchingContext[]
) => {
const responses = await Promise.all(
matchingContexts.map(({ xpath }: MatchingContext) =>
doesTabContentMatchExpression(tab, xpath)
)
matchingContexts.map(({ xpath, id }: MatchingContext) => {
return (
typeof xpath === 'undefined' ||
doesTabContentMatchExpression(tab, xpath, id)
);
})
);

return matchingContexts.filter((_, index) => responses[index]);
Expand Down
34 changes: 24 additions & 10 deletions src/libs/utils/doesDocumentMatchExpression.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
const doesDocumentMatchExpression = (expression: string) => {
const { booleanValue } = document.evaluate(
expression,
document,
null,
XPathResult.BOOLEAN_TYPE,
null
);

return booleanValue;
import { MatchingContext } from '../domain/matchingContext';

const doesDocumentMatchExpression = (
expression: string,
matchingContextId: MatchingContext['id']
) => {
try {
const { booleanValue } = document.evaluate(
expression,
document,
null,
XPathResult.BOOLEAN_TYPE,
null
);

return booleanValue;
} catch (error) {
// eslint-disable-next-line no-console
console.warn(
`[DisMoi] Could not evaluate XPath "${expression}" of matching ${matchingContextId}`
);

return false;
}
};

export default doesDocumentMatchExpression;

0 comments on commit 0a196c6

Please sign in to comment.