Skip to content

Commit 03bfd4f

Browse files
thedaviddiasjakewtaylor
andcommittedMay 18, 2020
feat: added attr-no-unnecessary-whitespace rule (#385)
* Added attr-no-unnecessary-whitespace rule * add tests * remove console.log statement * fix: merge conflicts Co-authored-by: pcfutures <me@jaketaylor.co>
1 parent 475aaca commit 03bfd4f

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export default {
2+
id: 'attr-no-unnecessary-whitespace',
3+
description: 'No spaces between attribute names and values.',
4+
init: function (parser, reporter, options) {
5+
var self = this;
6+
var exceptions = Array.isArray(options) ? options : [];
7+
parser.addListener('tagstart', function (event) {
8+
var attrs = event.attrs,
9+
col = event.col + event.tagName.length + 1;
10+
for (var i = 0; i < attrs.length; i++) {
11+
if (exceptions.indexOf(attrs[i].name) === -1 && /[^=](\s+=\s+|=\s+|\s+=)/g.test(attrs[i].raw.trim())) {
12+
reporter.error(
13+
'The attribute \'' + attrs[i].name + '\' must not have spaces between the name and value.',
14+
event.line,
15+
col + attrs[i].index,
16+
self,
17+
attrs[i].raw
18+
);
19+
}
20+
}
21+
});
22+
}
23+
};

‎src/rules/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ export { default as tagnameLowercase } from './tagname-lowercase';
2727
export { default as tagnameSpecialChars } from './tagname-specialchars';
2828
export { default as titleRequire } from './title-require';
2929
export { default as tagsCheck } from './tags-check';
30+
export { default as attrNoUnnecessaryWhitespace } from './attr-no-unnecessary-whitespace';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var expect = require('expect.js');
2+
3+
var HTMLHint = require('../../dist/htmlhint.js').HTMLHint;
4+
5+
var ruldId = 'attr-no-unnecessary-whitespace',
6+
ruleOptions = {};
7+
8+
ruleOptions[ruldId] = true;
9+
10+
describe('Rules: ' + ruldId, function () {
11+
it('Attribute with spaces should result in an error', function () {
12+
var codes = [
13+
'<div title = "a" />',
14+
'<div title= "a" />',
15+
'<div title ="a" />',
16+
];
17+
for (var i = 0; i < codes.length; i++) {
18+
var messages = HTMLHint.verify(codes[i], ruleOptions);
19+
expect(messages.length).to.be(1);
20+
expect(messages[0].rule.id).to.be(ruldId);
21+
expect(messages[0].line).to.be(1);
22+
expect(messages[0].col).to.be(5);
23+
}
24+
});
25+
26+
it('Attribute without spaces should not result in an error', function () {
27+
var code = '<div title="a" />';
28+
var messages = HTMLHint.verify(code, ruleOptions);
29+
expect(messages.length).to.be(0);
30+
});
31+
});

0 commit comments

Comments
 (0)
Please sign in to comment.