Skip to content

Commit

Permalink
Add tag filtering
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
kevinkace committed Sep 22, 2016
1 parent 7344732 commit e3e94b9
Show file tree
Hide file tree
Showing 24 changed files with 1,170 additions and 56 deletions.
58 changes: 50 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,68 @@ After:
</html>
```

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).
**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).

## Options

Options config has two properties &mdash; `include` and `exclude` &mdash; to define which psuedo class names to add. Both `include` and `exclude` can be:
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:

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

### Example Options Config

This config adds all supported pseudo class names to all appropriate elements using their default class names.

```js
let config = {
include : "all", // default is [ "all" ]
exclude : [ // default is []
"onlyChild",
":root",
":read-only"
]
include : {
classNames : "all" // all group
}
};
```

Here's something more complex, adding only two class names but only to elements that aren't `div`, `table` or `form.

```js
let config = {
include : {
classNames : [ ":first-child", ":last-child" ]
},
exclude : {
tags : [
"div", "table", "form"
]
}
};
```

And here's an unrealistic and irresponsible config showing more options.

```js
let config = {
include : {
classNames : [
"all", // include the "all" group using default class names
":first-child" : "fc", // custom class name; below using function
"form" : (className) => className.replace(":", "")
],
tags : [
"div",
"p",
"span"
]
},
exclude : {
classNames : [
"onlyChild",
":root",
":read-only"
],
tags : [
"div"
]
}
```
Expand Down
6 changes: 4 additions & 2 deletions lib/addClassNameToNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ const get = require("lodash/get"),
concat = require("lodash/concat");


module.exports = function(node) {
module.exports = function(node, config) {
let classes,
pseudoClasses;

if(!node.pseudo || !node.pseudo.length) {
if(!node.pseudo || !node.pseudo.length || config &&
((config.include.tags && config.include.tags.indexOf(node.tag) === -1) ||
(config.exclude.tags && config.exclude.tags.indexOf(node.tag) !== -1))) {
return node;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/getClassNameFns.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ module.exports = function(config) {
let classNames = {},
classFunctions = {};

classNames = addClassNames(classNames, config.include);
classNames = removeClassNames(classNames, config.exclude);
classNames = addClassNames(classNames, config.include.classNames);
classNames = removeClassNames(classNames, config.exclude.classNames);

each(classNames, (fn, className) => {
classFunctions[className] = classNameFunctions[className].bind(null, fn);
Expand Down
10 changes: 7 additions & 3 deletions lib/pseudo.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
"use strict";

const defaultConfig = {
include : [ "all" ],
exclude : []
include : {
classNames : [ "all" ]
},
exclude : {
classNames : []
}
},

getClassNameFns = require("./getClassNameFns"),
Expand Down Expand Up @@ -53,7 +57,7 @@ module.exports = function(config) {
});

// add the class names to the class attr (sorted)
tree.walk((node) => addClassNameToNode(node));
tree.walk((node) => addClassNameToNode(node, config));

// add <head> back
if(head) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "posthtml-pseudo",
"description": "Adds pseudo class names to elements.",
"version": "0.5.1",
"version": "0.6.0",
"author": "Kevin A. Cameron",
"bugs": "https://github.com/kevinkace/posthtml-pseudo/issues",
"dependencies": {
Expand Down
8 changes: 4 additions & 4 deletions test/addClassNameToNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ describe("/lib", () => {
});

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

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

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

it("should not duplicate a class name 3", () => {
assert.deepEqual(addClassNameToNode(fixtures.noDuplicate3.input, ":test"), fixtures.noDuplicate3.expected);
assert.deepEqual(addClassNameToNode(fixtures.noDuplicate3.input), fixtures.noDuplicate3.expected);
});
});
});
90 changes: 75 additions & 15 deletions test/api-classNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ describe("/lib", () => {
// :default
it("should add :default", () =>
posthtml()
.use(pseudo({ include : ":default" }))
.use(pseudo({
include : {
classNames : [ ":default" ]
}
}))
.process(fixtures[":default"].input)
.then((result) => {
assert.equal(result.html, fixtures[":default"].expected);
Expand All @@ -23,7 +27,11 @@ describe("/lib", () => {
// :disabled
it("should add :disabled", () =>
posthtml()
.use(pseudo({ include : ":disabled" }))
.use(pseudo({
include : {
classNames : [ ":disabled" ]
}
}))
.process(fixtures[":disabled"].input)
.then((result) => {
assert.equal(result.html, fixtures[":disabled"].expected);
Expand All @@ -33,7 +41,11 @@ describe("/lib", () => {
// :empty
it("should add :empty", () =>
posthtml()
.use(pseudo({ include : ":empty" }))
.use(pseudo({
include : {
classNames : [ ":empty" ]
}
}))
.process(fixtures[":empty"].input)
.then((result) => {
assert.equal(result.html, fixtures[":empty"].expected);
Expand All @@ -43,7 +55,11 @@ describe("/lib", () => {
// :enabled
it("should add :enabled", () =>
posthtml()
.use(pseudo({ include : ":enabled" }))
.use(pseudo({
include : {
classNames : [ ":enabled" ]
}
}))
.process(fixtures[":enabled"].input)
.then((result) => {
assert.equal(result.html, fixtures[":enabled"].expected);
Expand All @@ -53,7 +69,11 @@ describe("/lib", () => {
// :first-child
it("should add :first-child", () =>
posthtml()
.use(pseudo({ include : ":first-child" }))
.use(pseudo({
include : {
classNames : [ ":first-child" ]
}
}))
.process(fixtures[":first-child"].input)
.then((result) => {
assert.equal(result.html, fixtures[":first-child"].expected);
Expand All @@ -63,7 +83,11 @@ describe("/lib", () => {
// :first-of-type
it("should add :first-of-type", () =>
posthtml()
.use(pseudo({ include : ":first-of-type" }))
.use(pseudo({
include : {
classNames : [ ":first-of-type" ]
}
}))
.process(fixtures[":first-of-type"].input)
.then((result) => {
assert.equal(result.html, fixtures[":first-of-type"].expected);
Expand All @@ -73,7 +97,11 @@ describe("/lib", () => {
// :last-child
it("should add :last-child", () =>
posthtml()
.use(pseudo({ include : ":last-child" }))
.use(pseudo({
include : {
classNames : [ ":last-child" ]
}
}))
.process(fixtures[":last-child"].input)
.then((result) => {
assert.equal(result.html, fixtures[":last-child"].expected);
Expand All @@ -83,7 +111,11 @@ describe("/lib", () => {
// :last-of-type
it("should add :last-of-type", () =>
posthtml()
.use(pseudo({ include : ":last-of-type" }))
.use(pseudo({
include : {
classNames : [ ":last-of-type" ]
}
}))
.process(fixtures[":last-of-type"].input)
.then((result) => {
assert.equal(result.html, fixtures[":last-of-type"].expected);
Expand All @@ -93,7 +125,11 @@ describe("/lib", () => {
// :only-child
it("should add :only-child", () =>
posthtml()
.use(pseudo({ include : ":only-child" }))
.use(pseudo({
include : {
classNames : [ ":only-child" ]
}
}))
.process(fixtures[":only-child"].input)
.then((result) => {
assert.equal(result.html, fixtures[":only-child"].expected);
Expand All @@ -103,7 +139,11 @@ describe("/lib", () => {
// :only-of-type
it("should add :only-of-type", () =>
posthtml()
.use(pseudo({ include : ":only-of-type" }))
.use(pseudo({
include : {
classNames : [ ":only-of-type" ]
}
}))
.process(fixtures[":only-of-type"].input)
.then((result) => {
assert.equal(result.html, fixtures[":only-of-type"].expected);
Expand All @@ -113,7 +153,11 @@ describe("/lib", () => {
// :optional
it("should add :optional", () =>
posthtml()
.use(pseudo({ include : ":optional" }))
.use(pseudo({
include : {
classNames : [ ":optional" ]
}
}))
.process(fixtures[":optional"].input)
.then((result) => {
assert.equal(result.html, fixtures[":optional"].expected);
Expand All @@ -123,7 +167,11 @@ describe("/lib", () => {
// :read-only
it("should add :read-only", () =>
posthtml()
.use(pseudo({ include : ":read-only" }))
.use(pseudo({
include : {
classNames : [ ":read-only" ]
}
}))
.process(fixtures[":read-only"].input)
.then((result) => {
assert.equal(result.html, fixtures[":read-only"].expected);
Expand All @@ -133,7 +181,11 @@ describe("/lib", () => {
// :read-write
it("should add :read-write", () =>
posthtml()
.use(pseudo({ include : ":read-write" }))
.use(pseudo({
include : {
classNames : [ ":read-write" ]
}
}))
.process(fixtures[":read-write"].input)
.then((result) => {
assert.equal(result.html, fixtures[":read-write"].expected);
Expand All @@ -143,7 +195,11 @@ describe("/lib", () => {
// :required
it("should add :required", () =>
posthtml()
.use(pseudo({ include : ":required" }))
.use(pseudo({
include : {
classNames : [ ":required" ]
}
}))
.process(fixtures[":required"].input)
.then((result) => {
assert.equal(result.html, fixtures[":required"].expected);
Expand All @@ -153,7 +209,11 @@ describe("/lib", () => {
// :root
it("should add :root", () =>
posthtml()
.use(pseudo({ include : ":root" }))
.use(pseudo({
include : {
classNames : [ ":root" ]
}
}))
.process(fixtures[":root"].input)
.then((result) => {
assert.equal(result.html, fixtures[":root"].expected);
Expand Down
Loading

0 comments on commit e3e94b9

Please sign in to comment.