Skip to content

Avoid text selection workaround in modern Chromium#21514

Merged
timvandermeij merged 1 commit into
mozilla:masterfrom
nicolo-ribaudo:remove-selection-conflict-chrome
Jul 3, 2026
Merged

Avoid text selection workaround in modern Chromium#21514
timvandermeij merged 1 commit into
mozilla:masterfrom
nicolo-ribaudo:remove-selection-conflict-chrome

Conversation

@nicolo-ribaudo

@nicolo-ribaudo nicolo-ribaudo commented Jun 30, 2026

Copy link
Copy Markdown
Collaborator

Avoid text selection workaround in modern Chromium

Chromium 148+ improved their selection behavior when it comes to absolutely positioned elements, thus making text selectino in PDF.js much better.

Unfortunately this does not only mean that the workaround we currently have for Chromium is unnecessary, but it actually become harmful. It conflicts with Chromium's new behavior, making text selection worse on mobile.

As the change has been released in Chrome only a month ago, this patch keeps the workaround for older Chromium versions. There is no easy way to feture-detect is, so unfortunately we need to do user agent version detection.

Also, somehow the "right click on images" layer undoes the negative effects of the workaround, so even in modern mobile Chromium if there are images text selection already works well.

This is the behavior on various Chromium versions. By "scroll jump" I meant that the page scroll position moves to the wrong place (usually top right of the page) while selection; by "selection explodes" I mean that suddenly all the page is selected while trying to select something.

Chromium version Input Before this PR After this PR
≥ 148 Touch ❌ Scroll jump ✔️
≥ 148 Mouse ✔️ ✔️ (but running less code!)
< 148 Touch ❌ Scroll jump ❌ Scroll jump
< 148 Mouse ✔️ ✔️

The changed code is not included in the mozcentral build. In the Chromium build it now looks like this:

      if (isFirefoxOrModernChromium === undefined) {
        const chromiumVersion = navigator.userAgentData ? navigator.userAgentData.brands.find(({
          brand
        }) => brand === "Chromium")?.version : /\bChrome\/(\d+)\b/.exec(navigator.userAgent)?.[1];
        isFirefoxOrModernChromium = !!chromiumVersion && parseInt(chromiumVersion, 10) >= 148;
      }
      if (isFirefoxOrModernChromium) {
        return;
      }

while in the generic build it looks like this:

      if (isFirefoxOrModernChromium === undefined) {
        isFirefoxOrModernChromium = getComputedStyle(this.#textLayers.values().next().value).getPropertyValue("-moz-user-select") === "none";
        if (!isFirefoxOrModernChromium) {
          const chromiumVersion = navigator.userAgentData ? navigator.userAgentData.brands.find(({
            brand
          }) => brand === "Chromium")?.version : /\bChrome\/(\d+)\b/.exec(navigator.userAgent)?.[1];
          isFirefoxOrModernChromium = !!chromiumVersion && parseInt(chromiumVersion, 10) >= 148;
        }
      }
      if (isFirefoxOrModernChromium) {
        return;
      }

@timvandermeij timvandermeij left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, with passing CI (GitHub Actions) and browser (bot) tests. It looks like the GitHub Actions builds somehow did not start; could you perhaps re-push the change to retrigger them?

Thank you for improving this, and for the detailed analysis in the PR body (as that really helps to understand the change)!

@timvandermeij

Copy link
Copy Markdown
Collaborator

/botio browsertest

@moz-tools-bot

Copy link
Copy Markdown
Collaborator

From: Bot.io (Linux m4)


Received

Command cmd_browsertest from @timvandermeij received. Current queue size: 1

Live output at: http://54.241.84.105:8877/09d0b2d27676b7d/output.txt

@moz-tools-bot

Copy link
Copy Markdown
Collaborator

From: Bot.io (Windows)


Received

Command cmd_browsertest from @timvandermeij received. Current queue size: 1

Live output at: http://54.193.163.58:8877/71039d40cd4de38/output.txt

@moz-tools-bot

Copy link
Copy Markdown
Collaborator

From: Bot.io (Linux m4)


Success

Full output at http://54.241.84.105:8877/09d0b2d27676b7d/output.txt

Total script time: 18.13 mins

  • Regression tests: Passed

@moz-tools-bot

Copy link
Copy Markdown
Collaborator

From: Bot.io (Windows)


Success

Full output at http://54.193.163.58:8877/71039d40cd4de38/output.txt

Total script time: 25.00 mins

  • Regression tests: Passed

@codecov-commenter

codecov-commenter commented Jul 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.08%. Comparing base (04eeeec) to head (ea43bb4).
⚠️ Report is 39 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #21514      +/-   ##
==========================================
- Coverage   89.79%   89.08%   -0.72%     
==========================================
  Files         260      262       +2     
  Lines       66282    67013     +731     
==========================================
+ Hits        59520    59698     +178     
- Misses       6762     7315     +553     
Flag Coverage Δ
browsertest 66.51% <0.00%> (-0.18%) ⬇️
integrationtest 68.53% <100.00%> (-0.40%) ⬇️
unittest 57.29% <0.00%> (+0.04%) ⬆️
unittestcli 56.27% <0.00%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@nicolo-ribaudo

Copy link
Copy Markdown
Collaborator Author

I need to investigate the integration test failures, it's possible I just need to update the expected behavior

Chromium 148+ improved their selection behavior when it comes to absolutely
positioned elements, thus making text selectino in PDF.js much better.

Unfortunately this does not only mean that the workaround we currently have
for Chromium is unnecessary, but it actually become harmful. It conflicts
with Chromium's new behavior, making text selection *worse* on mobile.

As the change has been released in Chrome only a month ago, this patch keeps
the workaround for older Chromium versions. There is no easy way to
feture-detect is, so unfortunately we need to do user agent version detection.
@nicolo-ribaudo

Copy link
Copy Markdown
Collaborator Author

Done :)

@timvandermeij timvandermeij merged commit 70c5213 into mozilla:master Jul 3, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants