From 4a8d4db0870f2f02677dec89c93a6f905f8be1a3 Mon Sep 17 00:00:00 2001 From: BenjaminJ Date: Thu, 28 May 2020 11:47:33 +0200 Subject: [PATCH] fix: deal with multiple spaces between classes --- lib/knife/tag_utils.js | 7 +++++++ lib/rules/class-no-dup/index.js | 6 +----- lib/rules/class-style/__tests__/index.js | 12 ++++++++++++ lib/rules/class-style/index.js | 7 ++----- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/knife/tag_utils.js b/lib/knife/tag_utils.js index 73f1f2b82..00a9998ce 100644 --- a/lib/knife/tag_utils.js +++ b/lib/knife/tag_utils.js @@ -21,3 +21,10 @@ module.exports.attributeValue = function(tag, attributeName) { module.exports.isTagNode = function(node) { return ["tag", "style", "script"].indexOf(node.type) !== -1; }; + +module.exports.getClasses = function(node) { + const classes = node.attribs.class ? node.attribs.class.value : ""; + return classes + .trim() + .split(/\s+/); +}; diff --git a/lib/rules/class-no-dup/index.js b/lib/rules/class-no-dup/index.js index 49354e199..4e7ee2111 100644 --- a/lib/rules/class-no-dup/index.js +++ b/lib/rules/class-no-dup/index.js @@ -1,4 +1,4 @@ -const { isTagNode } = require("../../knife/tag_utils"); +const { isTagNode, getClasses } = require("../../knife/tag_utils"); module.exports = { name: "class-no-dup", @@ -6,10 +6,6 @@ module.exports = { need: "dom" }; -function getClasses(node) { - return node.attribs.class.value.trim().split(" "); -} - function filterClasses(classes, options) { const ignore = options["id-class-ignore-regex"]; diff --git a/lib/rules/class-style/__tests__/index.js b/lib/rules/class-style/__tests__/index.js index eb36e0fd7..dbc8e9588 100644 --- a/lib/rules/class-style/__tests__/index.js +++ b/lib/rules/class-style/__tests__/index.js @@ -151,6 +151,18 @@ describe("class-style", function() { expect(issues).to.have.lengthOf(0); }); + it("Should deal with multiple spaces between classes", async function() { + const linter = createLinter({ + "class-style": [ + true, + "lowercase" + ] + }); + const html = "
"; + const issues = await linter.lint(html); + expect(issues).to.have.lengthOf(0); + }); + describe("'lowercase' format", function() { it("Should not report an error for classes with valid format", async function() { const linter = createLinter({ diff --git a/lib/rules/class-style/index.js b/lib/rules/class-style/index.js index 5b53e7e14..8df9b8481 100644 --- a/lib/rules/class-style/index.js +++ b/lib/rules/class-style/index.js @@ -1,7 +1,7 @@ const proc = require("../../process_option"); /* eslint-disable-next-line */ const { isRegExp } = require("util"); -const { isTagNode } = require("../../knife/tag_utils"); +const { isTagNode, getClasses } = require("../../knife/tag_utils"); module.exports = { name: "class-style", @@ -19,10 +19,6 @@ module.exports = { } }; -function getClasses(node) { - return node.attribs.class.value.trim().split(" "); -} - function filterClasses(classes, options) { const ignore = options["id-class-ignore-regex"]; @@ -38,6 +34,7 @@ function lint(node, options, { report }) { if (isTagNode(node) === false || node.attribs.class === undefined) { return []; } + debugger; const format = options[this.name] || options["id-class-style"]; if (format === "none") { return [];