-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathplugin.html-validate.wysiwyg.js
More file actions
53 lines (45 loc) · 1.45 KB
/
plugin.html-validate.wysiwyg.js
File metadata and controls
53 lines (45 loc) · 1.45 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const { Rule } = require('html-validate');
const { nodeIgnore } = require('./plugin.html-validate.utils');
/**
* @typedef { import('html-validate').DOMReadyEvent } DOMReadyEvent
*/
/**
* Wysiwyg html-validate rule.
*/
class WysiwygClassAllowed extends Rule {
/**
* @param {object} options - plugin options
*/
constructor(options) {
super({ ignore: null, allowed: [], ...options });
this.domReady = this.domReady.bind(this);
}
/**
* Setup plugin events.
*/
setup() {
this.on('dom:ready', this.domReady);
}
/**
* Lint html document.
* @param {DOMReadyEvent.document} document - document object
*/
domReady({ document }) {
const items = document.querySelectorAll('.wysiwyg [class]');
const ignores = this.options.ignore ? document.querySelectorAll(this.options.ignore) : [];
const allowed = this.options.allowed ? this.options.allowed.map((i) => i.toLowerCase()) : [];
items.forEach((node) => {
if (nodeIgnore(node, ignores)) {
return;
}
const className = node.classList.find((i) => !allowed.includes(i.toLowerCase()));
if (className) {
this.report(node, `Class \`${className}\` not allowed inside \`.wysiwyg\`.`);
}
});
}
}
module.exports = { WysiwygClassAllowed };
module.exports.rules = {
'pitcher/wysiwyg-class-allowed': WysiwygClassAllowed,
};