Edit this page

Immutable Elements

The immutable feature enables you to add HTML elements that cannot be edited by the user.

Configuration

Enable and Add Immutable Elements

To define the immutable elements in the content area, set the contenteditable DOM attribute to false. To make the Editor prevent the user from editing this element, you also need to enable the immutables option.

Example
    <textarea id="editor">
        &lt;p&gt;A simple paragraph&lt;/p&gt;
        &lt;p contenteditable=&quot;false&quot;&gt;This paragraph cannot be edited&lt;/p&gt;
        &lt;p&gt;A simple paragraph&lt;/p&gt;
    </textarea>
    <script>
        $("#editor").kendoEditor({
            immutables: true
        });
    </script>

Serialize Immutables

The immutables.serialization option enables you to control the HTML representation of the immutable elements in the viewHtml dialog.

The immutables.serialization configuration option accepts the following parameters:

  • String—This plain string implements an opening and a closing tag of the representation you want to display in the viewHtml dialog.

    Example
        <textarea id="editor">
            &lt;p&gt;A simple paragraph&lt;/p&gt;
            &lt;p contenteditable=&quot;false&quot;&gt;This paragraph cannot be edited&lt;/p&gt;
            &lt;p&gt;A simple paragraph&lt;/p&gt;
        </textarea>
        <script>
            $("#editor").kendoEditor({
                tools: [
                    "bold", "italic", "underline", "justifyLeft", "justifyCenter", "justifyRight", "insertUnorderedList", "insertOrderedList", "indent", "outdent", "createLink", "unlink", "insertImage", "createTable", "viewHtml"
                ],
                immutables: {
                    serialization: "<div></div>"
                    }
                }
            });
        </script>
    
  • Kendo UI Template—In it, the immutable DOM element is data.

    Example
        <textarea id="editor">
            &lt;p&gt;A simple paragraph&lt;/p&gt;
            &lt;p contenteditable=&quot;false&quot;&gt;This paragraph cannot be edited&lt;/p&gt;
            &lt;p&gt;A simple paragraph&lt;/p&gt;
        </textarea>
        <script>
            $("#editor").kendoEditor({
                tools: [
                    "bold", "italic", "underline", "justifyLeft", "justifyCenter", "justifyRight", "insertUnorderedList", "insertOrderedList", "indent", "outdent", "createLink", "unlink", "insertImage", "createTable", "viewHtml"
                ],
                immutables: {
                    serialization: "<#= data.nodeName # data=\"immutable-element\"></#= data.nodeName #>"
                    }
                }
            });
        </script>
    
  • Function—This callback function exposes the immutable DOM element in the overload and is expected to return a string.

    Example
        <textarea id="editor">
            &lt;p&gt;A simple paragraph&lt;/p&gt;
            &lt;p contenteditable=&quot;false&quot;&gt;This paragraph cannot be edited&lt;/p&gt;
            &lt;p&gt;A simple paragraph&lt;/p&gt;
        </textarea>
        <script>
            $("#editor").kendoEditor({
                tools: [
                    "bold", "italic", "underline", "justifyLeft", "justifyCenter", "justifyRight", "insertUnorderedList", "insertOrderedList", "indent", "outdent", "createLink", "unlink", "insertImage", "createTable", "viewHtml"
                ],
                immutables: {
                    serialization: function(node) {
                        var tagName = node.tagName;
                        return "<" + tagName + ">" + "</" + tagName + ">";
                        }
                    }
                }
            });
        </script>
    

Deserialize Immutables

The immutables.deserialization does the opposite of the immutables.serialization one—it takes the HTML representation from the viewHtml dialog and alters the immutable DOM element based on the logic implemented in the callback function.

The following example demonstrates how to use the immutables.serialization and immutables.deserialization options to expose the CSS text-align property in the viewHtml dialog so that the user is able to change it from the HTML code.

Example
    <textarea id="editor">
        &lt;p&gt;A simple paragraph&lt;/p&gt;
        &lt;p contenteditable=&quot;false&quot; style=&quot;text-align:left;&quot; &gt;This paragraph cannot be edited&lt;/p&gt;
        &lt;p&gt;A simple paragraph&lt;/p&gt;
    </textarea>
    <script>
        $("#editor").kendoEditor({
            tools: [
                "bold", "italic", "underline", "justifyLeft", "justifyCenter", "justifyRight", "insertUnorderedList", "insertOrderedList", "indent", "outdent", "createLink", "unlink", "insertImage", "createTable", "viewHtml"
            ],
            serialization: "<immutable style='# if(data.style.textAlign){#text-align: #=data.style.textAlign##}#'></immutable>",
            deserialization: function (node, immutable) {
                    immutable.style.textAlign = node.style.textAlign;
            }
        });
    </script>

Apply Default Decoration to Immutables

To decorate all contenteditable="false" elements and improve user experience (UX), use a CSS rule.

If you use the classic mode, add the CSS rule to an external CSS file adjoined to the stylesheet collection of the Editor.

If you use the inline mode, place the CSS rule on the page as demonstrated in the following example.

Example
    <style>
    .k-editor [contenteditable='false']{
        opacity: 0.5;
    }
    </style>
    <textarea id="editor">
        &lt;p&gt;A simple paragraph&lt;/p&gt;
        &lt;p contenteditable=&quot;false&quot;&gt;This paragraph cannot be edited&lt;/p&gt;
        &lt;p&gt;A simple paragraph&lt;/p&gt;
    </textarea>
    <script>
        $("#editor").kendoEditor({
            immutables: true
        });
    </script>

See Also

Other articles on the Kendo UI Editor:

For how-to examples on the Kendo UI Editor widget, browse its How To documentation folder.

Is this article helpful? Yes / No
Thank you for your feedback!

Give article feedback

Tell us how we can improve this article

close
Dummy