Repository: mozilla/pdf.js
File: web/text_layer_builder.js (JavaScript)

Code before:
          isFirefox ??=
            getComputedStyle(
              this.#textLayers.values().next().value
            ).getPropertyValue("-moz-user-select") === "none";

          if (isFirefox) {
            return;
          }
        }
        // In non-Firefox browsers, when hovering over an empty space (thus,
        // on .endOfContent), the selection will expand to cover all the
        // text between the current selection and .endOfContent. By moving
        // .endOfContent to right after (or before, depending on which side
        // of the selection the user is moving), we limit the selection jump
        // to at most cover the enteirety of the <span> where the selection
        // is being modified.
        const range = selection.getRangeAt(0);
        const modifyStart =
          prevRange &&
          (range.compareBoundaryPoints(Range.END_TO_END, prevRange) === 0 ||
            range.compareBoundaryPoints(Range.START_TO_END, prevRange) === 0);
        let anchor = modifyStart ? range.startContainer : range.endContainer;
        if (anchor.nodeType === Node.TEXT_NODE) {
          anchor = anchor.parentNode;
        }
        if (anchor.classList?.contains("highlight")) {
          anchor = anchor.parentNode;
        }
        if (!modifyStart && range.endOffset === 0) {
          do {
            while (!anchor.previousSibling) {
              anchor = anchor.parentNode;
            }
            anchor = anchor.previousSibling;
          } while (!anchor.childNodes.length);
        }

        const parentTextLayer = anchor.parentElement?.closest(".textLayer");
        const endDiv = this.#textLayers.get(parentTextLayer);
        if (endDiv) {
          endDiv.style.width = parentTextLayer.style.width;
          endDiv.style.height = parentTextLayer.style.height;
          anchor.parentElement.insertBefore(
            endDiv,
            modifyStart ? anchor : anchor.nextSibling
          );
        }

        prevRange = range.cloneRange();
      },
      { signal }

Review comment (suggestion):
It is possible to write an integration test for this, to avoid regressions? We already have other tests for the text layer (see https://github.com/mozilla/pdf.js/blob/master/test/integration/text_layer_spec.mjs) and the search logic (see https://github.com/mozilla/pdf.js/blob/master/test/integration/find_spec.mjs) so I'd imagine there is some prior art for such a test.

Code after:
          isFirefox ??=
            getComputedStyle(
              this.#textLayers.values().next().value
            ).getPropertyValue("-moz-user-select") === "none";

          if (isFirefox) {
            return;
          }
        }
        // In non-Firefox browsers, when hovering over an empty space (thus,
        // on .endOfContent), the selection will expand to cover all the
        // text between the current selection and .endOfContent. By moving
        // .endOfContent to right after (or before, depending on which side
        // of the selection the user is moving), we limit the selection jump
        // to at most cover the enteirety of the <span> where the selection
        // is being modified.
        const range = selection.getRangeAt(0);
        const modifyStart =
          prevRange &&
          (range.compareBoundaryPoints(Range.END_TO_END, prevRange) === 0 ||
            range.compareBoundaryPoints(Range.START_TO_END, prevRange) === 0);
        let anchor = modifyStart ? range.startContainer : range.endContainer;
        if (anchor.nodeType === Node.TEXT_NODE) {
          anchor = anchor.parentNode;
        }
        if (anchor.classList?.contains("highlight")) {
          anchor = anchor.parentNode;
        }
        if (!modifyStart && range.endOffset === 0) {
          do {
            while (!anchor.previousSibling) {
              anchor = anchor.parentNode;
            }
            anchor = anchor.previousSibling;
          } while (!anchor.childNodes.length);
        }

        const parentTextLayer = anchor.parentElement?.closest(".textLayer");
        const endDiv = this.#textLayers.get(parentTextLayer);
        if (endDiv) {
          endDiv.style.width = parentTextLayer.style.width;
          endDiv.style.height = parentTextLayer.style.height;
          endDiv.style.userSelect = "text";
          anchor.parentElement.insertBefore(
            endDiv,
            modifyStart ? anchor : anchor.nextSibling
          );
        }

        prevRange = range.cloneRange();
      },