Paste Clean-up in React Block Editor component

15 Dec 202518 minutes to read

The Block Editor component provides robust paste clean-up functionalities to ensure that pasted content integrates seamlessly and maintains styling and structural consistency. This feature helps remove unwanted formatting, scripts, and elements copied from external sources like web pages or word processors.

You can configure the paste behavior using the pasteCleanupSettings property, which allows you to define how content is handled when pasted into the editor.

Configuring allowed styles

The allowedStyles property lets you define which CSS styles are permitted in pasted content. Any style not in this list is stripped out, ensuring that only desired visual attributes are preserved.

By default, the following styles are allowed:

[‘font-weight’, ‘font-style’, ‘text-decoration’, ‘text-transform’].

In the below example, only font-weight and font-style styles will be retained from the pasted content. All other inline styles will be removed.

<BlockEditorComponent pasteCleanupSettings={{ allowedStyles: ['font-weight', 'font-style'] }}></BlockEditorComponent>

Setting denied tags

The deniedTags property specifies a list of HTML tags to be removed from pasted content. This is useful for stripping potentially problematic elements like <script> or <iframe> tags. By default, this property is an empty array, so no tags are removed.

In the below example, any <script> or <iframe> tags found in the pasted content will be removed, preventing unwanted behavior or styling issues.

<BlockEditorComponent pasteCleanupSettings={{ deniedTags: ['script', 'iframe'] }}></BlockEditorComponent>

Below example demonstrates the usage of paste settings that allows only specific styles and also removes the specific tags from the pasted content.

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { BlockEditorComponent } from '@syncfusion/ej2-react-blockeditor';

function App() {
    
    const blocksData = [
    {
        blockType: 'Paragraph'
    }
];

    const handleAfterPaste = (args) => {
        displayOutput(`After Paste Event: Processed content length: ${args.content.length} characters`);
    };

    // Output helper function
    const displayOutput = (message) => {
        const outputDiv = document.getElementById('output');
        if (outputDiv) {
            outputDiv.textContent = message;
        }
    };

    React.useEffect(() => {
        displayOutput(`Paste Cleanup Settings Active:
- Allowed Styles: ['text-decoration']
- Denied Tags: ['script', 'iframe']

Copy content from the test area above and paste it into the editor to see the cleanup in action.`);
    }, []);

    return (
       <div>
            <div id="controls">
        <h4>Test Content to Copy and Paste:</h4>
        <div className="test-content">
                <div id="sampleContent" contentEditable={true}>
      <h2 style=>
        Formatted Heading
      </h2>
      <p style=>
        This is a <span style=>bold paragraph</span> with{' '}
        <span style=>italic text</span> and{' '}
        <span style=>underlined content</span>.
      </p>
      <iframe src="about:blank" width="100" height="50" />
      <div style=>
        <span style=>Heavy text</span> and{' '}
        <span style=>colored text</span>
      </div>
    </div>
        </div>
    </div>
        <BlockEditorComponent
            id="blockeditor"
            blocks={blocksData}
            pasteCleanupSettings=
            afterPasteCleanup={handleAfterPaste}
        ></BlockEditorComponent>
          <div id="output"></div>
                </div>
    );
}

export default App;
ReactDOM.render(<App />, document.getElementById('container'));
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { BlockEditorComponent, AfterPasteCleanupEventArgs } from '@syncfusion/ej2-react-blockeditor';

function App() {
    
    const blocksData = [
        {
            blockType: 'Paragraph'
        }
    ];

    const handleAfterPaste = (args: AfterPasteCleanupEventArgs) => {
        displayOutput(`After Paste Event: Processed content length: ${args.content.length} characters`);
    };

    // Output helper function
    const displayOutput = (message: string) => {
        const outputDiv = document.getElementById('output');
        if (outputDiv) {
            outputDiv.textContent = message;
        }
    };

    React.useEffect(() => {
        displayOutput(`Paste Cleanup Settings Active:
- Allowed Styles: ['text-decoration']
- Denied Tags: ['script', 'iframe']

Copy content from the test area above and paste it into the editor to see the cleanup in action.`);
    }, []);

    return (
        <div>
            <div id="controls">
        <h4>Test Content to Copy and Paste:</h4>
        <div className="test-content">
                <div id="sampleContent" contentEditable={true}>
      <h2 style=>
        Formatted Heading
      </h2>
      <p style=>
        This is a <span style=>bold paragraph</span> with{' '}
        <span style=>italic text</span> and{' '}
        <span style=>underlined content</span>.
      </p>
      <iframe src="about:blank" width="100" height="50" />
      <div style=>
        <span style=>Heavy text</span> and{' '}
        <span style=>colored text</span>
      </div>
    </div>
        </div>
    </div>
        <BlockEditorComponent
            id="blockeditor"
            blocks={blocksData}
            pasteCleanupSettings=
            afterPasteCleanup={handleAfterPaste}
        ></BlockEditorComponent>
                <div id="output"></div>
                </div>
    );
}

export default App;
ReactDOM.render(<App />, document.getElementById('container'));

Disable Keep format

By default, the editor retains the formatting of pasted content (e.g., bold, italics, links). You can disable this by setting the keepFormat property to false. When disabled, the editor primarily pastes content as plain text, regardless of the allowedStyles configuration.

<BlockEditorComponent pasteCleanupSettings={{ keepFormat: false }}></BlockEditorComponent>

Allowing plain text

To paste content as plain text, stripping all HTML tags and inline styles, set the plainText property to true in pasteCleanupSettings. This ensures that only raw text is inserted, which is ideal for maintaining strict content consistency. By default, this property is false.

<BlockEditorComponent pasteCleanupSettings={{ plainText: true }}></BlockEditorComponent>

Below example demonstrates the usage of paste settings that disables the keep format and allows plain text.

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { BlockEditorComponent } from '@syncfusion/ej2-react-blockeditor';

function App() {
  
const blocksData = [
    {
        blockType: 'Paragraph'
    }
];
    const handleAfterPaste = (args) => {
        displayOutput(`After Paste Event: Processed content length: ${args.content.length} characters`);
    };

    // Output helper function
    const displayOutput = (message) => {
        const outputDiv = document.getElementById('output');
        if (outputDiv) {
            outputDiv.textContent = message;
        }
    };

    React.useEffect(() => {
        displayOutput(`Paste Cleanup Settings Active:
- Keep Format: false
- Plain Text: true

Copy content from the test area above and paste it into the editor to see the cleanup in action.`);
    }, []);

    return (
              <div id="controls">
      <h4>Test Content to Copy and Paste:</h4>
      <div className="test-content">
        <div id="sampleContent" contentEditable={true}>
          <h2 style=>
            Formatted Heading
          </h2>
          <p style=>
            This is a <span style=>bold paragraph</span> with{' '}
            <span style=>italic text</span> and{' '}
            <span style=>underlined content</span>.
          </p>
          <div style=>
            <span style=>Heavy text</span> and{' '}
            <span style=>colored text</span>
          </div>
        </div>
      </div>
        <BlockEditorComponent
            id="blockeditor"
            blocks={blocksData}
            pasteCleanupSettings=
            afterPasteCleanup={handleAfterPaste}
        ></BlockEditorComponent>
         <div id="output"></div>
    </div>
    );
}

export default App;
ReactDOM.render(<App />, document.getElementById('container'));
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { BlockEditorComponent, AfterPasteCleanupEventArgs } from '@syncfusion/ej2-react-blockeditor';

function App() {
  
    const blocksData = [
        {
            id: 'demo-block',
            blockType: 'Paragraph'
        }
    ];
    const handleAfterPaste = (args: AfterPasteCleanupEventArgs) => {
        displayOutput(`After Paste Event: Processed content length: ${args.content.length} characters`);
    };

    // Output helper function
    const displayOutput = (message: string) => {
        const outputDiv = document.getElementById('output');
        if (outputDiv) {
            outputDiv.textContent = message;
        }
    };

    React.useEffect(() => {
        displayOutput(`Paste Cleanup Settings Active:
- Keep Format: false
- Plain Text: true

Copy content from the test area above and paste it into the editor to see the cleanup in action.`);
    }, []);

    return (
         <div id="controls">
      <h4>Test Content to Copy and Paste:</h4>
      <div className="test-content">
        <div id="sampleContent" contentEditable={true}>
          <h2 style=>
            Formatted Heading
          </h2>
          <p style=>
            This is a <span style=>bold paragraph</span> with{' '}
            <span style=>italic text</span> and{' '}
            <span style=>underlined content</span>.
          </p>
          <div style=>
            <span style=>Heavy text</span> and{' '}
            <span style=>colored text</span>
          </div>
        </div>
      </div>
        <BlockEditorComponent
            id="blockeditor"
            blocks={blocksData}
            pasteCleanupSettings=
            afterPasteCleanup={handleAfterPaste}
        ></BlockEditorComponent>
         <div id="output"></div>
    </div>
    );
}

export default App;
ReactDOM.render(<App />, document.getElementById('container'));

Events

The Block Editor provides events to monitor and interact with the paste action.

Name Args Description
beforePasteCleanup BeforePasteCleanupEventArgs Triggers before the content is pasted into the editor.
afterPasteCleanup AfterPasteCleanupEventArgs Triggers after the content is pasted into the editor.

Below snippet demonstrates how to configure above events in the editor.

const editor = new BlockEditor({
    beforePasteCleanup: onBeforePasteCleanup()
});
const editor = new BlockEditor({
    afterPasteCleanup: onAfterPasteCleanup()
});