Skip to content

HTML-tags of newly registered formats disappear when pasting #13292

Closed

Description

@websupporter

Describe the bug
I registered a new format type. Lets say: <span class="great-class">. When I copy a text now into the Component, the span.great-class tags get removed:

Received HTML:
df df <span class="great-class">df</span> df df. editor.min.js:55:146144
Received plain text:
df df df df df. editor.min.js:55:146187
Processed inline HTML:
df df df df df. blocks.min.js:2:115611

To Reproduce

const {createElement} = window.wp.element;
const {registerFormatType, toggleFormat} = window.wp.richText;
const {RichTextToolbarButton} = window.wp.editor;

registerFormatType('some/type', {
    title: 'Title',
    tagName: 'span',
    className: 'great-class',
    edit({ isActive, value, onChange}) {
        const onClick = () => {
            onChange(toggleFormat(value, { type: 'some/type' }));
        };

        return (
            createElement(RichTextToolbarButton, {
                icon: 'admin-home',
                title: 'Title',
                onClick,
                isActive,
                shortcutType: 'primary',
                shortcutCharacter: '8',
            })
        )
    }
});
  1. Add this script
  2. Add a text in a RichText component
  3. Select a part of the text and click the new button in the toolbar
  4. Copy all the text in the clipboard
  5. Paste the text in a RichText component
  6. See error

Expected behavior
The new format should survive the copy/pasting

Screenshots
https://www.youtube.com/watch?v=9SiyAROa1Iw

Desktop (please complete the following information):

  • OS: Ubuntu 14.04
  • Browser Firefox

Activity

  1. websupporter commented on Jan 11, 2019

    @websupporter
    Author

    I suspect, this could be fixed by expanding the list returned by getPhrasingContentSchema() with the types we get from wp.data.select('core/rich-text').getFormatTypes()

    But for <span> this might be also a problem:

    if ( node.nodeName === 'SPAN' ) {
    const {
    fontWeight,
    fontStyle,
    textDecorationLine,
    verticalAlign,
    } = node.style;
    if ( fontWeight === 'bold' || fontWeight === '700' ) {
    wrap( doc.createElement( 'strong' ), node );
    }
    if ( fontStyle === 'italic' ) {
    wrap( doc.createElement( 'em' ), node );
    }
    if ( textDecorationLine === 'line-through' ) {
    wrap( doc.createElement( 'del' ), node );
    }
    if ( verticalAlign === 'super' ) {
    wrap( doc.createElement( 'sup' ), node );
    } else if ( verticalAlign === 'sub' ) {
    wrap( doc.createElement( 'sub' ), node );
    }

  2. added
    Needs Technical Feedback
    [Package] Format library
    on Jan 11, 2019
  3. added
    [Type] Enhancement
    and removed
    Needs Technical Feedback
    on Apr 22, 2019
  4. added
    [Feature] Rich Text
    [Package] Rich text
    on Dec 10, 2019
  5. skorasaurus commented on Mar 28, 2024

    @skorasaurus
    Member

    Thanks for reporting; I'm not able to reproduce this using your example; nor in my custom example of a custom richtext format.

    Looks like this was fixed a while back in #27967 (ref #16240)

  6. websupporter commented on Mar 29, 2024

    @websupporter
    Author

    Amazing. Thanks for following up @skorasaurus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Feature] Paste[Feature] Rich Text[Package] Format library[Package] Rich text[Type] Enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions