/// <summary> /// 将元素重复指定次数 /// </summary> /// <param name="element">要重复的元素</param> /// <param name="count">要重复的次数</param> /// <returns>所产生的元素序列</returns> /// <exception cref="System.ArgumentOutOfRangeException">重复的次数小于 0</exception> /// <remarks> /// 若提供的 count 等于 0,则会从 DOM中 移除元素,若等于 1 则不做任何事情。 /// </remarks> public static IHtmlElement[] Repeat(this IHtmlElement element, int count) { if (count < 0) { throw new ArgumentOutOfRangeException("count"); } switch (count) { case 0: element.Remove(); return(new IHtmlElement[0]); case 1: return(new[] { element }); default: IHtmlElement[] result = new IHtmlElement[count]; result[0] = element; var container = element.Container; lock (element.Container) { var index = element.NodesIndexOfSelf(); for (int i = 1; i < count; i++) { result[i] = container.AddCopy(index + i, element); } } return(result); } }
public void BindValue(object value) { var parent = _targetElement.Parent(); if (parent == null) { throw new NotSupportedException(); } var text = (string)value; if (parent.ElementTextMode() == TextMode.Preformated) { parent.AddTextNode(_targetElement.NodesIndexOfSelf(), HtmlEncoding.HtmlEncode(text)); _targetElement.Remove(); } else { var encoded = HtmlEncoding.HtmlEncode(text); encoded = encoded.Replace(" ", " "); if (encoded.EndsWith(" ")) { encoded = encoded.Substring(0, encoded.Length - 1) + " ";//如果末尾多出一个空格,则替换为 } encoded = encoded.Replace("\r\n", "\n").Replace("\r", "\n"); encoded = encoded.Replace("\n", "<br />"); _targetElement.ReplaceWith(encoded); } }