Skip to content

Commit

Permalink
Merge branch 'mozilla:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
mirhmousavi authored Mar 19, 2024
2 parents 9493b40 + 0022310 commit 4224b5d
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 80 deletions.
17 changes: 16 additions & 1 deletion src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,16 @@ function getDocument(src) {
PDFJSDev.test("GENERIC") &&
isNodeJS
) {
return new PDFNodeStream(params);
const isFetchSupported = function () {
return (
typeof fetch !== "undefined" &&
typeof Response !== "undefined" &&
"body" in Response.prototype
);
};
return isFetchSupported() && isValidFetchUrl(params.url)
? new PDFFetchStream(params)
: new PDFNodeStream(params);
}
return isValidFetchUrl(params.url)
? new PDFFetchStream(params)
Expand Down Expand Up @@ -762,6 +771,9 @@ class PDFDocumentProxy {

if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
// For testing purposes.
Object.defineProperty(this, "getNetworkStreamName", {
value: () => this._transport.getNetworkStreamName(),
});
Object.defineProperty(this, "getXFADatasets", {
value: () => this._transport.getXFADatasets(),
});
Expand Down Expand Up @@ -2359,6 +2371,9 @@ class WorkerTransport {

if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
// For testing purposes.
Object.defineProperty(this, "getNetworkStreamName", {
value: () => networkStream?.constructor?.name || null,
});
Object.defineProperty(this, "getXFADatasets", {
value: () =>
this.messageHandler.sendWithPromise("GetXFADatasets", null),
Expand Down
27 changes: 21 additions & 6 deletions src/display/editor/annotation_editor_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ class AnnotationEditorLayer {

#annotationLayer = null;

#boundPointerup = this.pointerup.bind(this);
#boundPointerup = null;

#boundPointerdown = this.pointerdown.bind(this);
#boundPointerdown = null;

#boundTextLayerPointerDown = this.#textLayerPointerDown.bind(this);
#boundTextLayerPointerDown = null;

#editorFocusTimeoutId = null;

Expand Down Expand Up @@ -333,7 +333,8 @@ class AnnotationEditorLayer {
}

enableTextSelection() {
if (this.#textLayer?.div) {
if (this.#textLayer?.div && !this.#boundTextLayerPointerDown) {
this.#boundTextLayerPointerDown = this.#textLayerPointerDown.bind(this);
this.#textLayer.div.addEventListener(
"pointerdown",
this.#boundTextLayerPointerDown
Expand All @@ -343,11 +344,12 @@ class AnnotationEditorLayer {
}

disableTextSelection() {
if (this.#textLayer?.div) {
if (this.#textLayer?.div && this.#boundTextLayerPointerDown) {
this.#textLayer.div.removeEventListener(
"pointerdown",
this.#boundTextLayerPointerDown
);
this.#boundTextLayerPointerDown = null;
this.#textLayer.div.classList.remove("highlighting");
}
}
Expand Down Expand Up @@ -385,13 +387,23 @@ class AnnotationEditorLayer {
}

enableClick() {
if (this.#boundPointerdown) {
return;
}
this.#boundPointerdown = this.pointerdown.bind(this);
this.#boundPointerup = this.pointerup.bind(this);
this.div.addEventListener("pointerdown", this.#boundPointerdown);
this.div.addEventListener("pointerup", this.#boundPointerup);
}

disableClick() {
if (!this.#boundPointerdown) {
return;
}
this.div.removeEventListener("pointerdown", this.#boundPointerdown);
this.div.removeEventListener("pointerup", this.#boundPointerup);
this.#boundPointerdown = null;
this.#boundPointerup = null;
}

attach(editor) {
Expand Down Expand Up @@ -821,6 +833,8 @@ class AnnotationEditorLayer {
for (const editor of this.#uiManager.getEditors(this.pageIndex)) {
this.add(editor);
}
// We're maybe rendering a layer which was invisible when we started to edit
// so we must set the different callbacks for it.
this.updateMode();
}

Expand All @@ -833,6 +847,7 @@ class AnnotationEditorLayer {
// issues (see #15582), we must commit the current one before changing
// the viewport.
this.#uiManager.commitOrRemove();
this.#cleanup();

const oldRotation = this.viewport.rotation;
const rotation = viewport.rotation;
Expand All @@ -843,7 +858,7 @@ class AnnotationEditorLayer {
editor.rotate(rotation);
}
}
this.updateMode();
this.addInkEditorIfNeeded(/* isCommitting = */ false);
}

/**
Expand Down
58 changes: 38 additions & 20 deletions test/integration/freetext_editor_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -444,15 +444,16 @@ describe("FreeText Editor", () => {

await clearAll(page);

const editorSelector = getEditorSelector(9);
await page.mouse.click(rect.x + 200, rect.y + 100);
await page.waitForSelector(getEditorSelector(9), {
await page.waitForSelector(editorSelector, {
visible: true,
});

for (let i = 0; i < 5; i++) {
await page.type(`${getEditorSelector(9)} .internal`, "A");
await page.type(`${editorSelector} .internal`, "A");

const editorRect = await page.$eval(getEditorSelector(9), el => {
const editorRect = await page.$eval(editorSelector, el => {
const { x, y, width, height } = el.getBoundingClientRect();
return { x, y, width, height };
});
Expand All @@ -462,9 +463,7 @@ describe("FreeText Editor", () => {
editorRect.x + 1.5 * editorRect.width,
editorRect.y
);
await page.waitForSelector(
`${getEditorSelector(9)} .overlay.enabled`
);
await page.waitForSelector(`${editorSelector} .overlay.enabled`);

if (i < 4) {
// And select it again.
Expand All @@ -474,13 +473,13 @@ describe("FreeText Editor", () => {
{ count: 2 }
);
await page.waitForSelector(
`${getEditorSelector(9)} .overlay:not(.enabled)`
`${editorSelector} .overlay:not(.enabled)`
);
}
}

let prevText = await page.$eval(
`${getEditorSelector(9)} .internal`,
`${editorSelector} .internal`,
el => el.innerText
);

Expand All @@ -489,10 +488,10 @@ describe("FreeText Editor", () => {
(prev, sel) => document.querySelector(sel).innerText !== prev,
{},
previous,
`${getEditorSelector(9)} .internal`
`${editorSelector} .internal`
);
const getText = () =>
page.$eval(`${getEditorSelector(9)} .internal`, el => el.innerText);
page.$eval(`${editorSelector} .internal`, el => el.innerText);

// We're in the middle of the text.
await kbUndo(page);
Expand Down Expand Up @@ -528,15 +527,15 @@ describe("FreeText Editor", () => {
);

await kbRedo(page);
await page.waitForSelector(getEditorSelector(9), {
await page.waitForSelector(editorSelector, {
visible: true,
});

text = await getText();
expect(text).withContext(`In ${browserName}`).toEqual("A");

// Add a new A.
const editorRect = await page.$eval(getEditorSelector(9), el => {
let editorRect = await page.$eval(editorSelector, el => {
const { x, y, width, height } = el.getBoundingClientRect();
return { x, y, width, height };
});
Expand All @@ -545,17 +544,20 @@ describe("FreeText Editor", () => {
editorRect.y + editorRect.height / 2,
{ count: 2 }
);
await page.waitForSelector(
`${getEditorSelector(9)} .overlay:not(.enabled)`
);
await page.type(`${getEditorSelector(9)} .internal`, "A");
await page.waitForSelector(`${editorSelector} .overlay:not(.enabled)`);
await page.type(`${editorSelector} .internal`, "A");

editorRect = await page.$eval(editorSelector, el => {
const { x, y, width, height } = el.getBoundingClientRect();
return { x, y, width, height };
});

// Commit.
await page.mouse.click(
editorRect.x + 1.5 * editorRect.width,
editorRect.y
);
await page.waitForSelector(`${getEditorSelector(9)} .overlay.enabled`);
await page.waitForSelector(`${editorSelector} .overlay.enabled`);

text = await getText();
expect(text).withContext(`In ${browserName}`).toEqual("AA");
Expand Down Expand Up @@ -2305,7 +2307,7 @@ describe("FreeText Editor", () => {
pages.map(async ([browserName, page]) => {
await switchToFreeText(page);

const rect = await page.$eval(".annotationEditorLayer", el => {
let rect = await page.$eval(".annotationEditorLayer", el => {
const { x, y } = el.getBoundingClientRect();
return { x, y };
});
Expand All @@ -2322,7 +2324,15 @@ describe("FreeText Editor", () => {
`${getEditorSelector(0)} .overlay.enabled`
);

await page.mouse.click(rect.x + 110, rect.y + 150);
rect = await page.$eval(getEditorSelector(0), el => {
const { x, y, width, height } = el.getBoundingClientRect();
return { x, y, width, height };
});

await page.mouse.click(
rect.x + 5 * rect.width,
rect.y + 5 * rect.height
);
await page.waitForSelector(getEditorSelector(1), {
visible: true,
});
Expand All @@ -2334,7 +2344,15 @@ describe("FreeText Editor", () => {
`${getEditorSelector(1)} .overlay.enabled`
);

await page.mouse.click(rect.x + 111, rect.y + 151);
rect = await page.$eval(getEditorSelector(0), el => {
const { x, y, width, height } = el.getBoundingClientRect();
return { x, y, width, height };
});

await page.mouse.click(
rect.x + 5 * rect.width,
rect.y + 5 * rect.height
);
await waitForSelectedEditor(page, getEditorSelector(1));

const pos = n =>
Expand Down
29 changes: 22 additions & 7 deletions test/unit/api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
import {
buildGetDocumentParams,
CMAP_URL,
createTemporaryNodeServer,
DefaultFileReaderFactory,
TEST_PDFS_PATH,
} from "./test_utils.js";
Expand Down Expand Up @@ -67,13 +68,27 @@ describe("api", function () {
buildGetDocumentParams(tracemonkeyFileName);

let CanvasFactory;
let tempServer = null;

beforeAll(function () {
CanvasFactory = new DefaultCanvasFactory();

if (isNodeJS) {
tempServer = createTemporaryNodeServer();
}
});

afterAll(function () {
CanvasFactory = null;

if (isNodeJS) {
// Close the server from accepting new connections after all test
// finishes.
const { server } = tempServer;
server.close();

tempServer = null;
}
});

function waitSome(callback) {
Expand Down Expand Up @@ -119,13 +134,10 @@ describe("api", function () {
});

it("creates pdf doc from URL-object", async function () {
if (isNodeJS) {
pending("window.location is not supported in Node.js.");
}
const urlObj = new URL(
TEST_PDFS_PATH + basicApiFileName,
window.location
);
const urlObj = isNodeJS
? new URL(`http://127.0.0.1:${tempServer.port}/${basicApiFileName}`)
: new URL(TEST_PDFS_PATH + basicApiFileName, window.location);

const loadingTask = getDocument(urlObj);
expect(loadingTask instanceof PDFDocumentLoadingTask).toEqual(true);
const pdfDocument = await loadingTask.promise;
Expand All @@ -134,6 +146,9 @@ describe("api", function () {
expect(pdfDocument instanceof PDFDocumentProxy).toEqual(true);
expect(pdfDocument.numPages).toEqual(3);

// Ensure that the Fetch API was used to load the PDF document.
expect(pdfDocument.getNetworkStreamName()).toEqual("PDFFetchStream");

await loadingTask.destroy();
});

Expand Down
1 change: 1 addition & 0 deletions test/unit/clitests.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"encodings_spec.js",
"evaluator_spec.js",
"event_utils_spec.js",
"fetch_stream_spec.js",
"font_substitutions_spec.js",
"function_spec.js",
"message_handler_spec.js",
Expand Down
32 changes: 28 additions & 4 deletions test/unit/fetch_stream_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,40 @@
* limitations under the License.
*/

import { AbortException } from "../../src/shared/util.js";
import { AbortException, isNodeJS } from "../../src/shared/util.js";
import { createTemporaryNodeServer } from "./test_utils.js";
import { PDFFetchStream } from "../../src/display/fetch_stream.js";

describe("fetch_stream", function () {
const pdfUrl = new URL("../pdfs/tracemonkey.pdf", window.location).href;
let tempServer = null;

function getPdfUrl() {
return isNodeJS
? `http://127.0.0.1:${tempServer.port}/tracemonkey.pdf`
: new URL("../pdfs/tracemonkey.pdf", window.location).href;
}
const pdfLength = 1016315;

beforeAll(function () {
if (isNodeJS) {
tempServer = createTemporaryNodeServer();
}
});

afterAll(function () {
if (isNodeJS) {
// Close the server from accepting new connections after all test
// finishes.
const { server } = tempServer;
server.close();

tempServer = null;
}
});

it("read with streaming", async function () {
const stream = new PDFFetchStream({
url: pdfUrl,
url: getPdfUrl(),
disableStream: false,
disableRange: true,
});
Expand Down Expand Up @@ -57,7 +81,7 @@ describe("fetch_stream", function () {
it("read ranges with streaming", async function () {
const rangeSize = 32768;
const stream = new PDFFetchStream({
url: pdfUrl,
url: getPdfUrl(),
rangeChunkSize: rangeSize,
disableStream: false,
disableRange: false,
Expand Down
Loading

0 comments on commit 4224b5d

Please sign in to comment.