Skip to content

Commit

Permalink
Do not group attributes in element content. Fixes #29
Browse files Browse the repository at this point in the history
  • Loading branch information
oozcitak committed Jul 9, 2020
1 parent 8b0df63 commit 2e4508b
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 16 deletions.
25 changes: 17 additions & 8 deletions src/writers/ObjectWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,30 @@ export class ObjectWriter extends BaseWriter<ObjectWriterOptions, XMLSerializedA
// special case of an element node with a single text node
return (items[0] as TextNode)["#"]
} else if (hasNonUniqueNames) {
const obj: XMLSerializedAsObject | XMLSerializedAsObjectArray = { }
// process attributes first
for (let i = 0; i < items.length; i++) {
const item = items[i]
const key = Object.keys(item)[0]
if (key === "@") {
const attrs = (item as AttrNode)["@"]
const attrKeys = Object.keys(attrs)
if (attrKeys.length === 1) {
obj[defAttrKey + attrKeys[0]] = attrs[attrKeys[0]]
} else {
obj[defAttrKey] = (item as AttrNode)["@"]
}
}
}
// list contains element nodes with non-unique names
// return an array with mixed content notation
const result: XMLSerializedAsObject | XMLSerializedAsObjectArray = []
const obj: XMLSerializedAsObject | XMLSerializedAsObjectArray = { [defTextKey]: result }
for (let i = 0; i < items.length; i++) {
const item = items[i]
const key = Object.keys(item)[0]
switch (key) {
case "@":
const attrs = (item as AttrNode)["@"]
const attrKeys = Object.keys(attrs)
if (attrKeys.length === 1) {
result.push({ [defAttrKey + attrKeys[0]]: attrs[attrKeys[0]] })
} else {
result.push({ [defAttrKey]: (item as AttrNode)["@"] })
}
// attributes were processed above
break
case "#":
result.push({ [defTextKey]: (item as TextNode)["#"] })
Expand Down Expand Up @@ -171,6 +179,7 @@ export class ObjectWriter extends BaseWriter<ObjectWriterOptions, XMLSerializedA
break
}
}
obj[defTextKey] = result
return obj
} else {
// all element nodes have unique names
Expand Down
77 changes: 77 additions & 0 deletions test/issues/issue-029.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import $$ from "../TestHelpers";

describe("Replicate issue", () => {
// https://github.com/oozcitak/xmlbuilder2/issues/29
test("#29 - Attribute is a node", () => {
const input = $$.t`
<resources>
<string name="countdown">
<xliff:g id="count" example="2 days">%1$s</xliff:g>
until holiday
<xliff:g id="time" example="5 days">%1$s</xliff:g>
anything
</string>
</resources>`

// test with default converter
expect($$.convert(input, { format: 'object' })).toEqual(
{
resources: {
string: {
'@name': 'countdown',
'#': [
{
'xliff:g': {
'@id': 'count',
'@example': '2 days',
'#': '%1$s'
}
},
{ '#': '\n until holiday\n ' },
{
'xliff:g': {
'@id': 'time',
'@example': '5 days',
'#': '%1$s'
}
},
{ '#': '\n anything\n ' },
]
}
}
}
)

// test with custom converter
const obj = $$.convert({ encoding: 'UTF-8', convert: { text: 'value' } }, input, { format: 'object' })

expect(obj).toEqual(
{
resources: {
string: {
'@name': 'countdown',
value: [
{
'xliff:g': {
'@id': 'count',
'@example': '2 days',
value: '%1$s'
}
},
{ value: '\n until holiday\n ' },
{
'xliff:g': {
'@id': 'time',
'@example': '5 days',
value: '%1$s'
}
},
{ value: '\n anything\n ' },
]
}
}
}
)
})

})
14 changes: 6 additions & 8 deletions test/writers/ObjectWriter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ describe('ObjectWriter', () => {
expect($$.printMap(result)).toBe($$.t`
{
root: {
@att: val,
#: [
{ @att: val },
{ node: { @att: val } },
{ #: text },
{ node: { } }
Expand All @@ -324,14 +324,12 @@ describe('ObjectWriter', () => {
expect($$.printMap(result)).toBe($$.t`
{
root: {
@: {
att1: val1,
att2: val2,
att3: val3
},
#: [
{
@: {
att1: val1,
att2: val2,
att3: val3
}
},
{ node: { @att: val } },
{ #: text },
{ node: { } }
Expand Down

0 comments on commit 2e4508b

Please sign in to comment.