/// <summary> /// 将文本解析为产生同样文本流效果的 HTML 片段 /// </summary> /// <param name="text">要解析的文本</param> /// <param name="manager">HTML 片段管理器</param> /// <returns>HTML 片段</returns> public static IHtmlFragment ParseText(string text, IHtmlFragmentManager manager) { if (text == null) { throw new ArgumentNullException("text"); } if (manager == null) { throw new ArgumentNullException("manager"); } var parsed = HtmlEncoding.HtmlEncode(text); parsed = parsed.Replace(" ", " "); if (parsed.EndsWith(" ")) { parsed = parsed.Substring(0, parsed.Length - 1) + " ";//如果末尾多出一个空格,则替换为 } parsed = parsed.Replace("\r\n", "\n").Replace("\r", "\n"); parsed = parsed.Replace("\n", "<br />"); return(manager.ParseFragment(parsed)); }
/// <summary> /// 尝试生成元素开始标签的HTML形式 /// </summary> /// <param name="element">要生成HTML的元素</param> /// <param name="selfClosed">指示是否应产生自结束符号</param> /// <returns></returns> public static string GenerateTagHtml(IHtmlElement element, bool selfClosed) { if (element == null) { throw new ArgumentNullException("element"); } var builder = new StringBuilder(20); builder.Append("<"); builder.Append(element.Name); foreach (var attribute in element.Attributes()) { builder.Append(" "); builder.Append(attribute.Name); if (attribute.AttributeValue != null) { var specification = element.Document.HtmlSpecification; if ((specification.IsUriValue(attribute) || specification.IsScriptValue(attribute)) && !attribute.AttributeValue.Contains('"')) { builder.Append("=\"").Append(attribute.AttributeValue).Append("\""); } else { builder.Append("=\"").Append(HtmlEncoding.HtmlAttributeEncode(attribute.AttributeValue)).Append("\""); } } } if (selfClosed) { builder.Append(" /"); } builder.Append(">"); return(builder.ToString()); }
/// <summary> /// 使用指定文本替换元素内容(警告,此方法会清除元素所有内容) /// </summary> /// <param name="element">要替换内容的元素</param> /// <param name="text">文本内容</param> /// <param name="encodeWhiteSpaces">是否编码空白字符</param> public static T InnerText <T>(this T element, string text, bool encodeWhiteSpaces) where T : IHtmlElement { if (element == null) { throw new ArgumentNullException("element"); } var modifier = EnsureModifiable(element); var mode = element.ElementTextMode(); lock (element.SyncRoot) { ClearNodes(element); if (string.IsNullOrEmpty(text))//对于空输入,则只需要清空元素即可 { return(element); } if (mode == TextMode.CData) { modifier.AddTextNode(element, text); } else if (mode == TextMode.Preformated || !encodeWhiteSpaces) { modifier.AddTextNode(element, HtmlEncoding.HtmlEncode(text)); } else if (mode == TextMode.Normal) { ParseText(text, element.Document.FragmentManager).Into(element, 0); } else { throw new InvalidOperationException("元素不包含任何文本内容,无法设置 InnerText"); } } return(element); }
/// <summary> /// 尝试获取节点的文本表现形式,对于某些不支持文本表现形式的元素,将直接返回null /// </summary> /// <param name="node">要获取文本表现形式的节点</param> /// <returns></returns> public static string InnerText(this IHtmlNode node) { if (node == null) { throw new ArgumentNullException("node"); } var specification = node.Document.HtmlSpecification; var textNode = node as IHtmlTextNode; if (textNode != null) { var parent = textNode.Parent(); if (parent == null) { throw new InvalidOperationException(); } var textMode = specification.ElementTextMode(parent); if (textMode == TextMode.CData) { return(textNode.HtmlText); } else if (textMode == TextMode.Preformated) { return(HtmlEncoding.HtmlDecode(textNode.HtmlText)); } else if (textMode == TextMode.Normal) { return(HtmlEncoding.HtmlDecode(whitespaceRegex.Replace(textNode.HtmlText, " "))); } else { return(null); } } var commentNode = node as IHtmlComment; if (commentNode != null) { return(null); } var element = node as IHtmlElement; if (element != null) { if (element.Name.EqualsIgnoreCase("br")) { return(Environment.NewLine); } else if (specification.ElementTextMode(element) == TextMode.NonText) { return(null); } } var container = node as IHtmlContainer; if (container != null) { return(string.Join("", container.Nodes().Select(n => InnerText(n)).ToArray())); } throw new NotSupportedException(); }