Skip to content
Snippets Groups Projects
Verified Commit 5e9b2237 authored by Jan Loewe's avatar Jan Loewe :speech_balloon:
Browse files

fix(xml): add workaround for element order

parent e9052c8b
No related branches found
No related tags found
No related merge requests found
...@@ -32,10 +32,10 @@ export class DCCDocument { ...@@ -32,10 +32,10 @@ export class DCCDocument {
ignoreComment: true, ignoreComment: true,
trim: true, trim: true,
attributesKey: "_attr", attributesKey: "_attr",
elementNameFn: function (val) { elementNameFn: function(val) {
return val.replace("dcc:", "").replace("si:", ""); return val.replace("dcc:", "").replace("si:", "");
}, },
textFn: function (val) { textFn: function(val) {
return val return val
.split("\n") .split("\n")
.map((x) => x.trim()) .map((x) => x.trim())
...@@ -47,12 +47,16 @@ export class DCCDocument { ...@@ -47,12 +47,16 @@ export class DCCDocument {
} }
toXML(): string { toXML(): string {
return js2xml(<Element>this, { /* Properties order in objects is not guaranteed in JavaScript.
* This is a workaround to fix this.
* The keys are set in the right order by creating a new instance of DCCDocument */
const fixedOrderDoc = new DCCDocument(this);
return js2xml(<Element>fixedOrderDoc, {
compact: true, compact: true,
ignoreComment: true, ignoreComment: true,
spaces: 2, spaces: 2,
attributesKey: "_attr", attributesKey: "_attr",
elementNameFn: function (val, currentElementObj) { elementNameFn: function(val, currentElementObj) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
const currentElement = <any>currentElementObj; const currentElement = <any>currentElementObj;
return `${currentElement?.namespace ? `${currentElement.namespace}:` : ""}${val}`; return `${currentElement?.namespace ? `${currentElement.namespace}:` : ""}${val}`;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment