Skip to content

RichText: close non-void inline object elements - #85895

Open
salvatorecorsi wants to merge 6 commits into
WordPress:trunkfrom
salvatorecorsi:fix/85890-close-nonvoid-rich-text-objects
Open

salvatorecorsi wants to merge 6 commits into
WordPress:trunkfrom
salvatorecorsi:fix/85890-close-nonvoid-rich-text-objects

Conversation

@salvatorecorsi

@salvatorecorsi salvatorecorsi commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

What?

Close non-void RichText inline objects when converting them to HTML, and preserve their stored children.

Closes #85890.

Why?

A plugin can register a span as an inline object, but toHTMLString() currently emits only its opening tag. Text following the object then becomes part of that element when the HTML is parsed. A non-editable object also loses its stored inner HTML through the same early return.

How?

Use a private, case-insensitive lookup to keep only void object elements open-ended. Non-void objects use the existing children-and-closing-tag path. The lookup retains the obsolete void elements browsers still handle for compatibility. The check stays restricted to objects, so an existing non-object format registered with a void tag name keeps its selected text.

Testing Instructions

In a WordPress editor page with wp.richText loaded, run:

const name = 'example/inline-token';
wp.richText.registerFormatType( name, {
	title: 'Inline token',
	tagName: 'span',
	className: 'inline-token',
	object: true,
} );
const value = wp.richText.insertObject(
	wp.richText.create( { text: 'before after' } ),
	{ type: name },
	7,
	7
);
wp.richText.toHTMLString( { value } );
wp.richText.unregisterFormatType( name );

The result should be before <span class="inline-token"></span>after. Reparse it and confirm that after is outside the span. Repeat with a non-editable span containing <em>Token</em>: both the inner HTML and the following text should survive. Image and line-break objects should still serialize without closing tags.

The regression tests use real format registration, insertion and parsing. The initial regression set fails its five non-void cases on the exact unfixed source and passes ten compatibility controls. An additional case verifies that an empty object with a registered data-id retains its replacement character and mapped attributes after parsing. The fixed implementation passes the complete rich-text package tests (244 tests) and related raw-handling/serialization checks (459 tests, including all 377 existing block fixtures).

The full production build, rich-text development typecheck, scoped lint and test conventions passed. The built package's public APIs were also checked in WordPress with Chromium DOM reparsing. The existing parser behavior that ignores an empty class-only span remains unchanged; attributed and non-editable objects retain their data. The routing check retains the known Windows path-separator discovery failure. These checks do not cover the full editor save flow or Docker E2E.

Hosted E2E is not green: one WebKit shard failed during autocomplete REST fixture cleanup/setup and writing-flow selection assertions, and report merging failed while reading an archive. A targeted rerun was refused and has been requested once from a maintainer; the writing-flow failure has not been established as either intermittent or caused by this patch. The missing type-label checks are a separate maintainer-only requirement.

Validation after trunk update (2026-11-02)

Merged trunk c6b054ddff32e160158b538f62d40963c80f5743 normally and validated head efd4f047d8e2f5e2b0202506875aea7298262b29.

Current trunk merged without conflicts and the original serializer and regression test blobs remained identical.

  • The full production build and full repository typecheck passed on the merged tree.
  • All 703 RichText and related integration tests passed in 33 files: 244 package tests and 459 raw-handling/full-content checks.
  • The 16 inline-object serializer regression/control cases passed, alongside all 377 saved-block fixtures, 81 raw-handling cases and the non-matched-tags case.
  • Changed serializer/test lint and formatting, PR changelog validation and current Vitest conventions passed.
  • Hosted build and test jobs completed successfully on this head.

The required type label still needs maintainer action.

Validation update — 9 November 2026

Merged trunk afa2c6a9baec79a4fdfb40ca59adaae410cc5feb and validated published head 46cb0395927fdddd2bf0e94fb90527be5ba694c6. This includes the required trunk baseline.

  • Full JavaScript build and TypeScript checks passed.
  • Related automated tests passed: 737 cases across 33 files (unit).
  • scoped JavaScript lint, package changelog checks, repository conventions, tsconfig validation, and diff whitespace checks passed.

These are local validation results; hosted CI and maintainer-only label requirements remain separate.

Use of AI Tools

OpenAI Codex assisted with investigation, implementation, regression tests, automated validation and this description. Additional Codex review identified the non-object compatibility case. Maintainer review is requested.

Please add [Type] Bug; the contributor account cannot apply repository labels.

@github-actions github-actions Bot added the [Package] Rich text /packages/rich-text label Oct 29, 2026
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground

🎉 Props

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: salvatorecorsi <salvatorecorsi@git.wordpress.org>
Co-authored-by: celtislab <enomoto-celtislab@git.wordpress.org>

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

@salvatorecorsi
salvatorecorsi marked this pull request as ready for review October 29, 2026 06:39
@salvatorecorsi

salvatorecorsi commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor Author

The updated head 46cb0395 now passes the post-editor performance job, so the earlier environment/rerun request is no longer needed.

Earlier CI follow-up

Update for 4d950e49: the failed performance shard receives HTTP 500 because the Ipsum theme requires a missing twentytwentyone/inc/abilities.php. This happens during WordPress bootstrap, before performance measurements. Could a maintainer recheck the test-theme environment and rerun the shard once it can boot? The log doesn't indicate a RichText serializer correction.

Earlier CI follow-up (previous head)

Could a maintainer rerun the failed jobs in this E2E run? The autocomplete test failed during REST user setup/cleanup, and report merging hit a corrupt archive. The WebKit writing-flow test also failed its selection/toolbar assertions; I haven't established whether that is intermittent or related to this patch. My targeted rerun attempt was refused, so I haven't retried it.

@Mamaduka Mamaduka added the [Type] Bug An existing feature does not function as intended label Nov 13, 2026
@Mamaduka
Mamaduka requested a review from dmsnell November 13, 2026 07:29

@dmsnell dmsnell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good. It makes me wonder why we’re creating the HTML via string interpolation, and rather wonder if we would do well to rely on createElement() and createElementNS() and createComment() and the like.

it’s worth pointing out that the DOM methods and this code supports the creation of broken HTML comments, and that’s something we should check for and reject.

in fact, it looks like escape-html could use a lot of work. I’d be happy to help review and guide that.

'frame',
'keygen',
'param',
] );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this list matches the HTML API’s copy, lifted from the spec.

it would be worth linking to where these are defined.

/**
 * @link https://html.spec.whatwg.org/#void-elements
 * @link https://html.spec.whatwg.org/#serialising-html-fragments
 */

}

if ( object ) {
if ( object && type && VOID_ELEMENTS.has( type.toLowerCase() ) ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

is type guaranteed to be upper-case for tag names? if so, we could define the Set() with upper-case variants and skip the string conversion

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

[Package] Rich text /packages/rich-text [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

createElementHTML() omits closing tags for non-void elements

3 participants