Skip to content

Commit 5ca638b

Browse files
committed
Merge branch 'mdovhopo-master'
2 parents bb957d7 + 8d50e14 commit 5ca638b

File tree

9 files changed

+57
-66
lines changed

9 files changed

+57
-66
lines changed

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to this project are documented in this file. This project adheres to [Semantic Versioning](http://semver.org/#semantic-versioning-200).
44

5+
## [4.0.3] - 2025-11-01
6+
7+
### Bug Fixes
8+
9+
- Fixed a bug where text-only element nodes were printed on new lines in callback mode (see [#94](https://github.com/oozcitak/xmlbuilder2/issues/94)).
10+
511
## [4.0.2] - 2025-11-01
612

713
### Features
@@ -329,6 +335,7 @@ All notable changes to this project are documented in this file. This project ad
329335
[3.0.2]: https://github.com/oozcitak/xmlbuilder2/compare/v3.0.1...v3.0.2
330336
[3.1.0]: https://github.com/oozcitak/xmlbuilder2/compare/v3.0.2...v3.1.0
331337
[3.1.1]: https://github.com/oozcitak/xmlbuilder2/compare/v3.1.0...v3.1.1
332-
[4.0.0]: https://github.com/oozcitak/xmlbuilder2/compare/v3.1.1...v4.0.0
338+
[4.0.0]: https://github.com/oozcitak/xmlbuilder2/compare/v3.1.0...v4.0.0
333339
[4.0.1]: https://github.com/oozcitak/xmlbuilder2/compare/v4.0.0...v4.0.1
334-
[4.0.2]: https://github.com/oozcitak/xmlbuilder2/compare/v4.0.1...v4.0.2
340+
[4.0.2]: https://github.com/oozcitak/xmlbuilder2/compare/v4.0.1...v4.0.2
341+
[4.0.3]: https://github.com/oozcitak/xmlbuilder2/compare/v4.0.2...v4.0.3

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "xmlbuilder2",
3-
"version": "4.0.2",
3+
"version": "4.0.3",
44
"keywords": [
55
"xml",
66
"xmlbuilder"

src/builder/XMLBuilderCBImpl.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,24 @@ export class XMLBuilderCBImpl extends EventEmitter implements XMLBuilderCB {
4646
private _hasDocumentElement = false
4747
private _currentElement?: XMLBuilder
4848
private _currentElementSerialized = false
49-
private _openTags: Array<[string, string | null, NamespacePrefixMap, boolean]> = []
50-
49+
private _openTags: Array<[
50+
string, // qualified name
51+
string | null, // original inherited ns
52+
NamespacePrefixMap, // original prefix map
53+
boolean, // has children,
54+
boolean | undefined // has text payload
55+
]> = []
5156
private _prefixMap: NamespacePrefixMap
5257
private _prefixIndex: PrefixIndex
5358

5459
private _ended = false
5560

5661
/**
5762
* Initializes a new instance of `XMLStream`.
58-
*
63+
*
5964
* @param options - stream writer options
6065
* @param fragment - whether to create fragment stream or a document stream
61-
*
66+
*
6267
* @returns XML stream
6368
*/
6469
public constructor(options?: XMLBuilderCBCreateOptions, fragment = false) {
@@ -217,6 +222,11 @@ export class XMLBuilderCBImpl extends EventEmitter implements XMLBuilderCB {
217222
.replace(/>/g, '&gt;')
218223

219224
this._push(this._writer.text(markup))
225+
const lastEl = this._openTags[this._openTags.length - 1]
226+
// edge case: text on top level.
227+
if (lastEl) {
228+
lastEl[lastEl.length - 1] = true
229+
}
220230
return this
221231
}
222232

@@ -361,7 +371,7 @@ export class XMLBuilderCBImpl extends EventEmitter implements XMLBuilderCB {
361371

362372
/**
363373
* Serializes the opening tag of an element node.
364-
*
374+
*
365375
* @param hasChildren - whether the element node has child nodes
366376
*/
367377
private _serializeOpenTag(hasChildren: boolean): void {
@@ -478,7 +488,7 @@ export class XMLBuilderCBImpl extends EventEmitter implements XMLBuilderCB {
478488
* Save qualified name, original inherited ns, original prefix map, and
479489
* hasChildren flag.
480490
*/
481-
this._openTags.push([qualifiedName, inheritedNS, this._prefixMap, hasChildren])
491+
this._openTags.push([qualifiedName, inheritedNS, this._prefixMap, hasChildren, undefined])
482492

483493
/**
484494
* New values of inherited namespace and prefix map will be used while
@@ -507,20 +517,20 @@ export class XMLBuilderCBImpl extends EventEmitter implements XMLBuilderCB {
507517
return
508518
}
509519

510-
const [qualifiedName, ns, map, hasChildren] = lastEle
520+
const [qualifiedName, ns, map, hasChildren, hasTextPayload] = lastEle
511521
/**
512522
* Restore original values of inherited namespace and prefix map.
513523
*/
514524
this._prefixMap = map
515525
if (!hasChildren) return
516526

517-
this._push(this._writer.closeTag(qualifiedName))
527+
this._push(this._writer.closeTag(qualifiedName, hasTextPayload))
518528
this._writer.endElement(qualifiedName)
519529
}
520530

521531
/**
522532
* Pushes data to internal buffer.
523-
*
533+
*
524534
* @param data - data
525535
*/
526536
private _push(data: string | null): void {
@@ -537,7 +547,7 @@ export class XMLBuilderCBImpl extends EventEmitter implements XMLBuilderCB {
537547

538548
/**
539549
* Reads and serializes an XML tree.
540-
*
550+
*
541551
* @param node - root node
542552
*/
543553
private _fromNode(node: Node) {
@@ -573,7 +583,7 @@ export class XMLBuilderCBImpl extends EventEmitter implements XMLBuilderCB {
573583

574584
/**
575585
* Produces an XML serialization of the attributes of an element node.
576-
*
586+
*
577587
* @param node - node to serialize
578588
* @param map - namespace prefix map
579589
* @param prefixIndex - generated namespace prefix index
@@ -639,7 +649,7 @@ export class XMLBuilderCBImpl extends EventEmitter implements XMLBuilderCB {
639649
(!map.hasPrefix(attr.prefix) ||
640650
map.has(attr.prefix, attributeNamespace))) {
641651
/**
642-
* Check if we can use the attribute's own prefix.
652+
* Check if we can use the attribute's own prefix.
643653
* We deviate from the spec here.
644654
* TODO: This is not an efficient way of searching for prefixes.
645655
* Follow developments to the spec.
@@ -669,7 +679,7 @@ export class XMLBuilderCBImpl extends EventEmitter implements XMLBuilderCB {
669679

670680
/**
671681
* Produces an XML serialization of an attribute value.
672-
*
682+
*
673683
* @param value - attribute value
674684
* @param requireWellFormed - whether to check conformance
675685
*/
@@ -688,12 +698,12 @@ export class XMLBuilderCBImpl extends EventEmitter implements XMLBuilderCB {
688698
}
689699

690700
/**
691-
* Records namespace information for the given element and returns the
701+
* Records namespace information for the given element and returns the
692702
* default namespace attribute value.
693-
*
703+
*
694704
* @param node - element node to process
695705
* @param map - namespace prefix map
696-
* @param localPrefixesMap - local prefixes map
706+
* @param localPrefixesMap - local prefixes map
697707
*/
698708
private _recordNamespaceInformation(node: Element, map: NamespacePrefixMap,
699709
localPrefixesMap: { [key: string]: string }): string | null {
@@ -732,7 +742,7 @@ export class XMLBuilderCBImpl extends EventEmitter implements XMLBuilderCB {
732742

733743
/**
734744
* Generates a new prefix for the given namespace.
735-
*
745+
*
736746
* @param newNamespace - a namespace to generate prefix for
737747
* @param prefixMap - namespace prefix map
738748
* @param prefixIndex - generated namespace prefix index
@@ -748,7 +758,7 @@ export class XMLBuilderCBImpl extends EventEmitter implements XMLBuilderCB {
748758

749759
/**
750760
* Determines if the namespace prefix map was modified from its original.
751-
*
761+
*
752762
* @param originalMap - original namespace prefix map
753763
* @param newMap - new namespace prefix map
754764
*/

src/writers/BaseCBWriter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export abstract class BaseCBWriter<T extends BaseCBWriterOptions> {
103103
*
104104
* @param name - node name
105105
*/
106+
abstract closeTag(name: string, hasTextPayload?: boolean): string
106107
abstract closeTag(name: string): string
107108

108109
/**

src/writers/XMLCBWriter.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class XMLCBWriter extends BaseCBWriter<XMLCBWriterOptions> {
5555
}
5656
/** @inheritdoc */
5757
text(data: string): string {
58-
return this._beginLine() + data
58+
return data
5959
}
6060
/** @inheritdoc */
6161
instruction(target: string, data: string): string {
@@ -91,8 +91,9 @@ export class XMLCBWriter extends BaseCBWriter<XMLCBWriterOptions> {
9191
}
9292
}
9393
/** @inheritdoc */
94-
closeTag(name: string): string {
95-
return this._beginLine() + "</" + name + ">"
94+
closeTag(name: string, hasTextPayload?: boolean): string {
95+
const ending = hasTextPayload ? '' : this._beginLine();
96+
return ending + "</" + name + ">";
9697
}
9798
/** @inheritdoc */
9899
attribute(name: string, value: string): string {

test/callback/object.test.ts

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,40 +35,24 @@ $$.suite('object', () => {
3535

3636
await $$.expectCBResult(xmlStream, $$.t`
3737
<root>
38-
<ele>
39-
simple element
40-
</ele>
38+
<ele>simple element</ele>
4139
<person age="35">
42-
<name>
43-
John
44-
</name>
40+
<name>John</name>
4541
<?pi val?>
4642
<?pi?>
4743
<!--Good guy-->
4844
<![CDATA[well formed!]]>
4945
<address>
5046
<?pi?>
51-
<city>
52-
Istanbul
53-
</city>
54-
<street>
55-
End of long and winding road
56-
</street>
47+
<city>Istanbul</city>
48+
<street>End of long and winding road</street>
5749
</address>
5850
<contact>
59-
<phone>
60-
555-1234
61-
</phone>
62-
<phone>
63-
555-1235
64-
</phone>
51+
<phone>555-1234</phone>
52+
<phone>555-1235</phone>
6553
</contact>
66-
<id>
67-
42
68-
</id>
69-
<details>
70-
classified
71-
</details>
54+
<id>42</id>
55+
<details>classified</details>
7256
</person>
7357
</root>
7458
`)

test/callback/parse.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ $$.suite('parse()', () => {
1010
xmlStream.ele(str).end()
1111

1212
await $$.expectCBResult(xmlStream, $$.t`
13-
<root att="val">
14-
text
15-
</root>
13+
<root att="val">text</root>
1614
`)
1715
})
1816

test/issues/issue-002.test.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,12 @@ $$.suite("Replicate issue", () => {
2424
<?xml version="1.0"?>
2525
<rss>
2626
<item>
27-
<sku>
28-
001
29-
</sku>
30-
<name>
31-
Product name 1
32-
</name>
27+
<sku>001</sku>
28+
<name>Product name 1</name>
3329
</item>
3430
<item>
35-
<sku>
36-
002
37-
</sku>
38-
<name>
39-
Product name 2
40-
</name>
31+
<sku>002</sku>
32+
<name>Product name 2</name>
4133
</item>
4234
</rss>
4335
`)

test/issues/issue-078.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ $$.suite("Replicate issue", () => {
2828
await $$.expectCBResult(xmlStream, $$.t`
2929
<root>
3030
<title/>
31-
<description>
32-
Test description
33-
</description>
31+
<description>Test description</description>
3432
</root>
3533
`)
3634
})

0 commit comments

Comments
 (0)