Skip to content

Commit 63b8ac6

Browse files
MoOxromainmenke
authored andcommitted
Initial release
1 parent e38a763 commit 63b8ac6

File tree

12 files changed

+250
-0
lines changed

12 files changed

+250
-0
lines changed

plugins/postcss-selector-not/.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"stage": 0
3+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
charset = utf-8
6+
trim_trailing_whitespace = true
7+
insert_final_newline = true
8+
indent_style = space
9+
indent_size = 2
10+
11+
[*.{md,markdown}]
12+
# Allow <br/> from Markdown
13+
trim_trailing_whitespace = false
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.gitignore
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
# babel support more syntax stuff than eslint for now
3+
parser: babel-eslint
4+
5+
ecmaFeatures:
6+
modules: true
7+
8+
env:
9+
es6: true
10+
browser: true
11+
node: true
12+
13+
# 0: off, 1: warning, 2: error
14+
rules:
15+
# semicolons are useless
16+
semi: [2, "never"]
17+
18+
quotes: [2, "double"]
19+
20+
# 2 spaces indentation
21+
indent: [2, 2]
22+
23+
# trailing coma are cool for diff
24+
comma-dangle: [2, "always-multiline"]
25+
26+
# enforce comma at eol (never before)
27+
comma-style: [2, "last"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
sudo: false
2+
language: node_js
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 1.0.0 - 2015-04-30
2+
3+
✨ First release

plugins/postcss-selector-not/LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Maxime Thirouin
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# postcss-selector-not [![Build Status](https://travis-ci.org/postcss/postcss-selector-not.svg?branch=master)](https://travis-ci.org/postcss/postcss-selector-not)
2+
3+
> PostCSS plugin to transform `:not()` W3C CSS leve 4 pseudo class to :not() CSS level 3 selectors
4+
5+
## Installation
6+
7+
```console
8+
$ npm install postcss-selector-not
9+
```
10+
11+
## Usage
12+
13+
```js
14+
var postcss = require("postcss")
15+
16+
var output = postcss()
17+
.use(require("postcss-selector-not"))
18+
.process(require("fs").readFileSync("input.css", "utf8"))
19+
.css
20+
```
21+
22+
Using this `input.css`:
23+
24+
```css
25+
p:not(:first-child, .special) {
26+
color: red;
27+
}
28+
```
29+
30+
you will get:
31+
32+
```css
33+
p:not(:first-child), p:not(.special) {
34+
color: red;
35+
}
36+
```
37+
38+
---
39+
40+
## [Changelog](CHANGELOG.md)
41+
42+
## [License](LICENSE)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "postcss-selector-not",
3+
"version": "1.0.0",
4+
"description": "PostCSS plugin to transform :not() W3C CSS leve 4 pseudo class to :not() CSS level 3 selectors",
5+
"keywords": [
6+
"postcss",
7+
"selectors",
8+
"selector",
9+
"Not"
10+
],
11+
"author": "Maxime Thirouin",
12+
"license": "MIT",
13+
"repository": {
14+
"type": "git",
15+
"url": "https://github.com/postcss/postcss-selector-not.git"
16+
},
17+
"homepage": "https://github.com/postcss/postcss-selector-not",
18+
"bugs": {
19+
"url": "https://github.com/postcss/postcss-selector-not/issues"
20+
},
21+
"files": [
22+
"CHANGELOG.md",
23+
"LICENSE",
24+
"dist"
25+
],
26+
"main": "dist/index.js",
27+
"dependencies": {
28+
"balanced-match": "^0.2.0",
29+
"postcss": "^4.1.7"
30+
},
31+
"devDependencies": {
32+
"babel": "^5.1.13",
33+
"babel-eslint": "^3.0.1",
34+
"babel-tape-runner": "^1.1.0",
35+
"eslint": "^0.20.0",
36+
"tape": "^4.0.0"
37+
},
38+
"scripts": {
39+
"lint": "eslint .",
40+
"tape": "babel-tape-runner 'test/*.js'",
41+
"test": "npm run lint && npm run tape",
42+
"prepublish": "babel src --out-dir dist"
43+
}
44+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import postcss from "postcss"
2+
import list from "postcss/lib/list"
3+
4+
import balancedMatch from "balanced-match"
5+
6+
function explodeSelector(pseudoClass, selector) {
7+
if (selector && selector.indexOf(pseudoClass) > -1) {
8+
const start = `${pseudoClass}(`
9+
const end = ")"
10+
const matches = balancedMatch(start, end, selector)
11+
const selectors = []
12+
const bodySelectors = matches.body ?
13+
list
14+
.comma(matches.body)
15+
.reduce((acc, s) => [...acc, ...explodeSelector(pseudoClass, s)], {})
16+
: [""]
17+
const postSelectors = matches.post ? explodeSelector(pseudoClass, matches.post) : [""]
18+
postSelectors.forEach(postSelector => {
19+
bodySelectors.forEach(bodySelector => {
20+
selectors.push(`${matches.pre}${pseudoClass}(${bodySelector})${postSelector}`)
21+
})
22+
})
23+
return selectors
24+
}
25+
return [selector]
26+
}
27+
28+
function explodeSelectors(pseudoClass) {
29+
return () => {
30+
return (css) => {
31+
css.eachRule(rule => {
32+
if (rule.selector && rule.selector.indexOf(pseudoClass) > -1) {
33+
rule.selector = explodeSelector(pseudoClass, rule.selector).join(", ")
34+
}
35+
})
36+
}
37+
}
38+
}
39+
40+
export default postcss.plugin("postcss-selector-not", explodeSelectors(":not"))
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import tape from "tape"
2+
3+
import postcss from "postcss"
4+
import selectorNot from "../src/index.js"
5+
6+
function transform(css) {
7+
return postcss(selectorNot).process(css).css
8+
}
9+
10+
tape("postcss-selector-not", t => {
11+
t.equal(
12+
transform("body {}"),
13+
"body {}",
14+
"should do nothing if there is no :not"
15+
)
16+
17+
t.equal(
18+
transform("body, Not {}"),
19+
"body, Not {}",
20+
"should really do nothing if there is no :not"
21+
)
22+
23+
t.equal(
24+
transform(":not(a, b) {}"),
25+
":not(a), :not(b) {}",
26+
"should transform simple :not()"
27+
)
28+
29+
t.equal(
30+
transform("tag:not(.class, .class2) {}"),
31+
"tag:not(.class), tag:not(.class2) {}",
32+
"should transform directes :not()"
33+
)
34+
35+
t.equal(
36+
transform("tag :not(tag2, tag3) {}"),
37+
"tag :not(tag2), tag :not(tag3) {}",
38+
"should transform :not()"
39+
)
40+
41+
t.equal(
42+
transform("tag :not(tag2, tag3) :not(tag4, tag5) {}"),
43+
"tag :not(tag2) :not(tag4), tag :not(tag3) :not(tag4), tag :not(tag2) :not(tag5), tag :not(tag3) :not(tag5) {}",
44+
"should transform mutltiples :not()"
45+
)
46+
47+
t.equal(
48+
transform("tag :not(tag2 :not(tag4, tag5), tag3) {}"),
49+
"tag :not(tag2 :not(tag4)), tag :not(tag2 :not(tag5)), tag :not(tag3) {}",
50+
"should transform :not() recursively"
51+
)
52+
53+
t.end()
54+
})

0 commit comments

Comments
 (0)