Paste Clean-up in EJ2 JavaScript Block Editor control
15 Dec 202516 minutes to read
The Block Editor control 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.
const editor = new BlockEditor({
pasteCleanupSettings: {
allowedStyles: ['font-weight', 'font-style']
}
});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.
const editor = new BlockEditor({
pasteCleanupSettings: {
deniedTags: ['script', 'iframe']
}
});Below example demonstrates the usage of paste settings that allows only specific styles and also removes the specific tags from the pasted content.
// Initialize BlockEditor with paste cleanup settings
var blockEditor = new ej.blockeditor.BlockEditor({
blocks: [
{
id: 'demo-block',
blockType: 'Paragraph'
}
],
// Configure paste cleanup settings
pasteCleanupSettings: {
allowedStyles: ['text-decoration'],
deniedTags: ['script', 'iframe']
},
afterPasteCleanup: (args) => {
displayOutput(`After Paste Event: Processed content length: ${args.content.length} characters`);
}
});
blockEditor.appendTo('#blockeditor');
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.`);
// Output helper function
function displayOutput(message) {
var outputDiv = document.getElementById('output');
if (outputDiv) {
outputDiv.textContent = message;
}
}<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 - BlockEditor</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta name="description" content="Essential JS 2">
<meta name="author" content="Syncfusion">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-base/styles/fluent2.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-buttons/styles/fluent2.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-popups/styles/fluent2.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-inputs/styles/fluent2.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-navigations/styles/fluent2.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-splitbuttons/styles/fluent2.css" rel="stylesheet"/>
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-dropdowns/styles/fluent2.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-blockeditor/styles/fluent2.css" rel="stylesheet" />
<!--style reference from app-->
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/32.1.19/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="blockeditor"></div>
<div id="controls">
<h4>Test Content to Copy and Paste:</h4>
<div class="test-content">
<div id="sampleContent" contenteditable="true">
<h2 style="color: red; font-weight: bold; font-size: 24px;">Formatted Heading</h2>
<p style="background-color: yellow; font-style: italic;">
This is a <span style="font-weight: bold;">bold paragraph</span> with
<span style="color: blue; font-style: italic;">italic text</span> and
<span style="text-decoration: underline;">underlined content</span>.
</p>
<script> console.log('This script should be removed'); </script>
<iframe src="about:blank" width="100" height="50"></iframe>
<div style="border: 1px solid black; padding: 10px;">
<span style="font-weight: 600;">Heavy text</span> and
<span style="color: green; font-size: 18px;">colored text</span>
</div>
</div>
</div>
<div id="output"></div>
</div>
</div>
<script>
var ele = document.getElementById('container');
if (ele) {
ele.style.visibility = "visible";
}
</script>
<style>
#container {
margin: 50px;
gap: 20px;
display: flex;
flex-direction: column;
align-items: center;
}
</style>
<script src="index.js" type="text/javascript"></script>
</body>
</html>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.
const editor = new BlockEditor({
pasteCleanupSettings: {
keepFormat: false
}
});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.
const editor = new BlockEditor({
pasteCleanupSettings: {
plainText: true
}
});Below example demonstrates the usage of paste settings that disables the keep format and allows plain text.
// Initialize BlockEditor with paste cleanup settings
var blockEditor = new ej.blockeditor.BlockEditor({
blocks: [
{
id: 'demo-block',
blockType: 'Paragraph'
}
],
// Configure paste cleanup settings
pasteCleanupSettings: {
keepFormat: false,
plainText: true,
},
afterPasteCleanup: (args) => {
displayOutput(`After Paste Event: Processed content length: ${args.content.length} characters`);
}
});
blockEditor.appendTo('#blockeditor');
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.`);
// Output helper function
function displayOutput(message) {
var outputDiv = document.getElementById('output');
if (outputDiv) {
outputDiv.textContent = message;
}
}<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 - BlockEditor</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta name="description" content="Essential JS 2">
<meta name="author" content="Syncfusion">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-base/styles/fluent2.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-buttons/styles/fluent2.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-popups/styles/fluent2.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-inputs/styles/fluent2.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-navigations/styles/fluent2.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-splitbuttons/styles/fluent2.css" rel="stylesheet"/>
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-dropdowns/styles/fluent2.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-blockeditor/styles/fluent2.css" rel="stylesheet" />
<!--style reference from app-->
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/32.1.19/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="blockeditor"></div>
<div id="controls">
<h4>Test Content to Copy and Paste:</h4>
<div class="test-content">
<div id="sampleContent" contenteditable="true">
<h2 style="color: red; font-weight: bold; font-size: 24px;">Formatted Heading</h2>
<p style="background-color: yellow; font-style: italic;">
This is a <span style="font-weight: bold;">bold paragraph</span> with
<span style="color: blue; font-style: italic;">italic text</span> and
<span style="text-decoration: underline;">underlined content</span>.
</p>
<div style="border: 1px solid black; padding: 10px;">
<span style="font-weight: 600;">Heavy text</span> and
<span style="color: green; font-size: 18px;">colored text</span>
</div>
</div>
</div>
<div id="output"></div>
</div>
</div>
<script>
var ele = document.getElementById('container');
if (ele) {
ele.style.visibility = "visible";
}
</script>
<style>
#container {
margin: 50px;
gap: 20px;
display: flex;
flex-direction: column;
align-items: center;
}
</style>
<script src="index.js" type="text/javascript"></script>
</body>
</html>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()
});