Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(94): fix formatting to be compliant with XMLBuilder #180

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/builder/XMLBuilderCBImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -217,6 +217,11 @@ export class XMLBuilderCBImpl extends EventEmitter implements XMLBuilderCB {
.replace(/>/g, '&gt;')

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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
1 change: 1 addition & 0 deletions src/writers/BaseCBWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export abstract class BaseCBWriter<T extends BaseCBWriterOptions> {
*
* @param name - node name
*/
abstract closeTag(name: string, hasTextPayload?: boolean): string
abstract closeTag(name: string): string

/**
Expand Down
7 changes: 4 additions & 3 deletions src/writers/XMLCBWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class XMLCBWriter extends BaseCBWriter<XMLCBWriterOptions> {
}
/** @inheritdoc */
text(data: string): string {
return this._beginLine() + data
return data
}
/** @inheritdoc */
instruction(target: string, data: string): string {
Expand Down Expand Up @@ -91,8 +91,9 @@ export class XMLCBWriter extends BaseCBWriter<XMLCBWriterOptions> {
}
}
/** @inheritdoc */
closeTag(name: string): string {
return this._beginLine() + "</" + name + ">"
closeTag(name: string, hasTextPayload?: boolean): string {
const ending = hasTextPayload ? '' : this._beginLine();
return ending + "</" + name + ">";
}
/** @inheritdoc */
attribute(name: string, value: string): string {
Expand Down
32 changes: 8 additions & 24 deletions test/callback/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,24 @@ describe('object', () => {

$$.expectCBResult(xmlStream, $$.t`
<root>
<ele>
simple element
</ele>
<ele>simple element</ele>
<person age="35">
<name>
John
</name>
<name>John</name>
<?pi val?>
<?pi?>
<!--Good guy-->
<![CDATA[well formed!]]>
<address>
<?pi?>
<city>
Istanbul
</city>
<street>
End of long and winding road
</street>
<city>Istanbul</city>
<street>End of long and winding road</street>
</address>
<contact>
<phone>
555-1234
</phone>
<phone>
555-1235
</phone>
<phone>555-1234</phone>
<phone>555-1235</phone>
</contact>
<id>
42
</id>
<details>
classified
</details>
<id>42</id>
<details>classified</details>
</person>
</root>
`, done)
Expand Down
4 changes: 1 addition & 3 deletions test/callback/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ describe('parse()', () => {
xmlStream.ele(str).end()

$$.expectCBResult(xmlStream, $$.t`
<root att="val">
text
</root>
<root att="val">text</root>
`, done)
})

Expand Down
16 changes: 4 additions & 12 deletions test/issues/issue-002.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,12 @@ describe("Replicate issue", () => {
<?xml version="1.0"?>
<rss>
<item>
<sku>
001
</sku>
<name>
Product name 1
</name>
<sku>001</sku>
<name>Product name 1</name>
</item>
<item>
<sku>
002
</sku>
<name>
Product name 2
</name>
<sku>002</sku>
<name>Product name 2</name>
</item>
</rss>
`, done)
Expand Down
4 changes: 1 addition & 3 deletions test/issues/issue-078.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ describe("Replicate issue", () => {
$$.expectCBResult(xmlStream, $$.t`
<root>
<title/>
<description>
Test description
</description>
<description>Test description</description>
</root>
`, done)
})
Expand Down