public void Create_ReturnTagAsHtmlString_WhenTagHaveTextBetweenChildren() { // Arrange htmlBuilder = new HtmlBuilder(); HtmlNodeElement htmlTag = new HtmlNodeElement("div"); HtmlNodeElement span = new HtmlNodeElement("span"); htmlTag.Children.Add(span); htmlTag.Text("Text", span.UId); HtmlNodeElement span1 = new HtmlNodeElement("span"); htmlTag.Children.Add(span1); htmlTag.Text("Text1", span1.UId); HtmlNodeElement span2 = new HtmlNodeElement("span"); htmlTag.Children.Add(span2); htmlTag.Text("Text2", span2.UId); // Act IHtmlContent result = htmlBuilder.CreateHtmlContent(htmlTag); // Assert Assert.Equal("<div><span></span>Text<span></span>Text1<span></span>Text2</div>", result.ToString()); }
public void Get_ReturnElement_WhenCollectionIsNotEmptyAndAndElementExist() { // Arrange var searchedElement = new HtmlNodeElement("div") { Children = new HtmlElementsCollection { new HtmlNodeElement("span") } }; htmlElementsCollection = new HtmlElementsCollection() { searchedElement, new HtmlNodeElement("div") { Children = new HtmlElementsCollection { new HtmlNodeElement("span") } } }; // Act IHtmlElement result = htmlElementsCollection.Get(searchedElement.UId); // Assert Assert.NotNull(result); }
private HtmlNodeElement GetLabel(PropertyInfo propInfo) { DisplayAttribute displayAttr = propInfo.GetCustomAttribute <DisplayAttribute>(); bool IsRequired = propInfo.GetCustomAttribute <RequiredAttribute>() != null; string labelText = $"{(displayAttr != null ? displayAttr.Name : propInfo.Name)}{(IsRequired ? "*" : "")}:"; if (_options != null && _options.LabelTemplate != null) { labelText = String.Format(_options.LabelTemplate, labelText); } else if (_options != null && _options.LabelTemplateFunc != null) { labelText = _options.LabelTemplateFunc(labelText); } HtmlNodeElement label = new HtmlNodeElement("label", labelText); label.Attributes = new IHtmlAttribute[] { new HtmlAttribute("for", propInfo.Name) }; if (_options.LabelAttributes != null) { label.AddRangeAttributes(_options.LabelAttributes); } return(label); }
internal HtmlNodeElement GenerateFormElement() { HtmlNodeElement formContent = new HtmlNodeElement("div"); formContent.Children = GetHtmlFormGroups(); HtmlNodeElement formFooter = new HtmlNodeElement("div"); formFooter.Append(GetFormButtons()); if (_options.FormFooterAttributes != null) { formFooter.AddRangeAttributes(_options.FormFooterAttributes); } if (_options != null && _options.FormFooterTemplateFunc != null) { formFooter = _options.FormFooterTemplateFunc(formFooter); } HtmlNodeElement form = new HtmlNodeElement("form"); form.AddAttributeValue("action", _options.Action); form.Attributes = _options?.FormAttributes; form.Append(formContent); form.Append(formFooter); return(form); }
public override IHtmlElement Create(HtmlAttributesCollection attributes, string html, int startContentIndex, int endContentIndex, IHtmlParserManager htmlParserManager) { HtmlNodeElement element = (HtmlNodeElement)CreateInstance(); // add children and texts if (endContentIndex - startContentIndex > 0) { element.Children = htmlParserManager.ParseElements(startContentIndex, endContentIndex); Dictionary <int, string> texts = htmlParserManager.ParseText(startContentIndex, endContentIndex); foreach (var text in texts) { string decodedText = HttpUtility.HtmlDecode(text.Value); string value = text.Value; if (Regex.IsMatch(decodedText, HtmlRegexParserManager.commentRegex)) { value = decodedText; } if (text.Key >= 0) { element.Text(new HtmlString(value), element.Children[text.Key].UId); } else { element.Text(new HtmlString(value)); } } } element.Attributes = attributes; return(element); }
private HtmlNodeElement GetFormButtons() { HtmlSelfClosingTagElement submit = new HtmlSelfClosingTagElement("input"); submit.AddAttribute(new HtmlTypeAttribute("submit")); HtmlSelfClosingTagElement reset = new HtmlSelfClosingTagElement("input"); reset.AddAttribute(new HtmlTypeAttribute("reset")); if (_options.SubmitButtonAttributes != null) { submit.AddRangeAttributes(_options.SubmitButtonAttributes); } if (_options.ResetButtonAttributes != null) { reset.AddRangeAttributes(_options.ResetButtonAttributes); } HtmlNodeElement wrapper = new HtmlNodeElement("span"); wrapper.Append(submit); wrapper.Append(reset); return(wrapper); }
private HtmlNodeElement GeRadioButtonsGroup(string name, IEnumerable <SelectListItem> values, string value, bool isMultiple = false) { HtmlNodeElement wrapper = new HtmlNodeElement("div"); if (_options.RadioButtonsWrapperAttributes != null) { wrapper.AddRangeAttributes(_options.RadioButtonsWrapperAttributes); } foreach (var item in values) { HtmlNodeElement label = new HtmlNodeElement("label"); label.Text(item.Text); if (_options.RadioButtonsGroupLabelAttributes != null) { label.AddRangeAttributes(_options.RadioButtonsGroupLabelAttributes); } var radioInput = new HtmlSelfClosingTagElement("input") { Attributes = new IHtmlAttribute[] { new HtmlAttribute("name", name), new HtmlAttribute("value", item.Value), new HtmlAttribute("type", isMultiple ? "checkbox" : "radio") } }; if (_options.RadioButtonsGroupInputAttributes != null) { radioInput.AddRangeAttributes(_options.RadioButtonsGroupInputAttributes); } if (item.Selected || (value != null && item.Value == value.ToString())) { radioInput.AddAttribute(new HtmlAttribute("checked")); } label.Append(radioInput); var div = new HtmlNodeElement("div"); div.Append(label); if (_options.RadioButtonsGroupWrapperAttributes != null) { div.AddRangeAttributes(_options.RadioButtonsGroupWrapperAttributes); } wrapper.Append(div); } return(wrapper); }
public void ParseString_ReturnElementWithText_WhenTagHaveTextWhitoutChildren(string html) { // Arrange // Act HtmlNodeElement element = (HtmlNodeElement)htmlParser.ParseString(html).FirstOrDefault(); // Assert Assert.NotEmpty(element.Texts()); Assert.Equal("Text", element.Texts()[0].HtmlString.Value); Assert.Null(element.Texts()[0].AfterElementUId); }
public void ParseString_ReturnElementWithText_WhenHaveTextAfterChildren(string html) { // Arrange // Act HtmlNodeElement element = (HtmlNodeElement)htmlParser.ParseString(html).FirstOrDefault(); // Assert Assert.Single(element.Texts()); Assert.Equal("Text", element.Texts()[0].HtmlString.Value); Assert.Equal(element.Children.LastOrDefault().UId, element.Texts()[0].AfterElementUId); }
public void ParseString_ReturnElementWithText_WhenHaveTextBetweenChildren(string html) { // Arrange // Act HtmlNodeElement element = (HtmlNodeElement)htmlParser.ParseString(html).FirstOrDefault(); // Assert Assert.Equal(3, element.Texts().Length); Assert.Equal("Text", element.Texts()[0].HtmlString.Value); Assert.Equal("Text1", element.Texts()[1].HtmlString.Value); Assert.Equal("Text2", element.Texts()[2].HtmlString.Value); }
public void Append_ShouldAppendChildAndSetHimParrent() { // Arrange HtmlNodeElement element = new HtmlNodeElement("div"); // Act element.Append(new HtmlNodeElement("div")); // Assert Assert.NotNull(element.Children); Assert.NotEmpty(element.Children); Assert.Equal(element.UId, element.Children.FirstOrDefault().Parents.FirstOrDefault().UId); }
public IHtmlContent CreateFormElement(IHtmlHelper htmlHelper, HtmlFormOptions options) { var formGenerator = new HtmlFormGenerator(options); HtmlNodeElement formElement = formGenerator.GenerateFormElement(); AspValidationInjector aspValidationInjector = new AspValidationInjector(htmlHelper, options.FormValidationOptions); if (htmlHelper.ViewContext.ClientValidationEnabled) { aspValidationInjector.Inject(formElement); } return(HtmlBuilder.CreateHtmlContent(formElement)); }
public void Create_ReturnTagAsHtmlString_WhenTagHaveTextBeforeChildren() { // Arrange htmlBuilder = new HtmlBuilder(); HtmlNodeElement htmlTag = new HtmlNodeElement("div", "Text"); htmlTag.Children.Add(new HtmlNodeElement("span")); // Act IHtmlContent result = htmlBuilder.CreateHtmlContent(htmlTag); // Assert Assert.Equal("<div>Text<span></span></div>", result.ToString()); }
public void Create_ReturnTagAsHtmlString_WhenTagHaveTextAfterChildren() { // Arrange htmlBuilder = new HtmlBuilder(); HtmlNodeElement htmlTag = new HtmlNodeElement("div"); htmlTag.Children.Add(new HtmlSpanElement()); htmlTag.Text("Text", htmlTag.Children.LastOrDefault().UId); // Act IHtmlContent result = htmlBuilder.CreateHtmlContent(htmlTag); // Assert Assert.Equal("<div><span></span>Text</div>", result.ToString()); }
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++; } }
/// <summary> /// Convert html document string to HtmlDocument. /// </summary> /// <param name="html">Valid html document string.</param> /// <returns cref="HtmlDocument">HtmlDocument instance represent given html string.</returns> public HtmlDocument ParsePage(string html) { if (html.IsNullOrEmpty()) { throw new ArgumentNullException("html"); } IHtmlElementsCollection elements = ParseString(html); HtmlNodeElement element = (HtmlNodeElement)elements.FirstOrDefault(x => x.TagName == "html"); HtmlDocument document = new HtmlDocument(); document.Attributes = element.Attributes; document.Children = element.Children; document.Text(element.Texts()); document.Doctype = (HtmlDocTypeElement)elements.FirstOrDefault(x => x.TagName.IsEqualIgnoreCase("!DOCTYPE")); return(document); }
public void AppendRange_ShouldAppendChilrendAndSetHimParrent_WhenAppendSomeChildren() { // Arrange HtmlNodeElement element = new HtmlNodeElement("div"); HtmlElementsCollection htmlElementsCollection = new HtmlElementsCollection(); htmlElementsCollection.Add(new HtmlDivElement()); htmlElementsCollection.Add(new HtmlSpanElement()); htmlElementsCollection.Add(new HtmlInputElement()); htmlElementsCollection.Add(new HtmlPElement()); htmlElementsCollection.Add(new HtmlH1Element()); htmlElementsCollection.Add(new HtmlH2Element()); // Act element.AppendRange(htmlElementsCollection); // Assert Assert.NotNull(element.Children); Assert.NotEmpty(element.Children); Assert.False(htmlElementsCollection.Any(x => x.Parents.FirstOrDefault().UId != element.UId)); }
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> /// Create html content. /// </summary> /// <param name="htmlElement">Object of type HtmlNodeElement, for convert to standart HTML document content.</param> /// <returns>Html content repsresent specified node element.</returns> public IHtmlContent CreateHtmlContent(HtmlNodeElement htmlElement) { if (htmlElement.Children.IsNullOrEmpty()) { return(new HtmlString( $"{CreateStartTag(htmlElement.TagName)}{htmlElement.Text()}{CreateEndTag(htmlElement.TagName)}" .Insert((htmlElement.TagName.Length + 1), htmlElement.Attributes.IsNullOrEmpty() ? "" : $" {String.Join(" ", htmlElement.Attributes.Select(x => CreateAtribute(x)))}"))); } List <IHtmlContent> childContents = new List <IHtmlContent>(); List <HtmlNodeElementText> knownPossitionedTexts = new List <HtmlNodeElementText>(); foreach (var text in htmlElement.Texts()) { if (text.AfterElementUId == null || !htmlElement.Children.Any(x => x.UId == text.AfterElementUId)) { childContents.Add(text.HtmlString); } else { knownPossitionedTexts.Add(text); } } foreach (var child in htmlElement.Children) { childContents.Add(CreateHtmlContent(child)); if (knownPossitionedTexts.Any(x => x.AfterElementUId == child.UId)) { childContents.Add(knownPossitionedTexts.FirstOrDefault(x => x.AfterElementUId == child.UId).HtmlString); } } return(new HtmlString( $"{CreateStartTag(htmlElement.TagName)}{String.Join("", childContents.Select(x => x.ToString()).ToArray())}{CreateEndTag(htmlElement.TagName)}" .Insert((htmlElement.TagName.Length + 1), htmlElement.Attributes.IsNullOrEmpty() ? "" : $" {String.Join(" ", htmlElement.Attributes.Select(x => CreateAtribute(x)))}"))); }
private HtmlNodeElement GetSelect(string name, IEnumerable <SelectListItem> values, string value, bool isMultiple = false) { HtmlNodeElement select = new HtmlNodeElement("select"); select.AddAttribute(new HtmlNameAttribute(name)); if (isMultiple) { select.AddAttribute("multiple"); } if (_options.InputAttributes != null) { select.AddRangeAttributes(_options.InputAttributes); } foreach (var item in values) { var option = new HtmlPairTagsElement("option") { Attributes = new IHtmlAttribute[] { new HtmlAttribute("value", item.Value) } }; option.Text(new HtmlString(item.Text)); if (item.Selected || (value != null && item.Value == value.ToString())) { option.AddAttribute("selected"); } select.Append(option); } return(select); }