The highlight found the right letter in the wrong place
A document can contain the same word twice. When you highlight the second occurrence, a program can save the words you selected and their position. The next time it opens the document, finding those words at a saved position sounds like a good check. Is it enough?
We tested the position check from a PDF-highlighting tool on four invented characters: 😀AAB. Our highlight belonged on the second A. A position check selected the first A and accepted it, because the letter still matched the saved quote. The smiley face changed what the position number meant.
Think of a position as the boundary just before a character. The start of the text is boundary zero. Two ways of counting reach different numbers by the time they get past the emoji.
One smiley face takes one place in one count and two in the other
The Web Annotation standard records positions in the specified normalized text in units called Unicode code points. The smiley face in 😀AAB takes one code point. The first A starts at position 1 and the second at 2.
JavaScript indexes strings in UTF-16 code units, the units it uses to represent text. This particular emoji takes two of those units. The first A now starts at 2, and the second at 3. The letters themselves haven’t moved. The two systems mean different things by ‘start at 2.’ Web Annotation text positions · JavaScript substring positions
Why checking the quoted letter missed the error
We tested a specific version of the Hypothesis function used to find highlights in PDFs. We supplied fictional page text and simulated making a selection. In code points our range was [2, 3), which means start at position 2 and stop before 3. When the function sent those numbers straight to JavaScript, it got the first A. The saved quote was also A. The position passed. But the selected A followed the emoji. The intended A followed the other A.
We tried again after translating the positions into JavaScript’s units. The range became [3, 4), and the selection found the intended second A. Pinned PDF anchoring function
Two other strings help explain the result. Replace the emoji with X and you get XAAB. Both systems now put the second A at [2, 3), so testing only that text would hide the disagreement. In 😀XAB, the unconverted range selected X instead of the quoted A. This time the function rejected the position and handed the job to a search for the quoted text. We stopped the test at that handoff.
Check which occurrence was selected
Save the exact text version, any changes made to the text before counting (such as replacing characters), and the units used for positions. To test a delivery, include repeated text after an emoji. Check the letters or words on either side of the highlight along with the quote itself. They tell you whether it landed on the intended occurrence.