Skip to content
This repository was archived by the owner on Jun 6, 2022. It is now read-only.

Commit 74ee82e

Browse files
committed
Fix the lineBreak option keeping the selectors indent
1 parent a0c3207 commit 74ee82e

File tree

6 files changed

+55
-13
lines changed

6 files changed

+55
-13
lines changed

index.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ var postcss = require("postcss")
66
*/
77
var re_CUSTOM_SELECTOR = /([^,]*?)(:-{2,}[\w-]+)(.*)/g
88

9+
// 匹配换行符与空白
10+
var reBLANK_LINE = /(\r\n|\n|\r)(\s*?\1)+/gi
11+
912
/**
1013
* 暴露插件
1114
*/
@@ -50,11 +53,6 @@ module.exports = postcss.plugin("postcss-custom-selectors", function(options) {
5053
customSelectors[extension] = extensions[extension]
5154
})
5255

53-
//控制选择器是否换行
54-
if (!options.lineBreak && options.lineBreak == false) {
55-
line_break = ' '
56-
}
57-
5856
// 转换自定义的选择器别名
5957
styles.eachRule(function(rule) {
6058
for (var prop in customSelectors) {
@@ -64,9 +62,19 @@ module.exports = postcss.plugin("postcss-custom-selectors", function(options) {
6462
// $2 = <extension-name> (自定义的选择器名称)
6563
rule.selector = rule.selector.replace(re_CUSTOM_SELECTOR, function($0, $1, $2, $3) {
6664
if ($2 === prop) {
67-
return customSelector.split(",").map(function(selector) {
65+
var newSelector = customSelector.split(",").map(function(selector) {
6866
return $1 + selector.trim() + $3
69-
}).join("," + line_break)
67+
})
68+
69+
// 选择器不换行
70+
if (!options.lineBreak && options.lineBreak === false) {
71+
line_break = " "
72+
newSelector = newSelector.join("," + line_break)
73+
} else {
74+
// 选择器换行,同时替换多个换行为一个
75+
newSelector = newSelector.join("," + line_break + rule.before).replace(reBLANK_LINE, line_break)
76+
}
77+
return newSelector
7078
} else if ($2 !== prop) {
7179
console.log("Warning: The selector '" + $2 + "' is undefined in CSS!")
7280
return $2

test/fixtures/comment/input.css

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* comment */
2+
@custom-selector :--foo
3+
/* comment */
4+
.foo,
5+
.bar > .baz;
6+
7+
8+
/* comment */
9+
:--foo + p {
10+
display: block;
11+
}

test/fixtures/comment/output.css

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* comment */
2+
3+
4+
/* comment */
5+
.foo + p,
6+
.bar > .baz + p {
7+
display: block;
8+
}

test/fixtures/line-break/input.css

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@custom-selector :--heading h1, h2, h3, h4, h5, h6;
2+
/* comment */
3+
4+
article :--heading + p {
5+
margin-top: 0;
6+
}

test/fixtures/line-break/output.css

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* comment */
2+
3+
article h1 + p, article h2 + p, article h3 + p, article h4 + p, article h5 + p, article h6 + p {
4+
margin-top: 0;
5+
}

test/index.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,17 @@ test("@custom-selector", function(t) {
3131
compareFixtures(t, "some-hyphen", "should transform some hyphen")
3232
compareFixtures(t, "matches", "should transform matches selector")
3333
compareFixtures(t, "similar-matches", "should transform matches selector")
34-
35-
compareFixtures(t, "extension", "should transform local extensions", {
36-
extensions: {
37-
':--any' : 'section, article, aside, nav',
38-
':--foo': 'input[type="text"] > section, #nav .bar'
39-
}
34+
compareFixtures(t, "comment", "should transform comment")
35+
compareFixtures(t, "line-break", "should transform line break", {
36+
lineBreak: false
4037
})
4138

39+
// compareFixtures(t, "extension", "should transform local extensions", {
40+
// extensions: {
41+
// ':--any' : 'section, article, aside, nav',
42+
// ':--foo': 'input[type="text"] > section, #nav .bar'
43+
// }
44+
// })
45+
4246
t.end()
4347
})

0 commit comments

Comments
 (0)