Skip to content

Commit afe918c

Browse files
committed
Fixed: plugin is correctly exposed for normal commonjs environments (!babel) (1.2.1)
Close #5
1 parent 36a25f7 commit afe918c

5 files changed

Lines changed: 77 additions & 79 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 1.2.1 - 2015-07-14
2+
3+
- Fixed: plugin is correctly exposed for normal commonjs environments (!babel)
4+
([#5](https://github.com/postcss/postcss-selector-matches/issues/5))
5+
16
# 1.2.0 - 2015-07-14
27

38
- Fixed: indentation adjustment doesn't contain useless new lines

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-selector-matches",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "PostCSS plugin to transform :matches() W3C CSS pseudo class to more compatible CSS selectors",
55
"keywords": [
66
"postcss",

src/index.js

Lines changed: 3 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,18 @@
11
import postcss from "postcss"
2-
import list from "postcss/lib/list"
3-
4-
import balancedMatch from "balanced-match"
5-
6-
const pseudoClass = ":matches"
7-
8-
function explodeSelector(selector, options) {
9-
if (selector && selector.indexOf(pseudoClass) > -1) {
10-
let newSelectors = []
11-
const preWhitespaceMatches = selector.match(/^\s+/)
12-
const preWhitespace = preWhitespaceMatches
13-
? preWhitespaceMatches[0]
14-
: ""
15-
const selectorPart = list.comma(selector)
16-
selectorPart.forEach(part => {
17-
const position = part.indexOf(pseudoClass)
18-
const pre = part.slice(0, position)
19-
const body = part.slice(position)
20-
const matches = balancedMatch("(", ")", body)
21-
22-
const bodySelectors = matches && matches.body ?
23-
list
24-
.comma(matches.body)
25-
.reduce((acc, s) => [
26-
...acc,
27-
...explodeSelector(s, options),
28-
], [])
29-
: [body]
30-
31-
const postSelectors = matches && matches.post
32-
? explodeSelector(matches.post, options)
33-
: []
34-
35-
let newParts
36-
if (postSelectors.length === 0) {
37-
newParts = bodySelectors.map((s) => preWhitespace + pre + s)
38-
}
39-
else {
40-
newParts = []
41-
postSelectors.forEach(postS => {
42-
bodySelectors.forEach(s => {
43-
newParts.push(pre + s + postS)
44-
})
45-
})
46-
}
47-
newSelectors = [
48-
...newSelectors,
49-
...newParts,
50-
]
51-
})
52-
53-
return newSelectors
54-
}
55-
return [selector]
56-
}
57-
58-
function replaceRuleSelector(rule, options) {
59-
const indentation = rule.before
60-
? rule.before.split("\n").pop()
61-
: ""
62-
return (
63-
explodeSelector(rule.selector, options)
64-
.join("," + (options.lineBreak ? "\n" + indentation : " "))
65-
)
66-
67-
}
2+
import replaceRuleSelector from "./replaceRuleSelector"
683

694
function explodeSelectors(options = {}) {
705
return (css) => {
716
css.eachRule(rule => {
72-
if (rule.selector && rule.selector.indexOf(pseudoClass) > -1) {
7+
if (rule.selector && rule.selector.indexOf(":matches") > -1) {
738
rule.selector = replaceRuleSelector(rule, options)
749
}
7510
})
7611
}
7712

7813
}
7914

80-
const plugin = postcss.plugin(
15+
export default postcss.plugin(
8116
"postcss-selector-matches",
8217
explodeSelectors
8318
)
84-
85-
// expose for postcss-custom-selectors
86-
export {replaceRuleSelector}
87-
// old school fallback
88-
plugin.replaceRuleSelector = replaceRuleSelector
89-
90-
export default plugin

src/replaceRuleSelector.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import list from "postcss/lib/list"
2+
3+
import balancedMatch from "balanced-match"
4+
5+
const pseudoClass = ":matches"
6+
7+
function explodeSelector(selector, options) {
8+
if (selector && selector.indexOf(pseudoClass) > -1) {
9+
let newSelectors = []
10+
const preWhitespaceMatches = selector.match(/^\s+/)
11+
const preWhitespace = preWhitespaceMatches
12+
? preWhitespaceMatches[0]
13+
: ""
14+
const selectorPart = list.comma(selector)
15+
selectorPart.forEach(part => {
16+
const position = part.indexOf(pseudoClass)
17+
const pre = part.slice(0, position)
18+
const body = part.slice(position)
19+
const matches = balancedMatch("(", ")", body)
20+
21+
const bodySelectors = matches && matches.body ?
22+
list
23+
.comma(matches.body)
24+
.reduce((acc, s) => [
25+
...acc,
26+
...explodeSelector(s, options),
27+
], [])
28+
: [body]
29+
30+
const postSelectors = matches && matches.post
31+
? explodeSelector(matches.post, options)
32+
: []
33+
34+
let newParts
35+
if (postSelectors.length === 0) {
36+
newParts = bodySelectors.map((s) => preWhitespace + pre + s)
37+
}
38+
else {
39+
newParts = []
40+
postSelectors.forEach(postS => {
41+
bodySelectors.forEach(s => {
42+
newParts.push(pre + s + postS)
43+
})
44+
})
45+
}
46+
newSelectors = [
47+
...newSelectors,
48+
...newParts,
49+
]
50+
})
51+
52+
return newSelectors
53+
}
54+
return [selector]
55+
}
56+
57+
export default function replaceRuleSelector(rule, options) {
58+
const indentation = rule.before
59+
? rule.before.split("\n").pop()
60+
: ""
61+
return (
62+
explodeSelector(rule.selector, options)
63+
.join("," + (options.lineBreak ? "\n" + indentation : " "))
64+
)
65+
}

test/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import tape from "tape"
22

33
import postcss from "postcss"
4-
import plugin, {replaceRuleSelector} from "../src"
4+
import plugin from "../src"
5+
import replaceRuleSelector from "../src/replaceRuleSelector"
56

67
function transform(css, options = {}) {
78
return postcss(plugin(options)).process(css).css
89
}
910

1011
tape("postcss-selector-matches", t => {
1112
t.ok(
12-
typeof replaceRuleSelector === "function" &&
13-
typeof plugin.replaceRuleSelector === "function",
13+
typeof replaceRuleSelector === "function",
1414
"expose 'replaceRuleSelector' function (for postcss-custom-selectors)"
1515
)
1616

0 commit comments

Comments
 (0)