Quill Extension to paste only supported HTML
npm install quill-paste-smart



This plugin extends the default clipboard module of Quill.js to prevent users from pasting HTML that does not belong into the editor. To do so it looks into the toolbar configuration and decides which tags and attributes are allowed based on the possible formats.
However, you can also decide on your own, what is allowed.
You can install this plugin either with npm or with yarn.
Run one of the following commands from your projects root in a bash prompt.
``bash`
npm i quill-paste-smart@^1or: yarn add quill-paste-smart@^1
Since this plugin registers itself, it is sufficient to just import it.
`javascript`
import Quill from 'quill';
import 'quill-paste-smart';
* Vanilla JS
* ES6
* CommonJS
* Vue
* surmon-china/vue-quill-editor
* React
* zenoamaro/react-quill
* gtgalone/react-quilljs
Out of the box this plugin will remove all HTML tags and attributes that are not available in the toolbar formats.
If you don't agree with the default settings, you can decide what is allowed by yourself.
Also I thought it could be useful to keep the pasted content selected after pasting, so there is a setting for it too.
A valid configuration could look like this:
`javascript
const options = {
theme: 'snow',
modules: {
clipboard: {
allowed: {
tags: ['a', 'b', 'strong', 'u', 's', 'i', 'p', 'br', 'ul', 'ol', 'li', 'span'],
attributes: ['href', 'rel', 'target', 'class']
},
customButtons: [
{
module: 'quillEmbeds',
allowedTags: ['embed'],
allowedAttr: ['width', 'height'],
}
],
keepSelection: true,
substituteBlockElements: true,
magicPasteLinks: true,
hooks: {
uponSanitizeElement(node, data, config) {
console.log(node);
},
},
handleImagePaste(image) {
console.log("Image file pasted", image);
},
removeConsecutiveSubstitutionTags: true
},
},
};
new Quill('#editor', options);
`
> :raised_hand: Probably you don't need a custom configuration.
> You could stick with the default settings by completely omit the clipboard object in your quill options.
#### Configuration Object
| key | valid values | default value | type | description |
| :---------------------- | :----------------------------------------------------------: | :-----------: | :---------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| allowed.tags | HTML tags | undefined | Array | Here you can define any HTML tag that should be allowed to be pasted. If this setting is not specified, allowed tags are determined by possible formats in the toolbar |undefined
| allowed.attributes | HTML attributes | | Array | Here you can define any HTML attributes that should be allowed to be pasted. If this setting is not specified, allowed attributes are determined by possible formats in the toolbar |undefined
| customButtons | Array of button description objects | | Array
It is possible to use this module by including it though a