Friday 13 January 2017

Validation error on adding image in Rich-Text field from Experience editor


Validation error on adding image in Rich-Text field from Experience editor 

When we add an image in Rich-Text field through Experience Editor it throws validation error in (Sitecore 8.1 update-1(151207)).






Steps to reproduce:

Add an image in Rich-Text field from Experience Editor

As soon as we save the changes a validation error is thrown in experience editor

In Experience Editor the <img> tag created is,

<img alt="XXX" src="-/media/XXX.ashx?h=111&amp;w=539&amp;hash=CAF8436CA168BC18D2FD5D95597E874E66EDD1DF" style="width: 539px; height: 111px;" />

Open Content Editor

Click on Edit Html -> Click Fix

The Original HTML and Corrected HTML show no difference but if you see the <img> tag / is removed.

<img alt="XXX" src="-/media/XXX.ashx?h=111&amp;w=539&amp;hash=CAF8436CA168BC18D2FD5D95597E874E66EDD1DF" style="width: 539px; height: 111px;" >

/> is removed

After accepting the above changes, it doesn't throw any error in content editor as well as validation error from experience editor is gone.

Solution:

We raised this with Sitecore Support and they reported it as a Bug in Sitecore 8.1 update-1(151207) Thanks to Sitecore support as they provided a patch to resolve this issue.

So the problem appears while decoding the HTML rich text value in System.Net.WebUtility.HtmlDecode method. To let HtmlDecode generate valid output for rich text field apply following patch provided by Sitecore:

1) Backup your \Website\sitecore\shell\client\Sitecore\ExperienceEditor\ExperienceEditor.js

2) Find following code in mentioned ExperienceEditor.js:

encodeHtml: function (htmlSource) {
    htmlSource = htmlSource.replace(/\\/g, '\\\\').replace(/\"/g, "\\\"");
    var encodedHtml = encodeURIComponent(htmlSource);
    return encodedHtml;
},

and replace it with:

encodeHtml: function (htmlSource) {
    htmlSource = htmlSource.replace(/\\/g, '\\\\').replace(/\"/g, "\\\"").replace(/&amp;/g, "&amp;amp;"); // fix 102239
    var encodedHtml = encodeURIComponent(htmlSource);
    return encodedHtml;
},

3) Clear your browser cache. That’s it.

Check by adding image from Experience Editor it wouldn't throw any validation error.

Happy Coding 😊.



Wednesday 17 August 2016

Sitecore - Custom Code Snippets in Rich-Text Editor field

Add Custom Code Snippets in Rich-Text Editor field in Sitecore


Hello Devs,

If you want to have Html Formatting for the content displayed from Rich-Text Editor field in Sitecore than this blog post is for you.

How to add Custom Code Snippet in Rich-Text Editor?


Switch to Core Database in Sitecore.
     Navigate to the below mentioned path in Sitecore Path:/sitecore/system/Settings/Html Editor Profiles/Rich Text Full/Snippets



 
Create a Custom Code Snippet Item in Sitecore as per your requirement from below Template.    
Sitecore Path: /sitecore/templates/System/Html Editor Profiles/Html Editor Snippet

Code Snippet Item would have two fields :
     Header (Name of the Snippet)
     Value (This is the field where you specify predefined block of HTML code)

     In my case I’m creating a PDF Icon Snippet that would display a PDF ICON for the Link.Below is the example of the Snippet.



Header: PDF Icon
Value : <p class="pdf-icon"><a href="#">Add Link</a></p>
I have created a css class named pdf-icon which would display 
the PDF Icon.

.pdf-icon
{
    background: url('/-/media/Test/Images/Icons/icon_pdf')center left no-repeat;
    padding-left: 30px;

}

How to Use the Custom Snippet in the Rich-Text Editor Field?


Switch to the Master Database and navigate to the Rich-Text editor field where you want to use the custom code snippet.
Click on the Insert Code Snippet Icon and click on the PDF Icon snippet.
You can see the Add link text is added.


Right-click the Add Link Text , select Properties
Select the PDF File and provide the Link Text as shown below.

Save and Publish the Item.



That’s it, you are done!!! Just view in Front-End whether your Custom Code Snippet is applied.