Commit 4f9ad082 authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Split type guard into two helper functions

parent fbcd5bf2
...@@ -161,6 +161,10 @@ export function onDocumentReady( ...@@ -161,6 +161,10 @@ export function onDocumentReady(
{ pollInterval = 10 }: { pollInterval?: number } = {} { pollInterval = 10 }: { pollInterval?: number } = {}
): () => void { ): () => void {
let pollTimer: number | undefined; let pollTimer: number | undefined;
// Two linting rules are conflicting here, so muting one of them.
// This should be fixable by refactoring the whole function, as there are
// crossed dependencies between local callbacks, that rely on each other
// having been called in a specific order.
// eslint-disable-next-line prefer-const // eslint-disable-next-line prefer-const
let pollForDocumentChange: () => void; let pollForDocumentChange: () => void;
......
...@@ -52,10 +52,12 @@ export function forEachNodeInRange(range: Range, callback: (n: Node) => void) { ...@@ -52,10 +52,12 @@ export function forEachNodeInRange(range: Range, callback: (n: Node) => void) {
} }
function nodeIsText(node: Node): node is Text { function nodeIsText(node: Node): node is Text {
return node.nodeType === Node.TEXT_NODE;
}
function textNodeContainsText(textNode: Text): boolean {
const whitespaceOnly = /^\s*$/; const whitespaceOnly = /^\s*$/;
return ( return !textNode.textContent!.match(whitespaceOnly);
node.nodeType === Node.TEXT_NODE && !node.textContent!.match(whitespaceOnly)
);
} }
/** /**
...@@ -66,7 +68,7 @@ function nodeIsText(node: Node): node is Text { ...@@ -66,7 +68,7 @@ function nodeIsText(node: Node): node is Text {
export function getTextBoundingBoxes(range: Range): DOMRect[] { export function getTextBoundingBoxes(range: Range): DOMRect[] {
const textNodes: Text[] = []; const textNodes: Text[] = [];
forEachNodeInRange(range, node => { forEachNodeInRange(range, node => {
if (nodeIsText(node)) { if (nodeIsText(node) && textNodeContainsText(node)) {
textNodes.push(node); textNodes.push(node);
} }
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment