From b7cc70465d0400422553cd75d85907c308986e14 Mon Sep 17 00:00:00 2001 From: Maxim DOVHOPOLYI Date: Thu, 2 May 2024 15:25:53 +0300 Subject: [PATCH] fix(94): fix formatting to be compliant with XMLBuilder --- src/builder/XMLBuilderCBImpl.ts | 13 +++++++++---- src/writers/BaseCBWriter.ts | 1 + src/writers/XMLCBWriter.ts | 7 ++++--- test/callback/object.test.ts | 32 ++++++++------------------------ test/callback/parse.test.ts | 4 +--- test/issues/issue-002.test.ts | 16 ++++------------ test/issues/issue-078.test.ts | 4 +--- 7 files changed, 28 insertions(+), 49 deletions(-) diff --git a/src/builder/XMLBuilderCBImpl.ts b/src/builder/XMLBuilderCBImpl.ts index 7904906e..62687a35 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 2a85ecbd..2fe7e336 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 ac1b087a..5087b5eb 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 7247ae2a..5ed49cad 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 1146a73b..5052671c 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 8ef555de..22900d28 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 5e4fb876..b4b5be47 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) })