/
pattern-validity.js
42 lines (35 loc) · 1.08 KB
/
pattern-validity.js
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
document.addEventListener('DOMContentLoaded', () => {
const validTriggers = [
'The following patterns are not considered violations:',
'The following pattern is not considered a violation:',
];
const invalidTriggers = [
'The following patterns are considered violations:',
'The following pattern is considered a violation:',
];
const validClass = 'valid-pattern';
const invalidClass = 'invalid-pattern';
function nextCodeElements(el) {
const elements = [];
let next = el.nextElementSibling;
while (next && next.tagName === 'PRE') {
const code = next.firstElementChild;
if (code.tagName === 'CODE') {
elements.push(code);
}
next = next.nextElementSibling;
}
return elements;
}
Array.prototype.forEach.call(document.querySelectorAll('p'), (el) => {
if (validTriggers.indexOf(el.textContent) !== -1) {
nextCodeElements(el).forEach((code) => {
code.classList.add(validClass);
});
} else if (invalidTriggers.indexOf(el.textContent) !== -1) {
nextCodeElements(el).forEach((code) => {
code.classList.add(invalidClass);
});
}
});
});