-
-
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(rules): add empty-tag-not-self-closed rule (#696)
Fixes #311.
- Loading branch information
Showing
6 changed files
with
59 additions
and
2 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,26 @@ | ||
import { Rule } from '../types' | ||
|
||
export default { | ||
id: 'empty-tag-not-self-closed', | ||
description: 'Empty tags must not use self closed syntax.', | ||
init(parser, reporter) { | ||
const mapEmptyTags = parser.makeMap( | ||
'area,base,basefont,bgsound,br,col,frame,hr,img,input,isindex,link,meta,param,embed,track,command,source,keygen,wbr' | ||
) //HTML 4.01 + HTML 5 | ||
|
||
parser.addListener('tagstart', (event) => { | ||
const tagName = event.tagName.toLowerCase() | ||
if (mapEmptyTags[tagName] !== undefined) { | ||
if (event.close) { | ||
reporter.error( | ||
`The empty tag : [ ${tagName} ] must not use self closed syntax.`, | ||
event.line, | ||
event.col, | ||
this, | ||
event.raw | ||
) | ||
} | ||
} | ||
}) | ||
}, | ||
} as Rule |
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
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,30 @@ | ||
const expect = require('expect.js') | ||
|
||
const HTMLHint = require('../../dist/htmlhint.js').HTMLHint | ||
|
||
const ruldId = 'empty-tag-not-self-closed' | ||
const ruleOptions = {} | ||
|
||
ruleOptions[ruldId] = true | ||
|
||
describe(`Rules: ${ruldId}`, () => { | ||
it('The empty tag no closed should not result in an error', () => { | ||
const code = '<br><img src="test.jpg">' | ||
const messages = HTMLHint.verify(code, ruleOptions) | ||
expect(messages.length).to.be(0) | ||
}) | ||
|
||
it('Closed empty tag should result in an error', () => { | ||
const code = '<br /><img src="a.jpg"/>' | ||
const messages = HTMLHint.verify(code, ruleOptions) | ||
expect(messages.length).to.be(2) | ||
expect(messages[0].rule.id).to.be(ruldId) | ||
expect(messages[0].line).to.be(1) | ||
expect(messages[0].col).to.be(1) | ||
expect(messages[0].type).to.be('error') | ||
expect(messages[1].rule.id).to.be(ruldId) | ||
expect(messages[1].line).to.be(1) | ||
expect(messages[1].col).to.be(7) | ||
expect(messages[1].type).to.be('error') | ||
}) | ||
}) |
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