Immutable Elements

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

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() configuration method.

Example
@(Html.Kendo().Editor()
    .Name("editor")
    .Immutables(true)
    .Value("<div contenteditable='false'>this is immutable</div><div>this is mutable</div>")
)

Serialize Immutables

The Serialization() method enables you to control the HTML representation of the immutable elements in the viewHtml dialog.

The immutables.serialization configuration method 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
    @(Html.Kendo().Editor()
        .Name("editor")
        .Immutables(i => i
            .Serialization("<div></div>")
        )
        .Value("<div contenteditable='false'>this is immutable</div><div>this is mutable</div>")
    )
    
  • Kendo UI Template—In it, the immutable DOM element is data.

    Example
    (Html.Kendo().Editor()
        .Name("editor")
        .Immutables(i => i
            .Serialization("<#= data.nodeName # data=\"immutable-element\"></#= data.nodeName #>")
        )
        .Value("<div contenteditable='false'>this is immutable</div><div>this is mutable</div>")
    )
    
  • Function—This callback function exposes the immutable DOM element in the overload and is expected to return a string.

    Example
    (Html.Kendo().Editor()
        .Name("editor")
        .Immutables(i => i
            .SerializationHandler("immutablesSerializationHandler")
        )
        .Value("<div contenteditable='false'>this is immutable</div><div>this is mutable</div>")
    )
    
    <script>
        function immutablesSerializationHandler(node) {
            var tagName = node.tagName;
    
            return "<" + tagName + ">" + "</" + tagName + ">";
        }
    </script>
    

Deserialize Immutables

The Deserialization() does the opposite of the 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 Serialization() and 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
@(Html.Kendo().Editor()
    .Name("editor")
    .Immutables(i => i
        .Serialization("<immutable style='# if(data.style.textAlign){#text-align: #=data.style.textAlign##}#'></immutable>")
        .Deserialization("immutablesDeserializationHandler")
    )
    .Value("<div contenteditable='false'>this is immutable</div><div>this is mutable</div>")
)
<script>
    function immutablesDeserializationHandler(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>

@(Html.Kendo().Editor()
    .Name("editor")
    .Tag("div")
    .Immutables(true)
    .Value("<div contenteditable='false'>this is immutable</div><div>this is mutable</div>")
)

See Also

In this article
Not finding the help you need? Improve this article