Skip to content

Commit cab934b

Browse files
refactor: precise element selector over arbitrary sorting
1 parent 64c3600 commit cab934b

File tree

1 file changed

+12
-26
lines changed

1 file changed

+12
-26
lines changed

src/role-helpers.js

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -82,45 +82,31 @@ function buildElementRoleList(elementRolesMap) {
8282
return `${name}${attributes
8383
.map(({name: attributeName, value, constraints = []}) => {
8484
const shouldNotExist = constraints.indexOf('undefined') !== -1
85-
const hasValue = typeof value !== 'undefined'
86-
if (shouldNotExist) {
87-
return `:not([${attributeName}])`
88-
} else if (hasValue) {
85+
const shouldBeNonEmpty = constraints.indexOf('set') !== -1
86+
const hasExplicitValue = typeof value !== 'undefined'
87+
88+
if (hasExplicitValue) {
8989
return `[${attributeName}="${value}"]`
90-
} else {
91-
return `[${attributeName}]`
90+
} else if (shouldNotExist) {
91+
return `:not([${attributeName}])`
92+
} else if (shouldBeNonEmpty) {
93+
return `[${attributeName}]:not([${attributeName}=""])`
9294
}
95+
96+
return `[${attributeName}]`
9397
})
9498
.join('')}`
9599
}
96100

97101
function getSelectorSpecificity({attributes = []}) {
98-
return {
99-
primaryKey: attributes.length,
100-
secondaryKey: attributes.reduce((acc, {value, constraints = []}) => {
101-
const shouldNotExist = constraints.indexOf('undefined') !== -1
102-
const hasValue = typeof value !== 'undefined'
103-
104-
if (shouldNotExist) {
105-
return acc + 2
106-
} else if (hasValue) {
107-
return acc + 1
108-
}
109-
110-
return acc
111-
}, 0),
112-
}
102+
return attributes.length
113103
}
114104

115105
function bySelectorSpecificity(
116106
{specificity: leftSpecificity},
117107
{specificity: rightSpecificity},
118108
) {
119-
if (rightSpecificity.primaryKey !== leftSpecificity.primaryKey) {
120-
return rightSpecificity.primaryKey - leftSpecificity.primaryKey
121-
}
122-
123-
return rightSpecificity.secondaryKey - leftSpecificity.secondaryKey
109+
return rightSpecificity - leftSpecificity
124110
}
125111

126112
function match(element) {

0 commit comments

Comments
 (0)