Skip to content

Commit

Permalink
Also enable possibility of taking the tag name into account when deci…
Browse files Browse the repository at this point in the history
…ding whether to use a class/id/attr.
  • Loading branch information
eoghanmurray committed Oct 2, 2023
1 parent feef199 commit 59c67cf
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions finder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ type Path = Knot[]

export type Options = {
root: Element
idName: (name: string) => boolean
className: (name: string) => boolean
idName: (name: string, tagName: string) => boolean
className: (name: string, tagName: string) => boolean
tagName: (name: string) => boolean
attr: (name: string, value: string) => boolean
attr: (name: string, value: string, tagName: string) => boolean
seedMinLength: number
optimizedMinLength: number
threshold: number
Expand Down Expand Up @@ -186,7 +186,7 @@ function unique(path: Path) {

function id(input: Element): Knot | null {
const elementId = input.getAttribute('id')
if (elementId && config.idName(elementId)) {
if (elementId && config.idName(elementId, input.tagName.toLowerCase())) {
return {
name: '#' + CSS.escape(elementId),
penalty: 0,
Expand All @@ -197,7 +197,7 @@ function id(input: Element): Knot | null {

function attr(input: Element): Knot[] {
const attrs = Array.from(input.attributes).filter((attr) =>
config.attr(attr.name, attr.value)
config.attr(attr.name, attr.value, input.tagName.toLowerCase())
)
return attrs.map(
(attr): Knot => ({
Expand All @@ -208,7 +208,9 @@ function attr(input: Element): Knot[] {
}

function classNames(input: Element): Knot[] {
const names = Array.from(input.classList).filter(config.className)
const names = Array.from(input.classList).filter((className) =>
config.className(className, input.tagName.toLowerCase())
)
return names.map(
(name): Knot => ({
name: '.' + CSS.escape(name),
Expand Down

0 comments on commit 59c67cf

Please sign in to comment.