@@ -20,6 +20,7 @@ import {
20
20
$content ,
21
21
$extra ,
22
22
$getChildren ,
23
+ $getParent ,
23
24
$globalData ,
24
25
$nodeName ,
25
26
$onText ,
@@ -38,6 +39,7 @@ import {
38
39
import { getMeasurement , HTMLResult , stripQuotes } from "./utils.js" ;
39
40
40
41
const XHTML_NS_ID = NamespaceIds . xhtml . id ;
42
+ const $richText = Symbol ( ) ;
41
43
42
44
const VALID_STYLES = new Set ( [
43
45
"color" ,
@@ -109,6 +111,7 @@ const StyleMapping = new Map([
109
111
110
112
const spacesRegExp = / \s + / g;
111
113
const crlfRegExp = / [ \r \n ] + / g;
114
+ const crlfForRichTextRegExp = / \r \n ? / g;
112
115
113
116
function mapStyle ( styleStr , node ) {
114
117
const style = Object . create ( null ) ;
@@ -185,6 +188,7 @@ const NoWhites = new Set(["body", "html"]);
185
188
class XhtmlObject extends XmlObject {
186
189
constructor ( attributes , name ) {
187
190
super ( XHTML_NS_ID , name ) ;
191
+ this [ $richText ] = false ;
188
192
this . style = attributes . style || "" ;
189
193
}
190
194
@@ -197,11 +201,16 @@ class XhtmlObject extends XmlObject {
197
201
return ! NoWhites . has ( this [ $nodeName ] ) ;
198
202
}
199
203
200
- [ $onText ] ( str ) {
201
- str = str . replace ( crlfRegExp , "" ) ;
202
- if ( ! this . style . includes ( "xfa-spacerun:yes" ) ) {
203
- str = str . replace ( spacesRegExp , " " ) ;
204
+ [ $onText ] ( str , richText = false ) {
205
+ if ( ! richText ) {
206
+ str = str . replace ( crlfRegExp , "" ) ;
207
+ if ( ! this . style . includes ( "xfa-spacerun:yes" ) ) {
208
+ str = str . replace ( spacesRegExp , " " ) ;
209
+ }
210
+ } else {
211
+ this [ $richText ] = true ;
204
212
}
213
+
205
214
if ( str ) {
206
215
this [ $content ] += str ;
207
216
}
@@ -311,14 +320,23 @@ class XhtmlObject extends XmlObject {
311
320
return HTMLResult . EMPTY ;
312
321
}
313
322
323
+ let value ;
324
+ if ( this [ $richText ] ) {
325
+ value = this [ $content ]
326
+ ? this [ $content ] . replace ( crlfForRichTextRegExp , "\n" )
327
+ : undefined ;
328
+ } else {
329
+ value = this [ $content ] || undefined ;
330
+ }
331
+
314
332
return HTMLResult . success ( {
315
333
name : this [ $nodeName ] ,
316
334
attributes : {
317
335
href : this . href ,
318
336
style : mapStyle ( this . style , this ) ,
319
337
} ,
320
338
children,
321
- value : this [ $content ] || "" ,
339
+ value,
322
340
} ) ;
323
341
}
324
342
}
@@ -457,6 +475,10 @@ class P extends XhtmlObject {
457
475
}
458
476
459
477
[ $text ] ( ) {
478
+ const siblings = this [ $getParent ] ( ) [ $getChildren ] ( ) ;
479
+ if ( siblings [ siblings . length - 1 ] === this ) {
480
+ return super [ $text ] ( ) ;
481
+ }
460
482
return super [ $text ] ( ) + "\n" ;
461
483
}
462
484
}
0 commit comments