@@ -39,35 +39,48 @@ describe("find bar", () => {
39
39
it ( "must highlight text in the right position" , async ( ) => {
40
40
await Promise . all (
41
41
pages . map ( async ( [ browserName , page ] ) => {
42
+ // Highlight all occurrences of the letter A (case insensitive).
42
43
await page . click ( "#viewFind" ) ;
43
44
await page . waitForSelector ( "#viewFind" , { hidden : false } ) ;
44
45
await page . type ( "#findInput" , "a" ) ;
45
46
await page . click ( "#findHighlightAll" ) ;
46
47
await page . waitForSelector ( ".textLayer .highlight" ) ;
47
- // The PDF has the text "AB BA" in a monospace font.
48
- // Make sure we have the right number of highlighted divs.
48
+
49
+ // The PDF file contains the text 'AB BA' in a monospace font on a
50
+ // single line. Check if the two occurrences of A are highlighted.
49
51
const highlights = await page . $$ ( ".textLayer .highlight" ) ;
50
52
expect ( highlights . length ) . withContext ( `In ${ browserName } ` ) . toEqual ( 2 ) ;
51
- const glyphWidth = 15.98 ; // From the PDF.
52
- const pageDiv = await page . $ ( ".page canvas" ) ;
53
- const pageBox = await pageDiv . boundingBox ( ) ;
53
+
54
+ // Normalize the highlight's height. The font data in the PDF sets the
55
+ // size of the glyphs (and therefore the size of the highlights), but
56
+ // the viewer applies extra padding to them. For the comparison we
57
+ // therefore use the unpadded, glyph-sized parent element's height.
58
+ const parentSpan = ( await highlights [ 0 ] . $$ ( "xpath/.." ) ) [ 0 ] ;
59
+ const parentBox = await parentSpan . boundingBox ( ) ;
54
60
const firstA = await highlights [ 0 ] . boundingBox ( ) ;
55
61
const secondA = await highlights [ 1 ] . boundingBox ( ) ;
56
- // Subtract the page offset from the text bounding boxes;
57
- firstA . x -= pageBox . x ;
58
- firstA . y -= pageBox . y ;
59
- secondA . x -= pageBox . x ;
60
- secondA . y -= pageBox . y ;
61
- // They should be on the same line.
62
+ firstA . height = parentBox . height ;
63
+ secondA . height = parentBox . height ;
64
+
65
+ // Check if the vertical position of the highlights is correct. Both
66
+ // should be on a single line.
62
67
expect ( firstA . y ) . withContext ( `In ${ browserName } ` ) . toEqual ( secondA . y ) ;
68
+
69
+ // Check if the height of the two highlights is correct. Both should
70
+ // match the font size.
63
71
const fontSize = 26.66 ; // From the PDF.
64
- // The highlighted text has more padding.
65
- fuzzyMatch ( firstA . height , fontSize + 5 , browserName ) ;
66
- fuzzyMatch ( secondA . height , fontSize + 5 , browserName ) ;
67
- const expectedFirstAX = 28 ;
68
- fuzzyMatch ( firstA . x , expectedFirstAX , browserName ) ;
69
- // The second 'A' should be 4 glyphs widths from the first.
70
- fuzzyMatch ( secondA . x , expectedFirstAX + glyphWidth * 4 , browserName ) ;
72
+ fuzzyMatch ( firstA . height , fontSize , browserName ) ;
73
+ fuzzyMatch ( secondA . height , fontSize , browserName ) ;
74
+
75
+ // Check if the horizontal position of the highlights is correct. The
76
+ // second occurrence should be four glyph widths (three letters and
77
+ // one space) away from the first occurrence.
78
+ const pageDiv = await page . $ ( ".page canvas" ) ;
79
+ const pageBox = await pageDiv . boundingBox ( ) ;
80
+ const expectedFirstAX = 30 ; // From the PDF.
81
+ const glyphWidth = 15.98 ; // From the PDF.
82
+ fuzzyMatch ( firstA . x , pageBox . x + expectedFirstAX , browserName ) ;
83
+ fuzzyMatch ( secondA . x , firstA . x + glyphWidth * 4 , browserName ) ;
71
84
} )
72
85
) ;
73
86
} ) ;
0 commit comments