-
-
Notifications
You must be signed in to change notification settings - Fork 378
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
518a1a2
commit f87aef0
Showing
3 changed files
with
54 additions
and
29 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 |
---|---|---|
@@ -1,30 +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 | ||
var col = event.col + event.tagName.length + 1 | ||
|
||
for (var i = 0; i < attrs.length; i++) { | ||
if (exceptions.indexOf(attrs[i].name) === -1) { | ||
var match = /(\s*)=(\s*)/.exec(attrs[i].raw.trim()) | ||
if (match && (match[1].length !== 0 || match[2].length !== 0)) { | ||
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 | ||
) | ||
} | ||
} | ||
} | ||
}) | ||
}, | ||
} | ||
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); | ||
}); | ||
}); |