Skip to content

Add ability to filter unwanted HTML tags in RichText component #7591

@mirka

Description

@mirka

Is your feature request related to a problem? Please describe.

We don't have much control over what a user pastes into a RichText. The clipboard might contain HTML tags that are inappropriate for the intended use.

For example, I would want to prevent users from pasting a link (e.g. <a href="#">foo</a>) into a RichText intended to be a button.

Describe the solution you'd like

Some possibilities:

  • Allow onPaste handlers on RichText: Then the consumer could strip out unwanted clipboard content onPaste, or even use the plaintext version (event.clipboardData.getData( 'text/plain' )).
  • Add an allowedTags property on RichText: The consumer could pass it an array of acceptable HTML tagNames, and then RichText would handle the onPaste filtering for us.

Describe alternatives you've considered

  • Use onPaste on a wrapper div: While onCopy can be handled this way, it looks like onPaste must be set on the RichText component itself.

  • Try doing it onChange: This poses another set of problems, where any kind of string manipulation in an onChange–setState in React breaks CJK IME input, unless we add a workaround with composition event detection. (Related)

    For example, doing something like this would make CJK input unusable:

    <RichText
      tagName="div"
      value={ this.state.text }
      onChange={ ( value ) => this.setState( { text: value[0].replace( 'foo', 'bar' ) } ) }
    />
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Feature] ExtensibilityThe ability to extend blocks or the editing experience[Feature] Rich TextRelated to the Rich Text component that allows developers to render a contenteditable[Type] EnhancementA suggestion for improvement.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions