Handling Image Dimensions in the Richtext Editor

Something odd happens when an image is added in the rich text editor. Initially, when the image is inserted, the RTE places the width and height properties correctly. However, when we change the properties such as adding a class, the dimensions are now inline styled. Inline styling dimensions isn’t responsive friendly.

Clients should not be expected to edit the HTML. So how do we resolve this issue?

When the client is making changes in the RTE we can fire off code to alter the HTML when the RTE is closed. We can accomplish this by utilizing the saveRichTextContent pipeline.

For content items that haven’t been modified after the new pipeline has been added, we can create a new renderField pipeline.

First we create the code to cycle through the images in the HTML and modify the inline styles.

using System;
using System.Linq;
using HtmlAgilityPack;

namespace Company.Library.Pipelines.RichTextContent
{
    public static class RichText
    {
        public static string Content(string content)
        {
            var doc = new HtmlDocument();
            doc.LoadHtml(content);

            if (!doc.DocumentNode.Descendants("img").Any())
                return content;

            foreach (var img in doc.DocumentNode.Descendants("img"))
            {
                var imgAttr = img.Attributes["style"];

                if (imgAttr == null)
                    continue;

                var alteredStyles = string.Empty;
                var inlineStyleList = imgAttr.Value.Split(';');
                var removeImageDimensions = Settings.GetBoolSetting("RTE.Images.RemoveImageDimensions", false);

                foreach (var style in inlineStyleList)
                {
                    var attrList = style.Split(':');

                    if (!attrList.Count().Equals(2))
                        continue;

                    var property = attrList[0].Trim();
                    var val = attrList[1].Trim();

                    if ((property.Equals("width", StringComparison.InvariantCultureIgnoreCase) ||
                        property.Equals("height", StringComparison.InvariantCultureIgnoreCase)) && !removeImageDimensions)
                        img.Attributes.Add(property, val);
                    else
                        alteredStyles += string.Format("{0}:{1}; ", property, val);
                }

                imgAttr.Remove();

                if (!string.IsNullOrWhiteSpace(alteredStyles))
                    img.Attributes.Add("style", alteredStyles);
            }

            return doc.DocumentNode.OuterHtml;
        }
    }
}

saveRichTextContent Pipeline

using Sitecore.Shell.Controls.RichTextEditor.Pipelines.SaveRichTextContent;

namespace Company.Library.Pipelines.RichTextContent
{
    public class Save
    {
        public void Process(SaveRichTextContentArgs args)
        {
            args.Content = RichText.Content(args.Content);
        }
    }
}
Before:
<img style="width: 200px; height: 200px; border: solid 3px #000;" alt="" src="/someimage.png" />
After:
<img width="200" height="200" style="border: solid 3px #000;" alt="" src="/someimage.png" />

Config Settings

Create a custom config file in the App_Config/Include folder if you haven’t done so and add the following:

  
    	 	 
   	 	 
      	 	 
          	 	 
      	 	 
      	 	 
         	 	 
             	 	 
         	 	 
      	 	 
   	 	 


Video Demo


Do you enjoy my oddly themed blogs and wish you had access to even more of me and my ideas? Good news, you’re in luck!

If 140 characters is your thing, follow me on Twitter.


If you hate reading and watching Sitecore videos entertains you, head over to my YouTube channel! Sometimes I entertain, sometime I provide useful Sitecore information and sometimes I can do both in the same video.


I can also be found hanging out on the Sitecore channels on Slack, I like it, although it occasionally triggers AOL chat room flashbacks from the olden days.


You can also find me adding content on LinkedIn and on Reddit.