Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable possibility of taking the tag name into account when deciding whether to use a class/id/attr. #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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