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_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_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); }
/// <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); }
/// <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)))}"))); }