1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-20 15:18:08 +02:00

XFA - Add <a> element in button when an url is detected (bug 1716758)

- it aims to fix https://bugzilla.mozilla.org/show_bug.cgi?id=1716758;
  - some buttons have a JS action with the pattern `app.launchURL(...)` (or similar) so extract when it's possible the url and generate a <a> element with the href equals to the found url;
  - pdf.js already had some code to handle that so this patch slightly refactor that.
This commit is contained in:
Calixte Denizet 2021-09-25 14:46:40 +02:00
parent 3b1d547738
commit 558e58f354
8 changed files with 173 additions and 44 deletions

View file

@ -1108,7 +1108,7 @@ describe("annotation", function () {
jsEntry: "window.open('http://www.example.com/test.pdf')",
expectedUrl: new URL("http://www.example.com/test.pdf").href,
expectedUnsafeUrl: "http://www.example.com/test.pdf",
expectedNewWindow: undefined,
expectedNewWindow: false,
});
// Check that we accept a white-listed {Stream} 'JS' entry.

View file

@ -17,18 +17,20 @@ import { isNodeJS } from "../../src/shared/is_node.js";
import { XFAFactory } from "../../src/core/xfa/factory.js";
describe("XFAFactory", function () {
function searchHtmlNode(root, name, value, byAttributes = false) {
function searchHtmlNode(root, name, value, byAttributes = false, nth = [0]) {
if (
(!byAttributes && root[name] === value) ||
(byAttributes && root.attributes && root.attributes[name] === value)
) {
return root;
if (nth[0]-- === 0) {
return root;
}
}
if (!root.children) {
return null;
}
for (const child of root.children) {
const node = searchHtmlNode(child, name, value, byAttributes);
const node = searchHtmlNode(child, name, value, byAttributes, nth);
if (node) {
return node;
}
@ -588,4 +590,60 @@ describe("XFAFactory", function () {
expect(a.value).toEqual("qwerty/");
expect(a.attributes.href).toEqual("");
});
it("should replace button with an URL by a link", function () {
const xml = `
<?xml version="1.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
<template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
<subform name="root" mergeMode="matchTemplate">
<pageSet>
<pageArea>
<contentArea x="123pt" w="456pt" h="789pt"/>
<medium stock="default" short="456pt" long="789pt"/>
</pageArea>
</pageSet>
<subform name="first">
<field y="1pt" w="11pt" h="22pt" x="2pt">
<ui>
<button/>
</ui>
<event activity="click" name="event__click">
<script contentType="application/x-javascript">
app.launchURL("https://github.com/mozilla/pdf.js", true);
</script>
</event>
</field>
<field y="1pt" w="11pt" h="22pt" x="2pt">
<ui>
<button/>
</ui>
<event activity="click" name="event__click">
<script contentType="application/x-javascript">
xfa.host.gotoURL("https://github.com/allizom/pdf.js");
</script>
</event>
</field>
</subform>
</subform>
</template>
<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<xfa:data>
</xfa:data>
</xfa:datasets>
</xdp:xdp>
`;
const factory = new XFAFactory({ "xdp:xdp": xml });
expect(factory.numberPages).toEqual(1);
const pages = factory.getPages();
let a = searchHtmlNode(pages, "name", "a");
expect(a.attributes.href).toEqual("https://github.com/mozilla/pdf.js");
expect(a.attributes.target).toEqual("_blank");
a = searchHtmlNode(pages, "name", "a", false, [1]);
expect(a.attributes.href).toEqual("https://github.com/allizom/pdf.js");
expect(a.attributes.target).toBe(undefined);
});
});