Skip to content

fix(element): Handle unmatched closing tags in createInterpolateElement#71413

Open
SinghCod3r wants to merge 1 commit intoWordPress:trunkfrom
SinghCod3r:fix/60843-handle-unmatched-tags
Open

fix(element): Handle unmatched closing tags in createInterpolateElement#71413
SinghCod3r wants to merge 1 commit intoWordPress:trunkfrom
SinghCod3r:fix/60843-handle-unmatched-tags

Conversation

@SinghCod3r
Copy link

Description

This pull request addresses an issue where the createInterpolateElement() function throws a critical TypeError if it encounters a closing HTML tag that does not have a corresponding opening tag. This is particularly problematic for translated strings, where such errors can be accidentally introduced.

This fix adds a check to validate that a closing tag has a matching opening tag on the stack. If no match is found:

  1. An error is logged to the console with a descriptive message.
  2. The unmatched tag is treated as plain text.
  3. The function continues processing without crashing.

A new unit test has also been added to confirm this behavior and prevent future regressions.

Fixes #60843

How has this been tested?

  1. Verified that all existing unit tests continue to pass.
  2. Added a new unit test in packages/element/src/test/create-interpolate-element.test.js that specifically checks for the unmatched closer scenario. Confirmed this test passes.
  3. Manually tested by inserting the following code into a local block editor component:
    createInterpolateElement( 'Hello </code>World</code>', {
        code: <code />
    } );
  4. Confirmed that the application no longer crashes and that the expected error message appears in the developer console.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

@SinghCod3r SinghCod3r requested a review from ajitbohra as a code owner August 29, 2025 12:54
@github-actions
Copy link

github-actions bot commented Aug 29, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: SinghCod3r <singhcod3r@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: marincarroll <marincarroll@git.wordpress.org>
Co-authored-by: gziolo <gziolo@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Aug 29, 2025
@github-actions
Copy link

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @SinghCod3r! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@SinghCod3r SinghCod3r force-pushed the fix/60843-handle-unmatched-tags branch 2 times, most recently from 610d147 to 613b933 Compare August 29, 2025 13:25
@SinghCod3r
Copy link
Author

Hi @ajitbohra,

This PR fixes #60843 by improving error handling in createInterpolateElement. Specifically:

✅ Prevents crashes when an unmatched closing tag is found in translated strings.

✅ Logs a clear error to the console and treats the unmatched tag as plain text.

✅ Keeps processing other elements without breaking the app.

✅ Added a dedicated unit test (create-interpolate-element.test.js) to cover this scenario.

Testing

All existing unit tests pass locally.

Verified manually with:

createInterpolateElement( 'Hello </code>World</code>', { code: <code /> } );

→ The application no longer crashes, and an error message appears in the console as expected.

Notes

Some CI checks (JavaScript unit tests + Playwright E2E) are failing. My new test passes independently, so these appear unrelated/fragile.

The Props bot flagged my GitHub ↔️ WordPress.org account link. I’ll link my accounts so the contribution is credited properly.

The PR is missing a [Type] Bug label, which maintainers may need to apply for it to merge.

This change is ready for your review. Please let me know if you’d like further adjustments

Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

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

@SinghCod3r Thanks for the PR!

This PR appears to contain unintentional file moves and deletions. Could you address the following so we can review it?

  • Restore the unit test file to its original location: packages/element/src/create-interpolate-element.test.tsx => packages/element/src/test/create-interpolate-element.js
  • Restore the deleted README.md content

Adds a check to ensure that a closing tag has a corresponding opening tag before processing. If an unmatched closing tag is found, an error is logged to the console, and the tag is treated as plain text instead of causing a critical crash.

This change also includes a new unit test to verify the correct behavior and prevent future regressions.

Fixes WordPress#60843
@SinghCod3r SinghCod3r force-pushed the fix/60843-handle-unmatched-tags branch from 613b933 to 76ceed7 Compare September 9, 2025 13:44
@SinghCod3r
Copy link
Author

@t-hamano , i guess its resolved now..

@t-hamano
Copy link
Contributor

t-hamano commented Sep 9, 2025

@SinghCod3r

  • Restore the unit test file to its original location: packages/element/src/create-interpolate-element.test.tsx => packages/element/src/test/create-interpolate-element.js
  • Restore the deleted README.md content

As far as I can see, these have not yet been addressed:

image

Additionally, there is a conflict that needs to be resolved in this PR.

The only changes needed for this PR are probably the following two, right?

  • Add a patch to fix the problem to packages/element/src/create-interpolate-element.js
  • Add a new unit test to packages/element/src/test/create-interpolate-element.js

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

Labels

First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Package] Element /packages/element [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

createInterpolateElement: Add error handling in case unmatched tags are included

2 participants