1

我有一个覆盖 Umbraco.Core.Services.ContentService.Saving 的方法。它只是向图像添加一些类并将图像包装在 div 中。

但是现在我开始在富文本编辑器中添加一些宏,并且宏在保存后会消失。

经过大量挖掘,我发现如果我覆盖保存方法,那么宏会如何被删除。

如果其他人不得不处理这个问题,我会喜欢对正在发生的事情有所了解。

这是我覆盖它的方法:

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

这是我的方法(减去中间无聊的部分)。

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;
                    }
                }
            }
        }
    }
}
4

1 回答 1

0

实际上,您省略的代码可能很重要。宏存储为 HTML 标记的非标准位,您用于解析标记的任何内容都可能会剥离宏标记。尝试单步执行您的代码并在每个阶段检查 HTML 的值,看看是否是问题所在。

于 2015-12-15T15:52:09.320 回答