From 7b4a0a0eae74a73f9834f1e46e72e6bb7a7fc631 Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Wed, 4 Sep 2019 15:30:00 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20Remove=C2=A0universal=C2=A0selector=20w?= =?UTF-8?q?hen=C2=A0mixed=20with=20a=C2=A0tag=C2=A0name=C2=A0selector?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/replaceRuleSelector.js | 44 ++++++++++++++++++++++++++++++++++++-- test/index.js | 25 ++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/replaceRuleSelector.js b/src/replaceRuleSelector.js index 118b51e..4b1f32d 100644 --- a/src/replaceRuleSelector.js +++ b/src/replaceRuleSelector.js @@ -4,6 +4,8 @@ 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) @@ -11,9 +13,47 @@ function isElementSelector(selector) { 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 }` diff --git a/test/index.js b/test/index.js index 77d9def..0424da3 100644 --- a/test/index.js +++ b/test/index.js @@ -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() })