diff --git a/src/builder/XMLBuilderCBImpl.ts b/src/builder/XMLBuilderCBImpl.ts index 7904906..62687a3 100644 --- a/src/builder/XMLBuilderCBImpl.ts +++ b/src/builder/XMLBuilderCBImpl.ts @@ -46,7 +46,7 @@ export class XMLBuilderCBImpl extends EventEmitter implements XMLBuilderCB { private _hasDocumentElement = false private _currentElement?: XMLBuilder private _currentElementSerialized = false - private _openTags: Array<[string, string | null, NamespacePrefixMap, boolean]> = [] + private _openTags: Array<[string, string | null, NamespacePrefixMap, boolean /** has children */, boolean | undefined /** has text payload */]> = [] private _prefixMap: NamespacePrefixMap private _prefixIndex: PrefixIndex @@ -217,6 +217,11 @@ export class XMLBuilderCBImpl extends EventEmitter implements XMLBuilderCB { .replace(/>/g, '>') this._push(this._writer.text(markup)) + const lastEl = this._openTags[this._openTags.length - 1] + // edge case: text on top level. + if (lastEl) { + lastEl[lastEl.length - 1] = true + } return this } @@ -478,7 +483,7 @@ export class XMLBuilderCBImpl extends EventEmitter implements XMLBuilderCB { * Save qualified name, original inherited ns, original prefix map, and * hasChildren flag. */ - this._openTags.push([qualifiedName, inheritedNS, this._prefixMap, hasChildren]) + this._openTags.push([qualifiedName, inheritedNS, this._prefixMap, hasChildren, undefined]) /** * New values of inherited namespace and prefix map will be used while @@ -507,14 +512,14 @@ export class XMLBuilderCBImpl extends EventEmitter implements XMLBuilderCB { return } - const [qualifiedName, ns, map, hasChildren] = lastEle + const [qualifiedName, ns, map, hasChildren, hasTextPayload] = lastEle /** * Restore original values of inherited namespace and prefix map. */ this._prefixMap = map if (!hasChildren) return - this._push(this._writer.closeTag(qualifiedName)) + this._push(this._writer.closeTag(qualifiedName, hasTextPayload)) this._writer.endElement(qualifiedName) } diff --git a/src/writers/BaseCBWriter.ts b/src/writers/BaseCBWriter.ts index 2a85ecb..2fe7e33 100644 --- a/src/writers/BaseCBWriter.ts +++ b/src/writers/BaseCBWriter.ts @@ -103,6 +103,7 @@ export abstract class BaseCBWriter { * * @param name - node name */ + abstract closeTag(name: string, hasTextPayload?: boolean): string abstract closeTag(name: string): string /** diff --git a/src/writers/XMLCBWriter.ts b/src/writers/XMLCBWriter.ts index ac1b087..5087b5e 100644 --- a/src/writers/XMLCBWriter.ts +++ b/src/writers/XMLCBWriter.ts @@ -55,7 +55,7 @@ export class XMLCBWriter extends BaseCBWriter { } /** @inheritdoc */ text(data: string): string { - return this._beginLine() + data + return data } /** @inheritdoc */ instruction(target: string, data: string): string { @@ -91,8 +91,9 @@ export class XMLCBWriter extends BaseCBWriter { } } /** @inheritdoc */ - closeTag(name: string): string { - return this._beginLine() + "" + closeTag(name: string, hasTextPayload?: boolean): string { + const ending = hasTextPayload ? '' : this._beginLine(); + return ending + ""; } /** @inheritdoc */ attribute(name: string, value: string): string { diff --git a/test/callback/object.test.ts b/test/callback/object.test.ts index 7247ae2..5ed49ca 100644 --- a/test/callback/object.test.ts +++ b/test/callback/object.test.ts @@ -35,40 +35,24 @@ describe('object', () => { $$.expectCBResult(xmlStream, $$.t` - - simple element - + simple element - - John - + John
- - Istanbul - - - End of long and winding road - + Istanbul + End of long and winding road
- - 555-1234 - - - 555-1235 - + 555-1234 + 555-1235 - - 42 - -
- classified -
+ 42 +
classified
`, done) diff --git a/test/callback/parse.test.ts b/test/callback/parse.test.ts index 1146a73..5052671 100644 --- a/test/callback/parse.test.ts +++ b/test/callback/parse.test.ts @@ -10,9 +10,7 @@ describe('parse()', () => { xmlStream.ele(str).end() $$.expectCBResult(xmlStream, $$.t` - - text - + text `, done) }) diff --git a/test/issues/issue-002.test.ts b/test/issues/issue-002.test.ts index 8ef555d..22900d2 100644 --- a/test/issues/issue-002.test.ts +++ b/test/issues/issue-002.test.ts @@ -24,20 +24,12 @@ describe("Replicate issue", () => { - - 001 - - - Product name 1 - + 001 + Product name 1 - - 002 - - - Product name 2 - + 002 + Product name 2 `, done) diff --git a/test/issues/issue-078.test.ts b/test/issues/issue-078.test.ts index 5e4fb87..b4b5be4 100644 --- a/test/issues/issue-078.test.ts +++ b/test/issues/issue-078.test.ts @@ -28,9 +28,7 @@ describe("Replicate issue", () => { $$.expectCBResult(xmlStream, $$.t` - <description> - Test description - </description> + <description>Test description</description> </root> `, done) })