Skip to content

XFA - Elements created outside of XML must have all their properties (bug 1722029) #13795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 32 additions & 19 deletions src/core/xfa/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
$setValue,
$text,
XFAAttribute,
XFAObjectArray,
XmlObject,
} from "./xfa_object.js";
import { BindItems, Field, Items, SetProperty, Text } from "./template.js";
Expand Down Expand Up @@ -395,6 +396,8 @@ class Binder {
if (matches.length > 1) {
// Clone before binding to avoid bad state.
baseClone = formNode[$clone]();
baseClone[$removeChild](baseClone.occur);
baseClone.occur = null;
}

this._bindValue(formNode, matches[0], picture);
Expand All @@ -412,9 +415,6 @@ class Binder {
for (let i = 1, ii = matches.length; i < ii; i++) {
const match = matches[i];
const clone = baseClone[$clone]();
clone.occur.min = 1;
clone.occur.max = 1;
clone.occur.initial = 1;
parent[name].push(clone);
parent[$insertAt](pos + i, clone);

Expand All @@ -437,20 +437,39 @@ class Binder {
const parent = formNode[$getParent]();
const name = formNode[$nodeName];

for (let i = 0, ii = occur.initial; i < ii; i++) {
const clone = formNode[$clone]();
clone.occur.min = 1;
clone.occur.max = 1;
clone.occur.initial = 1;
parent[name].push(clone);
parent[$appendChild](clone);
if (!(parent[name] instanceof XFAObjectArray)) {
return;
}

let currentNumber;
if (formNode.name) {
currentNumber = parent[name].children.filter(
e => e.name === formNode.name
).length;
} else {
currentNumber = parent[name].children.length;
}

const pos = parent[$indexOf](formNode) + 1;
const ii = occur.initial - currentNumber;
if (ii) {
const nodeClone = formNode[$clone]();
nodeClone[$removeChild](nodeClone.occur);
nodeClone.occur = null;
parent[name].push(nodeClone);
parent[$insertAt](pos, nodeClone);

for (let i = 1; i < ii; i++) {
const clone = nodeClone[$clone]();
parent[name].push(clone);
parent[$insertAt](pos + i, clone);
}
}
}

_getOccurInfo(formNode) {
const { occur } = formNode;
const dataName = formNode.name;
if (!occur || !dataName) {
const { name, occur } = formNode;
if (!occur || !name) {
return [1, 1];
}
const max = occur.max === -1 ? Infinity : occur.max;
Expand Down Expand Up @@ -629,12 +648,6 @@ class Binder {
}

if (match) {
if (match.length < min) {
warn(
`XFA - Must have at least ${min} occurrences: ${formNode[$nodeName]}.`
);
continue;
}
this._bindOccurrences(child, match, picture);
} else if (min > 0) {
this._setProperties(child, dataNode);
Expand Down
66 changes: 51 additions & 15 deletions src/core/xfa/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -3603,25 +3603,60 @@ class Occur extends XFAObject {
constructor(attributes) {
super(TEMPLATE_NS_ID, "occur", /* hasChildren = */ true);
this.id = attributes.id || "";
this.initial = getInteger({
data: attributes.initial,
defaultValue: 1,
validate: x => true,
});
this.max = getInteger({
data: attributes.max,
defaultValue: 1,
validate: x => true,
});
this.min = getInteger({
data: attributes.min,
defaultValue: 1,
validate: x => true,
});
this.initial =
attributes.initial !== ""
? getInteger({
data: attributes.initial,
defaultValue: "",
validate: x => true,
})
: "";
this.max =
attributes.max !== ""
? getInteger({
data: attributes.max,
defaultValue: 1,
validate: x => true,
})
: "";
this.min =
attributes.min !== ""
? getInteger({
data: attributes.min,
defaultValue: 1,
validate: x => true,
})
: "";
this.use = attributes.use || "";
this.usehref = attributes.usehref || "";
this.extras = null;
}

[$clean]() {
const parent = this[$getParent]();
const originalMin = this.min;

if (this.min === "") {
this.min =
parent instanceof PageArea || parent instanceof PageSet ? 0 : 1;
}
if (this.max === "") {
if (originalMin === "") {
this.max =
parent instanceof PageArea || parent instanceof PageSet ? -1 : 1;
} else {
this.max = this.min;
}
}

if (this.max !== -1 && this.max < this.min) {
this.max = this.min;
}

if (this.initial === "") {
this.initial = parent instanceof Template ? 1 : this.min;
}
}
}

class Oid extends StringObject {
Expand Down Expand Up @@ -5705,6 +5740,7 @@ class Value extends XFAObject {
if (parent.ui && parent.ui.imageEdit) {
if (!this.image) {
this.image = new Image({});
this[$appendChild](this.image);
}
this.image[$content] = value[$content];
return;
Expand Down
6 changes: 6 additions & 0 deletions src/core/xfa/xfa_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ class XFAObject {
[$appendChild](child) {
child[_parent] = this;
this[_children].push(child);
if (!child[$globalData] && this[$globalData]) {
child[$globalData] = this[$globalData];
}
}

[$removeChild](child) {
Expand Down Expand Up @@ -236,6 +239,9 @@ class XFAObject {
[$insertAt](i, child) {
child[_parent] = this;
this[_children].splice(i, 0, child);
if (!child[$globalData] && this[$globalData]) {
child[$globalData] = this[$globalData];
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/xfa_bug1722029.pdf.link
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://bugzilla.mozilla.org/attachment.cgi?id=9233058
8 changes: 8 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,14 @@
"enableXfa": true,
"type": "eq"
},
{ "id": "xfa_bug1722029",
"file": "pdfs/xfa_bug1722029.pdf",
"md5": "d8dd6bb20599fc777b4c7ff7b74dd5e3",
"link": true,
"rounds": 1,
"enableXfa": true,
"type": "eq"
},
{ "id": "xfa_bug1721600",
"file": "pdfs/xfa_bug1721600.pdf",
"md5": "5f514f476169eeb7254da380a8db495a",
Expand Down