All files / src/rules class-pattern.ts

91.67% Statements 11/12
75% Branches 3/4
100% Functions 3/3
91.67% Lines 11/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   1x                   2x 2x   2x 2x       2x 2x 4x 1x          
import { Rule, RuleReport, RuleParserProxy } from '../rule';
import { AttributeEvent } from '../event';
import { DOMTokenList } from '../dom';
import { parsePattern } from '../pattern';
 
export = {
	name: 'class-pattern',
	init,
 
	defaults: {
		pattern: 'kebabcase',
	},
} as Rule;
 
function init(parser: RuleParserProxy, userOptions: any){
	const options = Object.assign({}, this.defaults, userOptions);
	const pattern = parsePattern(options.pattern);
 
	parser.on('attr', (event: AttributeEvent, report: RuleReport) => {
		Iif (event.key.toLowerCase() !== 'class'){
			return;
		}
 
		const classes = new DOMTokenList(event.value);
		classes.forEach(cur => {
			if (!cur.match(pattern)){
				report(event.target, `Class "${cur}" does not match required pattern "${pattern}"`);
			}
		});
	});
}