Skip to content

Commit

Permalink
Merge pull request #280 from laws-africa/query-selector-bug
Browse files Browse the repository at this point in the history
tolerate bad query selectors
  • Loading branch information
longhotsummer authored Jul 4, 2024
2 parents 240bc0b + 8fe9b5b commit e7bc599
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,14 @@ export class DecorateInternalRefs {
if (this.akomaNtosoElement) {
const href: string = tippy.reference.getAttribute('href') || '';
let html: string | null = '';
const provision: HTMLElement | null = this.akomaNtosoElement.querySelector(href);
let provision: HTMLElement | null = null;

try {
provision = this.akomaNtosoElement.querySelector(href);
} catch (e) {
// ignore query selector errors
console.log(e);
}

if (provision) {
html = provision.outerHTML;
Expand Down
7 changes: 6 additions & 1 deletion core/src/components/decorate-terms/decorate-terms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ export class DecorateTerms {
const term = reference.getAttribute('data-refersto');
// find where the term is defined
if (this.akomaNtosoElement) {
return this.akomaNtosoElement.querySelector(`[data-defines="${term}"]`);
try {
return this.akomaNtosoElement.querySelector(`[data-defines="${term}"]`);
} catch (e) {
// ignore query selector errors
console.log(e);
}
}
return null;
}
Expand Down
7 changes: 6 additions & 1 deletion core/src/components/gutter/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ export class GutterLayout {
if (item.anchor instanceof HTMLElement) {
return this.root.contains(item.anchor) ? item.anchor : null;
} else {
return this.root.querySelector(item.anchor);
try {
return this.root.querySelector(item.anchor);
} catch (e) {
// ignore query selector errors
console.log(e);
}
}
}
return null;
Expand Down
12 changes: 9 additions & 3 deletions core/src/utils/linking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,15 @@ export class AkomaNtosoTarget {

findElement(): HTMLElement | null {
if (this.selector) {
return this.selector instanceof HTMLElement
? this.selector
: this.component.ownerDocument.querySelector(this.selector);
try {
return this.selector instanceof HTMLElement
? this.selector
: this.component.ownerDocument.querySelector(this.selector);
} catch (e) {
// fail on query selector errors
console.log(e);
return null;
}
}

// try the nearest ancestor
Expand Down

0 comments on commit e7bc599

Please sign in to comment.