All files / src/rules button-type.ts

100% Statements 10/10
100% Branches 4/4
100% Functions 3/3
100% Lines 10/10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28        1x               8x       8x 8x 8x 5x 5x 1x 4x 1x        
import { Rule, RuleReport, RuleParserProxy } from '../rule';
import { DOMNode } from 'dom';
import { DOMReadyEvent } from '../event';
 
export = {
	name: 'button-type',
	init,
 
	validTypes: ['submit', 'button', 'reset'],
} as Rule;
 
function init(parser: RuleParserProxy){
	parser.on('dom:ready', validate);
}
 
function validate(event: DOMReadyEvent, report: RuleReport){
	const validTypes = this.validTypes;
	const buttons = event.document.getElementsByTagName('button');
	buttons.forEach(function(node: DOMNode){
		const type = node.getAttribute('type');
		if (type === null){
			report(node, "Button is missing type attribute");
		} else if (validTypes.indexOf(type.toLowerCase()) === -1){
			report(node, `Button has invalid type "${type}"`);
		}
	});
}