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

Commit

Permalink
Fixed: No more useless warnings for undefined non custom selectors
Browse files Browse the repository at this point in the history
Also:
- Changed: simpler message when a custom selector is undefined
- Changed: warnings now use PostCSS message API
- Some fixes in the Changelog + simpler formatting
- Added some references in the Changelog

Prepared as 2.2.0

Close #22
  • Loading branch information
MoOx committed Jun 30, 2015
1 parent 721c576 commit ac9a18c
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 41 deletions.
31 changes: 22 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
# 2.1.0 - 2015-06-30
# 2.2.0 - 2015-06-30

* \- Fixed: the lineBreak option keeping the selectors indent [#18](https://github.com/postcss/postcss-custom-selectors/issues/18).
* \- Fixed: the tip of an undefined selector [#20](https://github.com/postcss/postcss-custom-selectors/issues/20).
* Fixed: No more useless warnings for undefined non custom selectors
([#22](https://github.com/postcss/postcss-custom-selectors/issues/22))
* Changed: warnings now use PostCSS message API

# 2.1.1 - 2015-06-30

* Fixed: the lineBreak option keeping the selectors indent
([#18](https://github.com/postcss/postcss-custom-selectors/issues/18))
* Fixed: the tip of an undefined selector
([#20](https://github.com/postcss/postcss-custom-selectors/issues/20))

# 2.1.0 - 2015-06-04

* \- Fixed: use PostCSS 4.1 plugin API.
* Changed: use PostCSS 4.1 plugin API
([#13](https://github.com/postcss/postcss-custom-selectors/issues/13))

# 2.0.1 - 2015-06-03

* \- Fixed: `(foo, bar)` conversion error exists in the selector(See also [:matches() test](test/fixtures/matches/input.css)).
* Fixed: `(foo, bar)` conversion error exists in the selector
(See also [:matches() test](test/fixtures/matches/input.css))

# 2.0.0 - 2015-05-29

* \x Remove: no longer support `::` or `--` to defined a custom selectors, we must use the `:--` to defined it.
* Removed: no longer support `::` or `--` to defined a custom selectors,
you must use the syntax `:--` to define it.
([#6](https://github.com/postcss/postcss-custom-selectors/issues/6))
* Fixed: two or more consecutive hyphens in selector don't output `undefined`
([#14](https://github.com/postcss/postcss-custom-selectors/issues/14))

* \- Fixed: two or more consecutive hyphens in selector outputs is "undefined".

# 1.1.1 - 2015-04-06

* \- Fixed: add support for multilines definition
* Fixed: add support for multilines definition

# 1.1.0 - 2014-12-06

* \- Added: "lineBreak" option
* Added: "lineBreak" option

# 1.0.0 - 2014-12-06

Expand Down
56 changes: 29 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = postcss.plugin("postcss-custom-selectors", function(options) {
/**
* 读取和替换自定义选择器
*/
return function(css) {
return function(css, result) {
// 读取自定义选择器
css.eachAtRule(function(rule) {
if (rule.name !== "custom-selector") {
Expand Down Expand Up @@ -55,39 +55,41 @@ module.exports = postcss.plugin("postcss-custom-selectors", function(options) {

// 转换自定义的选择器别名
css.eachRule(function(rule) {
var flag = 0
for (var prop in customSelectors) {
if (rule.selector.indexOf(prop) >= 0) {
customSelector = customSelectors[prop]
if (rule.selector.indexOf(":--") > -1) {
var flag = 0
for (var prop in customSelectors) {
if (rule.selector.indexOf(prop) >= 0) {
customSelector = customSelectors[prop]

// $2 = <extension-name> (自定义的选择器名称)
rule.selector = rule.selector.replace(re_CUSTOM_SELECTOR, function($0, $1, $2, $3) {
// $2 = <extension-name> (自定义的选择器名称)
rule.selector = rule.selector.replace(re_CUSTOM_SELECTOR, function($0, $1, $2, $3) {

if ($2 === prop) {
var newSelector = customSelector.split(",").map(function(selector) {
return $1 + selector.trim() + $3
})
if ($2 === prop) {
var newSelector = customSelector.split(",").map(function(selector) {
return $1 + selector.trim() + $3
})

// 选择器不换行
if (!options.lineBreak && options.lineBreak === false) {
line_break = " "
newSelector = newSelector.join("," + line_break)
} else {
// 选择器换行,同时替换多个换行为一个
newSelector = newSelector.join("," + line_break + rule.before).replace(reBLANK_LINE, line_break)
// 选择器不换行
if (!options.lineBreak && options.lineBreak === false) {
line_break = " "
newSelector = newSelector.join("," + line_break)
} else {
// 选择器换行,同时替换多个换行为一个
newSelector = newSelector.join("," + line_break + rule.before).replace(reBLANK_LINE, line_break)
}
flag ++
return newSelector
}
flag ++
return newSelector
}
else if ($2 !== prop) {
return $2
else if ($2 !== prop) {
return $2
}
})
if(flag === 0){
result.warn("The selector '" + rule.selector + "' is undefined", {node: rule})
}
})
}
}
}
if(flag === 0){
console.log("Warning: The selector '" + rule.selector + "' is undefined in CSS!")
}
})

// 删除 @custom-selector
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postcss-custom-selectors",
"version": "2.1.1",
"version": "2.2.0",
"description": "PostCSS plugin to transform W3C CSS Extensions(Custom Selectors) to more compatible CSS",
"keywords": [
"css",
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/similar-matches/input.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@
:--test p {
display: block;
}

whatever {
display: block;
}
4 changes: 4 additions & 0 deletions test/fixtures/similar-matches/output.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ h4 h5 h6 {
:--test p {
display: block;
}

whatever {
display: block;
}
16 changes: 12 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ function compareFixtures(t, name, msg, opts, postcssOpts) {
//input
postcssOpts.from = filename("fixtures/" + name + "/input")
opts = opts || {}
var actual = postcss()
var result = postcss()
.use(plugin(opts))
.process(read(postcssOpts.from), postcssOpts)
.css

var actual = result.css
//output
var output = read(filename("fixtures/" + name + "/output"))
//actual
fs.writeFile(filename("fixtures/" + name + "/actual"), actual)
t.equal(actual.trim(), output.trim(), msg)

return result
}

test("@custom-selector", function(t) {
Expand All @@ -30,10 +33,15 @@ test("@custom-selector", function(t) {
compareFixtures(t, "multiline", "should transform multiline")
compareFixtures(t, "some-hyphen", "should transform some hyphen")
compareFixtures(t, "matches", "should transform matches selector")
compareFixtures(t, "similar-matches", "should transform matches selector")
var similarMatchesResult = compareFixtures(t, "similar-matches", "should transform matches selector")
t.ok(
similarMatchesResult.messages && similarMatchesResult.messages.length === 1,
"should add a message when a custom selectors is undefined"
)

compareFixtures(t, "comment", "should transform comment")
compareFixtures(t, "line-break", "should transform line break", {
lineBreak: false
lineBreak: false
})

compareFixtures(t, "extension", "should transform local extensions", {
Expand Down

0 comments on commit ac9a18c

Please sign in to comment.