From 2f71ac159091fa290d4ffaf0550de25fd1c3808e Mon Sep 17 00:00:00 2001 From: kevinkace Date: Tue, 20 Sep 2016 13:37:30 -0700 Subject: [PATCH] Merge issue3 --- .eslintrc | 4 +- lib/addClassNameToNode.js | 44 +++++---- lib/addClassNames.js | 48 +++++++-- lib/addPseudoToNode.js | 9 ++ lib/classNameFunctions/default.js | 19 ++-- lib/classNameFunctions/disabled.js | 9 +- lib/classNameFunctions/empty.js | 13 ++- lib/classNameFunctions/enabled.js | 18 ++-- lib/classNameFunctions/first-child.js | 9 +- lib/classNameFunctions/first-of-type.js | 9 +- lib/classNameFunctions/last-child.js | 9 +- lib/classNameFunctions/last-of-type.js | 11 ++- lib/classNameFunctions/only-child.js | 9 +- lib/classNameFunctions/only-of-type.js | 11 ++- lib/classNameFunctions/optional.js | 16 +-- lib/classNameFunctions/read-only.js | 16 +-- lib/classNameFunctions/read-write.js | 18 ++-- lib/classNameFunctions/required.js | 16 +-- lib/classNameFunctions/root.js | 9 +- lib/classNamer.js | 5 + lib/getClassNameFns.js | 23 +++++ lib/pseudo.js | 45 ++++----- lib/removeClassNames.js | 13 ++- test/addClassNameToNode.js | 31 +++--- test/addClassNames.js | 40 +++++--- test/api-classNames.js | 99 ++++++++++--------- test/api-customClassNames.js | 65 ++++++++++++ test/api-groups.js | 33 ++++--- test/api.js | 9 +- .../fixtures/addClassNameToNode/emptyClass.js | 3 +- test/fixtures/addClassNameToNode/endClass.js | 3 +- .../fixtures/addClassNameToNode/multiClass.js | 42 -------- .../addClassNameToNode/multiClass2.js | 42 -------- test/fixtures/addClassNameToNode/noClass.js | 3 +- .../{sortClass1.js => noDuplicate.js} | 7 +- .../{sortClass2.js => noDuplicate2.js} | 7 +- .../{sortClass3.js => noDuplicate3.js} | 7 +- .../class_function.expected.html | 65 ++++++++++++ .../customClassNames/class_function.html | 65 ++++++++++++ .../class_string.expected.html | 65 ++++++++++++ .../customClassNames/class_string.html | 65 ++++++++++++ .../class_string2.expected.html | 65 ++++++++++++ .../customClassNames/class_string2.html | 65 ++++++++++++ .../group_string.expected.html | 73 ++++++++++++++ .../customClassNames/group_string.html | 73 ++++++++++++++ test/fixtures/index.js | 32 ++++-- test/reduceGroup.js | 15 +++ test/removeClassNames.js | 35 +++++-- 48 files changed, 1035 insertions(+), 357 deletions(-) create mode 100644 lib/addPseudoToNode.js create mode 100644 lib/classNamer.js create mode 100644 lib/getClassNameFns.js create mode 100644 test/api-customClassNames.js delete mode 100644 test/fixtures/addClassNameToNode/multiClass.js delete mode 100644 test/fixtures/addClassNameToNode/multiClass2.js rename test/fixtures/addClassNameToNode/{sortClass1.js => noDuplicate.js} (83%) rename test/fixtures/addClassNameToNode/{sortClass2.js => noDuplicate2.js} (82%) rename test/fixtures/addClassNameToNode/{sortClass3.js => noDuplicate3.js} (84%) create mode 100644 test/fixtures/customClassNames/class_function.expected.html create mode 100644 test/fixtures/customClassNames/class_function.html create mode 100644 test/fixtures/customClassNames/class_string.expected.html create mode 100644 test/fixtures/customClassNames/class_string.html create mode 100644 test/fixtures/customClassNames/class_string2.expected.html create mode 100644 test/fixtures/customClassNames/class_string2.html create mode 100644 test/fixtures/customClassNames/group_string.expected.html create mode 100644 test/fixtures/customClassNames/group_string.html create mode 100644 test/reduceGroup.js diff --git a/.eslintrc b/.eslintrc index f904bcb..6a42db6 100644 --- a/.eslintrc +++ b/.eslintrc @@ -80,7 +80,7 @@ "no-mixed-spaces-and-tabs": 2, "no-multi-spaces": [2, { "exceptions" : { "VariableDeclarator": true } }], "no-multi-str": 2, - "no-multiple-empty-lines": [2, { "max": 1 }], + "no-multiple-empty-lines": [2, { "max": 2 }], "no-native-reassign": 2, "no-negated-in-lhs": 2, "no-new": 2, @@ -125,7 +125,7 @@ "no-with": 2, "object-property-newline": [2, { "allowMultiplePropertiesPerLine": true }], "one-var": [2, { "initialized": "always" }], - "operator-linebreak": [2, "after", { "overrides": { "?": "before", ":": "before" } }], + "operator-linebreak": [2, "after", { "overrides": { "?": "after", ":": "after" } }], "padded-blocks": [2, "never"], "quotes": [2, "double", { "avoidEscape": true, "allowTemplateLiterals": true }], "rest-spread-spacing": [2, "never"], diff --git a/lib/addClassNameToNode.js b/lib/addClassNameToNode.js index 1de21db..052a76c 100644 --- a/lib/addClassNameToNode.js +++ b/lib/addClassNameToNode.js @@ -1,33 +1,39 @@ "use strict"; -const get = require("lodash/get"), - set = require("lodash/set"), +const get = require("lodash/get"), + set = require("lodash/set"), + uniq = require("lodash/uniq"), + concat = require("lodash/concat"); - pseudoRegexp = require("./pseudoRegexp"); -module.exports = function(node, className) { - if(get(node, [ "attrs", "class" ])) { - node.attrs.class = `${node.attrs.class} ${className}` - .split(" ") - .sort((class1, class2) => { - let class1Test = pseudoRegexp.test(class1); +module.exports = function(node) { + let classes, + pseudoClasses; - if(class1Test ? pseudoRegexp.test(class2) : !pseudoRegexp.test(class2)) { - return class1 > class2; - } + if(!node.pseudo || !node.pseudo.length) { + return node; + } + + classes = (get(node, [ "attrs", "class" ]) || "").split(" "); + + pseudoClasses = uniq(node.pseudo) + .filter((pseudo) => { + // don't add undefined classes, or if already present + if(!pseudo || classes.indexOf(pseudo) !== -1) { + return false; + } - if(pseudoRegexp.test(class2)) { - return -1; - } + return true; + }) + .sort(); - return 1; - }) - .join(" "); + delete node.pseudo; + if(!pseudoClasses.length) { return node; } - set(node, [ "attrs", "class" ], className); + set(node, [ "attrs", "class" ], concat(classes, pseudoClasses).join(" ").trim()); return node; }; diff --git a/lib/addClassNames.js b/lib/addClassNames.js index 02268af..f01b677 100644 --- a/lib/addClassNames.js +++ b/lib/addClassNames.js @@ -4,18 +4,50 @@ const pseudoRegexp = require("./pseudoRegexp"), groups = require("./groups"), - uniq = require("lodash/uniq"); + classNamer = require("./classNamer"), -module.exports = function addClassNames(classNames, include) { - include.forEach((inc) => { - if(pseudoRegexp.test(inc)) { - return classNames.push(inc); + flatten = require("lodash/flatten"); + + +function key(o, i) { + i = i || 0; + + return Object.keys(o)[i]; +} + +function value(o, i) { + i = i || 0; + + return o[key(o, i)]; +} + + +module.exports = function(classNames, include) { + include = typeof include === "string" ? [ include ] : include; + + flatten(include).forEach((inc) => { + if(typeof inc === "string") { + inc = { + classOrGroup : inc, + classNamer : classNamer + }; + } else { + inc = { + classOrGroup : key(inc), + classNamer : typeof value(inc) === "function" ? value(inc) : classNamer.bind(value(inc)) + }; + } + + if(pseudoRegexp.test(inc.classOrGroup)) { + classNames[inc.classOrGroup] = inc.classNamer; + + return; } - groups[inc].forEach((groupItem) => { - classNames.push(groupItem); + groups[inc.classOrGroup].forEach((className) => { + classNames[className] = inc.classNamer; }); }); - return uniq(classNames); + return classNames; }; diff --git a/lib/addPseudoToNode.js b/lib/addPseudoToNode.js new file mode 100644 index 0000000..d4167b1 --- /dev/null +++ b/lib/addPseudoToNode.js @@ -0,0 +1,9 @@ +"use strict"; + +module.exports = function(node, className) { + node.pseudo ? + node.pseudo.push(className) : + node.pseudo = [ className ]; + + return node; +}; diff --git a/lib/classNameFunctions/default.js b/lib/classNameFunctions/default.js index 785faf5..2e1e2a8 100644 --- a/lib/classNameFunctions/default.js +++ b/lib/classNameFunctions/default.js @@ -1,17 +1,19 @@ "use strict"; -const walk = require("posthtml/lib/api").walk, - get = require("lodash/get"), - - addClassNameToNode = require("../addClassNameToNode"), - +const className = ":default", eligibleTags = [ "button", "input" ], - className = ":default"; -module.exports = function(node) { + walk = require("posthtml/lib/api").walk, + + addPseudoToNode = require("../addPseudoToNode"), + + get = require("lodash/get"); + + +module.exports = function(classNamer, node) { let test = false; if(!node.tag || node.tag !== "form" || !node.content.length) { @@ -25,9 +27,8 @@ module.exports = function(node) { } test = true; - addClassNameToNode(subNode, className); - return subNode; + return addPseudoToNode(subNode, classNamer(className)); }); return node; diff --git a/lib/classNameFunctions/disabled.js b/lib/classNameFunctions/disabled.js index a1c39b7..d76ca7b 100644 --- a/lib/classNameFunctions/disabled.js +++ b/lib/classNameFunctions/disabled.js @@ -1,12 +1,13 @@ "use strict"; -const addClassNameToNode = require("../addClassNameToNode"), +const className = ":disabled", - className = ":disabled"; + addPseudoToNode = require("../addPseudoToNode"); -module.exports = function(node) { + +module.exports = function(classNamer, node) { if(node.attrs && typeof node.attrs.disabled === "string") { - addClassNameToNode(node, className); + addPseudoToNode(node, classNamer(className)); } return node; diff --git a/lib/classNameFunctions/empty.js b/lib/classNameFunctions/empty.js index 36ff602..6e1cdf2 100644 --- a/lib/classNameFunctions/empty.js +++ b/lib/classNameFunctions/empty.js @@ -1,19 +1,18 @@ "use strict"; -const addClassNameToNode = require("../addClassNameToNode"), - - className = ":empty", - +const className = ":empty", ineligibleTags = [ "html" - ]; + ], + + addPseudoToNode = require("../addPseudoToNode"); -module.exports = function(node) { +module.exports = function(classNamer, node) { if(!node.tag || node.content || ineligibleTags.indexOf(node.tag) >= 0) { return node; } - addClassNameToNode(node, className); + addPseudoToNode(node, classNamer(className)); return node; }; diff --git a/lib/classNameFunctions/enabled.js b/lib/classNameFunctions/enabled.js index 20adec4..f3a4696 100644 --- a/lib/classNameFunctions/enabled.js +++ b/lib/classNameFunctions/enabled.js @@ -1,11 +1,6 @@ "use strict"; -const get = require("lodash/get"), - - addClassNameToNode = require("../addClassNameToNode"), - - className = ":enabled", - +const className = ":enabled", eligibleTags = [ "a", "button", @@ -15,9 +10,14 @@ const get = require("lodash/get"), "option", "select", "textarea" - ]; + ], + + addPseudoToNode = require("../addPseudoToNode"), + + get = require("lodash/get"); + -module.exports = function(node) { +module.exports = function(classNamer, node) { if(!node.tag) { return node; } @@ -27,7 +27,7 @@ module.exports = function(node) { return node; } - addClassNameToNode(node, className); + addPseudoToNode(node, classNamer(className)); } return node; diff --git a/lib/classNameFunctions/first-child.js b/lib/classNameFunctions/first-child.js index 0664f50..61f7842 100644 --- a/lib/classNameFunctions/first-child.js +++ b/lib/classNameFunctions/first-child.js @@ -1,8 +1,11 @@ "use strict"; -const addClassNameToNode = require("../addClassNameToNode"); +const className = ":first-child", -module.exports = function(node) { + addPseudoToNode = require("../addPseudoToNode"); + + +module.exports = function(classNamer, node) { if(!node.tag || !node.content || !node.content.length) { return node; } @@ -12,7 +15,7 @@ module.exports = function(node) { return false; } - addClassNameToNode(child, ":first-child"); + addPseudoToNode(child, classNamer(className)); return true; }); diff --git a/lib/classNameFunctions/first-of-type.js b/lib/classNameFunctions/first-of-type.js index 59d957d..034a096 100644 --- a/lib/classNameFunctions/first-of-type.js +++ b/lib/classNameFunctions/first-of-type.js @@ -1,8 +1,11 @@ "use strict"; -const addClassNameToNode = require("../addClassNameToNode"); +const className = ":first-of-type", -module.exports = function(node) { + addPseudoToNode = require("../addPseudoToNode"); + + +module.exports = function(classNamer, node) { let tags = []; if(!node.tag || !node.content || !node.content.length) { @@ -15,7 +18,7 @@ module.exports = function(node) { } tags.push(child.tag); - addClassNameToNode(child, ":first-of-type"); + addPseudoToNode(child, classNamer(className)); }); return node; diff --git a/lib/classNameFunctions/last-child.js b/lib/classNameFunctions/last-child.js index ceb382a..1886ecc 100644 --- a/lib/classNameFunctions/last-child.js +++ b/lib/classNameFunctions/last-child.js @@ -1,8 +1,11 @@ "use strict"; -const addClassNameToNode = require("../addClassNameToNode"); +const className = ":last-child", -module.exports = function(node) { + addPseudoToNode = require("../addPseudoToNode"); + + +module.exports = function(classNamer, node) { let lastChild; if(!node.tag || !node.content || !node.content.length) { @@ -18,7 +21,7 @@ module.exports = function(node) { }); if(lastChild) { - addClassNameToNode(lastChild, ":last-child"); + addPseudoToNode(lastChild, classNamer(className)); } return node; diff --git a/lib/classNameFunctions/last-of-type.js b/lib/classNameFunctions/last-of-type.js index f76c5be..caa1b1d 100644 --- a/lib/classNameFunctions/last-of-type.js +++ b/lib/classNameFunctions/last-of-type.js @@ -1,10 +1,13 @@ "use strict"; -const each = require("lodash/each"), +const className = ":last-of-type", - addClassNameToNode = require("../addClassNameToNode"); + addPseudoToNode = require("../addPseudoToNode"), -module.exports = function(node) { + each = require("lodash/each"); + + +module.exports = function(classNamer, node) { let lastChildren = {}; if(!node.tag || !node.content || !node.content.length) { @@ -20,7 +23,7 @@ module.exports = function(node) { }); each(lastChildren, (child) => { - addClassNameToNode(child, ":last-of-type"); + addPseudoToNode(child, classNamer(className)); }); return node; diff --git a/lib/classNameFunctions/only-child.js b/lib/classNameFunctions/only-child.js index 019222b..70eff00 100644 --- a/lib/classNameFunctions/only-child.js +++ b/lib/classNameFunctions/only-child.js @@ -1,8 +1,11 @@ "use strict"; -const addClassNameToNode = require("../addClassNameToNode"); +const className = ":only-child", -module.exports = function(node) { + addPseudoToNode = require("../addPseudoToNode"); + + +module.exports = function(classNamer, node) { let onlyChild; if(!node.tag || !node.content || !node.content.length) { @@ -28,7 +31,7 @@ module.exports = function(node) { }); if(onlyChild) { - addClassNameToNode(onlyChild, ":only-child"); + addPseudoToNode(onlyChild, classNamer(className)); } return node; diff --git a/lib/classNameFunctions/only-of-type.js b/lib/classNameFunctions/only-of-type.js index 58383a5..27856fa 100644 --- a/lib/classNameFunctions/only-of-type.js +++ b/lib/classNameFunctions/only-of-type.js @@ -1,10 +1,13 @@ "use strict"; -const each = require("lodash/each"), +const className = ":only-of-type", - addClassNameToNode = require("../addClassNameToNode"); + addPseudoToNode = require("../addPseudoToNode"), -module.exports = function(node) { + each = require("lodash/each"); + + +module.exports = function(classNamer, node) { let tags = {}; if(!node.tag || !node.content || !node.content.length) { @@ -30,7 +33,7 @@ module.exports = function(node) { return; } - addClassNameToNode(child, ":only-of-type"); + addPseudoToNode(child, classNamer(className)); }); return node; diff --git a/lib/classNameFunctions/optional.js b/lib/classNameFunctions/optional.js index 11e3535..7ba2923 100644 --- a/lib/classNameFunctions/optional.js +++ b/lib/classNameFunctions/optional.js @@ -1,24 +1,26 @@ "use strict"; -const get = require("lodash/get"), - - addClassNameToNode = require("../addClassNameToNode"), - +const className = ":optional", eligibleTags = [ "button", "input", "select", "textarea" - ]; + ], + + addPseudoToNode = require("../addPseudoToNode"), + + get = require("lodash/get"); + -module.exports = function(node) { +module.exports = function(classNamer, node) { if(!node.tag || eligibleTags.indexOf(node.tag) === -1 || typeof get(node, [ "attrs", "required"]) === "string") { return node; } - addClassNameToNode(node, ":optional"); + addPseudoToNode(node, classNamer(className)); return node; }; diff --git a/lib/classNameFunctions/read-only.js b/lib/classNameFunctions/read-only.js index 4b5b5dd..a1f228e 100644 --- a/lib/classNameFunctions/read-only.js +++ b/lib/classNameFunctions/read-only.js @@ -1,23 +1,25 @@ "use strict"; -const get = require("lodash/get"), - - addClassNameToNode = require("../addClassNameToNode"), - +const className = ":read-only", eligibleTags = [ "input", "select", "textarea" - ]; + ], + + addPseudoToNode = require("../addPseudoToNode"), + + get = require("lodash/get"); + -module.exports = function(node) { +module.exports = function(classNamer, node) { if(!node.tag || eligibleTags.indexOf(node.tag) === -1 || typeof get(node, [ "attrs", "readonly"]) !== "string") { return node; } - addClassNameToNode(node, ":read-only"); + addPseudoToNode(node, classNamer(className)); return node; }; diff --git a/lib/classNameFunctions/read-write.js b/lib/classNameFunctions/read-write.js index c45df0c..c93b5a5 100644 --- a/lib/classNameFunctions/read-write.js +++ b/lib/classNameFunctions/read-write.js @@ -1,23 +1,25 @@ "use strict"; -const get = require("lodash/get"), - - addClassNameToNode = require("../addClassNameToNode"), - +const className = ":read-write", eligibleTags = [ "input", "select", "textarea" - ]; + ], + + addPseudoToNode = require("../addPseudoToNode"), + + get = require("lodash/get"); + -module.exports = function(node) { +module.exports = function(classNamer, node) { if(!node.tag || eligibleTags.indexOf(node.tag) === -1 || - typeof get(node, [ "attrs", "readonly"]) === "string") { + typeof get(node, [ "attrs", "readonly" ]) === "string") { return node; } - addClassNameToNode(node, ":read-write"); + addPseudoToNode(node, classNamer(className)); return node; }; diff --git a/lib/classNameFunctions/required.js b/lib/classNameFunctions/required.js index d218dfb..36cfe1d 100644 --- a/lib/classNameFunctions/required.js +++ b/lib/classNameFunctions/required.js @@ -1,23 +1,25 @@ "use strict"; -const get = require("lodash/get"), - - addClassNameToNode = require("../addClassNameToNode"), - +const className = ":required", eligibleTags = [ "input", "select", "textarea" - ]; + ], + + addPseudoToNode = require("../addPseudoToNode"), + + get = require("lodash/get"); + -module.exports = function(node) { +module.exports = function(classNamer, node) { if(!node.tag || eligibleTags.indexOf(node.tag) === -1 || typeof get(node, [ "attrs", "required"]) !== "string") { return node; } - addClassNameToNode(node, ":required"); + addPseudoToNode(node, classNamer(className)); return node; }; diff --git a/lib/classNameFunctions/root.js b/lib/classNameFunctions/root.js index 380b3cf..69b96ea 100644 --- a/lib/classNameFunctions/root.js +++ b/lib/classNameFunctions/root.js @@ -1,13 +1,16 @@ "use strict"; -const addClassNameToNode = require("../addClassNameToNode"); +const className = ":root", -module.exports = function(node) { + addPseudoToNode = require("../addPseudoToNode"); + + +module.exports = function(classNamer, node) { if(node.tag !== "html") { return node; } - addClassNameToNode(node, ":root"); + addPseudoToNode(node, classNamer(className)); return node; }; diff --git a/lib/classNamer.js b/lib/classNamer.js new file mode 100644 index 0000000..59f8ba6 --- /dev/null +++ b/lib/classNamer.js @@ -0,0 +1,5 @@ +"use strict"; + +module.exports = function(name) { + return this || name; +}; diff --git a/lib/getClassNameFns.js b/lib/getClassNameFns.js new file mode 100644 index 0000000..7962e54 --- /dev/null +++ b/lib/getClassNameFns.js @@ -0,0 +1,23 @@ +"use strict"; + +const classNameFunctions = require("./classNameFunctions"), + + addClassNames = require("./addClassNames"), + removeClassNames = require("./removeClassNames"), + + each = require("lodash/each"); + + +module.exports = function(config) { + let classNames = {}, + classFunctions = {}; + + classNames = addClassNames(classNames, config.include); + classNames = removeClassNames(classNames, config.exclude); + + each(classNames, (fn, className) => { + classFunctions[className] = classNameFunctions[className].bind(null, fn); + }); + + return classFunctions; +}; diff --git a/lib/pseudo.js b/lib/pseudo.js index 93880e3..a38cec6 100644 --- a/lib/pseudo.js +++ b/lib/pseudo.js @@ -1,36 +1,23 @@ "use strict"; -const defaults = require("lodash/defaults"), - flatten = require("lodash/flatten"), - - classNameFunctions = require("./classNameFunctions"), - - addClassNames = require("./addClassNames"), - removeClassNames = require("./removeClassNames"), - - defaultConfig = { +const defaultConfig = { include : [ "all" ], exclude : [] - }; + }, + + getClassNameFns = require("./getClassNameFns"), + addClassNameToNode = require("./addClassNameToNode"), -function shallowArray(item) { - if(!Array.isArray(item)) { - item = [ item ]; - } + defaults = require("lodash/defaults"), + each = require("lodash/each"); - return flatten(item); -} module.exports = function(config) { - let classNames = []; + let classNameFns = {}; config = defaults(config, defaultConfig); - config.include = shallowArray(config.include); - config.exclude = shallowArray(config.exclude); - - classNames = addClassNames(classNames, config.include); - classNames = removeClassNames(classNames, config.exclude); + classNameFns = getClassNameFns(config); return function postthmlPseudo(tree) { let head, @@ -49,23 +36,25 @@ module.exports = function(config) { return true; } + return false; }); return html; }); - // add pseudo class names + // add pseudo class names to temp attr tree.walk((node) => { - classNames - .sort() - .forEach((className) => { - node = classNameFunctions[className](node); - }); + each(classNameFns, (fn, cn) => { + node = fn(node); + }); return node; }); + // add the class names to the class attr (sorted) + tree.walk((node) => addClassNameToNode(node)); + // add back if(head) { tree.match({ tag : "html" }, (html) => { diff --git a/lib/removeClassNames.js b/lib/removeClassNames.js index 9bd33d7..9416719 100644 --- a/lib/removeClassNames.js +++ b/lib/removeClassNames.js @@ -4,16 +4,21 @@ const pseudoRegexp = require("./pseudoRegexp"), groups = require("./groups"), - pull = require("lodash/pull"); + flatten = require("lodash/flatten"); + module.exports = function(classNames, exclude) { - exclude.forEach((exc) => { + exclude = typeof exclude === "string" ? [ exclude ] : exclude; + + flatten(exclude).forEach((exc) => { if(pseudoRegexp.test(exc)) { - return pull(classNames, exc); + delete classNames[exc]; + + return; } groups[exc].forEach((groupItem) => { - pull(classNames, groupItem); + delete classNames[groupItem]; }); }); diff --git a/test/addClassNameToNode.js b/test/addClassNameToNode.js index 91c1726..85ff1e8 100644 --- a/test/addClassNameToNode.js +++ b/test/addClassNameToNode.js @@ -1,43 +1,36 @@ "use strict"; -const assert = require("assert"), +const addClassNameToNode = require("../lib/addClassNameToNode"), - addClassNameToNode = require("../lib/addClassNameToNode"), + fixtures = require("./fixtures").addClassNameToNode, + + assert = require("assert"); - fixtures = require("./fixtures").addClassNameToNode; 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); + assert.deepEqual(addClassNameToNode(fixtures.noClass.input), fixtures.noClass.expected); }); it("should add a class name to a node with an empty class attr", () => { - assert.deepEqual(addClassNameToNode(fixtures.emptyClass.input, ":test"), fixtures.emptyClass.expected); + assert.deepEqual(addClassNameToNode(fixtures.emptyClass.input), fixtures.emptyClass.expected); }); 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); }); - 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); - }); - - 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); - }); - - 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); + it("should not duplicate a class name", () => { + assert.deepEqual(addClassNameToNode(fixtures.noDuplicate.input, ":test"), fixtures.noDuplicate.expected); }); - it("should add a class name sorted properly to a node with a multiple classes", () => { - assert.deepEqual(addClassNameToNode(fixtures.multiClass.input, ":first-of-type"), fixtures.multiClass.expected); + it("should not duplicate a class name 2", () => { + assert.deepEqual(addClassNameToNode(fixtures.noDuplicate2.input, ":test"), fixtures.noDuplicate2.expected); }); - it("should add a class name sorted properly to a node with a multiple classes 2", () => { - assert.deepEqual(addClassNameToNode(fixtures.multiClass2.input, ":first-of-type"), fixtures.multiClass2.expected); + it("should not duplicate a class name 3", () => { + assert.deepEqual(addClassNameToNode(fixtures.noDuplicate3.input, ":test"), fixtures.noDuplicate3.expected); }); }); }); diff --git a/test/addClassNames.js b/test/addClassNames.js index 1f8e992..b95c153 100644 --- a/test/addClassNames.js +++ b/test/addClassNames.js @@ -1,48 +1,62 @@ "use strict"; -const assert = require("assert"), +const addClassNames = require("../lib/addClassNames"), - forEach = require("lodash/forEach"), - concat = require("lodash/concat"), + groups = require("../lib/groups"), + + classNamer = require("../lib/classNamer"), + + reduceGroup = require("./reduceGroup"), + + assert = require("assert"), - addClassNames = require("../lib/addClassNames"), + forEach = require("lodash/forEach"), + merge = require("lodash/merge"); - groups = require("../lib/groups"); describe("/lib", () => { describe("/addClassNames.js", () => { forEach(groups, (classNames, groupName) => { it(`should add all classes from the ${groupName} group`, () => { - assert.deepEqual(addClassNames([], [ groupName ]), groups[groupName]); + assert.deepEqual(addClassNames({}, [ groupName ]), reduceGroup(groupName)); }); }); it("should add individual class names", () => { - assert.deepEqual(addClassNames([], [ ":first-child" ]), [ ":first-child"]); + assert.deepEqual(addClassNames({}, [ ":first-child" ]), { ":first-child" : classNamer }); }); it("should merge a class name", () => { - assert.deepEqual(addClassNames([ ":first-child" ], [ ":last-child" ]), [ ":first-child", ":last-child" ]); + assert.deepEqual(addClassNames({ ":first-child" : classNamer }, [ ":last-child" ]), { ":first-child" : classNamer, ":last-child" : classNamer }); }); it("should merge a class group", () => { - assert.deepEqual(addClassNames([ ":first-child" ], [ "only" ]), concat([ ":first-child" ], groups.only)); + assert.deepEqual(addClassNames({ ":first-child" : classNamer }, [ "only" ]), reduceGroup("only", { ":first-child" : classNamer })); }); it("should add multiple class names", () => { - assert.deepEqual(addClassNames([], [ ":first-child", ":last-child" ]), [ ":first-child", ":last-child" ]); + assert.deepEqual(addClassNames({}, [ ":first-child", ":last-child" ]), { ":first-child" : classNamer, ":last-child" : classNamer }); }); it("should merge multiple class names", () => { - assert.deepEqual(addClassNames([ ":read-only" ], [ ":first-child", ":last-child" ]), [ ":read-only", ":first-child", ":last-child" ]); + assert.deepEqual(addClassNames({ ":read-only" : classNamer }, [ ":first-child", ":last-child" ]), { + ":read-only" : classNamer, + ":first-child" : classNamer, + ":last-child" : classNamer + }); }); it("should add class names and group names", () => { - assert.deepEqual(addClassNames([], [ ":first-child", "form" ]), concat([ ":first-child" ], groups.form)); + assert.deepEqual(addClassNames({}, [ ":first-child", "form" ]), reduceGroup("form", { ":first-child" : classNamer })); }); it("should merge class names and group names", () => { - assert.deepEqual(addClassNames([ ":root" ], [ "form", ":first-child" ]), concat([ ":root" ], groups.form, ":first-child")); + assert.deepEqual(addClassNames({ ":root" : classNamer }, [ "form", ":first-child" ]), + merge({ ":root" : classNamer }, reduceGroup("form", { ":first-child" : classNamer }))); + }); + + it("should add class names without duplication", () => { + assert.deepEqual(addClassNames({}, [ "all", ":first-child" ]), reduceGroup("all")); }); }); }); diff --git a/test/api-classNames.js b/test/api-classNames.js index e8be929..382fcb1 100644 --- a/test/api-classNames.js +++ b/test/api-classNames.js @@ -1,161 +1,162 @@ "use strict"; -const assert = require("assert"), - +const pseudo = require("../index"), posthtml = require("posthtml"), - pseudo = require("../index"), - fixtures = require("./fixtures"); + fixtures = require("./fixtures").classNames, + + assert = require("assert"); + describe("/lib", () => { describe("/pseudo.js", () => { // :default - it("should add default", () => + it("should add :default", () => posthtml() .use(pseudo({ include : ":default" })) - .process(fixtures.classNames[":default"].input) + .process(fixtures[":default"].input) .then((result) => { - assert.equal(result.html, fixtures.classNames[":default"].expected); + assert.equal(result.html, fixtures[":default"].expected); }) ); // :disabled - it("should add disabled", () => + it("should add :disabled", () => posthtml() .use(pseudo({ include : ":disabled" })) - .process(fixtures.classNames[":disabled"].input) + .process(fixtures[":disabled"].input) .then((result) => { - assert.equal(result.html, fixtures.classNames[":disabled"].expected); + assert.equal(result.html, fixtures[":disabled"].expected); }) ); // :empty - it("should add empty", () => + it("should add :empty", () => posthtml() .use(pseudo({ include : ":empty" })) - .process(fixtures.classNames[":empty"].input) + .process(fixtures[":empty"].input) .then((result) => { - assert.equal(result.html, fixtures.classNames[":empty"].expected); + assert.equal(result.html, fixtures[":empty"].expected); }) ); // :enabled - it("should add enabled", () => + it("should add :enabled", () => posthtml() .use(pseudo({ include : ":enabled" })) - .process(fixtures.classNames[":enabled"].input) + .process(fixtures[":enabled"].input) .then((result) => { - assert.equal(result.html, fixtures.classNames[":enabled"].expected); + assert.equal(result.html, fixtures[":enabled"].expected); }) ); // :first-child - it("should add first-child", () => + it("should add :first-child", () => posthtml() .use(pseudo({ include : ":first-child" })) - .process(fixtures.classNames[":first-child"].input) + .process(fixtures[":first-child"].input) .then((result) => { - assert.equal(result.html, fixtures.classNames[":first-child"].expected); + assert.equal(result.html, fixtures[":first-child"].expected); }) ); // :first-of-type - it("should add first-of-type", () => + it("should add :first-of-type", () => posthtml() .use(pseudo({ include : ":first-of-type" })) - .process(fixtures.classNames[":first-of-type"].input) + .process(fixtures[":first-of-type"].input) .then((result) => { - assert.equal(result.html, fixtures.classNames[":first-of-type"].expected); + assert.equal(result.html, fixtures[":first-of-type"].expected); }) ); // :last-child - it("should add last-child", () => + it("should add :last-child", () => posthtml() .use(pseudo({ include : ":last-child" })) - .process(fixtures.classNames[":last-child"].input) + .process(fixtures[":last-child"].input) .then((result) => { - assert.equal(result.html, fixtures.classNames[":last-child"].expected); + assert.equal(result.html, fixtures[":last-child"].expected); }) ); // :last-of-type - it("should add last-of-type", () => + it("should add :last-of-type", () => posthtml() .use(pseudo({ include : ":last-of-type" })) - .process(fixtures.classNames[":last-of-type"].input) + .process(fixtures[":last-of-type"].input) .then((result) => { - assert.equal(result.html, fixtures.classNames[":last-of-type"].expected); + assert.equal(result.html, fixtures[":last-of-type"].expected); }) ); // :only-child - it("should add only-child", () => + it("should add :only-child", () => posthtml() .use(pseudo({ include : ":only-child" })) - .process(fixtures.classNames[":only-child"].input) + .process(fixtures[":only-child"].input) .then((result) => { - assert.equal(result.html, fixtures.classNames[":only-child"].expected); + assert.equal(result.html, fixtures[":only-child"].expected); }) ); // :only-of-type - it("should add only-of-type", () => + it("should add :only-of-type", () => posthtml() .use(pseudo({ include : ":only-of-type" })) - .process(fixtures.classNames[":only-of-type"].input) + .process(fixtures[":only-of-type"].input) .then((result) => { - assert.equal(result.html, fixtures.classNames[":only-of-type"].expected); + assert.equal(result.html, fixtures[":only-of-type"].expected); }) ); // :optional - it("should add optional", () => + it("should add :optional", () => posthtml() .use(pseudo({ include : ":optional" })) - .process(fixtures.classNames[":optional"].input) + .process(fixtures[":optional"].input) .then((result) => { - assert.equal(result.html, fixtures.classNames[":optional"].expected); + assert.equal(result.html, fixtures[":optional"].expected); }) ); // :read-only - it("should add read-only", () => + it("should add :read-only", () => posthtml() .use(pseudo({ include : ":read-only" })) - .process(fixtures.classNames[":read-only"].input) + .process(fixtures[":read-only"].input) .then((result) => { - assert.equal(result.html, fixtures.classNames[":read-only"].expected); + assert.equal(result.html, fixtures[":read-only"].expected); }) ); // :read-write - it("should add read-write", () => + it("should add :read-write", () => posthtml() .use(pseudo({ include : ":read-write" })) - .process(fixtures.classNames[":read-write"].input) + .process(fixtures[":read-write"].input) .then((result) => { - assert.equal(result.html, fixtures.classNames[":read-write"].expected); + assert.equal(result.html, fixtures[":read-write"].expected); }) ); // :required - it("should add required", () => + it("should add :required", () => posthtml() .use(pseudo({ include : ":required" })) - .process(fixtures.classNames[":required"].input) + .process(fixtures[":required"].input) .then((result) => { - assert.equal(result.html, fixtures.classNames[":required"].expected); + assert.equal(result.html, fixtures[":required"].expected); }) ); // :root - it("should add root", () => + it("should add :root", () => posthtml() .use(pseudo({ include : ":root" })) - .process(fixtures.classNames[":root"].input) + .process(fixtures[":root"].input) .then((result) => { - assert.equal(result.html, fixtures.classNames[":root"].expected); + assert.equal(result.html, fixtures[":root"].expected); }) ); }); diff --git a/test/api-customClassNames.js b/test/api-customClassNames.js new file mode 100644 index 0000000..bcb40af --- /dev/null +++ b/test/api-customClassNames.js @@ -0,0 +1,65 @@ +"use strict"; + +const pseudo = require("../"), + posthtml = require("posthtml"), + + fixtures = require("./fixtures").customClassNames, + + assert = require("assert"); + + +describe("/lib", () => { + describe("/pseudo.js", () => { + it("should add custom class name for a pseudo class", () => + posthtml() + .use(pseudo({ + include : [{ ":first-child" : "fc" }] + })) + .process(fixtures.class_string.input) + .then((result) => { + assert.equal(result.html, fixtures.class_string.expected); + }) + ); + + it("should add custom class name for a class group", () => + posthtml() + .use(pseudo({ + include : [{ "firstLastOnly" : "flo" }] + })) + .process(fixtures.group_string.input) + .then((result) => { + assert.equal(result.html, fixtures.group_string.expected); + }) + ); + + it("should add different custom class names for different pseudo classes", () => + posthtml() + .use(pseudo({ + include : [{ + ":first-child" : "fc" + }, { + ":last-child" : "lc" + }] + })) + .process(fixtures.class_string2.input) + .then((result) => { + assert.equal(result.html, fixtures.class_string2.expected); + }) + ); + + it("should add custom class name using function", () => + posthtml() + .use(pseudo({ + include : [{ + "firstLastOnly" : (className) => { + return className.replace(/:|-/g, ""); + } + }] + })) + .process(fixtures.class_function.input) + .then((result) => { + assert.equal(result.html, fixtures.class_function.expected); + }) + ); + }); +}); diff --git a/test/api-groups.js b/test/api-groups.js index e9268e0..cf45b5b 100644 --- a/test/api-groups.js +++ b/test/api-groups.js @@ -1,11 +1,12 @@ "use strict"; -const assert = require("assert"), - +const pseudo = require("../"), posthtml = require("posthtml"), - pseudo = require("../index"), - fixtures = require("./fixtures"); + fixtures = require("./fixtures").groups, + + assert = require("assert"); + describe("/lib", () => { describe("/pseudo.js", () => { @@ -13,9 +14,9 @@ describe("/lib", () => { it("should run posthtml with pseudo using group all", () => posthtml() .use(pseudo({ include : "all" })) - .process(fixtures.groups.all.input) + .process(fixtures.all.input) .then((result) => { - assert.equal(result.html, fixtures.groups.all.expected); + assert.equal(result.html, fixtures.all.expected); }) ); @@ -23,9 +24,9 @@ describe("/lib", () => { it("should run posthtml with pseudo using group firstLastOnly", () => posthtml() .use(pseudo({ include : "firstLastOnly" })) - .process(fixtures.groups.firstLastOnly.input) + .process(fixtures.firstLastOnly.input) .then((result) => { - assert.equal(result.html, fixtures.groups.firstLastOnly.expected); + assert.equal(result.html, fixtures.firstLastOnly.expected); }) ); @@ -33,9 +34,9 @@ describe("/lib", () => { it("should run posthtml with pseudo using group firstLast", () => posthtml() .use(pseudo({ include : "firstLast" })) - .process(fixtures.groups.firstLast.input) + .process(fixtures.firstLast.input) .then((result) => { - assert.equal(result.html, fixtures.groups.firstLast.expected); + assert.equal(result.html, fixtures.firstLast.expected); }) ); @@ -43,9 +44,9 @@ describe("/lib", () => { it("should run posthtml with pseudo using group form", () => posthtml() .use(pseudo({ include : "form" })) - .process(fixtures.groups.form.input) + .process(fixtures.form.input) .then((result) => { - assert.equal(result.html, fixtures.groups.form.expected); + assert.equal(result.html, fixtures.form.expected); }) ); @@ -53,9 +54,9 @@ describe("/lib", () => { it("should run posthtml with pseudo using group only", () => posthtml() .use(pseudo({ include : "only" })) - .process(fixtures.groups.only.input) + .process(fixtures.only.input) .then((result) => { - assert.equal(result.html, fixtures.groups.only.expected); + assert.equal(result.html, fixtures.only.expected); }) ); @@ -63,9 +64,9 @@ describe("/lib", () => { it("should run posthtml with pseudo using group readWrite", () => posthtml() .use(pseudo({ include : "readWrite" })) - .process(fixtures.groups.readWrite.input) + .process(fixtures.readWrite.input) .then((result) => { - assert.equal(result.html, fixtures.groups.readWrite.expected); + assert.equal(result.html, fixtures.readWrite.expected); }) ); }); diff --git a/test/api.js b/test/api.js index b74b299..d58789b 100644 --- a/test/api.js +++ b/test/api.js @@ -1,11 +1,12 @@ "use strict"; -const assert = require("assert"), - +const pseudo = require("../"), posthtml = require("posthtml"), - pseudo = require("../"), - fixtures = require("./fixtures"); + fixtures = require("./fixtures"), + + assert = require("assert"); + describe("/lib", () => { describe("/pseudo.js", () => { diff --git a/test/fixtures/addClassNameToNode/emptyClass.js b/test/fixtures/addClassNameToNode/emptyClass.js index a7fd31d..798ca0d 100644 --- a/test/fixtures/addClassNameToNode/emptyClass.js +++ b/test/fixtures/addClassNameToNode/emptyClass.js @@ -18,7 +18,8 @@ module.exports = { content : ["Cat"] }, "\n" - ] + ], + pseudo : [ ":test" ] }, expected : { tag : "a", diff --git a/test/fixtures/addClassNameToNode/endClass.js b/test/fixtures/addClassNameToNode/endClass.js index 01e2490..b3f979c 100644 --- a/test/fixtures/addClassNameToNode/endClass.js +++ b/test/fixtures/addClassNameToNode/endClass.js @@ -18,7 +18,8 @@ module.exports = { content : ["Cat"] }, "\n" - ] + ], + pseudo : [ ":test" ] }, expected : { tag : "a", diff --git a/test/fixtures/addClassNameToNode/multiClass.js b/test/fixtures/addClassNameToNode/multiClass.js deleted file mode 100644 index 1b8a232..0000000 --- a/test/fixtures/addClassNameToNode/multiClass.js +++ /dev/null @@ -1,42 +0,0 @@ -"use strict"; - -module.exports = { - input : { - tag : "a", - attrs : { - href : "#", - class : "something another-thing :first-child" - }, - content : [ - "\n ", - { - tag : "span", - attrs : { - class : "animals__cat", - style : "background: url(cat.png)" - }, - content : ["Cat"] - }, - "\n" - ] - }, - expected : { - tag : "a", - attrs : { - class : "another-thing something :first-child :first-of-type", - href : "#" - }, - content : [ - "\n ", - { - tag : "span", - attrs : { - class : "animals__cat", - style : "background: url(cat.png)" - }, - content : ["Cat"] - }, - "\n" - ] - } -}; diff --git a/test/fixtures/addClassNameToNode/multiClass2.js b/test/fixtures/addClassNameToNode/multiClass2.js deleted file mode 100644 index 2076398..0000000 --- a/test/fixtures/addClassNameToNode/multiClass2.js +++ /dev/null @@ -1,42 +0,0 @@ -"use strict"; - -module.exports = { - input : { - tag : "a", - attrs : { - href : "#", - class : "something :first-child another-thing" - }, - content : [ - "\n ", - { - tag : "span", - attrs : { - class : "animals__cat", - style : "background: url(cat.png)" - }, - content : ["Cat"] - }, - "\n" - ] - }, - expected : { - tag : "a", - attrs : { - class : "another-thing something :first-child :first-of-type", - href : "#" - }, - content : [ - "\n ", - { - tag : "span", - attrs : { - class : "animals__cat", - style : "background: url(cat.png)" - }, - content : ["Cat"] - }, - "\n" - ] - } -}; diff --git a/test/fixtures/addClassNameToNode/noClass.js b/test/fixtures/addClassNameToNode/noClass.js index 4ef57b5..cb40285 100644 --- a/test/fixtures/addClassNameToNode/noClass.js +++ b/test/fixtures/addClassNameToNode/noClass.js @@ -17,7 +17,8 @@ module.exports = { content : ["Cat"] }, "\n" - ] + ], + pseudo : [ ":test" ] }, expected : { tag : "a", diff --git a/test/fixtures/addClassNameToNode/sortClass1.js b/test/fixtures/addClassNameToNode/noDuplicate.js similarity index 83% rename from test/fixtures/addClassNameToNode/sortClass1.js rename to test/fixtures/addClassNameToNode/noDuplicate.js index 111b6e3..dda1dfb 100644 --- a/test/fixtures/addClassNameToNode/sortClass1.js +++ b/test/fixtures/addClassNameToNode/noDuplicate.js @@ -5,7 +5,7 @@ module.exports = { tag : "a", attrs : { href : "#", - class : "something :first-child" + class : "something :test" }, content : [ "\n ", @@ -18,12 +18,13 @@ module.exports = { content : ["Cat"] }, "\n" - ] + ], + pseudo : [ ":test" ] }, expected : { tag : "a", attrs : { - class : "something :first-child :first-of-type", + class : "something :test", href : "#" }, content : [ diff --git a/test/fixtures/addClassNameToNode/sortClass2.js b/test/fixtures/addClassNameToNode/noDuplicate2.js similarity index 82% rename from test/fixtures/addClassNameToNode/sortClass2.js rename to test/fixtures/addClassNameToNode/noDuplicate2.js index 76edfe9..3a6a791 100644 --- a/test/fixtures/addClassNameToNode/sortClass2.js +++ b/test/fixtures/addClassNameToNode/noDuplicate2.js @@ -5,7 +5,7 @@ module.exports = { tag : "a", attrs : { href : "#", - class : "something :first-of-type" + class : "something :test something" }, content : [ "\n ", @@ -18,12 +18,13 @@ module.exports = { content : ["Cat"] }, "\n" - ] + ], + pseudo : [ ":test" ] }, expected : { tag : "a", attrs : { - class : "something :first-child :first-of-type", + class : "something :test something", href : "#" }, content : [ diff --git a/test/fixtures/addClassNameToNode/sortClass3.js b/test/fixtures/addClassNameToNode/noDuplicate3.js similarity index 84% rename from test/fixtures/addClassNameToNode/sortClass3.js rename to test/fixtures/addClassNameToNode/noDuplicate3.js index 3af886d..a98def7 100644 --- a/test/fixtures/addClassNameToNode/sortClass3.js +++ b/test/fixtures/addClassNameToNode/noDuplicate3.js @@ -5,7 +5,7 @@ module.exports = { tag : "a", attrs : { href : "#", - class : "something :last-child" + class : ":test" }, content : [ "\n ", @@ -18,12 +18,13 @@ module.exports = { content : ["Cat"] }, "\n" - ] + ], + pseudo : [ ":test" ] }, expected : { tag : "a", attrs : { - class : "something :first-child :last-child", + class : ":test", href : "#" }, content : [ diff --git a/test/fixtures/customClassNames/class_function.expected.html b/test/fixtures/customClassNames/class_function.expected.html new file mode 100644 index 0000000..2c6effb --- /dev/null +++ b/test/fixtures/customClassNames/class_function.expected.html @@ -0,0 +1,65 @@ + + + + page title + + +

header

+
+ + + + + + + +
+ +
+ outer +
Inner
+
+ +
+ + + + + +

+ This paragraph + contains a lot of lines + in the source code, + but the browser + ignores it. + no href and empty href +

+ +

+ This paragraph + contains a lot of spaces + in the source code, + but the browser + ignores it. +

+ +

+ The number of lines in a paragraph depends on the size of the browser window. If you resize the browser window, the number of lines in this paragraph will change. +

+ + \ No newline at end of file diff --git a/test/fixtures/customClassNames/class_function.html b/test/fixtures/customClassNames/class_function.html new file mode 100644 index 0000000..d63dae9 --- /dev/null +++ b/test/fixtures/customClassNames/class_function.html @@ -0,0 +1,65 @@ + + + + page title + + +

header

+
+ + + + + + + +
+ +
+ outer +
Inner
+
+ +
+ + + + + +

+ This paragraph + contains a lot of lines + in the source code, + but the browser + ignores it. + no href and empty href +

+ +

+ This paragraph + contains a lot of spaces + in the source code, + but the browser + ignores it. +

+ +

+ The number of lines in a paragraph depends on the size of the browser window. If you resize the browser window, the number of lines in this paragraph will change. +

+ + \ No newline at end of file diff --git a/test/fixtures/customClassNames/class_string.expected.html b/test/fixtures/customClassNames/class_string.expected.html new file mode 100644 index 0000000..3599b46 --- /dev/null +++ b/test/fixtures/customClassNames/class_string.expected.html @@ -0,0 +1,65 @@ + + + + page title + + +

header

+
+ + + + + + + +
+ +
+ outer +
Inner
+
+ +
+ + + + + +

+ This paragraph + contains a lot of lines + in the source code, + but the browser + ignores it. + no href and empty href +

+ +

+ This paragraph + contains a lot of spaces + in the source code, + but the browser + ignores it. +

+ +

+ The number of lines in a paragraph depends on the size of the browser window. If you resize the browser window, the number of lines in this paragraph will change. +

+ + \ No newline at end of file diff --git a/test/fixtures/customClassNames/class_string.html b/test/fixtures/customClassNames/class_string.html new file mode 100644 index 0000000..d63dae9 --- /dev/null +++ b/test/fixtures/customClassNames/class_string.html @@ -0,0 +1,65 @@ + + + + page title + + +

header

+
+ + + + + + + +
+ +
+ outer +
Inner
+
+ +
+ + + + + +

+ This paragraph + contains a lot of lines + in the source code, + but the browser + ignores it. + no href and empty href +

+ +

+ This paragraph + contains a lot of spaces + in the source code, + but the browser + ignores it. +

+ +

+ The number of lines in a paragraph depends on the size of the browser window. If you resize the browser window, the number of lines in this paragraph will change. +

+ + \ No newline at end of file diff --git a/test/fixtures/customClassNames/class_string2.expected.html b/test/fixtures/customClassNames/class_string2.expected.html new file mode 100644 index 0000000..e62096e --- /dev/null +++ b/test/fixtures/customClassNames/class_string2.expected.html @@ -0,0 +1,65 @@ + + + + page title + + +

header

+
+ + + + + + + +
+ +
+ outer +
Inner
+
+ +
+ + + + + +

+ This paragraph + contains a lot of lines + in the source code, + but the browser + ignores it. + no href and empty href +

+ +

+ This paragraph + contains a lot of spaces + in the source code, + but the browser + ignores it. +

+ +

+ The number of lines in a paragraph depends on the size of the browser window. If you resize the browser window, the number of lines in this paragraph will change. +

+ + \ No newline at end of file diff --git a/test/fixtures/customClassNames/class_string2.html b/test/fixtures/customClassNames/class_string2.html new file mode 100644 index 0000000..d63dae9 --- /dev/null +++ b/test/fixtures/customClassNames/class_string2.html @@ -0,0 +1,65 @@ + + + + page title + + +

header

+
+ + + + + + + +
+ +
+ outer +
Inner
+
+ +
+ + + + + +

+ This paragraph + contains a lot of lines + in the source code, + but the browser + ignores it. + no href and empty href +

+ +

+ This paragraph + contains a lot of spaces + in the source code, + but the browser + ignores it. +

+ +

+ The number of lines in a paragraph depends on the size of the browser window. If you resize the browser window, the number of lines in this paragraph will change. +

+ + \ No newline at end of file diff --git a/test/fixtures/customClassNames/group_string.expected.html b/test/fixtures/customClassNames/group_string.expected.html new file mode 100644 index 0000000..9b48525 --- /dev/null +++ b/test/fixtures/customClassNames/group_string.expected.html @@ -0,0 +1,73 @@ + + + + page title + + +

header

+
+ + + + + + + + + + + +
+ +
+ outer +
Inner
+
+ +
+ + + + + +

+ This paragraph + contains a lot of lines + in the source code, + but the browser + ignores it. + no href and empty href +

+ +

+ This paragraph + contains a lot of spaces + in the source code, + but the browser + ignores it. +

+ +

+ The number of lines in a paragraph depends on the size of the browser window. If you resize the browser window, the number of lines in this paragraph will change. +

+ + diff --git a/test/fixtures/customClassNames/group_string.html b/test/fixtures/customClassNames/group_string.html new file mode 100644 index 0000000..75c5135 --- /dev/null +++ b/test/fixtures/customClassNames/group_string.html @@ -0,0 +1,73 @@ + + + + page title + + +

header

+
+ + + + + + + + + + + +
+ +
+ outer +
Inner
+
+ +
+ + + + + +

+ This paragraph + contains a lot of lines + in the source code, + but the browser + ignores it. + no href and empty href +

+ +

+ This paragraph + contains a lot of spaces + in the source code, + but the browser + ignores it. +

+ +

+ The number of lines in a paragraph depends on the size of the browser window. If you resize the browser window, the number of lines in this paragraph will change. +

+ + diff --git a/test/fixtures/index.js b/test/fixtures/index.js index 5b94d3e..5f9a495 100644 --- a/test/fixtures/index.js +++ b/test/fixtures/index.js @@ -116,13 +116,29 @@ module.exports = { } }, 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"), - multiClass : require("./addClassNameToNode/multiClass"), - multiClass2 : require("./addClassNameToNode/multiClass2") + noClass : require("./addClassNameToNode/noClass"), + emptyClass : require("./addClassNameToNode/emptyClass"), + endClass : require("./addClassNameToNode/endClass"), + noDuplicate : require("./addClassNameToNode/noDuplicate"), + noDuplicate2 : require("./addClassNameToNode/noDuplicate2"), + noDuplicate3 : require("./addClassNameToNode/noDuplicate3") + }, + customClassNames : { + class_string : { + input : require("./customClassNames/class_string"), + expected : require("./customClassNames/class_string.expected") + }, + group_string : { + input : require("./customClassNames/group_string"), + expected : require("./customClassNames/group_string.expected") + }, + class_string2 : { + input : require("./customClassNames/class_string2"), + expected : require("./customClassNames/class_string2.expected") + }, + class_function : { + input : require("./customClassNames/class_function"), + expected : require("./customClassNames/class_function.expected") + } } }; diff --git a/test/reduceGroup.js b/test/reduceGroup.js new file mode 100644 index 0000000..b55c156 --- /dev/null +++ b/test/reduceGroup.js @@ -0,0 +1,15 @@ +"use strict"; + +const groups = require("../lib/groups"), + + classNamer = require("../lib/classNamer"); + +module.exports = function(groupName, base) { + base = base || {}; + + return groups[groupName].reduce((acc, cur) => { + acc[cur] = classNamer; + + return acc; + }, base); +}; diff --git a/test/removeClassNames.js b/test/removeClassNames.js index 7e2232e..92d3206 100644 --- a/test/removeClassNames.js +++ b/test/removeClassNames.js @@ -1,37 +1,52 @@ "use strict"; -const assert = require("assert"), +const removeClassNames = require("../lib/removeClassNames"), + + groups = require("../lib/groups"), + + classNamer = require("../lib/classNamer"), + + reduceGroup = require("./reduceGroup"), + + assert = require("assert"), forEach = require("lodash/forEach"), - clone = require("lodash/clone"), - concat = require("lodash/concat"), + concat = require("lodash/concat"); + + +function filteredGroups(excludeClasses) { + return groups.all + .filter((className) => excludeClasses.indexOf(className) === -1) + .reduce((acc, cur) => { + acc[cur] = classNamer; - removeClassNames = require("../lib/removeClassNames"), + return acc; + }, {}); +}; - groups = require("../lib/groups"); describe("/lib", () => { describe("/removeClassNames.js", () => { forEach(groups, (classNames, groupName) => { it(`should remove all classes from the ${groupName} group`, () => { - assert.deepEqual(removeClassNames(clone(groups.all), [ groupName ]), groups.all.filter((className) => groups[groupName].indexOf(className) < 0)); + assert.deepEqual(removeClassNames(reduceGroup("all"), [ groupName ]), filteredGroups(groups[groupName])); }); }); it("should remove individual class names", () => { - assert.deepEqual(removeClassNames(groups.all, [ ":first-child" ]), groups.all.filter((className) => className !== ":first-child")); + assert.deepEqual(removeClassNames(reduceGroup("all"), [ ":first-child" ]), filteredGroups([ ":first-child" ])); }); it("should remove multiple class names", () => { - assert.deepEqual(removeClassNames(groups.all, [ ":first-child", ":last-child" ]), groups.all.filter((className) => [ ":first-child", ":last-child" ].indexOf(className) < 0)); + assert.deepEqual(removeClassNames(reduceGroup("all"), [ ":first-child", ":last-child" ]), filteredGroups([ ":first-child", ":last-child" ])); }); it("should remove multiple group names", () => { - assert.deepEqual(removeClassNames(groups.all, [ "only", "readWrite" ]), groups.all.filter((className) => concat(groups.only, groups.readWrite).indexOf(className) < 0)); + assert.deepEqual(removeClassNames(reduceGroup("all"), [ "only", "readWrite" ]), filteredGroups(concat(groups.only, groups.readWrite))); }); it("should remove class names and group names", () => { - assert.deepEqual(removeClassNames(groups.all, [ ":first-child", "form" ]), groups.all.filter((className) => className !== ":first-child" && groups.form.indexOf(className) < 0)); + assert.deepEqual(removeClassNames(reduceGroup("all"), [ ":first-child", "form" ]), filteredGroups(concat([ ":first-child" ], groups.form))); }); }); });