Skip to content

Commit dc81fae

Browse files
committed
wait for all images to be loaded
1 parent 5334d04 commit dc81fae

File tree

1 file changed

+55
-24
lines changed

1 file changed

+55
-24
lines changed

lib/output.js

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,42 @@
2121
*/
2222

2323
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
24-
/*global define, $ */
24+
/*global define, $, Image */
2525

2626
define(function (require, exports) {
2727
"use strict";
2828

29+
// Fn to allow an event to fire after all images are loaded
30+
$.fn.imagesLoaded = function () {
31+
// get all the images (excluding those with no src attribute)
32+
var $imgs = this.find('img[src!=""]');
33+
// if there's no images, just return an already resolved promise
34+
if (!$imgs.length) {
35+
return $.Deferred().resolve().promise();
36+
}
37+
38+
// for each image, add a deferred object to the array which resolves
39+
// when the image is loaded (or if loading fails)
40+
var dfds = [];
41+
$imgs.each(function () {
42+
var dfd = $.Deferred();
43+
dfds.push(dfd);
44+
var img = new Image();
45+
img.onload = function () {
46+
dfd.resolve();
47+
};
48+
img.onerror = function () {
49+
dfd.resolve();
50+
};
51+
img.src = this.src;
52+
53+
});
54+
55+
// return a master promise object which will resolve when all the deferred objects have resolved
56+
// IE - when all the images are loaded
57+
return $.when.apply($, dfds);
58+
};
59+
2960
var utils = require("lib/utils");
3061

3162
// Array to keep all important configuration
@@ -254,34 +285,34 @@ define(function (require, exports) {
254285
$iframe[0].contentWindow.stopMath();
255286
}
256287

257-
$(body).find('#body-text').html(result.html);
288+
$(body).find('#body-text').html(result.html).imagesLoaded().then(function () {
289+
if (result.stem && inputData.renderMath) {
290+
$iframe[0].contentWindow.startMath();
291+
}
258292

259-
if (result.stem && inputData.renderMath) {
260-
$iframe[0].contentWindow.startMath();
261-
}
293+
// Highlight source code
294+
if (result.sourceHighlighter === 'highlightjs') {
295+
$('pre code', body).each(function (i, block) {
296+
$iframe[0].contentWindow.hljs.highlightBlock(block);
297+
});
298+
} else if (result.sourceHighlighter === 'prettify') {
299+
$iframe[0].contentWindow.prettyPrint();
300+
}
262301

263-
// Highlight source code
264-
if (result.sourceHighlighter === 'highlightjs') {
265-
$('pre code', body).each(function (i, block) {
266-
$iframe[0].contentWindow.hljs.highlightBlock(block);
302+
// make page anchors work and add titles to links
303+
$iframe.contents().find('a[href]').each(function (index, link) {
304+
var href = link.getAttribute("href");
305+
if (href.match(/^#/)) {
306+
$(link).attr('onclick', 'goto("' + href.substring(1) + '");return false;');
307+
}
308+
var title = link.getAttribute('title');
309+
if (!title) {
310+
link.setAttribute("title", href);
311+
}
267312
});
268-
} else if (result.sourceHighlighter === 'prettify') {
269-
$iframe[0].contentWindow.prettyPrint();
270-
}
271313

272-
// make page anchors work and add titles to links
273-
$iframe.contents().find('a[href]').each(function (index, link) {
274-
var href = link.getAttribute("href");
275-
if (href.match(/^#/)) {
276-
$(link).attr('onclick', 'goto("' + href.substring(1) + '");return false;');
277-
}
278-
var title = link.getAttribute('title');
279-
if (!title) {
280-
link.setAttribute("title", href);
281-
}
314+
onCompleteCallback();
282315
});
283-
284-
onCompleteCallback();
285316
}
286317

287318
if (isNewDocument || needsFullUpdate(inputData, result)) {

0 commit comments

Comments
 (0)