Ejemplo n.º 1
0
 private static void Entitize(HtmlNodeCollection collection)
 {
     foreach (HtmlNode current in (IEnumerable <HtmlNode>)collection)
     {
         if (current.HasAttributes)
         {
             HtmlEntity.Entitize(current.Attributes);
         }
         if (current.HasChildNodes)
         {
             HtmlEntity.Entitize(current.ChildNodes);
         }
         else
         {
             if (current.NodeType == HtmlNodeType.Text)
             {
                 ((HtmlTextNode)current).Text = HtmlEntity.Entitize(((HtmlTextNode)current).Text, true, true);
             }
         }
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the unescaped inner text of a node, optionally with new lines inserted for line-breaking elements such as "br" and "p".
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="multiline">True to replace line-breaking elements with new lines; otherwise false to collapse to a single line.</param>
        /// <param name="newLine">The new line string to use, defaults to <see cref="System.Environment.NewLine"/>, only applicable if <paramref name="multiline"/> is true.</param>
        /// <param name="lineBreakTags">The element tag names that cause a new line to be inserted, defaults to "br" and "p" if none are specified, only applicable if <paramref name="multiline"/> is true.</param>
        /// <returns>The unescaped inner text of a node, optionally with new lines inserted for line-breaking elements.</returns>
        public static string GetInnerText(this HtmlNode node, bool multiline = false, string newLine = null, params string[] lineBreakTags)
        {
            if (multiline)
            {
                if (lineBreakTags == null || !lineBreakTags.Any())
                {
                    lineBreakTags = _defaultLineBreakTags;
                }
                var n = HtmlNode.CreateNode(node.OuterHtml);
                foreach (var lineBreakTag in lineBreakTags)
                {
                    foreach (var lineBreakNode in n.SelectSafeNodes($"//{lineBreakTag.ToLowerInvariant()}").ToList())
                    {
                        lineBreakNode.ParentNode.ReplaceChild(HtmlNode.CreateNode(_newLineToken + (lineBreakNode.InnerText ?? "") + _newLineToken), lineBreakNode);
                    }
                }
                node = n;
            }
            var text = HtmlEntity.DeEntitize(node.InnerText).Replace("\r", "").Replace("\n", " ").Replace(_newLineToken, newLine ?? Environment.NewLine).CollapseAndTrim(multiline, newLine);

            return(text);
        }
Ejemplo n.º 3
0
 public static string Entitize(string text, bool useNames)
 {
     return(HtmlEntity.Entitize(text, useNames, false));
 }
Ejemplo n.º 4
0
 public static string Entitize(string text)
 {
     return(HtmlEntity.Entitize(text, true));
 }