1- import { describe , it , expect } from 'vitest'
1+ import { describe , it , expect , vi } from 'vitest'
22import { svg2pdf } from '../../src/svg2pdf'
33import jsPDF from 'jspdf'
44import { loadSvg } from '../utils/loadSvg'
@@ -8,6 +8,36 @@ describe('anchor', () => {
88 const { svgElement, width, height } = await loadSvg ( '/test/anchor/anchor.svg' )
99 const pdf = new jsPDF ( width > height ? 'l' : 'p' , 'pt' , [ width , height ] )
1010 await svg2pdf ( svgElement , pdf , { loadExternalStyleSheets : true } )
11+
1112 await expect ( pdf . output ( 'arraybuffer' ) ) . toMatchPdfSnapshot ( './anchor.pdf' )
1213 } )
14+
15+ it ( 'aligns rectangular link hitboxes with their SVG bounds' , async ( ) => {
16+ const { svgElement, width, height } = await loadSvg ( '/test/anchor/rect-link.svg' )
17+ const pdf = new jsPDF ( width > height ? 'l' : 'p' , 'pt' , [ width , height ] )
18+ const link = vi . spyOn ( pdf , 'link' )
19+ await svg2pdf ( svgElement , pdf )
20+
21+ const rectHitbox = link . mock . calls . find (
22+ ( [ , , , , options ] ) => options . url === 'https://example.com/hitbox'
23+ )
24+ expect ( rectHitbox ) . toBeDefined ( )
25+ expect ( rectHitbox ?. slice ( 0 , 4 ) ) . toEqual ( [ 180 , 190 , 100 , 100 ] )
26+ } )
27+
28+ it ( 'preserves text link hitboxes while normalizing text bounds' , async ( ) => {
29+ const { svgElement, width, height } = await loadSvg ( '/test/anchor/anchor.svg' )
30+ const pdf = new jsPDF ( width > height ? 'l' : 'p' , 'pt' , [ width , height ] )
31+ const link = vi . spyOn ( pdf , 'link' )
32+ await svg2pdf ( svgElement , pdf , { loadExternalStyleSheets : true } )
33+
34+ const textHitbox = link . mock . calls . find (
35+ ( [ , , , , options ] ) =>
36+ options . url ===
37+ 'https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/dominant-baseline#hanging'
38+ )
39+ expect ( textHitbox ) . toBeDefined ( )
40+ expect ( textHitbox ?. [ 1 ] ) . toBeCloseTo ( 277 )
41+ expect ( textHitbox ?. [ 3 ] ) . toBe ( 30 )
42+ } )
1343} )
0 commit comments