-
-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
90 additions
and
8 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
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,7 +1,3 @@ | ||
TODO | ||
================== | ||
|
||
1. add rule: Relative path | ||
2. add rule: Absolute path | ||
4. reporter support | ||
5. w3c rule |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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 @@ | ||
/** | ||
* Copyright (c) 2014, Yanis Wang <[email protected]> | ||
* MIT Licensed | ||
*/ | ||
HTMLHint.addRule({ | ||
id: 'href-abs-or-rel', | ||
description: 'Href must be absolute or relative.', | ||
init: function(parser, reporter, options){ | ||
var self = this; | ||
|
||
var hrefMode = options === 'abs' ? 'absolute' : 'relative'; | ||
|
||
parser.addListener('tagstart', function(event){ | ||
var attrs = event.attrs; | ||
var attr; | ||
var col = event.col + event.tagName.length + 1; | ||
|
||
for(var i=0, l=attrs.length;i<l;i++){ | ||
attr = attrs[i]; | ||
if(attr.name === 'href'){ | ||
if((hrefMode === 'absolute' && /^\w+?:/.test(attr.value) === false) || | ||
(hrefMode === 'relative' && /^https?:\/\//.test(attr.value) === true)){ | ||
reporter.error('The value of href [ '+attr.value+' ] must be '+hrefMode+'.', event.line, col + attr.index, self, attr.raw); | ||
} | ||
break; | ||
} | ||
} | ||
}); | ||
} | ||
}); |
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,55 @@ | ||
/** | ||
* Copyright (c) 2014, Yanis Wang <[email protected]> | ||
* MIT Licensed | ||
*/ | ||
|
||
var expect = require("expect.js"); | ||
|
||
var HTMLHint = require("../../index").HTMLHint; | ||
|
||
var ruldId = 'href-abs-or-rel'; | ||
var ruleOptions = {}; | ||
|
||
describe('Rules: '+ruldId, function(){ | ||
|
||
it('Href value is not absolute with abs mode should result in an error', function(){ | ||
var code = '<a href="a.html">aaa</a><a href="../b.html">bbb</a><a href="tel:12345678">ccc</a><a href="javascript:void()">ddd</a>'; | ||
ruleOptions[ruldId] = 'abs'; | ||
var 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(3); | ||
expect(messages[1].rule.id).to.be(ruldId); | ||
expect(messages[1].line).to.be(1); | ||
expect(messages[1].col).to.be(27); | ||
}); | ||
|
||
it('Href value is absolute with abs mode should not result in an error', function(){ | ||
var code = '<a href="http://www.alibaba.com/">aaa</a><a href="https://www.alibaba.com/">bbb</a><a href="tel:12345678">ccc</a><a href="javascript:void()">ddd</a>'; | ||
ruleOptions[ruldId] = 'abs'; | ||
var messages = HTMLHint.verify(code, ruleOptions); | ||
expect(messages.length).to.be(0); | ||
}); | ||
|
||
it('Href value is not relative with rel mode should result in an error', function(){ | ||
var code = '<a href="http://www.alibaba.com/">aaa</a><a href="https://www.alibaba.com/">bbb</a><a href="tel:12345678">ccc</a><a href="javascript:void()">ddd</a>'; | ||
ruleOptions[ruldId] = 'rel'; | ||
var 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(3); | ||
expect(messages[1].rule.id).to.be(ruldId); | ||
expect(messages[1].line).to.be(1); | ||
expect(messages[1].col).to.be(44); | ||
}); | ||
|
||
it('Href value is relative with rel mode should not result in an error', function(){ | ||
var code = '<a href="a.html">aaa</a><a href="../b.html">bbb</a><a href="tel:12345678">ccc</a><a href="javascript:void()">ddd</a>'; | ||
ruleOptions[ruldId] = 'rel'; | ||
var messages = HTMLHint.verify(code, ruleOptions); | ||
expect(messages.length).to.be(0); | ||
}); | ||
|
||
}); |