@@ -22,6 +22,8 @@ interface XmlNode {
2222 attrs : Record < string , string > ;
2323 children : Array < XmlNode | string > ;
2424 spaceCount ?: number ;
25+ hasText ?: boolean ;
26+ hasTextChoice ?: boolean ;
2527}
2628
2729function xml ( source : string , odfSpaces = false , htmlEntities = false ) : XmlNode {
@@ -47,16 +49,22 @@ function xml(source: string, odfSpaces = false, htmlEntities = false): XmlNode {
4749 stack . push ( node ) ;
4850 } ) ;
4951 parser . on ( "closetag" , ( ) => {
52+ const node = stack . pop ( ) ! ;
53+ const parent = stack . at ( - 1 ) ! ;
5054 // Markup-compatibility fallbacks repeat their mc:Choice content, e.g. Word text boxes.
5155 // Keep one when no Choice carries text, such as a chart with a VML caption.
52- if ( stack . pop ( ) ! . name !== "Fallback" ) return ;
53- const parent = stack . at ( - 1 ) ! ;
54- if ( children ( parent ) . some ( ( child ) => child . name === "Choice" && hasText ( child ) ) ) {
56+ if ( node . name === "Fallback" && parent . hasTextChoice ) {
5557 parent . children . pop ( ) ;
58+ return ;
5659 }
60+ if ( ! node . hasText ) return ;
61+ parent . hasText = true ;
62+ if ( node . name === "Choice" ) parent . hasTextChoice = true ;
5763 } ) ;
5864 const text = ( value : string ) => {
59- stack . at ( - 1 ) ! . children . push ( value ) ;
65+ const node = stack . at ( - 1 ) ! ;
66+ node . children . push ( value ) ;
67+ if ( value . trim ( ) !== "" ) node . hasText = true ;
6068 } ;
6169 parser . on ( "text" , text ) ;
6270 parser . on ( "cdata" , text ) ;
@@ -66,8 +74,6 @@ function xml(source: string, odfSpaces = false, htmlEntities = false): XmlNode {
6674
6775const children = ( node : XmlNode ) =>
6876 node . children . filter ( ( child ) : child is XmlNode => typeof child !== "string" ) ;
69- const hasText = ( node : XmlNode ) : boolean =>
70- node . children . some ( ( child ) => ( typeof child === "string" ? child . trim ( ) !== "" : hasText ( child ) ) ) ;
7177function * all ( node : XmlNode , name : string | readonly string [ ] ) : Generator < XmlNode , void > {
7278 for ( const child of children ( node ) ) {
7379 if ( typeof name === "string" ? child . name === name : name . includes ( child . name ) ) yield child ;
0 commit comments