mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-26 10:08:06 +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:
parent
3b1d547738
commit
558e58f354
8 changed files with 173 additions and 44 deletions
|
@ -26,10 +26,14 @@ import {
|
|||
$toStyle,
|
||||
XFAObject,
|
||||
} from "./xfa_object.js";
|
||||
import {
|
||||
addDefaultProtocolToUrl,
|
||||
tryConvertUrlEncoding,
|
||||
} from "../core_utils.js";
|
||||
import { createValidAbsoluteUrl, warn } from "../../shared/util.js";
|
||||
import { getMeasurement, stripQuotes } from "./utils.js";
|
||||
import { selectFont } from "./fonts.js";
|
||||
import { TextMeasure } from "./text.js";
|
||||
import { warn } from "../../shared/util.js";
|
||||
|
||||
function measureToString(m) {
|
||||
if (typeof m === "string") {
|
||||
|
@ -633,11 +637,24 @@ function setFontFamily(xfaFont, node, fontFinder, style) {
|
|||
}
|
||||
}
|
||||
|
||||
function fixURL(str) {
|
||||
if (typeof str === "string") {
|
||||
let url = addDefaultProtocolToUrl(str);
|
||||
url = tryConvertUrlEncoding(url);
|
||||
const absoluteUrl = createValidAbsoluteUrl(url);
|
||||
if (absoluteUrl) {
|
||||
return absoluteUrl.href;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export {
|
||||
computeBbox,
|
||||
createWrapper,
|
||||
fixDimensions,
|
||||
fixTextIndent,
|
||||
fixURL,
|
||||
isPrintOnly,
|
||||
layoutClass,
|
||||
layoutNode,
|
||||
|
|
|
@ -76,6 +76,7 @@ import {
|
|||
createWrapper,
|
||||
fixDimensions,
|
||||
fixTextIndent,
|
||||
fixURL,
|
||||
isPrintOnly,
|
||||
layoutClass,
|
||||
layoutNode,
|
||||
|
@ -100,6 +101,7 @@ import {
|
|||
} from "./utils.js";
|
||||
import { stringToBytes, Util, warn } from "../../shared/util.js";
|
||||
import { getMetrics } from "./fonts.js";
|
||||
import { recoverJsURL } from "../core_utils.js";
|
||||
import { searchNode } from "./som.js";
|
||||
|
||||
const TEMPLATE_NS_ID = NamespaceIds.template.id;
|
||||
|
@ -1066,7 +1068,10 @@ class Button extends XFAObject {
|
|||
|
||||
[$toHTML](availableSpace) {
|
||||
// TODO: highlight.
|
||||
return HTMLResult.success({
|
||||
|
||||
const parent = this[$getParent]();
|
||||
const grandpa = parent[$getParent]();
|
||||
const htmlButton = {
|
||||
name: "button",
|
||||
attributes: {
|
||||
id: this[$uid],
|
||||
|
@ -1074,7 +1079,38 @@ class Button extends XFAObject {
|
|||
style: {},
|
||||
},
|
||||
children: [],
|
||||
});
|
||||
};
|
||||
|
||||
for (const event of grandpa.event.children) {
|
||||
// if (true) break;
|
||||
if (event.activity !== "click" || !event.script) {
|
||||
continue;
|
||||
}
|
||||
const jsURL = recoverJsURL(event.script[$content]);
|
||||
if (!jsURL) {
|
||||
continue;
|
||||
}
|
||||
const href = fixURL(jsURL.url);
|
||||
if (!href) {
|
||||
continue;
|
||||
}
|
||||
const target = jsURL.newWindow ? "_blank" : undefined;
|
||||
|
||||
// we've an url so generate a <a>
|
||||
htmlButton.children.push({
|
||||
name: "a",
|
||||
attributes: {
|
||||
id: "link" + this[$uid],
|
||||
href,
|
||||
target,
|
||||
class: ["xfaLink"],
|
||||
style: {},
|
||||
},
|
||||
children: [],
|
||||
});
|
||||
}
|
||||
|
||||
return HTMLResult.success(htmlButton);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2897,7 +2933,12 @@ class Field extends XFAObject {
|
|||
ui.attributes.style = Object.create(null);
|
||||
}
|
||||
|
||||
let aElement = null;
|
||||
|
||||
if (this.ui.button) {
|
||||
if (ui.children.length === 1) {
|
||||
[aElement] = ui.children.splice(0, 1);
|
||||
}
|
||||
Object.assign(ui.attributes.style, borderStyle);
|
||||
} else {
|
||||
Object.assign(style, borderStyle);
|
||||
|
@ -2955,6 +2996,10 @@ class Field extends XFAObject {
|
|||
}
|
||||
}
|
||||
|
||||
if (aElement) {
|
||||
ui.children.push(aElement);
|
||||
}
|
||||
|
||||
if (!caption) {
|
||||
if (ui.attributes.class) {
|
||||
// Even if no caption this class will help to center the ui.
|
||||
|
|
|
@ -30,12 +30,12 @@ import {
|
|||
} from "./xfa_object.js";
|
||||
import { $buildXFAObject, NamespaceIds } from "./namespaces.js";
|
||||
import {
|
||||
addDefaultProtocolToUrl,
|
||||
tryConvertUrlEncoding,
|
||||
} from "../core_utils.js";
|
||||
import { fixTextIndent, measureToString, setFontFamily } from "./html_utils.js";
|
||||
fixTextIndent,
|
||||
fixURL,
|
||||
measureToString,
|
||||
setFontFamily,
|
||||
} from "./html_utils.js";
|
||||
import { getMeasurement, HTMLResult, stripQuotes } from "./utils.js";
|
||||
import { createValidAbsoluteUrl } from "../../shared/util.js";
|
||||
|
||||
const XHTML_NS_ID = NamespaceIds.xhtml.id;
|
||||
|
||||
|
@ -326,16 +326,7 @@ class XhtmlObject extends XmlObject {
|
|||
class A extends XhtmlObject {
|
||||
constructor(attributes) {
|
||||
super(attributes, "a");
|
||||
let href = "";
|
||||
if (typeof attributes.href === "string") {
|
||||
let url = addDefaultProtocolToUrl(attributes.href);
|
||||
url = tryConvertUrlEncoding(url);
|
||||
const absoluteUrl = createValidAbsoluteUrl(url);
|
||||
if (absoluteUrl) {
|
||||
href = absoluteUrl.href;
|
||||
}
|
||||
}
|
||||
this.href = href;
|
||||
this.href = fixURL(attributes.href) || "";
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue