@@ -21,6 +21,7 @@ import {
2121 getRGB ,
2222 isValidFetchUrl ,
2323 PDFDateString ,
24+ renderRichText ,
2425} from "../../src/display/display_utils.js" ;
2526import { isNodeJS , toBase64Util } from "../../src/shared/util.js" ;
2627
@@ -342,4 +343,67 @@ describe("display_utils", function () {
342343 expect ( applyOpacity ( 123 , 45 , 67 , ctx . globalAlpha ) ) . toEqual ( [ r , g , b ] ) ;
343344 } ) ;
344345 } ) ;
346+
347+ describe ( "renderRichText" , function ( ) {
348+ // Unlike other tests we cannot simply compare the HTML-strings since
349+ // Chrome and Firefox produce different results. Instead we compare sets
350+ // containing the individual parts of the HTML-strings.
351+ const splitParts = s => new Set ( s . split ( / [ < > / ] + / ) . filter ( x => x ) ) ;
352+
353+ it ( "should render plain text" , function ( ) {
354+ if ( isNodeJS ) {
355+ pending ( "DOM is not supported in Node.js." ) ;
356+ }
357+ const container = document . createElement ( "div" ) ;
358+ renderRichText (
359+ {
360+ html : "Hello world!\nThis is a test." ,
361+ dir : "ltr" ,
362+ className : "foo" ,
363+ } ,
364+ container
365+ ) ;
366+ expect ( splitParts ( container . innerHTML ) ) . toEqual (
367+ splitParts (
368+ '<p dir="ltr" class="richText foo">Hello world!<br>This is a test.</p>'
369+ )
370+ ) ;
371+ } ) ;
372+
373+ it ( "should render XFA rich text" , function ( ) {
374+ if ( isNodeJS ) {
375+ pending ( "DOM is not supported in Node.js." ) ;
376+ }
377+ const container = document . createElement ( "div" ) ;
378+ const xfaHtml = {
379+ name : "div" ,
380+ attributes : { style : { color : "red" } } ,
381+ children : [
382+ {
383+ name : "p" ,
384+ attributes : { style : { fontSize : "20px" } } ,
385+ children : [
386+ {
387+ name : "span" ,
388+ attributes : { style : { fontWeight : "bold" } } ,
389+ value : "Hello" ,
390+ } ,
391+ { name : "#text" , value : " world!" } ,
392+ ] ,
393+ } ,
394+ ] ,
395+ } ;
396+ renderRichText (
397+ { html : xfaHtml , dir : "ltr" , className : "foo" } ,
398+ container
399+ ) ;
400+ expect ( splitParts ( container . innerHTML ) ) . toEqual (
401+ splitParts (
402+ '<div style="color: red;" class="richText foo">' +
403+ '<p style="font-size: 20px;">' +
404+ '<span style="font-weight: bold;">Hello</span> world!</p></div>'
405+ )
406+ ) ;
407+ } ) ;
408+ } ) ;
345409} ) ;
0 commit comments