diff --git a/packages/rich-text/CHANGELOG.md b/packages/rich-text/CHANGELOG.md index 36c9d8850351a0..13bca1b68bc7ce 100644 --- a/packages/rich-text/CHANGELOG.md +++ b/packages/rich-text/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Bug Fixes + +- `toHTMLString`: Close non-void inline objects and preserve their children ([#85895](https://github.com/WordPress/gutenberg/pull/85895)). + ## 8.2.0 (2026-11-18) ## 8.1.0 (2026-11-04) diff --git a/packages/rich-text/src/test/to-html-string-objects.jsdom.test.ts b/packages/rich-text/src/test/to-html-string-objects.jsdom.test.ts new file mode 100644 index 00000000000000..ccd3743a4f536e --- /dev/null +++ b/packages/rich-text/src/test/to-html-string-objects.jsdom.test.ts @@ -0,0 +1,135 @@ +import { afterEach, describe, expect, it } from 'vitest'; +import '../store'; +import { create } from '../create'; +import { applyFormat } from '../apply-format'; +import { insertObject } from '../insert-object'; +import { registerFormatType } from '../register-format-type'; +import { unregisterFormatType } from '../unregister-format-type'; +import { toHTMLString } from '../to-html-string'; +import { OBJECT_REPLACEMENT_CHARACTER } from '../special-characters'; + +const FORMAT_NAME = 'test/serialization-object'; +const CLASS_NAME = 'test-serialization-object'; + +function registerObjectFormat( tagName: string, contentEditable?: boolean ) { + registerFormatType( FORMAT_NAME, { + title: 'Serialization object', + tagName, + className: CLASS_NAME, + object: true, + contentEditable, + } ); +} + +describe( 'toHTMLString object elements', () => { + afterEach( () => { + unregisterFormatType( FORMAT_NAME ); + } ); + + it.each( [ 'span', 'a', 'custom-object', 'SPAN' ] )( + 'closes a non-void %s object before following text', + ( tagName ) => { + registerObjectFormat( tagName ); + const value = insertObject( + create( { text: 'before after' } ), + { type: FORMAT_NAME }, + 7, + 7 + ); + const html = toHTMLString( { value } ); + + expect( html ).toBe( + `before <${ tagName } class="${ CLASS_NAME }">after` + ); + + const element = document.createElement( 'div' ); + element.innerHTML = html; + expect( element.querySelector( tagName )?.textContent ).toBe( '' ); + expect( element.lastChild?.textContent ).toBe( 'after' ); + } + ); + + it( 'round-trips an empty object with registered attributes', () => { + registerFormatType( FORMAT_NAME, { + title: 'Inline token', + tagName: 'span', + className: CLASS_NAME, + object: true, + attributes: { id: 'data-id' }, + } ); + const value = insertObject( + create( { text: 'before after' } ), + { type: FORMAT_NAME, attributes: { id: 'sample' } }, + 7, + 7 + ); + const html = toHTMLString( { value } ); + + expect( html ).toBe( + `before after` + ); + const parsed = create( { html } ); + + expect( parsed.text ).toBe( value.text ); + expect( parsed.replacements[ 7 ] ).toMatchObject( { + type: FORMAT_NAME, + attributes: { id: 'sample' }, + } ); + } ); + + it( 'preserves the children of a non-editable object and the following text', () => { + registerObjectFormat( 'span', false ); + const html = `before Tokenafter`; + const value = create( { html } ); + + expect( value.text ).toBe( + `before ${ OBJECT_REPLACEMENT_CHARACTER }after` + ); + const serialized = toHTMLString( { value } ); + + expect( serialized ).toBe( html ); + expect( create( { html: serialized } ).text ).toBe( value.text ); + } ); + + it.each( [ + 'img', + 'br', + 'input', + 'IMG', + 'param', + 'keygen', + 'frame', + 'basefont', + 'bgsound', + ] )( 'keeps a void %s object without a closing tag', ( tagName ) => { + registerObjectFormat( tagName ); + const value = insertObject( + create( { text: 'before after' } ), + { type: FORMAT_NAME }, + 7, + 7 + ); + + expect( toHTMLString( { value } ) ).toBe( + `before <${ tagName } class="${ CLASS_NAME }">after` + ); + } ); + + it( 'preserves text in a non-object format registered with a void tag name', () => { + registerFormatType( FORMAT_NAME, { + title: 'Image wrapper', + tagName: 'img', + className: CLASS_NAME, + } ); + const value = applyFormat( + create( { text: 'selected' } ), + { type: FORMAT_NAME }, + 0, + 8 + ); + + expect( toHTMLString( { value } ) ).toBe( + `selected` + ); + } ); +} ); diff --git a/packages/rich-text/src/to-html-string.ts b/packages/rich-text/src/to-html-string.ts index 937d6a3315ae0d..929027415ddccb 100644 --- a/packages/rich-text/src/to-html-string.ts +++ b/packages/rich-text/src/to-html-string.ts @@ -6,6 +6,28 @@ import { import { toTree } from './to-tree'; import type { HTMLTreeNode, RichTextValue } from './types'; +const VOID_ELEMENTS = new Set( [ + 'area', + 'base', + 'br', + 'col', + 'embed', + 'hr', + 'img', + 'input', + 'link', + 'meta', + 'source', + 'track', + 'wbr', + // Browsers also keep these obsolete elements void when parsing HTML. + 'basefont', + 'bgsound', + 'frame', + 'keygen', + 'param', +] ); + /** * Create an HTML string from a Rich Text value. * @@ -146,7 +168,7 @@ function createElementHTML( { ) }"`; } - if ( object ) { + if ( object && type && VOID_ELEMENTS.has( type.toLowerCase() ) ) { return `<${ type }${ attributeString }>`; }