File tree Expand file tree Collapse file tree 3 files changed +55
-0
lines changed
Expand file tree Collapse file tree 3 files changed +55
-0
lines changed Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change @@ -27,3 +27,4 @@ export { default as tagnameLowercase } from './tagname-lowercase';
2727export { default as tagnameSpecialChars } from './tagname-specialchars' ;
2828export { default as titleRequire } from './title-require' ;
2929export { default as tagsCheck } from './tags-check' ;
30+ export { default as attrNoUnnecessaryWhitespace } from './attr-no-unnecessary-whitespace' ;
Original file line number Diff line number Diff line change 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+ } ) ;
You can’t perform that action at this time.
0 commit comments