forked from openwebf/webf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_loader.js
More file actions
75 lines (67 loc) · 2.18 KB
/
Copy pathhtml_loader.js
File metadata and controls
75 lines (67 loc) · 2.18 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* Copyright (C) 2022-present The Kraken authors. All rights reserved.
*/
const path = require('path');
const HTMLParser = require('node-html-parser');
const SCRIPT = 'script';
const traverseParseHTML = (ele, scripts) => {
ele.childNodes && ele.childNodes.forEach(e => {
if (e.rawTagName === SCRIPT) {
e.childNodes.forEach(item => {
// TextNode of script element.
if (item.nodeType === 3) {
scripts.push(item._rawText);
}
// Delete content of script element for avoid to script repetition.
item._rawText = '';
})
}
traverseParseHTML(e, scripts);
});
}
const loader = function(source) {
const filepath = this.resourcePath
const filename = path.basename(filepath)
const opts = this.query || {};
const scripts = []
const testRelativePath = path.relative(opts.testPath, filepath)
const snapshotFilepath = path.relative(
opts.workspacePath,
path.join(
opts.snapshotPath,
testRelativePath,
)
);
let root = HTMLParser.parse(source);
traverseParseHTML(root, scripts);
// Set attr of HTML can let the case use fit. For example: <html fit> xxx </html>.
let isFit = false;
let isXit = false;
root.childNodes && root.childNodes.forEach(ele => {
if (ele.rawAttrs && ele.rawAttrs.indexOf('fit') >= 0) {
isFit = true;
}
if (ele.rawAttrs && ele.rawAttrs.indexOf('xit') >= 0) {
isXit = true;
}
})
const htmlString = root.toString().replace(/['\n]/g, function(c){
return {'\n': '','\'': '\\'}[c];
});
return `
describe('HTMLSpec/${testRelativePath}', () => {
// Use html_parse to parser html in html file.
const html_parse = () => __webf_parse_html__('${htmlString}');
var index = 0;
const snapshotAction = async () => { await snapshot(null, '${snapshotFilepath}', ${scripts.length === 0 ? 'null' : 'index.toString()'}); index++; };
${isFit ? 'fit' : isXit ? 'xit' : 'it'}("should work", async (done) => {\
html_parse();\
requestAnimationFrame(async () => {
${scripts.length === 0 ? `await snapshotAction();` : scripts.join('\n')}
done();
});
})
});
`;
};
module.exports = loader;