Skip to content
This repository was archived by the owner on Apr 22, 2020. It is now read-only.

Commit b84166d

Browse files
amroamroamromikesamuel
authored andcommitted
prettify_tests: add a simple module loader instead of document.write
It is needed because lang-*.js files are dependent on prettify.js being loaded first, but scripts dynamically inserted using document.write are executed asynchronously in no guaranteed order.. This was causing the tests to fail when run under zombie/jsdom (using test_in_node), while running in a live browser didn't always manifest the problem.
1 parent 74828a3 commit b84166d

File tree

3 files changed

+93
-34
lines changed

3 files changed

+93
-34
lines changed

tests/prettify_test.html

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,19 @@
77
<link rel="stylesheet" type="text/css" href="test_styles.css">
88
<script type="text/javascript">
99
(function () {
10+
// keep track of when window.onload fires, this is to make sure that we dont
11+
// miss it as it might occur before or after dynamic scripts are loaded
12+
var isReady = false;
13+
window.onload = function () { isReady = true; };
14+
15+
// run tests when page is fully loaded
16+
function onReady() {
17+
runTests(goldens);
18+
}
19+
1020
// JS & CSS prettify files to load
1121
var base = /[&?]loader\b/.test(location.search) ? '../loader/' : '../src/';
1222
var sources = [
13-
'prettify.js',
1423
'lang-css.js',
1524
'lang-erlang.js',
1625
'lang-go.js',
@@ -30,25 +39,28 @@
3039
var styles = [
3140
'prettify.css'
3241
];
33-
for (var i = 0; i < sources.length; ++i) {
34-
document.write('<script src="' + base + sources[i] + '"><\/script>');
35-
}
36-
for (var i = 0; i < styles.length; ++i) {
37-
document.write('<link rel="stylesheet" href="' + base + styles[i] + '">');
42+
43+
// load skin(s)
44+
while (styles.length) {
45+
injectCSS(base + styles.shift());
3846
}
39-
})();
4047

41-
window.onload = (function() {
42-
var attempt = function() {
43-
var prettyPrint = window['prettyPrint'];
44-
var goldens = window['goldens'];
45-
if(prettyPrint && goldens) {
46-
runTests(goldens);
47-
} else {
48-
setTimeout(attempt, 20);
48+
// first load prettify.js
49+
injectJS(base + 'prettify.js', function () {
50+
// then load all lang-*.js concurrently
51+
while (sources.length) {
52+
injectJS(base + sources.shift(),
53+
(sources.length) ? undefined : function () {
54+
// then after last one is loaded, run tests when page is ready
55+
// (piggyback on window.onload if it hasn't already fired)
56+
if (isReady) {
57+
onReady();
58+
} else {
59+
window.onload = onReady;
60+
}
61+
});
4962
}
50-
};
51-
return attempt;
63+
});
5264
})();
5365
</script>
5466
</head>

tests/prettify_test_2.html

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,19 @@
77
<link rel="stylesheet" type="text/css" href="test_styles.css">
88
<script type="text/javascript">
99
(function () {
10+
// keep track of when window.onload fires, this is to make sure that we dont
11+
// miss it as it might occur before or after dynamic scripts are loaded
12+
var isReady = false;
13+
window.onload = function () { isReady = true; };
14+
15+
// run tests when page is fully loaded
16+
function onReady() {
17+
runTests(goldens);
18+
}
19+
1020
// JS & CSS prettify files to load
1121
var base = /[&?]loader\b/.test(location.search) ? '../loader/' : '../src/';
1222
var sources = [
13-
'prettify.js',
1423
'lang-basic.js',
1524
'lang-clj.js',
1625
'lang-css.js',
@@ -29,25 +38,28 @@
2938
var styles = [
3039
'prettify.css'
3140
];
32-
for (var i = 0; i < sources.length; ++i) {
33-
document.write('<script src="' + base + sources[i] + '"><\/script>');
34-
}
35-
for (var i = 0; i < styles.length; ++i) {
36-
document.write('<link rel="stylesheet" href="' + base + styles[i] + '">');
41+
42+
// load skin(s)
43+
while (styles.length) {
44+
injectCSS(base + styles.shift());
3745
}
38-
})();
3946

40-
window.onload = (function() {
41-
var attempt = function() {
42-
var prettyPrint = window['prettyPrint'];
43-
var goldens = window['goldens'];
44-
if(prettyPrint && goldens) {
45-
runTests(goldens);
46-
} else {
47-
setTimeout(attempt, 20);
47+
// first load prettify.js
48+
injectJS(base + 'prettify.js', function () {
49+
// then load all lang-*.js concurrently
50+
while (sources.length) {
51+
injectJS(base + sources.shift(),
52+
(sources.length) ? undefined : function () {
53+
// then after last one is loaded, run tests when page is ready
54+
// (piggyback on window.onload if it hasn't already fired)
55+
if (isReady) {
56+
onReady();
57+
} else {
58+
window.onload = onReady;
59+
}
60+
});
4861
}
49-
};
50-
return attempt;
62+
});
5163
})();
5264
</script>
5365
</head>

tests/test_base.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,41 @@
1515
* limitations under the License.
1616
*/
1717

18+
/**
19+
* Dynamically load script
20+
*
21+
* @param {string} url JavaScript file
22+
* @param {Function=} opt_func onload callback
23+
*/
24+
function injectJS(url, opt_func) {
25+
var el = document.createElement('script');
26+
if (typeof opt_func === 'function') {
27+
el.onload = el.onerror = el.onreadystatechange = function () {
28+
if (el && (!el.readyState || /loaded|complete/.test(el.readyState))) {
29+
el.onerror = el.onload = el.onreadystatechange = null;
30+
el = null;
31+
opt_func();
32+
}
33+
};
34+
}
35+
el.type = 'text/javascript';
36+
el.src = url;
37+
document.getElementsByTagName('head')[0].appendChild(el);
38+
}
39+
40+
/**
41+
* Dynamically load stylesheet
42+
*
43+
* @param {string} url CSS file
44+
*/
45+
function injectCSS(url) {
46+
var el = document.createElement('link');
47+
el.rel = 'stylesheet';
48+
el.type = 'text/css';
49+
el.href = url;
50+
document.getElementsByTagName('head')[0].appendChild(el);
51+
}
52+
1853
/**
1954
* Perform syntax highlighting and execute tests to verify results.
2055
*

0 commit comments

Comments
 (0)