File tree Expand file tree Collapse file tree 2 files changed +50
-1
lines changed
Expand file tree Collapse file tree 2 files changed +50
-1
lines changed Original file line number Diff line number Diff line change @@ -66,4 +66,33 @@ describe("Test Text and Desc", () => {
6666 expect ( $texts . length ) . toBeTruthy ( ) ;
6767 expect ( $texts [ 0 ] . firstChild . data ) . toBe ( "_DESC_TEST_" ) ;
6868 } ) ;
69+
70+ it ( "Desc multiline" , ( ) => {
71+ const { req, res } = route ( {
72+ type : "venom" ,
73+ desc : "_DESC_LINE_1_-nl-_DESC_LINE_2_" ,
74+ descSize : 40 ,
75+ descAlign : 30 ,
76+ descAlignY : 90 ,
77+ } ) ;
78+
79+ api ( req , res ) ;
80+
81+ expect ( res . setHeader ) . toBeCalledWith ( "Content-Type" , "image/svg+xml" ) ;
82+ expect ( res . send ) . toHaveBeenCalled ( ) ;
83+
84+ const svgContent = res . send . mock . lastCall [ 0 ] ;
85+ const parser = new DOMParser ( ) ;
86+ const svgDocument = parser . parseFromString ( svgContent , "image/svg+xml" ) ;
87+ const $svg = svgDocument . documentElement ;
88+ expect ( $svg . tagName ) . toBe ( "svg" ) ;
89+
90+ const $texts = svgDocument . getElementsByTagName ( "text" ) ;
91+ expect ( $texts . length ) . toBeTruthy ( ) ;
92+
93+ const $tspans = svgDocument . getElementsByTagName ( "tspan" ) ;
94+ expect ( $tspans . length ) . toBe ( 2 ) ;
95+ expect ( $tspans [ 0 ] . firstChild . data ) . toBe ( "_DESC_LINE_1_" ) ;
96+ expect ( $tspans [ 1 ] . firstChild . data ) . toBe ( "_DESC_LINE_2_" ) ;
97+ } ) ;
6998} ) ;
Original file line number Diff line number Diff line change @@ -185,6 +185,26 @@ export const getDesc = (
185185) => {
186186 if ( desc === "" || desc === undefined ) return "" ;
187187
188+ const lines = desc . split ( "-nl-" ) ;
189+
190+ if ( lines . length > 1 )
191+ return `<text
192+ text-anchor="middle"
193+ alignment-baseline="middle"
194+ x="${ descAlign } %"
195+ y="${ descAlignY } %"
196+ class="desc"
197+ style="fill:#${ descColor } ;"
198+ >
199+ ${ lines
200+ . map (
201+ ( line , i ) =>
202+ `<tspan x="${ descAlign } %" dy="${ i === 0 ? 0 : "1em" } ">${ line } </tspan>` ,
203+ )
204+ . join ( "" ) }
205+ </text>
206+ ` ;
207+
188208 // debate : adjustable text-anchor|pos-y. not only pos-x
189- return `<text text-anchor="middle" alignment-baseline="middle" x="${ descAlign } %" y="${ descAlignY } %" class="desc" style="fill:#${ descColor } ;">${ desc } </text>` ;
209+ return `<text text-anchor="middle" alignment-baseline="middle" x="${ descAlign } %" y="${ descAlignY } %" class="desc" style="fill:#${ descColor } ;">${ lines [ 0 ] } </text>` ;
190210} ;
You can’t perform that action at this time.
0 commit comments