Skip to content

Commit

Permalink
Add support for :default, and rename group input to form
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkace committed Sep 10, 2016
1 parent 6022eb3 commit 744f44b
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 14 deletions.
1 change: 1 addition & 0 deletions lib/classNameFunctions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

module.exports = {
":default" : require("./classNameFunctions/default"),
":disabled" : require("./classNameFunctions/disabled"),
":empty" : require("./classNameFunctions/empty"),
":enabled" : require("./classNameFunctions/enabled"),
Expand Down
34 changes: 34 additions & 0 deletions lib/classNameFunctions/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"use strict";

const walk = require("posthtml/lib/api").walk,
get = require("lodash/get"),

addClassNameToNode = require("../addClassNameToNode"),

eligibleTags = [
"button",
"input"
],
className = ":default";

module.exports = function(node) {
let test = false;

if(!node.tag || node.tag !== "form" || !node.content.length) {
return node;
}

walk.call(node, (subNode) => {
if(test || !subNode.tag || eligibleTags.indexOf(subNode.tag) === -1 ||
(subNode.tag === "input" && get(subNode, [ "attrs", "type"]) !== "submit")) {
return subNode;
}

test = true;
addClassNameToNode(subNode, className);

return subNode;
});

return node;
};
4 changes: 3 additions & 1 deletion lib/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module.exports = {
all : [
":default",
":disabled",
":empty",
":enabled",
Expand All @@ -27,7 +28,8 @@ module.exports = {
":first-child",
":last-child"
],
input : [
form : [
":default",
":disabled",
":enabled",
":optional",
Expand Down
4 changes: 2 additions & 2 deletions test/addClassNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ describe("/lib", () => {
});

it("should add class names and group names", () => {
assert.deepEqual(addClassNames([], [ ":first-child", "input" ]), concat([ ":first-child" ], groups.input));
assert.deepEqual(addClassNames([], [ ":first-child", "form" ]), concat([ ":first-child" ], groups.form));
});

it("should merge class names and group names", () => {
assert.deepEqual(addClassNames([ ":root" ], [ "input", ":first-child" ]), concat([ ":root" ], groups.input, ":first-child"));
assert.deepEqual(addClassNames([ ":root" ], [ "form", ":first-child" ]), concat([ ":root" ], groups.form, ":first-child"));
});
});
});
10 changes: 10 additions & 0 deletions test/api-classNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ const assert = require("assert"),

describe("/lib", () => {
describe("/pseudo.js", () => {
// :default
it("should add default", () =>
posthtml()
.use(pseudo({ include : ":default" }))
.process(fixtures.classNames[":default"].input)
.then((result) => {
assert.equal(result.html, fixtures.classNames[":default"].expected);
})
);

// :disabled
it("should add disabled", () =>
posthtml()
Expand Down
10 changes: 5 additions & 5 deletions test/api-groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ describe("/lib", () => {
})
);

// input
it("should run posthtml with pseudo using group input", () =>
// form
it("should run posthtml with pseudo using group form", () =>
posthtml()
.use(pseudo({ include : "input" }))
.process(fixtures.groups.input.input)
.use(pseudo({ include : "form" }))
.process(fixtures.groups.form.input)
.then((result) => {
assert.equal(result.html, fixtures.groups.input.expected);
assert.equal(result.html, fixtures.groups.form.expected);
})
);

Expand Down
50 changes: 50 additions & 0 deletions test/fixtures/classNames/default.expected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!doctype html>
<html>
<head>
<title>page title</title>
</head>
<body>
<h1>header</h1>
<form action="#">
<input type="text" placeholder="Name" disabled="">
<input type="text" placeholder="Address" readonly="">
<input class="zipcode" type="text" placeholder="Zip Code">

<input class="submit :default" type="submit" name="" value="submit" readonly="readonly">

<select readonly="">
<option>option 1</option>
<option>option 2</option>
<option>option 3</option>
</select>

<textarea></textarea>

<button type="button">Button</button>
</form>

<form>
<input type="text" name="">
<div>
<button class=":default">Click</button>
</div>
<button>Click 2</button>
</form>

<div>
outer
<div>Inner</div>
</div>

<form>
<input type="text">
<input type="text" disabled="">
<div>
<button disabled="" class=":default">click</button>
<input type="submit" value="submit">
</div>
</form>

<button>button</button>
</body>
</html>
50 changes: 50 additions & 0 deletions test/fixtures/classNames/default.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!doctype html>
<html>
<head>
<title>page title</title>
</head>
<body>
<h1>header</h1>
<form action="#">
<input type="text" placeholder="Name" disabled>
<input type="text" placeholder="Address" readonly>
<input class="zipcode" type="text" placeholder="Zip Code">

<input class="submit" type="submit" name="" value="submit" readonly="readonly">

<select readonly>
<option>option 1</option>
<option>option 2</option>
<option>option 3</option>
</select>

<textarea></textarea>

<button type="button">Button</button>
</form>

<form>
<input type="text" name="">
<div>
<button>Click</button>
</div>
<button>Click 2</button>
</form>

<div>
outer
<div>Inner</div>
</div>

<form>
<input type="text">
<input type="text" disabled>
<div>
<button disabled="">click</button>
<input type="submit" value="submit">
</div>
</form>

<button>button</button>
</body>
</html>
2 changes: 1 addition & 1 deletion test/fixtures/groups/all.expected.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ <h1 class=":first-child :first-of-type :last-of-type :only-of-type">header</h1>
<input type="text" placeholder="Address" readonly="" class=":empty :enabled :optional :read-only">
<input class="zipcode :disabled :empty :read-write :required" type="text" placeholder="Zip Code" disabled="" required="">

<input class="submit :empty :enabled :last-of-type :optional :read-only" type="submit" name="" value="submit" readonly="readonly">
<input class="submit :default :empty :enabled :last-of-type :optional :read-only" type="submit" name="" value="submit" readonly="readonly">

<select readonly="" class=":enabled :first-of-type :last-of-type :only-of-type :optional :read-only">
<option class=":enabled :first-child :first-of-type">option 1</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ <h1>header</h1>
<input type="text" placeholder="Address" readonly="" class=":enabled :optional :read-only">
<input class="zipcode :enabled :optional :read-write" type="text" placeholder="Zip Code">

<input class="submit :enabled :optional :read-only" type="submit" name="" value="submit" readonly="readonly">
<input class="submit :default :enabled :optional :read-only" type="submit" name="" value="submit" readonly="readonly">

<select readonly="" class=":enabled :optional :read-only">
<option class=":enabled">option 1</option>
Expand Down
File renamed without changes.
10 changes: 7 additions & 3 deletions test/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ module.exports = {
expected : require("./excludeAll.expected.html")
},
classNames : {
":default" : {
input : require("./classNames/default.html"),
expected : require("./classNames/default.expected.html")
},
":disabled" : {
input : require("./classNames/disabled.html"),
expected : require("./classNames/disabled.expected.html")
Expand Down Expand Up @@ -98,9 +102,9 @@ module.exports = {
input : require("./groups/firstLast.html"),
expected : require("./groups/firstLast.expected.html")
},
input : {
input : require("./groups/input.html"),
expected : require("./groups/input.expected.html")
form : {
input : require("./groups/form.html"),
expected : require("./groups/form.expected.html")
},
only : {
input : require("./groups/only.html"),
Expand Down
2 changes: 1 addition & 1 deletion test/removeClassNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe("/lib", () => {
});

it("should remove class names and group names", () => {
assert.deepEqual(removeClassNames(groups.all, [ ":first-child", "input" ]), groups.all.filter((className) => className !== ":first-child" && groups.input.indexOf(className) < 0));
assert.deepEqual(removeClassNames(groups.all, [ ":first-child", "form" ]), groups.all.filter((className) => className !== ":first-child" && groups.form.indexOf(className) < 0));
});
});
});

0 comments on commit 744f44b

Please sign in to comment.