@@ -964,10 +964,26 @@ describe('annotator/anchoring/pdf', () => {
964
964
965
965
describe ( 'describeShape' , ( ) => {
966
966
let elementsFromPoint ;
967
+ let textLayer ;
968
+ let fakeTextInDOMRect ;
967
969
968
970
const borderLeft = 5 ;
969
971
const borderTop = 8 ;
970
972
973
+ // Create a matcher for a `DOMRect`.
974
+ //
975
+ // Note that if you pass a `DOMRect` directly to eg. `assert.calledWith`,
976
+ // the match will always succeed, whether the values are equal or not.
977
+ const matchRect = expected =>
978
+ sinon . match (
979
+ rect =>
980
+ rect . x === expected . x &&
981
+ rect . y === expected . y &&
982
+ rect . width === expected . width &&
983
+ rect . height === expected . height ,
984
+ `DOMRect { x=${ expected . x } , y=${ expected . y } width=${ expected . width } height=${ expected . height } }` ,
985
+ ) ;
986
+
971
987
beforeEach ( ( ) => {
972
988
for ( let i = 0 ; i < viewer . pdfViewer . pagesCount ; i ++ ) {
973
989
const pageDiv = viewer . pdfViewer . getPageView ( i ) . div ;
@@ -979,6 +995,9 @@ describe('annotator/anchoring/pdf', () => {
979
995
// which are not a PDF page container, are ignored.
980
996
const dummy = document . createElement ( 'div' ) ;
981
997
998
+ textLayer = document . createElement ( 'div' ) ;
999
+ textLayer . className = 'textLayer' ;
1000
+
982
1001
// Override `elementsFromPoint` to control how viewport coordinates are
983
1002
// mapped to pages.
984
1003
elementsFromPoint = sinon . stub ( document , 'elementsFromPoint' ) ;
@@ -994,7 +1013,13 @@ describe('annotator/anchoring/pdf', () => {
994
1013
}
995
1014
996
1015
const pageDiv = viewer . pdfViewer . getPageView ( pageIndex ) . div ;
997
- return [ dummy , pageDiv ] ;
1016
+ return [ dummy , textLayer , pageDiv ] ;
1017
+ } ) ;
1018
+
1019
+ fakeTextInDOMRect = sinon . stub ( ) . returns ( 'text-in-shape' ) ;
1020
+
1021
+ pdfAnchoring . $imports . $mock ( {
1022
+ './text-in-rect' : { textInDOMRect : fakeTextInDOMRect } ,
998
1023
} ) ;
999
1024
} ) ;
1000
1025
@@ -1024,6 +1049,11 @@ describe('annotator/anchoring/pdf', () => {
1024
1049
y : 10 + borderTop ,
1025
1050
} ) ;
1026
1051
1052
+ assert . calledWith (
1053
+ fakeTextInDOMRect ,
1054
+ textLayer ,
1055
+ matchRect ( new DOMRect ( 10 + borderLeft , 10 + borderTop , 1 , 1 ) ) ,
1056
+ ) ;
1027
1057
assert . deepEqual ( selectors , [
1028
1058
{
1029
1059
type : 'PageSelector' ,
@@ -1044,9 +1074,21 @@ describe('annotator/anchoring/pdf', () => {
1044
1074
right : 100 ,
1045
1075
top : 200 ,
1046
1076
} ,
1077
+ text : 'text-in-shape' ,
1047
1078
} ,
1048
1079
] ) ;
1049
1080
} ) ;
1081
+
1082
+ it ( 'does not extract text if there is no text layer' , async ( ) => {
1083
+ textLayer . className = 'notTheTextLayer' ;
1084
+ const selectors = await describeShape ( {
1085
+ type : 'point' ,
1086
+ x : 10 + borderLeft ,
1087
+ y : 10 + borderTop ,
1088
+ } ) ;
1089
+ const shapeSelector = selectors . find ( s => s . type === 'ShapeSelector' ) ;
1090
+ assert . isUndefined ( shapeSelector . text ) ;
1091
+ } ) ;
1050
1092
} ) ;
1051
1093
1052
1094
context ( 'when shape is a rect' , ( ) => {
@@ -1090,14 +1132,29 @@ describe('annotator/anchoring/pdf', () => {
1090
1132
const [ expectedLeft , expectedTop ] = pageView . getPagePoint ( 10 , 10 ) ;
1091
1133
const [ expectedRight , expectedBottom ] = pageView . getPagePoint ( 30 , 50 ) ;
1092
1134
1093
- const selectors = await describeShape ( {
1094
- type : 'rect' ,
1135
+ const rect = {
1095
1136
left : 10 + borderLeft ,
1096
1137
top : 10 + borderTop ,
1097
1138
right : 30 + borderLeft ,
1098
1139
bottom : 50 + borderTop ,
1140
+ } ;
1141
+ const selectors = await describeShape ( {
1142
+ type : 'rect' ,
1143
+ ...rect ,
1099
1144
} ) ;
1100
1145
1146
+ assert . calledWith (
1147
+ fakeTextInDOMRect ,
1148
+ textLayer ,
1149
+ matchRect (
1150
+ new DOMRect (
1151
+ rect . left ,
1152
+ rect . top ,
1153
+ rect . right - rect . left ,
1154
+ rect . bottom - rect . top ,
1155
+ ) ,
1156
+ ) ,
1157
+ ) ;
1101
1158
assert . deepEqual ( selectors , [
1102
1159
{
1103
1160
type : 'PageSelector' ,
@@ -1120,11 +1177,38 @@ describe('annotator/anchoring/pdf', () => {
1120
1177
right : 100 ,
1121
1178
top : 200 ,
1122
1179
} ,
1180
+ text : 'text-in-shape' ,
1123
1181
} ,
1124
1182
] ) ;
1125
1183
} ) ;
1126
1184
} ) ;
1127
1185
1186
+ it ( 'does not extract text if there is no text layer' , async ( ) => {
1187
+ textLayer . className = 'notTheTextLayer' ;
1188
+ const selectors = await describeShape ( {
1189
+ type : 'rect' ,
1190
+ left : 10 + borderLeft ,
1191
+ top : 10 + borderTop ,
1192
+ right : 30 + borderLeft ,
1193
+ bottom : 50 + borderTop ,
1194
+ } ) ;
1195
+ const shapeSelector = selectors . find ( s => s . type === 'ShapeSelector' ) ;
1196
+ assert . isUndefined ( shapeSelector . text ) ;
1197
+ } ) ;
1198
+
1199
+ it ( 'truncates extracted text' , async ( ) => {
1200
+ fakeTextInDOMRect . returns ( 'a' . repeat ( 300 ) ) ;
1201
+ const selectors = await describeShape ( {
1202
+ type : 'rect' ,
1203
+ left : 10 + borderLeft ,
1204
+ top : 10 + borderTop ,
1205
+ right : 100 ,
1206
+ bottom : 100 ,
1207
+ } ) ;
1208
+ const shapeSelector = selectors . find ( s => s . type === 'ShapeSelector' ) ;
1209
+ assert . equal ( shapeSelector . text , 'a' . repeat ( 256 ) ) ;
1210
+ } ) ;
1211
+
1128
1212
it ( 'throws if shape is unsupported' , async ( ) => {
1129
1213
let err ;
1130
1214
try {
0 commit comments