Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/rich-text/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
135 changes: 135 additions & 0 deletions packages/rich-text/src/test/to-html-string-objects.jsdom.test.ts
Original file line number Diff line number Diff line change
@@ -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 }"></${ tagName }>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 <span data-id="sample" class="${ CLASS_NAME }"></span>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 <span class="${ CLASS_NAME }"><em>Token</em></span>after`;
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(
`<img class="${ CLASS_NAME }">selected</img>`
);
} );
} );
24 changes: 23 additions & 1 deletion packages/rich-text/src/to-html-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
] );

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
 */


/**
* Create an HTML string from a Rich Text value.
*
Expand Down Expand Up @@ -146,7 +168,7 @@ function createElementHTML( {
) }"`;
}

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

return `<${ type }${ attributeString }>`;
}

Expand Down
Loading