1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-23 08:38:06 +02:00

Render rich text with only one text element

This commit is contained in:
Calixte Denizet 2023-11-02 15:35:52 +01:00
parent 50c0fccda6
commit 085aa4207d
4 changed files with 29 additions and 4 deletions

View file

@ -176,8 +176,8 @@ class XfaLayer {
linkService,
});
}
const stack = [[root, -1, rootHtml]];
const isNotForRichText = intent !== "richText";
const rootDiv = parameters.div;
rootDiv.append(rootHtml);
@ -187,13 +187,28 @@ class XfaLayer {
}
// Set defaults.
if (intent !== "richText") {
if (isNotForRichText) {
rootDiv.setAttribute("class", "xfaLayer xfaFont");
}
// Text nodes used for the text highlighter.
const textDivs = [];
// In the rich text context, it's possible to just have a text node without
// a root element, so we handle this case here (see issue 17215).
if (root.children.length === 0) {
if (root.value) {
const node = document.createTextNode(root.value);
rootHtml.append(node);
if (isNotForRichText && XfaText.shouldBuildText(root.name)) {
textDivs.push(node);
}
}
return { textDivs };
}
const stack = [[root, -1, rootHtml]];
while (stack.length > 0) {
const [parent, i, html] = stack.at(-1);
if (i + 1 === parent.children.length) {
@ -233,7 +248,7 @@ class XfaLayer {
stack.push([child, -1, childHtml]);
} else if (child.value) {
const node = document.createTextNode(child.value);
if (XfaText.shouldBuildText(name)) {
if (isNotForRichText && XfaText.shouldBuildText(name)) {
textDivs.push(node);
}
childHtml.append(node);