【问题标题】:Umbraco - On Saving Override removes Macros from contentUmbraco - On Saving Override 从内容中删除宏
【发布时间】:2015-12-15 15:30:27
【问题描述】:

我有一个覆盖 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;
                    }
                }
            }
        }
    }
}

【问题讨论】:

    标签: umbraco umbraco7 umbraco-mvc umbraco-macros


    【解决方案1】:

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

    【讨论】:

    • 我更新了我的帖子以显示完整的代码。我还单步执行了代码,并且我在宏代码开头得到的值始终保持不变。我之前使用的是 HAP,它是 Macro Tag 的小写字母,所以我切换到 Angle Sharp,现在它的输入和输出完全相同。我还尝试获取变量并再次传递它(没有编辑),它做同样的事情。似乎当我覆盖它时,它只是跳过了该过程的一个步骤。
    • HtmlAgilityPack 可能正在剥离宏。尝试将其注释掉,然后手动调整字段的值(例如,在末尾添加一些文本),看看宏是否仍然存在。
    • 忽略最后一条评论。这很奇怪,可能值得在issues.umbraco.org上记录一个重现步骤的问题。
    • 好的,我在 7.3.4 上进行了快速测试,对字段进行了非常基本的更改(只是在字符串末尾添加了一些文本),并保留了宏。您使用的是哪个版本的 Umbraco?
    • 我在 Umbraco 7.1.4
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    相关资源
    最近更新 更多