1

I have a method overriding the Umbraco.Core.Services.ContentService.Saving. It's just adding some classes to images and wrapping the image in a div.

But now I started enabling some Macros to be added in the rich-text editor and the Macros disappear after saving.

After much digging I found out that if I override the saving method some how the Macros get removes.

If anyone else had to deal with this I would love some insight on what is going on.

Here is how I override it:

Umbraco.Core.Services.ContentService.Saving += OverrideSave.ContentService_Saving;

And here is my method (minus the middle boring part).

public class OverrideSave
{
    public static void ContentService_Saving(Umbraco.Core.Services.IContentService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IContent> e)
    {
        foreach (var c in e.SavedEntities)
        {
            var list = c.PropertyTypes.Where(x => x.PropertyEditorAlias == "Umbraco.TinyMCEv3").ToList();

            if (list.Count > 0)
            {
                List<Property> propList = new List<Property>();

                foreach (var i in list)
                {
                    propList.Add(c.Properties.Where(x => x.Alias == i.Alias).FirstOrDefault());
                }

                if (propList.Count > 0)
                {
                    foreach (var t in propList)
                    {
                        //string html = t.Value.ToString();
                        //string outputHtml = html;

                        //HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                        //doc.LoadHtml((string)t.Value);
                        var parser = new HtmlParser();
                        var doc = parser.Parse((string)t.Value);

                        //if (doc.DocumentNode.SelectNodes("//img/@src") != null)
                        if (doc.DocumentElement.GetElementsByTagName("img") != null)
                        {
                            for (int i = 0; i < doc.DocumentElement.QuerySelectorAll("img").Count(); i++)
                            {
                                //add S3  URL to images
                                string s3Url = ConfigurationManager.AppSettings["cdnDomain"];
                                if (!doc.DocumentElement.QuerySelectorAll("img")[i].Attributes["src"].Value.Contains(s3Url))
                                {
                                    doc.DocumentElement.QuerySelectorAll("img")[i].SetAttribute("src", s3Url + doc.DocumentElement.QuerySelectorAll("img")[i].Attributes["src"].Value);
                                }

                                var wrapperNode = doc.CreateElement("div");

                                //add description paragraph
                                if (!string.IsNullOrEmpty(doc.DocumentElement.QuerySelectorAll("img")[i].Attributes["alt"].Value))
                                {
                                    wrapperNode.InnerHtml = doc.DocumentElement.QuerySelectorAll("img")[i].OuterHtml + "<p>" + doc.DocumentElement.QuerySelectorAll("img")[i].Attributes["alt"].Value + "</p>";
                                }
                                else
                                {
                                    wrapperNode.InnerHtml = doc.DocumentElement.QuerySelectorAll("img")[i].OuterHtml;
                                }

                                //add image width to wrapper div
                                if (!string.IsNullOrEmpty(doc.DocumentElement.QuerySelectorAll("img")[i].Attributes["style"].Value))
                                {
                                    string style = doc.DocumentElement.QuerySelectorAll("img")[i].Attributes["style"].Value;
                                    string pattern = @"(width:\s*.*?;)";
                                    string width = Regex.Match(style, pattern, RegexOptions.IgnoreCase).Groups[1].Value;
                                    if (!string.IsNullOrEmpty(width))
                                    {
                                        wrapperNode.SetAttribute("style", width);
                                    }
                                }

                                //add appropriate classes to div wrapper
                                if (doc.DocumentElement.QuerySelectorAll("img")[i].Attributes["style"].Value.Contains("float: left;"))
                                {
                                    wrapperNode.SetAttribute("class", "image-with-caption align-left");
                                }
                                else if (doc.DocumentElement.QuerySelectorAll("img")[i].Attributes["style"].Value.Contains("float: right;"))
                                {
                                    wrapperNode.SetAttribute("class", "image-with-caption align-right");
                                }
                                else
                                {
                                    wrapperNode.SetAttribute("class", "image-with-caption");
                                }

                                //add new node to html - check to make sure divs are not doubled
                                if (doc.DocumentElement.QuerySelectorAll("img")[i].ParentElement.TagName == "div"
                                    && !string.IsNullOrEmpty(doc.DocumentElement.QuerySelectorAll("img")[i].ParentElement.GetAttribute("class")) // Check to make sure a class attribute exists so the next part doesn't fail
                                    && doc.DocumentElement.QuerySelectorAll("img")[i].ParentElement.Attributes["class"].Value.Contains("image-with-caption")) // Check to see if the parent node is infact the one we want.
                                {
                                    doc.DocumentElement.QuerySelectorAll("img")[i].ParentElement.ParentElement.ReplaceChild(wrapperNode, doc.DocumentElement.QuerySelectorAll("img")[i].ParentElement);
                                }
                                else
                                {
                                    doc.DocumentElement.QuerySelectorAll("img")[i].ParentElement.ReplaceChild(wrapperNode, doc.DocumentElement.QuerySelectorAll("img")[i]);
                                }
                            }                                
                        }
                        t.Value = (object)doc.DocumentElement.OuterHtml;
                    }
                }
            }
        }
    }
}
Ricardo
  • 41
  • 4

1 Answers1

0

Actually, the code you've omitted might be important. The macro is stored as a non-standard bit of HTML markup, it's possible that whatever you're using to parse the markup is stripping out the macro markup. Try stepping through your code and checking the value of the HTML at each stage to see if that's the issue.

Tim
  • 4,217
  • 1
  • 15
  • 21
  • I updated my post to show the full code. I also have stepped through the code and the value I get at the beginning of the code for the Macro remains the same through out. I was using HAP before and it was lower casing the Macro Tag so I switched to Angle Sharp and now it is exactly the same coming in as it is coming out. I also tried to just get the variable and pass it along again (with no edits) and it does the same thing. It seems that when I override it it just skips a step on the process. – Ricardo Dec 15 '15 at 15:56
  • It's possible the HtmlAgilityPack is stripping the macro. Try commenting that out and just making a manual tweak to the value of the field (adding some text to the end for example), and see if the Macro is still there. – Tim Dec 15 '15 at 16:00
  • Ignore that last comment. That's rather odd, might be worth logging an issue with repro steps on issues.umbraco.org. – Tim Dec 15 '15 at 16:02
  • OK, I've run a quick test, on 7.3.4, doing a very basic change to a field (just adding some text to the end of the string), and it keeps the macro. Which version of Umbraco are you on? – Tim Dec 15 '15 at 16:20
  • I am on Umbraco 7.1.4 – Ricardo Dec 15 '15 at 17:08
  • I actually thought of upgrading but my site uses Contour and Contour is not supported on 7.2.X – Ricardo Dec 15 '15 at 17:09
  • I have upgraded to Umbraco 7.3.4 and I am still having the same issue. The Macro markup goes in like this: `
    Macro alias: CostCalculator
    ` And comes out after save like this: ``
    – Ricardo Jan 12 '16 at 21:39