-
-
Notifications
You must be signed in to change notification settings - Fork 388
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
c0be01d
commit 5e4cf81
Showing
3 changed files
with
55 additions
and
0 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 |
---|---|---|
@@ -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 | ||
); | ||
} | ||
} | ||
}); | ||
} | ||
}; |
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
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,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 = [ | ||
'<div title = "a" />', | ||
'<div title= "a" />', | ||
'<div title ="a" />', | ||
]; | ||
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 = '<div title="a" />'; | ||
var messages = HTMLHint.verify(code, ruleOptions); | ||
expect(messages.length).to.be(0); | ||
}); | ||
}); |