@@ -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, '>' )
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 */
0 commit comments