Skip to content

Commit 95349ef

Browse files
Resize for slow loading images (#36)
1 parent 3e18471 commit 95349ef

File tree

4 files changed

+87
-19
lines changed

4 files changed

+87
-19
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Next Release
22
-------------
33

4+
1.7.1
5+
------
6+
* Add Image resizing for slow loading images.
7+
48
1.7.0
59
------
610
* Fix resizing: use scrollHeight instead of offsetHeight

package-lock.json

Lines changed: 17 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/provider/application.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class Application extends EventEmitter {
3131
this.emitError = this.emitError.bind(this);
3232
this.unload = this.unload.bind(this);
3333

34+
// Resize for slow loading images
35+
document.body.addEventListener('load', this.imageRequestResize.bind(this), true);
36+
3437
// If the document referer (parent frame) origin is trusted, default that
3538
// to the active ACL;
3639
const parentOrigin = new URI(document.referrer).origin;
@@ -76,7 +79,19 @@ class Application extends EventEmitter {
7679
);
7780
}
7881

82+
/**
83+
* imageRequestResize function to call requestResize event for slow loading image
84+
* @param {object} event - event which triggered the listener
85+
*/
86+
imageRequestResize(event) {
87+
const tgt = event.target;
88+
if (tgt.tagName === 'IMG' && !(tgt.hasAttribute('height') || tgt.hasAttribute('width'))) {
89+
this.requestResize();
90+
}
91+
}
92+
7993
requestResize() {
94+
if (!this.resizeConfig) return;
8095
if (this.resizeConfig.customCal) {
8196
this.JSONRPC.notification('resize');
8297
} else if (this.resizeConfig.autoResizeWidth) {

test/application.js

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('Application', () => {
1717
const application = new Application();
1818

1919
const oldDocument = global.document;
20-
global.document = {referrer: 'http://localhost:8080', createElement: document.createElement.bind(document) };
20+
global.document = {referrer: 'http://localhost:8080', createElement: document.createElement.bind(document), body: { addEventListener: () => console.log('mock addEventListener') } };
2121
application.init({acls, secret, onReady});
2222
global.document = oldDocument;
2323

@@ -27,7 +27,7 @@ describe('Application', () => {
2727

2828
it ("doesn't set activeACL to document referrer if not in ACL", () => {
2929
const insecureApp = new Application();
30-
global.document = {referrer: 'http://evilsite.com', createElement: document.createElement.bind(document) };
30+
global.document = {referrer: 'http://evilsite.com', createElement: document.createElement.bind(document), body: { addEventListener: () => console.log('mock addEventListener') } };
3131
insecureApp.init({acls, secret, onReady});
3232
global.document = oldDocument;
3333

@@ -49,6 +49,11 @@ describe('Application', () => {
4949
it("sets application's JSONRPC", () => {
5050
expect(application.JSONRPC).to.be.an.instanceof(JSONRPC);
5151
});
52+
53+
it("calls addEventListener", sinon.test(function() {
54+
sinon.spy(document.body, 'addEventListener');
55+
expect(document.body.addEventListener.calledOnce);
56+
}));
5257

5358
describe('#trigger(event, detail)', () => {
5459
it("calls this.JSONRPC.notification of 'event' with event and detail", sinon.test(function() {
@@ -262,6 +267,50 @@ describe('Application', () => {
262267
});
263268
});
264269

270+
describe('#imageRequestResize(event)', () => {
271+
const application = new Application();
272+
it("calls requestResize for an image", sinon.test(function() {
273+
const requestResize = this.stub(application, 'requestResize');
274+
application.resizeConfig = {}
275+
const event = {
276+
target: {
277+
tagName: "IMG",
278+
hasAttribute: (attr) => { attr === 'height'}
279+
}
280+
};
281+
application.imageRequestResize(event);
282+
283+
sinon.assert.called(requestResize);
284+
}));
285+
286+
it("does not call requestResize for an image with height", sinon.test(function() {
287+
const requestResize = this.stub(application, 'requestResize');
288+
application.resizeConfig = {}
289+
290+
let event = {
291+
target: {
292+
tagName: "IMG",
293+
hasAttribute: (attr) => { return ['width', 'height'].includes(attr)},
294+
}
295+
};
296+
application.imageRequestResize(event);
297+
298+
sinon.assert.notCalled(requestResize);
299+
}));
300+
});
301+
302+
describe('#requestResize()', () => {
303+
it("does not resize when resizeConfig is null", sinon.test(function() {
304+
const application = new Application();
305+
application.init({ acls: ['*']});
306+
const notification = this.stub(application.JSONRPC, 'notification');
307+
application.resizeConfig = null;
308+
application.requestResize();
309+
310+
sinon.assert.notCalled(notification);
311+
}));
312+
});
313+
265314
describe('#launch()', () => {
266315
describe('if window.self === window.top', () => {
267316
const tests = [

0 commit comments

Comments
 (0)