-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sort adding class names and associated tests
- Loading branch information
Showing
9 changed files
with
331 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters