forked from PrismJS/prism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples-test.js
185 lines (159 loc) · 4.69 KB
/
examples-test.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const fs = require('fs');
const { assert } = require('chai');
const { Parser } = require('htmlparser2');
// use the JSON file because this file is less susceptible to merge conflicts
const { languages } = require('../components.json');
describe('Examples', function () {
const exampleFiles = new Set(fs.readdirSync(__dirname + '/../examples'));
const ignore = new Set([
// these are libraries and not languages
'markup-templating',
't4-templating',
// this does alter some languages but it's mainly a library
'javadoclike'
]);
const validFiles = new Set();
/** @type {string[]} */
const missing = [];
for (const lang in languages) {
if (lang === 'meta') {
continue;
}
const file = `prism-${lang}.html`;
if (!exampleFiles.has(file)) {
if (!ignore.has(lang)) {
missing.push(lang);
}
} else {
validFiles.add(file);
}
}
const superfluous = [...exampleFiles].filter(f => !validFiles.has(f));
it('- should be available for every language', function () {
assert.isEmpty(missing, 'Following languages do not have an example file in ./examples/\n'
+ missing.join('\n'));
});
it('- should only be available for registered languages', function () {
assert.isEmpty(superfluous, 'Following files are not associated with any language\n'
+ superfluous.map(f => `./examples/${f}`).join('\n'));
});
describe('Validate HTML templates', function () {
for (const file of validFiles) {
it('- ./examples/' + file, async function () {
const content = fs.readFileSync(__dirname + '/../examples/' + file, 'utf-8');
await validateHTML(content);
});
}
});
});
/**
* Validates the given HTML string of an example file.
*
* @param {string} html
*/
async function validateHTML(html) {
const root = await parseHTML(html);
/**
* @param {TagNode} node
*/
function checkCodeElements(node) {
if (node.tagName === 'code') {
assert.equal(node.children.length, 1,
'A <code> element is only allowed to contain text, no tags. '
+ 'Did you perhaps not escape all "<" characters?');
const child = node.children[0];
if (child.type !== 'text') {
// throw to help TypeScript's flow analysis
throw assert.equal(child.type, 'text', 'The child of a <code> element must be text only.');
}
const text = child.rawText;
assert.notMatch(text, /</, 'All "<" characters have to be escape with "<".');
assert.notMatch(text, /&(?!amp;|lt;|gt;)(?:[#\w]+);/, 'Only certain entities are allowed.');
} else {
node.children.forEach(n => {
if (n.type === 'tag') {
checkCodeElements(n);
}
});
}
}
for (const node of root.children) {
if (node.type === 'text') {
assert.isEmpty(node.rawText.trim(), 'All non-whitespace text has to be in <p> tags.');
} else {
// only known tags
assert.match(node.tagName, /^(?:h2|h3|p|pre|ul|ol)$/, 'Only some tags are allowed as top level tags.');
// <pre> elements must have only one child, a <code> element
if (node.tagName === 'pre') {
assert.equal(node.children.length, 1,
'<pre> element must have one and only one child node, a <code> element.'
+ ' This also means that spaces and line breaks around the <code> element are not allowed.');
const child = node.children[0];
if (child.type !== 'tag') {
// throw to help TypeScript's flow analysis
throw assert.equal(child.type, 'tag', 'The child of a <pre> element must be a <code> element.');
}
assert.equal(child.tagName, 'code', 'The child of a <pre> element must be a <code> element.');
}
checkCodeElements(node);
}
}
}
/**
* Parses the given HTML fragment and returns a simple tree of the fragment.
*
* @param {string} html
* @returns {Promise<TagNode>}
*
* @typedef TagNode
* @property {"tag"} type
* @property {string | null} tagName
* @property {Object<string, string>} attributes
* @property {(TagNode | TextNode)[]} children
*
* @typedef TextNode
* @property {"text"} type
* @property {string} rawText
*/
function parseHTML(html) {
return new Promise((resolve, reject) => {
/** @type {TagNode} */
const tree = {
type: 'tag',
tagName: null,
attributes: {},
children: []
};
/** @type {TagNode[]} */
let stack = [tree];
const p = new Parser({
onerror(err) {
reject(err)
},
onend() {
resolve(tree);
},
ontext(data) {
stack[stack.length - 1].children.push({
type: 'text',
rawText: data
});
},
onopentag(name, attrs) {
/** @type {TagNode} */
const newElement = {
type: 'tag',
tagName: name,
attributes: attrs,
children: []
};
stack[stack.length - 1].children.push(newElement);
stack.push(newElement);
},
onclosetag() {
stack.pop();
}
}, { lowerCaseTags: false });
p.end(html);
});
}