当前位置: 首页>>代码示例>>C#>>正文


C# IDomObject.Render方法代码示例

本文整理汇总了C#中IDomObject.Render方法的典型用法代码示例。如果您正苦于以下问题:C# IDomObject.Render方法的具体用法?C# IDomObject.Render怎么用?C# IDomObject.Render使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IDomObject的用法示例。


在下文中一共展示了IDomObject.Render方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CompareNodes

        bool CompareNodes(IDomObject n, IDomObject compareToNode, string path)
        {

            var outerHtml = n.Render();
            var compareToOuterHtml = (compareToNode == null) ? "" : compareToNode.Render();

            var getAttributes = new Func<IDomObject, string>(f => (f.Attributes == null) ? "" : string.Join(",", f.Attributes.Select(a => a.Key + "=" + a.Value)));

            var thisNodeEquals = (compareToNode != null
                && n.NodeName == compareToNode.NodeName
                && n.NodeType == compareToNode.NodeType
                && n.NodeValue == compareToNode.NodeValue)
                && getAttributes(n) == getAttributes(compareToNode);


            var nodeHtmlEquals = (compareToNode != null && outerHtml == compareToOuterHtml);

            var pathAttributes = "";
            if (n.Attributes != null)
            {
                var nSelectorAttributes = n.Attributes.Where(a => a.Key == "id" || a.Key == "class");
                if (nSelectorAttributes.Any())
                {
                    var idAttribute = nSelectorAttributes.Where(sa => sa.Key == "id");
                    var classAttribute = nSelectorAttributes.Where(sa => sa.Key == "class");

                    if (idAttribute.Any()) pathAttributes = "#" + idAttribute.First().Value;
                    else
                        if (classAttribute.Any()) pathAttributes = "." + classAttribute.First().Value;

                }
            }
            path = path + ">" + n.NodeName + pathAttributes + "(" + n.Index.ToString() + ")";

            if (thisNodeEquals)
            {
                if (n.HasChildren && compareToNode != null)
                {
                    return TraverseNode(n, compareToNode, path);
                }
                return true;

            }
            else
            {

                var p = n;
                var pp = "";
                var breakNow = false;

                var diff = new Diff();

                do
                {

                    if (p != null && p.Attributes != null)
                    {
                        var idAttr = p.Attributes.Where(a => a.Key == "id");
                        if (idAttr.Any())
                        {
                            pp = p.NodeName + "#" + idAttr.First().Value + " " + pp;
                            breakNow = true;
                        }
                    }
                    var hasClassAttribute = false;
                    if (!breakNow)
                    {
                        if (p != null && p.Attributes != null)
                        {
                            var idCssClass = p.Attributes.Where(a => a.Key == "class");
                            if (idCssClass.Any())
                            {
                                hasClassAttribute = true;
                                pp = p.NodeName.ToLower() + "." + idCssClass.First().Value.Replace(" ", ".") + ((pp!="")?" > " + pp:"");
                            }
                        }
                    }
                    if (!breakNow && !hasClassAttribute)
                    {
                        if (p.NodeType == NodeType.TEXT_NODE) 
                            diff.isText = true;
                        if (p.NodeType == NodeType.ELEMENT_NODE)
                        {
                            var indexSelector = "";
                            if (p.ParentNode.ChildNodes.Count(t=>t.NodeName==p.NodeName)> 1) indexSelector = ":nth-child(" + (p.Index+1).ToString() + ")";
                            pp = p.NodeName.ToLower() + indexSelector + ((pp != "") ? " > " + pp : ""); //(" + p.Index.ToString() + ")
                        }
                    }

                    p = p.ParentNode;
                }
                while (!(p == null || breakNow));

                diff.selector = pp;
                diff.compareSourceHtml = outerHtml;
                diff.compareWithHtml = compareToOuterHtml;
                Diffs.Add(diff);

                return false;

//.........这里部分代码省略.........
开发者ID:joeriks,项目名称:DryHtml,代码行数:101,代码来源:HtmlComparer.cs


注:本文中的IDomObject.Render方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。