Skip to content

Commit

Permalink
Sort adding class names and associated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkace committed Sep 8, 2016
1 parent fe7bfde commit e9285b7
Show file tree
Hide file tree
Showing 9 changed files with 331 additions and 3 deletions.
25 changes: 22 additions & 3 deletions lib/addClassNameToNode.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
"use strict";

const get = require("lodash/get"),
set = require("lodash/set");
set = require("lodash/set"),

pseudoRegexp = require("./pseudoRegexp");

module.exports = function(node, className) {
if(get(node, [ "attrs", "class"])) {
node.attrs.class = `${node.attrs.class} ${className}`;
if(get(node, [ "attrs", "class" ])) {
node.attrs.class = `${node.attrs.class} ${className}`
.split(" ")
.sort((class1, class2) => {
let class1Test = pseudoRegexp.test(class1);

if(class1Test ? pseudoRegexp.test(class2) : !pseudoRegexp.test(class2)) {
return class1 > class2;
}

if(pseudoRegexp.test(class2)) {
return -1;
}

return 1;
})
.join(" ");

return node;
}

set(node, [ "attrs", "class" ], className);

return node;
};
50 changes: 50 additions & 0 deletions test/addClassNameToNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"use strict";

const assert = require("assert"),

forEach = require("lodash/forEach"),
concat = require("lodash/concat"),

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

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

groups = require("../lib/groups");

describe("/lib", () => {
describe("/addClassNameToNode.js", () => {
it("should add a class name to a node without a class attr", () => {
assert.deepEqual(addClassNameToNode(fixtures.noClass.input, ":test"), fixtures.noClass.expected);
});
});

describe("/addClassNameToNode.js", () => {
it("should add a class name to a node with an empty class attr", () => {
assert.deepEqual(addClassNameToNode(fixtures.emptyClass.input, ":test"), fixtures.emptyClass.expected);
});
});

describe("/addClassNameToNode.js", () => {
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);
});
});

describe("/addClassNameToNode.js", () => {
it("should add a class name sorted properly to a node with a class attr 1", () => {
assert.deepEqual(addClassNameToNode(fixtures.sortClass1.input, ":first-of-type"), fixtures.sortClass1.expected);
});
});

describe("/addClassNameToNode.js", () => {
it("should add a class name sorted properly to a node with a class attr 2", () => {
assert.deepEqual(addClassNameToNode(fixtures.sortClass2.input, ":first-child"), fixtures.sortClass2.expected);
});
});

describe("/addClassNameToNode.js", () => {
it("should add a class name sorted properly to a node with a class attr 3", () => {
assert.deepEqual(addClassNameToNode(fixtures.sortClass3.input, ":first-child"), fixtures.sortClass3.expected);
});
});
});
42 changes: 42 additions & 0 deletions test/fixtures/addClassNameToNode/emptyClass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"use strict";

module.exports = {
input : {
tag: "a",
attrs: {
href: "#",
class: ""
},
content: [
"\n ",
{
tag: "span",
attrs: {
class: "animals__cat",
style: "background: url(cat.png)"
},
content: ["Cat"]
},
"\n"
]
},
expected : {
tag: "a",
attrs: {
class: ":test",
href: "#"
},
content: [
"\n ",
{
tag: "span",
attrs: {
class: "animals__cat",
style: "background: url(cat.png)"
},
content: ["Cat"]
},
"\n"
]
}
};
42 changes: 42 additions & 0 deletions test/fixtures/addClassNameToNode/endClass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"use strict";

module.exports = {
input : {
tag: "a",
attrs: {
href: "#",
class: "something"
},
content: [
"\n ",
{
tag: "span",
attrs: {
class: "animals__cat",
style: "background: url(cat.png)"
},
content: ["Cat"]
},
"\n"
]
},
expected : {
tag: "a",
attrs: {
class: "something :test",
href: "#"
},
content: [
"\n ",
{
tag: "span",
attrs: {
class: "animals__cat",
style: "background: url(cat.png)"
},
content: ["Cat"]
},
"\n"
]
}
};
41 changes: 41 additions & 0 deletions test/fixtures/addClassNameToNode/noClass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"use strict";

module.exports = {
input : {
tag: "a",
attrs: {
href: "#"
},
content: [
"\n ",
{
tag: "span",
attrs: {
class: "animals__cat",
style: "background: url(cat.png)"
},
content: ["Cat"]
},
"\n"
]
},
expected : {
tag: "a",
attrs: {
class: ":test",
href: "#"
},
content: [
"\n ",
{
tag: "span",
attrs: {
class: "animals__cat",
style: "background: url(cat.png)"
},
content: ["Cat"]
},
"\n"
]
}
};
42 changes: 42 additions & 0 deletions test/fixtures/addClassNameToNode/sortClass1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"use strict";

module.exports = {
input : {
tag: "a",
attrs: {
href: "#",
class: "something :first-child"
},
content: [
"\n ",
{
tag: "span",
attrs: {
class: "animals__cat",
style: "background: url(cat.png)"
},
content: ["Cat"]
},
"\n"
]
},
expected : {
tag: "a",
attrs: {
class: "something :first-child :first-of-type",
href: "#"
},
content: [
"\n ",
{
tag: "span",
attrs: {
class: "animals__cat",
style: "background: url(cat.png)"
},
content: ["Cat"]
},
"\n"
]
}
};
42 changes: 42 additions & 0 deletions test/fixtures/addClassNameToNode/sortClass2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"use strict";

module.exports = {
input : {
tag: "a",
attrs: {
href: "#",
class: "something :first-of-type"
},
content: [
"\n ",
{
tag: "span",
attrs: {
class: "animals__cat",
style: "background: url(cat.png)"
},
content: ["Cat"]
},
"\n"
]
},
expected : {
tag: "a",
attrs: {
class: "something :first-child :first-of-type",
href: "#"
},
content: [
"\n ",
{
tag: "span",
attrs: {
class: "animals__cat",
style: "background: url(cat.png)"
},
content: ["Cat"]
},
"\n"
]
}
};
42 changes: 42 additions & 0 deletions test/fixtures/addClassNameToNode/sortClass3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"use strict";

module.exports = {
input : {
tag: "a",
attrs: {
href: "#",
class: "something :last-child"
},
content: [
"\n ",
{
tag: "span",
attrs: {
class: "animals__cat",
style: "background: url(cat.png)"
},
content: ["Cat"]
},
"\n"
]
},
expected : {
tag: "a",
attrs: {
class: "something :first-child :last-child",
href: "#"
},
content: [
"\n ",
{
tag: "span",
attrs: {
class: "animals__cat",
style: "background: url(cat.png)"
},
content: ["Cat"]
},
"\n"
]
}
};
8 changes: 8 additions & 0 deletions test/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,13 @@ module.exports = {
input : require("./groups/readWrite.html"),
expected : require("./groups/readWrite.expected.html")
}
},
addClassNameToNode : {
noClass : require("./addClassNameToNode/noClass"),
emptyClass : require("./addClassNameToNode/emptyClass"),
endClass : require("./addClassNameToNode/endClass"),
sortClass1 : require("./addClassNameToNode/sortClass1"),
sortClass2 : require("./addClassNameToNode/sortClass2"),
sortClass3 : require("./addClassNameToNode/sortClass3")
}
};

0 comments on commit e9285b7

Please sign in to comment.