Bug 1620289 modified the behavior of printing selection ranges to remove its dependency on the original document's current selections.
To solve this, the selected ranges are cached on the static clone of the document when printing is invoked.
To print only the selections, the complement set of ranges (the non-selected data) is deleted from the cloned document and ellipses are added where appropriate (when content is cut off within text in the same node).
The old way of achieving this involved traversing both the original document and the static clone at the same time, deleting the relevant data in the clone while keeping track of the same position in the original. This led to code that would need to offset the position in the original document based on the addition of an ellipsis in the cloned document.
Now that the original document is no longer involved in this process, we no longer need to separately keep track of the difference in position with the added ellipses. It is now implicitly taken care of in the modification to the node itself.
Failing to remove the code that explicitly accounted for the added ellipses was causing an off-by-one issue with some printing selections, reported in Bug 1652746. This issue occurs especially when there are multiple selections within the same text (see attached image).
The attached image shows (from left to right):
- The highlighted text in the test case
- The incorrect output caused by the regression.
- The output before the regression.
- The output after applying this patch.