Skip to content

Commit

Permalink
add rule: href-abs-or-rel
Browse files Browse the repository at this point in the history
  • Loading branch information
yaniswang committed Jun 20, 2014
1 parent 37b01ed commit 902d26a
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 8 deletions.
5 changes: 3 additions & 2 deletions CHANGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ HTMLHint change log
1. add rule: attr-no-duplication
2. add rule: space-tab-mixed-disabled
3. add rule: id-class-ad-disabled
4. add default rule: attr-no-duplication
5. add inline ruleset support
4. add rule: href-abs-or-rel
5. add default rule: attr-no-duplication
6. add inline ruleset support

## ver 0.9.4 (2013-9-27)

Expand Down
4 changes: 0 additions & 4 deletions TODO.md
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
2 changes: 1 addition & 1 deletion coverage.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/htmlhint.js

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions src/rules/href-abs-or-rel.js
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;
}
}
});
}
});
55 changes: 55 additions & 0 deletions test/rules/href-abs-or-rel.js
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);
});

});

0 comments on commit 902d26a

Please sign in to comment.