Skip to content

Commit

Permalink
Fix: Document print in Chrome (#406)
Browse files Browse the repository at this point in the history
Revoke object URL during viewer cleanup.
  • Loading branch information
tonyjin authored Sep 22, 2017
1 parent 08da16b commit 54fa047
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
10 changes: 6 additions & 4 deletions src/lib/viewers/doc/DocBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class DocBaseViewer extends BaseViewer {

// Clean up print blob
this.printBlob = null;
URL.revokeObjectURL(this.printURL);

if (this.pageControls) {
this.pageControls.removeListener('pagechange', this.setPage);
Expand Down Expand Up @@ -692,8 +693,11 @@ class DocBaseViewer extends BaseViewer {

// For other browsers, open and print in a new tab
} else {
const printURL = URL.createObjectURL(this.printBlob);
const printResult = window.open(printURL);
if (!this.printURL) {
this.printURL = URL.createObjectURL(this.printBlob);
}

const printResult = window.open(this.printURL);

// Open print popup if possible
if (printResult && typeof printResult.print === 'function') {
Expand Down Expand Up @@ -721,8 +725,6 @@ class DocBaseViewer extends BaseViewer {
} else {
this.emit('printsuccess');
}

URL.revokeObjectURL(printURL);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/lib/viewers/doc/__tests__/DocBaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,12 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {
describe('destroy()', () => {
it('should unbind listeners and clear the print blob', () => {
const unbindDOMListenersStub = sandbox.stub(docBase, 'unbindDOMListeners');
sandbox.stub(URL, 'revokeObjectURL');

docBase.destroy();
expect(unbindDOMListenersStub).to.be.called;
expect(docBase.printBlob).to.equal(null);
expect(URL.revokeObjectURL).to.be.calledWith(docBase.printUrl);
});

it('should destroy the controls', () => {
Expand Down Expand Up @@ -387,10 +389,9 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {
describe('browserPrint()', () => {
beforeEach(() => {
stubs.emit = sandbox.stub(docBase, 'emit');
stubs.createObject = sandbox.stub(URL, 'createObjectURL');
stubs.createObject = sandbox.stub(URL, 'createObjectURL').returns('test');
stubs.open = sandbox.stub(window, 'open').returns(false);
stubs.browser = sandbox.stub(Browser, 'getName').returns('Chrome');
stubs.revokeObjectURL = sandbox.stub(URL, 'revokeObjectURL');
stubs.printResult = { print: sandbox.stub(), addEventListener: sandbox.stub() };
docBase.printBlob = true;
window.navigator.msSaveOrOpenBlob = sandbox.stub().returns(true);
Expand Down Expand Up @@ -421,7 +422,7 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {

docBase.browserPrint();
expect(stubs.createObject).to.be.calledWith(docBase.printBlob);
expect(stubs.open).to.be.called.with;
expect(stubs.open).to.be.calledWith(docBase.printURL);
expect(stubs.emit).to.be.called;
});

Expand All @@ -431,10 +432,9 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {

docBase.browserPrint();
expect(stubs.createObject).to.be.calledWith(docBase.printBlob);
expect(stubs.open).to.be.called.with;
expect(stubs.open).to.be.calledWith(docBase.printURL);
expect(stubs.browser).to.be.called;
expect(stubs.emit).to.be.called;
expect(stubs.revokeObjectURL).to.be.called;
});

it('should use a timeout in safari', () => {
Expand Down

0 comments on commit 54fa047

Please sign in to comment.