Skip to content

Commit e504f08

Browse files
committed
No longer support :: or -- to defined a custom selectors and fixed csstools#14
1 parent d4580b0 commit e504f08

11 files changed

+76
-45
lines changed

CHANGELOG.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
# 2.0.0 - 2015-05-29
2+
3+
* x Remove: no longer support `::` or `--` to defined a custom selectors, we must use the `:--` to defined it.
4+
5+
* - Fixed: two or more consecutive hyphens in selector outputs is "undefined".
6+
17
# 1.1.1 - 2015-04-06
28

3-
- Fixed: add support for multilines definition
9+
* - Fixed: add support for multilines definition
410

511
# 1.1.0 - 2014-12-06
612

7-
- Added: "lineBreak" option
13+
* - Added: "lineBreak" option
814

915
# 1.0.0 - 2014-12-06
1016

11-
First release
17+
* First release

README-zh.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ var output = postcss(selector())
4343
input.css:
4444

4545
```css
46-
@custom-selector --heading h1, h2, h3, h4, h5, h6;
46+
@custom-selector :--heading h1, h2, h3, h4, h5, h6;
4747

48-
article --heading + p {
48+
article :--heading + p {
4949
margin-top: 0;
5050
}
5151
```
@@ -65,17 +65,13 @@ article h6 + p {
6565

6666
## CSS 语法
6767

68-
@custom-selector = @custom-selector <extension-name> <selector>;
68+
@custom-selector = @custom-selector :<extension-name> <selector>;
6969

7070

7171
## 如何使用
7272

73-
### 伪元素/伪类
73+
自定义选择器是一个伪类,所以必须使用 `:--`来定义。
7474

75-
你可以使用
76-
77-
* `:` 自定义一个伪类。
78-
* `::`自定义一个伪元素。
7975

8076
例如,模拟一个 [:any-link](http://dev.w3.org/csswg/selectors/#the-any-link-pseudo) 选择器:
8177

@@ -99,20 +95,22 @@ a:visited {
9995
color: blue;
10096
}
10197
```
98+
10299
### 多个选择器
103100

104-
`@custom-selector` 类似 CSS [`:matches()`](http://dev.w3.org/csswg/selectors-4/#matches)[`-moz-any()`](https://developer.mozilla.org/en-US/docs/Web/CSS/:any)/[`-webkit-any()`](http://css-tricks.com/almanac/selectors/m/matches/))选择器,但是目前**不支持**在同一个选择器中调用多个自定义选择器,例如:
101+
`@custom-selector` 目前**不支持**在同一个选择器中调用多个自定义选择器,例如:
105102

106103
示例3:
107104

108105
```css
109-
@custom-selector --heading h1, h2, h3, h4, h5, h6;
106+
@custom-selector :--heading h1, h2, h3, h4, h5, h6;
110107
@custom-selector :--any-link :link, :visited;
111108

112-
.demo --heading, a:--any-link {
109+
.demo :--heading, a:--any-link {
113110
font-size: 32px;
114111
}
115112
```
113+
116114
将会输出错误的 CSS 代码。
117115

118116
```css
@@ -121,11 +119,13 @@ a:visited {
121119
.demo h3,
122120
.demo h4,
123121
.demo h5,
124-
.demo h6,undefined {
122+
.demo h6,:link,
123+
:visited {
125124
font-size: 32px;
126125
}
127126
```
128127

128+
129129
### Node Watch
130130

131131
依赖 [chokidar](https://github.com/paulmillr/chokidar) 模块。

README.md

+10-12
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ var output = postcss(selector())
4343
input.css:
4444

4545
```css
46-
@custom-selector --heading h1, h2, h3, h4, h5, h6;
46+
@custom-selector :--heading h1, h2, h3, h4, h5, h6;
4747

48-
article --heading + p {
48+
article :--heading + p {
4949
margin-top: 0;
5050
}
5151
```
@@ -65,17 +65,12 @@ article h6 + p {
6565

6666
## CSS syntax
6767

68-
@custom-selector = @custom-selector <extension-name> <selector>;
68+
@custom-selector = @custom-selector :<extension-name> <selector>;
6969

7070

7171
## How to use
7272

73-
### Pseudo-element/Pseudo-class
74-
75-
You can use
76-
77-
* `:` to customise a class.
78-
* `::`to customise a Pseudo-element.
73+
The custom selector is a pseudo-class, so we must use the `:--` to defined it.
7974

8075
For example to simulate [:any-link](http://dev.w3.org/csswg/selectors/#the-any-link-pseudo) selector:
8176

@@ -99,20 +94,22 @@ a:visited {
9994
color: blue;
10095
}
10196
```
97+
10298
### Multiple selectors
10399

104-
`@custom-selector` similar to CSS [`:matches()`](http://dev.w3.org/csswg/selectors-4/#matches)([`-moz-any()`](https://developer.mozilla.org/en-US/docs/Web/CSS/:any)/[`-webkit-any()`](http://css-tricks.com/almanac/selectors/m/matches/))selector,but it **doesn’t support** call multiple custom selector in the same selector, e.g.
100+
`@custom-selector` it **doesn’t support** call multiple custom selector in the same selector, e.g.
105101

106102
Example 3:
107103

108104
```css
109-
@custom-selector --heading h1, h2, h3, h4, h5, h6;
105+
@custom-selector :--heading h1, h2, h3, h4, h5, h6;
110106
@custom-selector :--any-link :link, :visited;
111107

112108
.demo --heading, a:--any-link {
113109
font-size: 32px;
114110
}
115111
```
112+
116113
This will throw an error CSS code.
117114

118115
```css
@@ -121,7 +118,8 @@ This will throw an error CSS code.
121118
.demo h3,
122119
.demo h4,
123120
.demo h5,
124-
.demo h6,undefined {
121+
.demo h6,:link,
122+
:visited {
125123
font-size: 32px;
126124
}
127125
```

index.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
22
* 匹配自定义选择器
3-
* --foo, :--foo, ::--foo 三种类型
3+
* :--foo
44
* 注意:CSS 选择器区分大小写
55
*/
6-
var re_CUSTOM_SELECTOR = /([^,]*?)((?::|::)?(?:--[\w-]+))([^,]*)/g
6+
var re_CUSTOM_SELECTOR = /([^,]*?)(:-{2,}[\w-]+)([^,]*)/g
77

88
/**
99
* 暴露插件
@@ -58,16 +58,18 @@ function customSelector(options) {
5858
for (var prop in customSelectors) {
5959
if (rule.selector.indexOf(prop) >= 0) {
6060
customSelector = customSelectors[prop]
61-
6261
// $2 = <extension-name> (自定义的选择器名称)
62+
6363
rule.selector = rule.selector.replace(re_CUSTOM_SELECTOR, function($0, $1, $2, $3) {
64-
if ($2 == prop) {
64+
if ($2 === prop) {
6565
return customSelector.split(",").map(function(selector) {
6666
return $1 + selector.trim() + $3
6767
}).join("," + line_break)
68+
} else if ($2 !== prop){
69+
console.log("Warning: The selector '" + $2 + "' is undefined in CSS!")
70+
return $2
6871
}
6972
})
70-
7173
}
7274
}
7375
})

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-custom-selectors",
3-
"version": "1.1.1",
3+
"version": "2.0.0",
44
"description": "PostCSS plugin to transform W3C CSS Extensions(Custom Selectors) to more compatible CSS",
55
"keywords": [
66
"css",

test/fixtures/extension.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
@custom-selector :--any .foo, .bar;
2-
@custom-selector --foo .baz;
2+
@custom-selector :--foo .baz;
33

44
:--any h1 {
55
margin-top: 16px;
66
}
77

8-
main --foo + p {
8+
main :--foo + p {
99
margin-top: 16px;
1010
}

test/fixtures/heading.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
@custom-selector --heading h1, h2, h3, h4, h5, h6;
1+
@custom-selector :--heading h1, h2, h3, h4, h5, h6;
22

3-
article --heading + p {
3+
article :--heading + p {
44
margin-top: 0;
55
}

test/fixtures/pseudo.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
@custom-selector ::--pseudo ::before, ::after;
1+
@custom-selector :--pseudo ::before, ::after;
22

3-
.foo > a::--pseudo img {
3+
.foo > a:--pseudo img {
44
display: block;
55
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@custom-selector :--foo h1, h2, h3;
2+
@custom-selector :--ba-----r h4, h5, h6;
3+
4+
.fo--oo > :--foo {
5+
margin: auto;
6+
}
7+
8+
:--ba-----r:hover .ba--z {
9+
display: block;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.fo--oo > h1,
2+
.fo--oo > h2,
3+
.fo--oo > h3 {
4+
margin: auto;
5+
}
6+
7+
h4:hover .ba--z,
8+
h5:hover .ba--z,
9+
h6:hover .ba--z {
10+
display: block;
11+
}

test/index.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var test = require("tape")
44

55
var postcss = require("postcss")
66
var plugin = require("..")
7+
var matches = require("postcss-selector-matches")
78

89
function filename(name) { return "test/" + name + ".css" }
910
function read(name) { return fs.readFileSync(name, "utf8") }
@@ -12,21 +13,24 @@ function compareFixtures(t, name, msg, opts, postcssOpts) {
1213
postcssOpts = postcssOpts || {}
1314
postcssOpts.from = filename("fixtures/" + name)
1415
opts = opts || {}
15-
var actual = postcss().use(plugin(opts)).process(read(postcssOpts.from), postcssOpts).css
16+
var actual = postcss()
17+
.use(plugin(opts))
18+
.process(read(postcssOpts.from), postcssOpts).css
1619
var expected = read(filename("fixtures/" + name + ".expected"))
1720
fs.writeFile(filename("fixtures/" + name + ".actual"), actual)
1821
t.equal(actual.trim(), expected.trim(), msg)
1922
}
2023

2124
test("@custom-selector", function(t) {
22-
compareFixtures(t, "heading", "should transform custom selector")
23-
compareFixtures(t, "pseudo", "should transform custom selector")
24-
compareFixtures(t, "multiline", "should transform custom selector")
25+
compareFixtures(t, "heading", "should transform heading")
26+
compareFixtures(t, "pseudo", "should transform pseudo")
27+
compareFixtures(t, "multiline", "should transform multiline")
28+
compareFixtures(t, "some-hyphen-selector", "should transform some-hyphen-selector")
2529

26-
compareFixtures(t, "extension", "local extensions", {
30+
compareFixtures(t, "extension", "should transform local extensions", {
2731
extensions: {
2832
':--any' : 'section, article, aside, nav',
29-
'--foo': 'input[type="text"] > section, #nav .bar'
33+
':--foo': 'input[type="text"] > section, #nav .bar'
3034
}
3135
})
3236

0 commit comments

Comments
 (0)