Skip to content

fix(button): prevent crash when loading changes on a translated page - #1790

Open
alexcpn wants to merge 1 commit into
dhis2:masterfrom
alexcpn:LIBS-876/button-google-translate-crash
Open

alexcpn wants to merge 1 commit into
dhis2:masterfrom
alexcpn:LIBS-876/button-google-translate-crash

Conversation

@alexcpn

@alexcpn alexcpn commented Nov 11, 2026 •

Copy link
Copy Markdown

Implements LIBS-876

Relates to DHIS2-15842 (Capture crashes when saving a new enrollment with Google Translate on).


Description

When the browser translates the page, Google Translate replaces a Button's label text node with <font> elements. If loading then becomes true, React inserts the .loader span before the original text node, which is no longer a child of the button. React throws NotFoundError: Failed to execute 'insertBefore' on 'Node' and the whole app crashes. Adding an icon after translation fails the same way.

The fix wraps the label in <span className="button-label">, so React's insertion reference is always an element it owns. Translation then only changes nodes inside that span.

The span has display: contents, so the label's children remain direct flex items of the button and the layout does not change.

How it was tested

  • Two new unit tests mimic the translation (replace every text node in the button with a <font> element), then turn loading on or add an icon. Both fail on master with NotFoundError and pass with this change.
  • All 24 tests in components/button pass. eslint and prettier --check are clean for the package (one existing react-hooks/exhaustive-deps warning in button.js is untouched).
  • Full yarn test: 1047 pass, 3 fail in components/selector-bar. Those 3 fail the same way on unmodified master.
  • Layout: in Storybook (Chrome), I measured getBoundingClientRect() for every <button> and every icon, loader and svg inside it, across all 45 stories of the button package (79 buttons), on master and on this branch. All boxes are identical.

Known issues

  • Consumer CSS that targets direct children of the button (for example button > svg for elements passed as children) will no longer match, because children are now inside the .button-label span.
  • Cypress feature tests were not run locally.
  • Not tested with the real Google Translate extension; the unit tests reproduce the DOM change it makes.

Checklist

  • API docs are generated
  • Tests were added
  • Storybook demos were added

No props changed, so there are no API doc or Storybook changes.


Screenshots

No visual change. See the layout measurement above.


AI disclosure

This change was written and tested by an AI model, Claude Fable 5.2 (via Claude Code), and submitted by a human contributor.

#T4G (tokens for good project)

🤖 Generated with Claude Code

@alexcpn
alexcpn requested a review from a team as a code owner November 11, 2026 07:06
@alexcpn alexcpn changed the title fix(button): prevent crash when loading changes on a translated page fix(button): prevent crash when loading changes on a translated page Nov 11, 2026
@kabaros

kabaros commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

@alexcpn thanks for the contribution - our CI pipeline blocked the PR because the commits aren't signed. Would you be able to sign them and force push?

Browser translation (e.g. Google Translate) replaces a button's label text
node with <font> elements. When `loading` (or `icon`) was then turned on,
React tried to insert the loader before the detached text node and threw
NotFoundError, crashing the whole app.

The label is now wrapped in a span with `display: contents`, so React always
has a stable element as the insertion reference and the layout is unchanged.

Refs: LIBS-876

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@alexcpn
alexcpn force-pushed the LIBS-876/button-google-translate-crash branch from ae06cba to e157fb9 Compare December 4, 2026 13:44
@sonarqubecloud

sonarqubecloud Bot commented Dec 4, 2026

Copy link
Copy Markdown

@alexcpn

alexcpn commented Dec 4, 2026

Copy link
Copy Markdown
Author

@kabaros thanks for the review! I've signed the commit and force-pushed, so it should now show as verified. Let me know if anything else is needed.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants