From 9adae278d4960922de92cee19c5a8f58d4a916a3 Mon Sep 17 00:00:00 2001 From: Eoghan Murray Date: Wed, 3 May 2023 13:35:12 +0100 Subject: [PATCH] Omit internal tag names when they are the direct child of some parent element --- finder.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/finder.ts b/finder.ts index 724705b..4dca506 100644 --- a/finder.ts +++ b/finder.ts @@ -4,6 +4,7 @@ type Knot = { name: string + nameAsChild?: string penalty: number level?: number } @@ -154,11 +155,15 @@ function findUniquePath( function selector(path: Path): string { let node = path[0] let query = node.name + let queryAsChild = node.nameAsChild || node.name for (let i = 1; i < path.length; i++) { const level = path[i].level || 0 + let nameAsChild = path[i].nameAsChild || path[i].name if (node.level === level - 1) { - query = `${path[i].name} > ${query}` + query = `${path[i].name} > ${queryAsChild}` + queryAsChild = `${nameAsChild} > ${queryAsChild}` } else { + queryAsChild = `${nameAsChild} ${query}` query = `${path[i].name} ${query}` } node = path[i] @@ -265,6 +270,7 @@ function index(input: Element): number | null { function nthChild(node: Knot, i: number): Knot { return { name: node.name + `:nth-child(${i})`, + nameAsChild: `:nth-child(${i})`, penalty: node.penalty + 1, } }