diff --git a/src/rules/attr-no-unnecessary-whitespace.js b/src/rules/attr-no-unnecessary-whitespace.js new file mode 100644 index 000000000..167b21228 --- /dev/null +++ b/src/rules/attr-no-unnecessary-whitespace.js @@ -0,0 +1,23 @@ +export default { + id: 'attr-no-unnecessary-whitespace', + description: 'No spaces between attribute names and values.', + init: function (parser, reporter, options) { + var self = this; + var exceptions = Array.isArray(options) ? options : []; + parser.addListener('tagstart', function (event) { + var attrs = event.attrs, + col = event.col + event.tagName.length + 1; + for (var i = 0; i < attrs.length; i++) { + if (exceptions.indexOf(attrs[i].name) === -1 && /[^=](\s+=\s+|=\s+|\s+=)/g.test(attrs[i].raw.trim())) { + reporter.error( + 'The attribute \'' + attrs[i].name + '\' must not have spaces between the name and value.', + event.line, + col + attrs[i].index, + self, + attrs[i].raw + ); + } + } + }); + } +}; diff --git a/src/rules/index.js b/src/rules/index.js index 2719df7a4..c924ed29e 100644 --- a/src/rules/index.js +++ b/src/rules/index.js @@ -27,3 +27,4 @@ export { default as tagnameLowercase } from './tagname-lowercase'; export { default as tagnameSpecialChars } from './tagname-specialchars'; export { default as titleRequire } from './title-require'; export { default as tagsCheck } from './tags-check'; +export { default as attrNoUnnecessaryWhitespace } from './attr-no-unnecessary-whitespace'; diff --git a/test/rules/attr-no-unnecessary-whitespace.js b/test/rules/attr-no-unnecessary-whitespace.js new file mode 100644 index 000000000..6476dd893 --- /dev/null +++ b/test/rules/attr-no-unnecessary-whitespace.js @@ -0,0 +1,31 @@ +var expect = require('expect.js'); + +var HTMLHint = require('../../dist/htmlhint.js').HTMLHint; + +var ruldId = 'attr-no-unnecessary-whitespace', + ruleOptions = {}; + +ruleOptions[ruldId] = true; + +describe('Rules: ' + ruldId, function () { + it('Attribute with spaces should result in an error', function () { + var codes = [ + '
', + '', + '', + ]; + for (var i = 0; i < codes.length; i++) { + var messages = HTMLHint.verify(codes[i], ruleOptions); + expect(messages.length).to.be(1); + expect(messages[0].rule.id).to.be(ruldId); + expect(messages[0].line).to.be(1); + expect(messages[0].col).to.be(5); + } + }); + + it('Attribute without spaces should not result in an error', function () { + var code = ''; + var messages = HTMLHint.verify(code, ruleOptions); + expect(messages.length).to.be(0); + }); +});