Skip to content

Commit 79dd40d

Browse files
committed
fix #705: don not group and nest attributes when both preserveOrder and attributesGroupName are set
1 parent d6bce3b commit 79dd40d

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

docs/v4, v5/2.XMLparseOptions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ Output
154154
}
155155
```
156156

157+
If `preserveOrder` is set to `true`, `attributesGroupName` is ignored (because attributes are already preserved in their original order within the node).
158+
157159
## attributeNamePrefix
158160

159161
To recognize attributes in the JS object separately. You can prepend some string with each attribute name.

spec/attr_spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,27 @@ id="7" data="foo bar" bug="true"/>`;
330330
//console.log(JSON.stringify(result,null,4));
331331
expect(result).toEqual(expected);
332332
});
333+
334+
it("should preser over with attribute group", function () {
335+
const xmlData = "<rootNode abc='23' />";
336+
const expected = [
337+
{
338+
"rootNode": [],
339+
":@": {
340+
"@_abc": "23"
341+
}
342+
}
343+
]
344+
345+
const options = {
346+
attributesGroupName: ':@',
347+
preserveOrder: true,
348+
ignoreAttributes: false,
349+
}
350+
const parser = new XMLParser(options);
351+
352+
const result = parser.parse(xmlData);
353+
// console.log(JSON.stringify(result, null, 4));
354+
expect(result).toEqual(expected);
355+
});
333356
});

src/xmlparser/OrderedObjParser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ function buildAttributesMap(attrStr, jPath, tagName, force = false) {
269269

270270
if (!hasAttrs) return;
271271

272-
if (options.attributesGroupName) {
272+
if (options.attributesGroupName && !options.preserveOrder) {
273273
const attrCollection = {};
274274
attrCollection[options.attributesGroupName] = attrs;
275275
return attrCollection;

0 commit comments

Comments
 (0)