@@ -134,9 +134,11 @@ export class HtmlToMultiTextHelper {
134134 return ;
135135 }
136136
137- const run = this . processTextNode ( child ) ;
138- if ( run . text ) {
139- textRuns . push ( run ) ;
137+ const result = this . processTextNode ( child ) ;
138+ if ( Array . isArray ( result ) ) {
139+ textRuns . push ( ...result . filter ( ( run ) => run . text ) ) ;
140+ } else if ( result . text ) {
141+ textRuns . push ( result ) ;
140142 }
141143 } ) ;
142144
@@ -176,22 +178,31 @@ export class HtmlToMultiTextHelper {
176178 // Process all child nodes to create text runs
177179 // Using Array.from to convert NodeList to array that has forEach
178180 Array . from ( node . childNodes ) . forEach ( ( child ) => {
179- const run = this . processTextNode ( child ) ;
180- if ( run . text ) {
181- textRuns . push ( run ) ;
181+ const result = this . processTextNode ( child ) ;
182+ if ( Array . isArray ( result ) ) {
183+ textRuns . push ( ...result . filter ( ( run ) => run . text ) ) ;
184+ } else if ( result . text ) {
185+ textRuns . push ( result ) ;
182186 }
183187 } ) ;
184188
185189 return textRuns ;
186190 }
187191
188192 /**
189- * Processes a text node and creates a TextRun
193+ * Processes a text node and creates a TextRun or array of TextRuns
190194 */
191- private processTextNode ( node : ChildNode , style : TextStyle = { } ) : TextRun {
195+ private processTextNode (
196+ node : ChildNode ,
197+ style : TextStyle = { } ,
198+ ) : TextRun | TextRun [ ] {
192199 // If this is a text node, return its content
193200 if ( node . nodeType === Node . TEXT_NODE ) {
194- return { text : node . textContent || '' , style } ;
201+ const text = node . textContent || '' ;
202+ if ( text . trim ( ) === '' ) {
203+ return { text : text , style } ;
204+ }
205+ return { text, style } ;
195206 }
196207
197208 // If this is an element, handle specific styling
@@ -260,25 +271,20 @@ export class HtmlToMultiTextHelper {
260271 private processElementWithChildren (
261272 element : Element ,
262273 style : TextStyle ,
263- ) : TextRun {
274+ ) : TextRun [ ] {
264275 const runs : TextRun [ ] = [ ] ;
265276
266277 Array . from ( element . childNodes ) . forEach ( ( child ) => {
267278 const childRun = this . processTextNode ( child , style ) ;
268- if ( childRun . text ) {
279+ if ( Array . isArray ( childRun ) ) {
280+ // If the result is an array of runs, add them all
281+ runs . push ( ...childRun . filter ( ( run ) => run . text ) ) ;
282+ } else if ( childRun . text ) {
283+ // If it's a single run, add it
269284 runs . push ( childRun ) ;
270285 }
271286 } ) ;
272287
273- // If we have a single run, return it directly
274- if ( runs . length === 1 ) {
275- return runs [ 0 ] ;
276- }
277-
278- // If we have multiple runs, concatenate the text
279- return {
280- text : runs . map ( ( run ) => run . text ) . join ( '' ) ,
281- style,
282- } ;
288+ return runs ;
283289 }
284290}
0 commit comments