/// <summary> /// 设置属性值 /// </summary> /// <param name="element">要设置属性的元素</param> /// <param name="attributeName">属性名</param> /// <param name="evaluator">用于替换属性值的计算函数</param> /// <returns></returns> public static IHtmlElement SetAttribute(this IHtmlElement element, string attributeName, Func <string, string> evaluator) { if (element == null) { throw new ArgumentNullException("element"); } if (evaluator == null) { throw new ArgumentNullException("evaluator"); } var attribute = element.Attribute(attributeName); var value = attribute.Value(); if (value == null) { return(element); } value = evaluator(value); attribute.Remove(); element.AddAttribute(attributeName, value); return(element); }
/// <summary> /// 设置属性值 /// </summary> /// <param name="element">要设置属性值的元素</param> /// <param name="attributeName">属性名</param> /// <param name="value">属性值</param> /// <param name="attribute">设置好的属性</param> /// <returns>设置了属性的元素</returns> public static IHtmlElement SetAttribute(this IHtmlElement element, string attributeName, string value, out IHtmlAttribute attribute) { if (element == null) { throw new ArgumentNullException("element"); } if (attributeName == null) { throw new ArgumentNullException("attributeName"); } lock (element.SyncRoot) { var _attribute = element.Attribute(attributeName); if (_attribute != null) { _attribute.Remove(); } attribute = element.AddAttribute(attributeName, value); } return(element); }
public void Inject(HtmlNodeElement element) { IHtmlElement input = element.Children.FirstOrDefault(x => x.TagName == "input"); if (input != null) { HtmlParser parser = new HtmlParser(); if (input.GetAttribute("name")?.Value != null) { IHtmlElement validationMessageTag; using (var writer = new System.IO.StringWriter()) { _htmlHelper.ValidationMessage(input.GetAttribute("name").Value, _options?.ValidationMessageAttributes?.ToDictionary()).WriteTo(writer, HtmlEncoder.Default); validationMessageTag = parser.ParseString(writer.ToString()).FirstOrDefault(); } using (var writer = new System.IO.StringWriter()) { _htmlHelper.TextBox(input.GetAttribute("name").Value).WriteTo(writer, HtmlEncoder.Default); IHtmlElement textBox = parser.ParseString(writer.ToString()).FirstOrDefault(); foreach (var attr in textBox.Attributes) { if (attr.Name.StartsWith("data")) { input.AddAttribute(attr); } } } element.AddAfter(input.UId, validationMessageTag); } } int index = 0; while (element.Children.Count > index) { if (element.Children[index] is IHtmlNodeElement) { Inject((HtmlNodeElement)element.Children[index]); } index++; } }
private HtmlPairTagsElement GetFormGroup(PropertyInfo propInfo) { IHtmlElement input = GetProperInput(propInfo); HtmlNodeElement inputWrapper = new HtmlNodeElement("div"); if (propInfo.GetCustomAttribute <HtmlInputAttribute>() != null) { input.AddAttribute(propInfo.GetCustomAttribute <HtmlInputAttribute>().Name); } inputWrapper.Append(input); // TODO Add validation element if (_options.InputWrapperAttributes != null) { inputWrapper.AddRangeAttributes(_options.InputWrapperAttributes); } HtmlNodeElement label = GetLabel(propInfo); HtmlNodeElement formGroup = new HtmlNodeElement("div"); formGroup.Append(label); if (_options.FormGroupAttributes != null) { formGroup.AddRangeAttributes(_options.FormGroupAttributes); } formGroup.Append(inputWrapper); if (_options.FormGroupTemplateFunc != null) { formGroup = _options.FormGroupTemplateFunc(formGroup); } return(formGroup); }
/// <summary> /// 设置属性值 /// </summary> /// <param name="element">要设置属性值的元素</param> /// <param name="attributeName">属性名</param> /// <param name="value">属性值</param> /// <returns>设置了属性的元素</returns> public static IHtmlElement SetAttribute(this IHtmlElement element, string attributeName, string value) { if (element == null) { throw new ArgumentNullException("element"); } if (attributeName == null) { throw new ArgumentNullException("attributeName"); } var attribute = element.Attribute(attributeName); if (attribute != null) { attribute.Remove(); } element.AddAttribute(attributeName, value); return(element); }