Skip to content

Commit

Permalink
Merge pull request #11449 from Snuffleupagus/AutoPrintRegExp
Browse files Browse the repository at this point in the history
Move the regular expression, used with auto printing in the viewer, to `web/ui_utils.js` and also use it in the API unit-tests
  • Loading branch information
timvandermeij authored Dec 26, 2019
2 parents db8fcdf + d9d8560 commit 9c767e3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
10 changes: 4 additions & 6 deletions test/unit/api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
PDFPageProxy,
PDFWorker,
} from "../../src/display/api";
import { AutoPrintRegExp } from "../../web/ui_utils";
import { GlobalWorkerOptions } from "../../src/display/worker_options";
import { isNodeJS } from "../../src/shared/is_node";
import { Metadata } from "../../src/display/metadata";
Expand Down Expand Up @@ -885,9 +886,6 @@ describe("api", function() {
})
.catch(done.fail);
});
// Keep this in sync with the pattern in viewer.js. The pattern is used to
// detect whether or not to automatically start printing.
var viewerPrintRegExp = /\bprint\s*\(/;
it("gets javascript with printing instructions (Print action)", function(done) {
// PDF document with "Print" Named action in the OpenAction dictionary.
var loadingTask = getDocument(buildGetDocumentParams("bug1001080.pdf"));
Expand All @@ -897,7 +895,7 @@ describe("api", function() {
promise
.then(function(data) {
expect(data).toEqual(["print({});"]);
expect(data[0]).toMatch(viewerPrintRegExp);
expect(data[0]).toMatch(AutoPrintRegExp);
loadingTask.destroy().then(done);
})
.catch(done.fail);
Expand All @@ -914,7 +912,7 @@ describe("api", function() {
promise
.then(function(data) {
expect(data).toEqual(["print({});"]);
expect(data[0]).toMatch(viewerPrintRegExp);
expect(data[0]).toMatch(AutoPrintRegExp);
loadingTask.destroy().then(done);
})
.catch(done.fail);
Expand All @@ -930,7 +928,7 @@ describe("api", function() {
expect(data).toEqual([
"this.print({bUI:true,bSilent:false,bShrinkToFit:true});",
]);
expect(data[0]).toMatch(viewerPrintRegExp);
expect(data[0]).toMatch(AutoPrintRegExp);
loadingTask.destroy().then(done);
})
.catch(done.fail);
Expand Down
7 changes: 3 additions & 4 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import {
animationStarted,
AutoPrintRegExp,
DEFAULT_SCALE_VALUE,
getGlobalEventBus,
getPDFFileNameFromURL,
Expand Down Expand Up @@ -1229,10 +1230,8 @@ let PDFViewerApplication = {
});

// Hack to support auto printing.
let regex = /\bprint\s*\(/;
for (let i = 0, ii = javaScript.length; i < ii; i++) {
let js = javaScript[i];
if (js && regex.test(js)) {
for (const js of javaScript) {
if (js && AutoPrintRegExp.test(js)) {
setTimeout(function() {
window.print();
});
Expand Down
4 changes: 4 additions & 0 deletions web/ui_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ const SpreadMode = {
EVEN: 2,
};

// Used by `PDFViewerApplication`, and by the API unit-tests.
const AutoPrintRegExp = /\bprint\s*\(/;

// Replaces {{arguments}} with their values.
function formatL10nValue(text, args) {
if (!args) {
Expand Down Expand Up @@ -936,6 +939,7 @@ function moveToEndOfArray(arr, condition) {
}

export {
AutoPrintRegExp,
CSS_UNITS,
DEFAULT_SCALE_VALUE,
DEFAULT_SCALE,
Expand Down

0 comments on commit 9c767e3

Please sign in to comment.