Skip to content
Open
Show file tree
Hide file tree
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
44 changes: 42 additions & 2 deletions src/replaceRuleSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,56 @@ import balancedMatch from "balanced-match"

const pseudoClass = ":matches"
const selectorElementRE = /^[a-zA-Z]/
const selectorUniversalRE = /^\*(?!\|)/
const selectorNamespacedUniversalRE = /\|\*$/

function isElementSelector(selector) {
const matches = selectorElementRE.exec(selector)
// console.log({selector, matches})
return matches
}

function isUniversalSelector(selector) {
const matches = selectorUniversalRE.exec(selector)
// console.log({selector, matches})
return matches
}

function isUniversalSelectorWithNS(selector) {
const matches = selectorNamespacedUniversalRE.exec(selector)
// console.log({selector, matches})
return matches
}

function normalizeSelector(selector, preWhitespace, pre) {
if (isElementSelector(selector) && !isElementSelector(pre)) {
return `${ preWhitespace}${ selector }${ pre }`
if (isUniversalSelector(selector) && isUniversalSelector(pre)) {
return `${ preWhitespace }${ pre }${ selector.substring(1) }`
}

if (isElementSelector(pre)) {
if (isUniversalSelector(selector)) {
return `${ preWhitespace }${ pre }${ selector.substring(1) }`
}
else if (isUniversalSelectorWithNS(selector)) {
return `${ preWhitespace }${
selector.substring(0, selector.length - 1)
}${ pre }`
}
}

if (isElementSelector(selector)) {
if (isUniversalSelector(pre)) {
return `${ preWhitespace }${ selector }${ pre.substring(1) }`
}
else if (isUniversalSelectorWithNS(pre)) {
return `${ preWhitespace }${
pre.substring(0, pre.length - 1)
}${ selector }`
}

else if (!isElementSelector(pre)) {
return `${ preWhitespace}${ selector }${ pre }`
}
}

return `${ preWhitespace }${ pre }${ selector }`
Expand Down
25 changes: 25 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,30 @@ article h3 + p {}`,
"regression https://github.com/postcss/postcss-selector-matches/issues/10"
)

t.equal(
transform(
"*:matches(a), " +
"c:matches(*), " +
"ns|*:matches(b), " +
"*|*:matches(d), " +
"*:matches(ns|e) {}"
),
"a, c, ns|b, *|d, ns|e {}",
"universal selector is removed when mixed with a tag name selector"
)

t.equal(
transform("*.foo:matches(bar) {}"),
"bar.foo {}",
"universal selector is removed from compound selector when mixed with " +
"a tag name selector"
)

t.equal(
transform("*.foo:matches(*.bar, tag:baz), *:matches(*) {}"),
"*.foo.bar, tag:baz.foo, * {}",
"only one universal selector is kept"
)

t.end()
})