Skip to content

Commit

Permalink
[Editor] Correctly serialize highlight data (regression from mozilla#…
Browse files Browse the repository at this point in the history
  • Loading branch information
calixteman committed Jan 12, 2024
1 parent 61e5dae commit fc7c320
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 25 deletions.
19 changes: 6 additions & 13 deletions src/display/editor/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,19 +385,12 @@ class HighlightEditor extends AnnotationEditor {

#serializeOutlines(rect) {
const [pageWidth, pageHeight] = this.pageDimensions;
const width = this.width * pageWidth;
const height = this.height * pageHeight;
const [tx, ty] = rect;
const outlines = [];
for (const outline of this.#highlightOutlines.outlines) {
const points = new Array(outline.length);
for (let i = 0; i < outline.length; i += 2) {
points[i] = tx + outline[i] * width;
points[i + 1] = ty + (1 - outline[i + 1]) * height;
}
outlines.push(points);
}
return outlines;
return this.#highlightOutlines.serialize(
rect[0],
rect[1],
this.width * pageWidth,
this.height * pageHeight
);
}

/** @inheritdoc */
Expand Down
13 changes: 13 additions & 0 deletions src/display/editor/outliner.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,19 @@ class HighlightOutline extends Outline {
return buffer.join(" ");
}

serialize(x, y, width, height) {
const outlines = [];
for (const outline of this.#outlines) {
const points = new Array(outline.length);
for (let i = 0; i < outline.length; i += 2) {
points[i] = x + outline[i] * width;
points[i + 1] = y + (1 - outline[i + 1]) * height;
}
outlines.push(points);
}
return outlines;
}

get box() {
return this.#box;
}
Expand Down
73 changes: 61 additions & 12 deletions test/integration/highlight_editor_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
import {
closePages,
getEditorSelector,
getSerialized,
kbSelectAll,
loadAndWait,
scrollIntoView,
waitForSerialized,
} from "./test_utils.mjs";

const selectAll = async page => {
Expand Down Expand Up @@ -160,18 +162,7 @@ describe("Highlight Editor", () => {
await page.click("#editorHighlight");
await page.waitForSelector(".annotationEditorLayer.highlightEditing");

const rect = await page.evaluate(() => {
for (const el of document.querySelectorAll(
`.page[data-page-number="1"] > .textLayer > span`
)) {
if (el.textContent === "Abstract") {
const { x, y, width, height } = el.getBoundingClientRect();
return { x, y, width, height };
}
}
return null;
});

const rect = await getSpanRectFromText(page, 1, "Abstract");
const x = rect.x + rect.width / 2;
const y = rect.y + rect.height / 2;
await page.mouse.click(x, y, { count: 2 });
Expand Down Expand Up @@ -282,4 +273,62 @@ describe("Highlight Editor", () => {
);
});
});

describe("Highlight data serialization", () => {
let pages;

beforeAll(async () => {
pages = await loadAndWait(
"tracemonkey.pdf",
".annotationEditorLayer",
null,
null,
{ highlightEditorColors: "red=#AB0000" }
);
});

afterAll(async () => {
await closePages(pages);
});

it("must be correctly serialized", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await page.click("#editorHighlight");
await page.waitForSelector(".annotationEditorLayer.highlightEditing");

const rect = await getSpanRectFromText(page, 1, "Abstract");
const x = rect.x + rect.width / 2;
const y = rect.y + rect.height / 2;
await page.mouse.click(x, y, { count: 2 });

await page.waitForSelector(`${getEditorSelector(0)}`);
await page.waitForSelector(
`.page[data-page-number = "1"] svg.highlightOutline.selected`
);

await waitForSerialized(page, 1);
const serialized = (await getSerialized(page))[0];
expect(serialized.annotationType)
.withContext(`In ${browserName}`)
.toEqual(9);
expect(serialized.color)
.withContext(`In ${browserName}`)
.toEqual([0xab, 0, 0]);

// We don't check the quadPoints and outlines values because they're
// dependent on the font used in the text layer.
expect(serialized.quadPoints.length)
.withContext(`In ${browserName}`)
.toEqual(8);
expect(serialized.outlines.length)
.withContext(`In ${browserName}`)
.toEqual(1);
expect(serialized.outlines[0].length)
.withContext(`In ${browserName}`)
.toEqual(8);
})
);
});
});
});

0 comments on commit fc7c320

Please sign in to comment.