Sanitize saved data
Some of the Tool's fields can contain any HTML code, for example, our Caption field with Inline Toolbar enabled. It's good to be sure that saved HTML content includes only allowed tags and attributes — only created by Inline Toolbar in our case.
Editor.js has a build-in Sanitizer module. There are two ways to use it.
Call sanitizer.clean()
API method for fields that can contain HTML on saving. This method accepts two arguments: string to sanitize and a config described rules for sanitizing.
In this example, we will clean every tag except b
, a
and i
.
There is a second way to use the sanitizer for our Tool. If you implement the sanitize
static getter with the sanitizer config, Editor.js will automatically clean your saved data.
Useful thing is that sanitizer config will be automatically extended by enabled Inline Tools tags.
Clean up manual sanitizing lines that we've added at the save
method in the previous code example. Then, add the sanitize
static getter.
We pass false
as config for the url
field because it can't contain HTML code. For the caption
field we pass the {}
-rule which means that field can contain HTML only made by Inline Toolbar.
To check how it works, try to use Inline Toolbar in the caption
field. Then add some unwanted tags, for example, div
manually via browser web-inspector to the caption content editable element.
Then press Save and take a look at the output. There should be only tags from Inline Toolbar. Divs should be sanitized.
In the next chapter we will learn how to provide a way for our Tool users to pass some own configuration for our Tool.