Skip to content

Commit

Permalink
Omit internal tag names when they are the direct child of some parent…
Browse files Browse the repository at this point in the history
… element
  • Loading branch information
eoghanmurray committed May 3, 2023
1 parent 81377fc commit 9adae27
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion finder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

type Knot = {
name: string
nameAsChild?: string
penalty: number
level?: number
}
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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,
}
}
Expand Down

0 comments on commit 9adae27

Please sign in to comment.