Skip to content

Commit

Permalink
chore: simplify sibling selector getters
Browse files Browse the repository at this point in the history
  • Loading branch information
fczbkk committed Aug 15, 2021
1 parent b0179e8 commit 81f96a7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
13 changes: 4 additions & 9 deletions src/selector-nth-child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,10 @@ export function getNthChildSelector (element: Element): Array<CssSelector> {
const parent = element.parentNode

if (parent) {
let counter = 0
const siblings = parent.childNodes
for (let i = 0; i < siblings.length; i++) {
if (isElement(siblings[i])) {
counter += 1
if (siblings[i] === element) {
return [`:nth-child(${counter})`]
}
}
const siblings = [...parent.childNodes].filter(isElement)
const elementIndex = siblings.indexOf(element)
if (elementIndex > -1) {
return [`:nth-child(${elementIndex + 1})`]
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/selector-nth-of-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ export function getNthOfTypeSelector (element: Element): Array<CssSelector> {
const parentElement = element.parentElement

if (parentElement) {
const siblings = parentElement.querySelectorAll(tag)
for (let i = 0; i < siblings.length; i++) {
if (siblings[i] === element) {
return [`${tag}:nth-of-type(${i + 1})`]
}
const siblings = [...parentElement.querySelectorAll(tag)]
const elementIndex = siblings.indexOf(element)
if (elementIndex > -1) {
return [`${tag}:nth-of-type(${elementIndex + 1})`]
}
}

Expand Down

0 comments on commit 81f96a7

Please sign in to comment.