Skip to content

Commit e3e94b9

Browse files
committed
Add tag filtering
A larger commit again, closing #10. Changes the structure of the config so class are now under include.classNames, and tags under include.tags (same for exclude).
1 parent 7344732 commit e3e94b9

24 files changed

+1170
-56
lines changed

README.md

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,68 @@ After:
2828
</html>
2929
```
3030

31-
Pseudo classes dependent on input values (`:valid`, `:invalid`, ...), browser history (`:visted`, `:link`, ...), interaction (`:hover`, `:focus:`), parameters (`:nth-child()`, `:lang()`, ...), page url (`:target`) or require JS (`:indeterminate`), have been excluded. See [support list](#pseudo-class-names).
31+
**Note on supported classes**: Pseudo classes dependent on input values (`:valid`, `:invalid`, ...), browser history (`:visted`, `:link`, ...), interaction (`:hover`, `:focus:`), parameters (`:nth-child()`, `:lang()`, ...), page url (`:target`) or require JS (`:indeterminate`), have been excluded. See [support list](#pseudo-class-names).
3232

3333
## Options
3434

35-
Options config has two properties &mdash; `include` and `exclude` &mdash; to define which psuedo class names to add. Both `include` and `exclude` can be:
35+
Options config has two properties &mdash; `include` and `exclude` &mdash; to define which psuedo class names to add, and which tags to add them to. Both `include.classNames` and `exclude.classNames` can be:
3636

3737
- a string of a [class name group](#class-name-groups)
3838
- a string of a class name (`/^:\S+/`, from those in the `all` group)
3939
- an array of class name groups and/or class names
4040

4141
### Example Options Config
4242

43+
This config adds all supported pseudo class names to all appropriate elements using their default class names.
44+
4345
```js
4446
let config = {
45-
include : "all", // default is [ "all" ]
46-
exclude : [ // default is []
47-
"onlyChild",
48-
":root",
49-
":read-only"
50-
]
47+
include : {
48+
classNames : "all" // all group
49+
}
50+
};
51+
```
52+
53+
Here's something more complex, adding only two class names but only to elements that aren't `div`, `table` or `form.
54+
55+
```js
56+
let config = {
57+
include : {
58+
classNames : [ ":first-child", ":last-child" ]
59+
},
60+
exclude : {
61+
tags : [
62+
"div", "table", "form"
63+
]
64+
}
65+
};
66+
```
67+
68+
And here's an unrealistic and irresponsible config showing more options.
69+
70+
```js
71+
let config = {
72+
include : {
73+
classNames : [
74+
"all", // include the "all" group using default class names
75+
":first-child" : "fc", // custom class name; below using function
76+
"form" : (className) => className.replace(":", "")
77+
],
78+
tags : [
79+
"div",
80+
"p",
81+
"span"
82+
]
83+
},
84+
exclude : {
85+
classNames : [
86+
"onlyChild",
87+
":root",
88+
":read-only"
89+
],
90+
tags : [
91+
"div"
92+
]
5193
}
5294
```
5395

lib/addClassNameToNode.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ const get = require("lodash/get"),
66
concat = require("lodash/concat");
77

88

9-
module.exports = function(node) {
9+
module.exports = function(node, config) {
1010
let classes,
1111
pseudoClasses;
1212

13-
if(!node.pseudo || !node.pseudo.length) {
13+
if(!node.pseudo || !node.pseudo.length || config &&
14+
((config.include.tags && config.include.tags.indexOf(node.tag) === -1) ||
15+
(config.exclude.tags && config.exclude.tags.indexOf(node.tag) !== -1))) {
1416
return node;
1517
}
1618

lib/getClassNameFns.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ module.exports = function(config) {
1212
let classNames = {},
1313
classFunctions = {};
1414

15-
classNames = addClassNames(classNames, config.include);
16-
classNames = removeClassNames(classNames, config.exclude);
15+
classNames = addClassNames(classNames, config.include.classNames);
16+
classNames = removeClassNames(classNames, config.exclude.classNames);
1717

1818
each(classNames, (fn, className) => {
1919
classFunctions[className] = classNameFunctions[className].bind(null, fn);

lib/pseudo.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
"use strict";
22

33
const defaultConfig = {
4-
include : [ "all" ],
5-
exclude : []
4+
include : {
5+
classNames : [ "all" ]
6+
},
7+
exclude : {
8+
classNames : []
9+
}
610
},
711

812
getClassNameFns = require("./getClassNameFns"),
@@ -53,7 +57,7 @@ module.exports = function(config) {
5357
});
5458

5559
// add the class names to the class attr (sorted)
56-
tree.walk((node) => addClassNameToNode(node));
60+
tree.walk((node) => addClassNameToNode(node, config));
5761

5862
// add <head> back
5963
if(head) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "posthtml-pseudo",
33
"description": "Adds pseudo class names to elements.",
4-
"version": "0.5.1",
4+
"version": "0.6.0",
55
"author": "Kevin A. Cameron",
66
"bugs": "https://github.com/kevinkace/posthtml-pseudo/issues",
77
"dependencies": {

test/addClassNameToNode.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ describe("/lib", () => {
1818
});
1919

2020
it("should add a class name to the end of a node with a class attr", () => {
21-
assert.deepEqual(addClassNameToNode(fixtures.endClass.input, ":test"), fixtures.endClass.expected);
21+
assert.deepEqual(addClassNameToNode(fixtures.endClass.input), fixtures.endClass.expected);
2222
});
2323

2424
it("should not duplicate a class name", () => {
25-
assert.deepEqual(addClassNameToNode(fixtures.noDuplicate.input, ":test"), fixtures.noDuplicate.expected);
25+
assert.deepEqual(addClassNameToNode(fixtures.noDuplicate.input), fixtures.noDuplicate.expected);
2626
});
2727

2828
it("should not duplicate a class name 2", () => {
29-
assert.deepEqual(addClassNameToNode(fixtures.noDuplicate2.input, ":test"), fixtures.noDuplicate2.expected);
29+
assert.deepEqual(addClassNameToNode(fixtures.noDuplicate2.input), fixtures.noDuplicate2.expected);
3030
});
3131

3232
it("should not duplicate a class name 3", () => {
33-
assert.deepEqual(addClassNameToNode(fixtures.noDuplicate3.input, ":test"), fixtures.noDuplicate3.expected);
33+
assert.deepEqual(addClassNameToNode(fixtures.noDuplicate3.input), fixtures.noDuplicate3.expected);
3434
});
3535
});
3636
});

test/api-classNames.js

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ describe("/lib", () => {
1313
// :default
1414
it("should add :default", () =>
1515
posthtml()
16-
.use(pseudo({ include : ":default" }))
16+
.use(pseudo({
17+
include : {
18+
classNames : [ ":default" ]
19+
}
20+
}))
1721
.process(fixtures[":default"].input)
1822
.then((result) => {
1923
assert.equal(result.html, fixtures[":default"].expected);
@@ -23,7 +27,11 @@ describe("/lib", () => {
2327
// :disabled
2428
it("should add :disabled", () =>
2529
posthtml()
26-
.use(pseudo({ include : ":disabled" }))
30+
.use(pseudo({
31+
include : {
32+
classNames : [ ":disabled" ]
33+
}
34+
}))
2735
.process(fixtures[":disabled"].input)
2836
.then((result) => {
2937
assert.equal(result.html, fixtures[":disabled"].expected);
@@ -33,7 +41,11 @@ describe("/lib", () => {
3341
// :empty
3442
it("should add :empty", () =>
3543
posthtml()
36-
.use(pseudo({ include : ":empty" }))
44+
.use(pseudo({
45+
include : {
46+
classNames : [ ":empty" ]
47+
}
48+
}))
3749
.process(fixtures[":empty"].input)
3850
.then((result) => {
3951
assert.equal(result.html, fixtures[":empty"].expected);
@@ -43,7 +55,11 @@ describe("/lib", () => {
4355
// :enabled
4456
it("should add :enabled", () =>
4557
posthtml()
46-
.use(pseudo({ include : ":enabled" }))
58+
.use(pseudo({
59+
include : {
60+
classNames : [ ":enabled" ]
61+
}
62+
}))
4763
.process(fixtures[":enabled"].input)
4864
.then((result) => {
4965
assert.equal(result.html, fixtures[":enabled"].expected);
@@ -53,7 +69,11 @@ describe("/lib", () => {
5369
// :first-child
5470
it("should add :first-child", () =>
5571
posthtml()
56-
.use(pseudo({ include : ":first-child" }))
72+
.use(pseudo({
73+
include : {
74+
classNames : [ ":first-child" ]
75+
}
76+
}))
5777
.process(fixtures[":first-child"].input)
5878
.then((result) => {
5979
assert.equal(result.html, fixtures[":first-child"].expected);
@@ -63,7 +83,11 @@ describe("/lib", () => {
6383
// :first-of-type
6484
it("should add :first-of-type", () =>
6585
posthtml()
66-
.use(pseudo({ include : ":first-of-type" }))
86+
.use(pseudo({
87+
include : {
88+
classNames : [ ":first-of-type" ]
89+
}
90+
}))
6791
.process(fixtures[":first-of-type"].input)
6892
.then((result) => {
6993
assert.equal(result.html, fixtures[":first-of-type"].expected);
@@ -73,7 +97,11 @@ describe("/lib", () => {
7397
// :last-child
7498
it("should add :last-child", () =>
7599
posthtml()
76-
.use(pseudo({ include : ":last-child" }))
100+
.use(pseudo({
101+
include : {
102+
classNames : [ ":last-child" ]
103+
}
104+
}))
77105
.process(fixtures[":last-child"].input)
78106
.then((result) => {
79107
assert.equal(result.html, fixtures[":last-child"].expected);
@@ -83,7 +111,11 @@ describe("/lib", () => {
83111
// :last-of-type
84112
it("should add :last-of-type", () =>
85113
posthtml()
86-
.use(pseudo({ include : ":last-of-type" }))
114+
.use(pseudo({
115+
include : {
116+
classNames : [ ":last-of-type" ]
117+
}
118+
}))
87119
.process(fixtures[":last-of-type"].input)
88120
.then((result) => {
89121
assert.equal(result.html, fixtures[":last-of-type"].expected);
@@ -93,7 +125,11 @@ describe("/lib", () => {
93125
// :only-child
94126
it("should add :only-child", () =>
95127
posthtml()
96-
.use(pseudo({ include : ":only-child" }))
128+
.use(pseudo({
129+
include : {
130+
classNames : [ ":only-child" ]
131+
}
132+
}))
97133
.process(fixtures[":only-child"].input)
98134
.then((result) => {
99135
assert.equal(result.html, fixtures[":only-child"].expected);
@@ -103,7 +139,11 @@ describe("/lib", () => {
103139
// :only-of-type
104140
it("should add :only-of-type", () =>
105141
posthtml()
106-
.use(pseudo({ include : ":only-of-type" }))
142+
.use(pseudo({
143+
include : {
144+
classNames : [ ":only-of-type" ]
145+
}
146+
}))
107147
.process(fixtures[":only-of-type"].input)
108148
.then((result) => {
109149
assert.equal(result.html, fixtures[":only-of-type"].expected);
@@ -113,7 +153,11 @@ describe("/lib", () => {
113153
// :optional
114154
it("should add :optional", () =>
115155
posthtml()
116-
.use(pseudo({ include : ":optional" }))
156+
.use(pseudo({
157+
include : {
158+
classNames : [ ":optional" ]
159+
}
160+
}))
117161
.process(fixtures[":optional"].input)
118162
.then((result) => {
119163
assert.equal(result.html, fixtures[":optional"].expected);
@@ -123,7 +167,11 @@ describe("/lib", () => {
123167
// :read-only
124168
it("should add :read-only", () =>
125169
posthtml()
126-
.use(pseudo({ include : ":read-only" }))
170+
.use(pseudo({
171+
include : {
172+
classNames : [ ":read-only" ]
173+
}
174+
}))
127175
.process(fixtures[":read-only"].input)
128176
.then((result) => {
129177
assert.equal(result.html, fixtures[":read-only"].expected);
@@ -133,7 +181,11 @@ describe("/lib", () => {
133181
// :read-write
134182
it("should add :read-write", () =>
135183
posthtml()
136-
.use(pseudo({ include : ":read-write" }))
184+
.use(pseudo({
185+
include : {
186+
classNames : [ ":read-write" ]
187+
}
188+
}))
137189
.process(fixtures[":read-write"].input)
138190
.then((result) => {
139191
assert.equal(result.html, fixtures[":read-write"].expected);
@@ -143,7 +195,11 @@ describe("/lib", () => {
143195
// :required
144196
it("should add :required", () =>
145197
posthtml()
146-
.use(pseudo({ include : ":required" }))
198+
.use(pseudo({
199+
include : {
200+
classNames : [ ":required" ]
201+
}
202+
}))
147203
.process(fixtures[":required"].input)
148204
.then((result) => {
149205
assert.equal(result.html, fixtures[":required"].expected);
@@ -153,7 +209,11 @@ describe("/lib", () => {
153209
// :root
154210
it("should add :root", () =>
155211
posthtml()
156-
.use(pseudo({ include : ":root" }))
212+
.use(pseudo({
213+
include : {
214+
classNames : [ ":root" ]
215+
}
216+
}))
157217
.process(fixtures[":root"].input)
158218
.then((result) => {
159219
assert.equal(result.html, fixtures[":root"].expected);

0 commit comments

Comments
 (0)