Repository navigation
RichText: close non-void inline object elements #85895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
salvatorecorsi
wants to merge
6
commits into
WordPress:trunk
Choose a base branch
from
salvatorecorsi:fix/85890-close-nonvoid-rich-text-objects
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+162
−1
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a0d6d99
RichText: close non-void inline object elements
salvatorecorsi ba75315
Docs: reference RichText object serialization fix
salvatorecorsi efd4f04
Merge current trunk into inline RichText object serialization fix
salvatorecorsi 4d950e4
Merge trunk into fix/85890-close-nonvoid-rich-text-objects
salvatorecorsi 46cb039
Merge current trunk into PR #85895
salvatorecorsi 507e076
Merge trunk and preserve original Gutenberg contribution changelogs
salvatorecorsi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
packages/rich-text/src/test/to-html-string-objects.jsdom.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>` | ||
| ); | ||
| } ); | ||
| } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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() ) ) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is |
||
| return `<${ type }${ attributeString }>`; | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.