-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb64535
commit d04a6f4
Showing
2 changed files
with
90 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,69 @@ | ||
import { getObjectType } from '../utils/utils' | ||
import { getObjectType } from "../utils/utils"; | ||
|
||
export const getNodeText = (node: any): string => { | ||
let text = '' | ||
const objType = getObjectType(node) | ||
let text = ""; | ||
const objType = getObjectType(node); | ||
switch (objType) { | ||
case '[object HTMLTimeElement]': { | ||
text = node.dateTime || '' | ||
break | ||
case "[object HTMLTimeElement]": { | ||
text = node.dateTime || ""; | ||
break; | ||
} | ||
case '[object Text]': { | ||
text = node.textContent || '' | ||
break | ||
case "[object Text]": { | ||
text = node.textContent || ""; | ||
break; | ||
} | ||
case '[object HTMLSpanElement]': { | ||
text = node.innerText || '' | ||
break | ||
case "[object HTMLSpanElement]": { | ||
text = node.innerText || ""; | ||
break; | ||
} | ||
// TODO: more options | ||
default: { | ||
break | ||
break; | ||
} | ||
} | ||
return text | ||
return text; | ||
}; | ||
|
||
export function getDomQueryPath(el: HTMLElement) { | ||
if (!el) return ''; | ||
|
||
const stack = []; | ||
while (el.parentNode != null) { | ||
let sibCount = 0; | ||
let sibIndex = 0; | ||
for (let i = 0; i < el.parentNode.childNodes.length; i++) { | ||
const sib = el.parentNode.childNodes[i]; | ||
if (sib.nodeName == el.nodeName) { | ||
if (sib === el) { | ||
sibIndex = sibCount; | ||
} | ||
sibCount++; | ||
} | ||
} | ||
const name = el.nodeName.toLowerCase(); | ||
if (el.hasAttribute("id") && el.id != "") { | ||
stack.unshift(`${name}#${el.id}`); | ||
} else if (sibCount > 1) { | ||
stack.unshift(`${name}:n-th-of-type(${sibIndex + 1})`); | ||
} else { | ||
stack.unshift(name); | ||
} | ||
el = el.parentNode as HTMLElement; | ||
} | ||
|
||
return stack.slice(1).join(' '); // removes the html element | ||
} | ||
|
||
export function filterAncestorNodes(nodes: Node[]) { | ||
const ancestors = new Set(); | ||
for (const node of nodes) { | ||
let parent = node.parentNode; | ||
while(parent) { | ||
if (nodes.includes(parent) && node.textContent) { | ||
ancestors.add(parent); | ||
} | ||
parent = parent.parentNode; | ||
} | ||
} | ||
return nodes.filter(n => !ancestors.has(n)); | ||
} |