All files / src/rules element-permitted-occurrences.ts

100% Statements 13/13
100% Branches 6/6
100% Functions 4/4
100% Lines 12/12
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 29 30 31 32      1x   1x           410x       410x 410x 859x   859x 410x     449x 531x   449x 18x        
import { Rule, RuleReport, RuleParserProxy } from '../rule';
import { DOMNode } from '../dom';
import { DOMReadyEvent } from '../event';
import { Validator } from '../meta';
 
export = {
	name: 'element-permitted-occurrences',
	init,
} as Rule;
 
function init(parser: RuleParserProxy){
	parser.on('dom:ready', validate);
}
 
function validate(event: DOMReadyEvent, report: RuleReport){
	const doc = event.document;
	doc.visitDepthFirst((node: DOMNode) => {
		const parent = node.parent;
 
		if (!parent.meta){
			return;
		}
 
		const rules = parent.meta.permittedContent;
		const numSiblings = parent.children.filter(cur => cur.tagName === node.tagName).length;
 
		if (parent.meta && !Validator.validateOccurrences(node, rules, numSiblings)){
			report(node, `Element <${node.tagName}> can only appear once under <${parent.tagName}>`);
		}
	});
}