From 696510a50fad42182440f26be78b79292f3d7287 Mon Sep 17 00:00:00 2001 From: gholk Date: Sun, 4 Sep 2022 15:08:56 +0800 Subject: [PATCH] Support focus anchor button input details in find The dom.ts is fixed a little to improve type check. --- src/content/finding.ts | 17 +++++++++++++---- src/lib/dom.ts | 6 ++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/content/finding.ts b/src/content/finding.ts index 224a8d83..471dea70 100644 --- a/src/content/finding.ts +++ b/src/content/finding.ts @@ -74,11 +74,20 @@ class FindHighlight extends HTMLSpanElement { inline: "center", }) } - let parentNode = this.range.startContainer.parentNode - while (parentNode && !(parentNode instanceof HTMLAnchorElement)) { - parentNode = parentNode.parentNode + + let parentElement = this.range.startContainer.parentElement + loop: while (parentElement) { + switch (parentElement.nodeName.toLowerCase()) { + case "a": + case "input": + case "button": + case "details": + parentElement.focus() + break loop + } + parentElement = parentElement.parentElement } - if (parentNode) parentNode.focus() + for (const node of this.children) { ;(node as HTMLElement).style.background = `rgba(255,127,255,0.5)` } diff --git a/src/lib/dom.ts b/src/lib/dom.ts index 10ddc55a..5f0d2388 100644 --- a/src/lib/dom.ts +++ b/src/lib/dom.ts @@ -222,8 +222,10 @@ export function isVisibleFilter( */ export function isVisible(thing: Element | Range) { - while (!(thing.getBoundingClientRect instanceof Function)) { - thing = thing.parentElement + if (thing instanceof Element) { + while (typeof thing.getBoundingClientRect !== "function") { + thing = thing.parentElement + } } const clientRect = thing.getBoundingClientRect() switch (true) {