Skip to content
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

Enhance support for SVG in RichText #40496

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions packages/rich-text/src/register-format-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { store as richTextStore } from './store';
* unique across all registered formats.
* @property {string} tagName The HTML tag this format will wrap the
* selection with.
* @property {string} [namespace] The namespace of the `tagName`.
* @property {boolean} interactive Whether format makes content interactive or not.
* @property {string | null} [className] A class to match the format.
* @property {string} title Name of the format.
Expand Down Expand Up @@ -60,6 +61,11 @@ export function registerFormatType( name, settings ) {
return;
}

if ( 'namespace' in settings && typeof settings.namespace !== 'string' ) {
window.console.error( 'Format namespaces must be a string.' );
return;
}

if (
( typeof settings.className !== 'string' ||
settings.className === '' ) &&
Expand Down
41 changes: 41 additions & 0 deletions packages/rich-text/src/test/to-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import { toDom, applyValue } from '../to-dom';
import { createElement } from '../create-element';
import { spec } from './helpers';
import { OBJECT_REPLACEMENT_CHARACTER } from '../special-characters';
import { registerFormatType } from '../register-format-type';
import { unregisterFormatType } from '../unregister-format-type';

describe( 'recordToDom', () => {
beforeAll( () => {
Expand All @@ -21,6 +24,44 @@ describe( 'recordToDom', () => {
expect( selection ).toEqual( { startPath, endPath } );
} );
} );

it( 'should use the namespace specfied by the format', () => {
const formatName = 'my-plugin/nom';
const namespace = 'http://www.w3.org/1998/Math/MathML';

registerFormatType( formatName, {
namespace,
title: 'Math',
tagName: 'math',
className: 'nom-math',
contentEditable: false,
edit() {},
} );

const { body } = toDom( {
value: {
formats: [ , ],
replacements: [
{
type: 'my-plugin/nom',
tagName: 'math',
attributes: {},
unregisteredAttributes: {},
innerHTML: '0',
},
],
text: OBJECT_REPLACEMENT_CHARACTER,
},
} );

unregisterFormatType( formatName );

const subject = body.firstElementChild;
expect( subject.outerHTML ).toBe(
`<math class="nom-math" contenteditable="false">0</math>`
);
expect( subject.namespaceURI ).toBe( namespace );
} );
} );

describe( 'applyValue', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/rich-text/src/to-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ function append( element, child ) {
const { type, attributes } = child;

if ( type ) {
child = element.ownerDocument.createElement( type );
const namespaceURI = child.namespace || element.namespaceURI;
child = element.ownerDocument.createElementNS( namespaceURI, type );

for ( const key in attributes ) {
child.setAttribute( key, attributes[ key ] );
Expand Down
1 change: 1 addition & 0 deletions packages/rich-text/src/to-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ function fromFormat( {
type: tagName || formatType.tagName,
object: formatType.object,
attributes: restoreOnAttributes( elementAttributes, isEditableTree ),
namespace: formatType.namespace,
};
}

Expand Down