Skip to content

Commit 5369530

Browse files
committed
Enforce group = false option on attributes even if element names are not unique and should be grouped. Fixes #192
1 parent 2ba20fa commit 5369530

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

src/writers/ObjectWriter.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class ObjectWriter extends BaseWriter<ObjectWriterOptions, XMLSerializedA
1717

1818
/**
1919
* Initializes a new instance of `ObjectWriter`.
20-
*
20+
*
2121
* @param builderOptions - XML builder options
2222
* @param writerOptions - serialization options
2323
*/
@@ -33,7 +33,7 @@ export class ObjectWriter extends BaseWriter<ObjectWriterOptions, XMLSerializedA
3333

3434
/**
3535
* Produces an XML serialization of the given node.
36-
*
36+
*
3737
* @param node - node to serialize
3838
*/
3939
serialize(node: Node): XMLSerializedAsObject | XMLSerializedAsObjectArray {
@@ -67,7 +67,7 @@ export class ObjectWriter extends BaseWriter<ObjectWriterOptions, XMLSerializedA
6767
* {
6868
* root: {
6969
* node: [
70-
* {
70+
* {
7171
* "@att1": "val1",
7272
* "@att2": "val2",
7373
* "#1": "node text",
@@ -141,10 +141,12 @@ export class ObjectWriter extends BaseWriter<ObjectWriterOptions, XMLSerializedA
141141
if (key === "@") {
142142
const attrs = (item as AttrNode)["@"]
143143
const attrKeys = Object.keys(attrs)
144-
if (attrKeys.length === 1) {
145-
obj[defAttrKey + attrKeys[0]] = attrs[attrKeys[0]]
144+
if (!options.group || attrKeys.length === 1) {
145+
for (const attrName in attrs) {
146+
obj[defAttrKey + attrName] = attrs[attrName]
147+
}
146148
} else {
147-
obj[defAttrKey] = (item as AttrNode)["@"]
149+
obj[defAttrKey] = attrs
148150
}
149151
}
150152
}
@@ -432,7 +434,7 @@ export class ObjectWriter extends BaseWriter<ObjectWriterOptions, XMLSerializedA
432434

433435
/**
434436
* Returns an object key for the given node type.
435-
*
437+
*
436438
* @param nodeType - node type to get a key for
437439
*/
438440
private _getNodeKey(nodeType: NodeType): string {

test/issues/issue-105.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import $$ from "../TestHelpers";
22

33
describe("Replicate issue", () => {
4-
// https://github.com/oozcitak/xmlbuilder2/issues/90
4+
// https://github.com/oozcitak/xmlbuilder2/issues/105
55
test(`#105 - Illegal character does not get sanitized.`, () => {
66
const b = $$.create()
77
b.ele('doc').ele('test').txt('some & text; foo')

test/issues/issue-178.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import $$ from '../TestHelpers';
22

33
describe('Replicate issue', () => {
4-
// https://github.com/oozcitak/xmlbuilder2/issues/90
4+
// https://github.com/oozcitak/xmlbuilder2/issues/178
55
describe(`#178 - Namespace is removed when importing fragments.`, () => {
66
const expectedOutput = $$.t`<?xml version="1.0"?><Root xmlns="ns1"><Parent xmlns="ns2"><Child xmlns="ns3"><GrandChild xmlns="ns4">txt</GrandChild></Child></Parent></Root>`;
77
describe(`with defined namespaces on each element`, () => {

test/issues/issue-192.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import $$ from '../TestHelpers';
2+
3+
describe('Replicate issue', () => {
4+
test(`#192 - Attributes being grouped in ObjectWriter in specific case despite group being false`, () => {
5+
const expectedOutput = {
6+
mode: {
7+
'@attr1': "value",
8+
'@attr2': "value",
9+
"#": [
10+
{
11+
model: {}
12+
},
13+
{
14+
blockers: {}
15+
},
16+
{
17+
model: {}
18+
}
19+
]
20+
}
21+
}
22+
const xml = `<?xml version="1.0"?><mode attr1='value' attr2='value'><model /><blockers /><model /></mode>`;
23+
const obj = $$.create(xml).end({ format: 'object', group: false });
24+
expect(obj).toEqual(expectedOutput)
25+
});
26+
});

0 commit comments

Comments
 (0)