Skip to content

Commit

Permalink
XFA - Remove namespace from nodes under xfa:data node
Browse files Browse the repository at this point in the history
  - in real life some xfa contains xml like <xfa:data><xfa:Foo><xfa:Bar>...</xfa:data>
    since there are no Foo or Bar in the xfa namespace the JS representation are empty
    and that leads to errors.
  - so the idea is to make all nodes under xfa:data namespace agnostic which means
    that ns are removed from nodes in the parser but only xfa:data descendants.
  • Loading branch information
calixteman committed Jul 21, 2021
1 parent 6c9b6bc commit 5555114
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
18 changes: 16 additions & 2 deletions src/core/xfa/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
$cleanup,
$finalize,
$ids,
$isNsAgnostic,
$nsAttributes,
$onChild,
$resolvePrototypes,
Expand Down Expand Up @@ -67,6 +68,7 @@ class Empty extends XFAObject {
class Builder {
constructor() {
this._namespaceStack = [];
this._nsAgnosticLevel = 0;

// Each prefix has its own stack
this._namespacePrefixes = new Map();
Expand Down Expand Up @@ -118,18 +120,27 @@ class Builder {
(namespaceToUse && namespaceToUse[$buildXFAObject](name, attributes)) ||
new Empty();

if (node[$isNsAgnostic]()) {
this._nsAgnosticLevel++;
}

// In case the node has some namespace things,
// we must pop the different stacks.
if (hasNamespaceDef || prefixes) {
if (hasNamespaceDef || prefixes || node[$isNsAgnostic]()) {
node[$cleanup] = {
hasNamespace: hasNamespaceDef,
prefixes,
nsAgnostic: node[$isNsAgnostic](),
};
}

return node;
}

isNsAgnostic() {
return this._nsAgnosticLevel > 0;
}

_searchNamespace(nsName) {
let ns = this._namespaces.get(nsName);
if (ns) {
Expand Down Expand Up @@ -178,7 +189,7 @@ class Builder {
}

clean(data) {
const { hasNamespace, prefixes } = data;
const { hasNamespace, prefixes, nsAgnostic } = data;
if (hasNamespace) {
this._currentNamespace = this._namespaceStack.pop();
}
Expand All @@ -187,6 +198,9 @@ class Builder {
this._namespacePrefixes.get(prefix).pop();
});
}
if (nsAgnostic) {
this._nsAgnosticLevel--;
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/core/xfa/datasets.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import {
$appendChild,
$isNsAgnostic,
$namespaceId,
$nodeName,
$onChild,
Expand All @@ -29,6 +30,10 @@ class Data extends XmlObject {
constructor(attributes) {
super(DATASETS_NS_ID, "data", attributes);
}

[$isNsAgnostic]() {
return true;
}
}

class Datasets extends XFAObject {
Expand Down
9 changes: 6 additions & 3 deletions src/core/xfa/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,23 @@ class XFAParser extends XMLParserBase {
return [namespace, prefixes, attributeObj];
}

_getNameAndPrefix(name) {
_getNameAndPrefix(name, nsAgnostic) {
const i = name.indexOf(":");
if (i === -1) {
return [name, null];
}
return [name.substring(i + 1), name.substring(0, i)];
return [name.substring(i + 1), nsAgnostic ? "" : name.substring(0, i)];
}

onBeginElement(tagName, attributes, isEmpty) {
const [namespace, prefixes, attributesObj] = this._mkAttributes(
attributes,
tagName
);
const [name, nsPrefix] = this._getNameAndPrefix(tagName);
const [name, nsPrefix] = this._getNameAndPrefix(
tagName,
this._builder.isNsAgnostic()
);
const node = this._builder.build({
nsPrefix,
name,
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 @@ -60,6 +60,7 @@ const $isCDATAXml = Symbol();
const $isBindable = Symbol();
const $isDataValue = Symbol();
const $isDescendent = Symbol();
const $isNsAgnostic = Symbol();
const $isSplittable = Symbol();
const $isThereMoreWidth = Symbol();
const $isTransparent = Symbol();
Expand Down Expand Up @@ -160,6 +161,10 @@ class XFAObject {
);
}

[$isNsAgnostic]() {
return false;
}

[$acceptWhitespace]() {
return false;
}
Expand Down Expand Up @@ -1087,6 +1092,7 @@ export {
$isCDATAXml,
$isDataValue,
$isDescendent,
$isNsAgnostic,
$isSplittable,
$isThereMoreWidth,
$isTransparent,
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/xfa_bug1721600.pdf.link
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://bugzilla.mozilla.org/attachment.cgi?id=9232367
9 changes: 9 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,15 @@
"enableXfa": true,
"type": "eq"
},
{ "id": "xfa_bug1721600",
"file": "pdfs/xfa_bug1721600.pdf",
"md5": "5f514f476169eeb7254da380a8db495a",
"link": true,
"rounds": 1,
"lastPage": 1,
"enableXfa": true,
"type": "eq"
},
{ "id": "xfa_bug1720182",
"file": "pdfs/xfa_bug1720182.pdf",
"md5": "1351f816f0509fe750ca61ef2bd40872",
Expand Down

0 comments on commit 5555114

Please sign in to comment.