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); }
public void Get_ReturnNull_WhenCollectionIsNotEmptyAndElementsHaveChildrenAndElementNotExist() { // Arrange htmlElementsCollection = new HtmlElementsCollection() { new HtmlNodeElement("div") { Children = new HtmlElementsCollection { new HtmlNodeElement("span") } }, new HtmlNodeElement("div") { Children = new HtmlElementsCollection { new HtmlNodeElement("span") } } }; // Act IHtmlElement result = htmlElementsCollection.Get("1"); // Assert Assert.Null(result); }
public IEnumerable <TElement> FindAll <TElement>(Locator locator) where TElement : IHtmlElement, new() { IHtmlElementsCollection <TElement> elementsCollection = new HtmlElementsCollection <TElement>(this, locator); foreach (var element in elementsCollection) { yield return(element); } }
public void Get_ReturnNull_WhenCollectionIsEmpty() { // Arrange htmlElementsCollection = new HtmlElementsCollection(); // Act IHtmlElement result = htmlElementsCollection.Get("1"); // Assert Assert.Null(result); }
public void Get_ReturnNull_WhenCollectionIsNotEmptyAndElementNotExist() { // Arrange htmlElementsCollection = new HtmlElementsCollection() { new HtmlPairTagsElement("div"), new HtmlPairTagsElement("div") }; // Act IHtmlElement result = htmlElementsCollection.Get("1"); // Assert Assert.Null(result); }
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 HtmlElementsCollection GetHtmlFormGroups() { HtmlElementsCollection formElements = new HtmlElementsCollection(); PropertyInfo[] propertiesInfo = _options.Model.GetType().GetProperties(); foreach (var propInfo in propertiesInfo) { bool isSourceProperty = propertiesInfo.Any( prop => prop.GetCustomAttributes().Any( attr => attr.GetType().GetProperty(nameof(IHtmlMultipleValuesInputAttribute.SourceProperty))?.GetValue(attr) == propInfo.Name)); // TODO Test if (propInfo.GetCustomAttribute <IgonreUIAttribute>() != null || isSourceProperty) { continue; } // if Type is class, should generate inputs for him properties if (!propInfo.PropertyType.IsPrimitive && propInfo.PropertyType != typeof(string) && propInfo.PropertyType != typeof(DateTime)) { //foreach (var fieldSet in GetHtmlFormFiledSets<T>(definition)) // formFieldSets.Add(fieldSet); } if (propInfo.GetCustomAttribute <HiddenAttribute>() != null) { formElements.Add(GetProperInput(propInfo)); } else { formElements.Add(GetFormGroup(propInfo)); } } return(formElements); }
public IHtmlElementsCollection ParseElements(int startIndex, int endIndex) { IHtmlElementsCollection elements = new HtmlElementsCollection(); // get all matches in current range List <Match> startTagMatchesInCurrentRange = _startTagsMathes.Where(x => x.Index >= startIndex && x.Index < endIndex).ToList(); List <Match> pairStartTagMatchesInCurrentRange = _pairStartTagsMathes.Where(x => x.Index >= startIndex && x.Index < endIndex).ToList(); List <Match> endTagMatchesInCurrentRange = _endTagsMathes.Where(x => x.Index > startIndex && x.Index < endIndex).ToList(); try { // get root start tags in current range List <Match> rootStartTagMatches = startTagMatchesInCurrentRange .Where(currentStartTag => pairStartTagMatchesInCurrentRange .Count(startTag => startTag.Index < currentStartTag.Index) == endTagMatchesInCurrentRange.Count(endTag => endTag.Index <= currentStartTag.Index) ) .ToList(); // get root end tags in current range List <Match> rootEndTagMatches = endTagMatchesInCurrentRange .Where(currentEndTag => pairStartTagMatchesInCurrentRange .Count(startTag => startTag.Index < currentEndTag.Index) == endTagMatchesInCurrentRange.Count(endTag2 => endTag2.Index <= currentEndTag.Index) ) .ToList(); // create IHtmlElement for every start tag match and add to collection foreach (Match startTagMatch in rootStartTagMatches) { // get tag name string tagName = _htmlHelper.ExtractTagNameFromStartTag(startTagMatch.Value); // parse attributes HtmlAttributesCollection attributes = ParseAttributes(startTagMatch.Value); // if tag is self closing if (_htmlHelper.IsSelfClosingHtmlTag(tagName)) { // create elements factory IHtmlSelfClosingTagElementFactory htmlElementsFactory; switch (tagName.ToLower()) { case "!doctype": htmlElementsFactory = new HtmlDoctypeElementFactory(); break; default: htmlElementsFactory = new HtmlSelfClosingTagElementFactory(tagName); break; } // create element IHtmlElement element = htmlElementsFactory.Create(attributes, _encodedHtml, this); // add elements.Add(element); } // when tag have pair tags else { // create elements factory IHtmlPairTagsElementFactory htmlElementsFactory; switch (tagName.ToLower()) { case "noscript": htmlElementsFactory = new HtmlNoScriptElementFactory(); break; case "script": htmlElementsFactory = new HtmlScriptElementFactory(); break; case "style": htmlElementsFactory = new HtmlStyleElementFactory(); break; case "code": htmlElementsFactory = new HtmlCodeElementFactory(); break; default: htmlElementsFactory = new HtmlNodeElementFactory(tagName); break; } // find start content index int startContentIndex = startTagMatch.Index + startTagMatch.Value.Length; // find cloing tang on current star tag Match endTagMatch = rootEndTagMatches.FirstOrDefault(x => x.Index > startTagMatch.Index); // in html may have tags which should have end tag but he is not defined, in this case just skip him as set end index to be end index on current start tag int endContentIndex = startTagMatch.Index + startTagMatch.Value.Length; if (endTagMatch != null) { endContentIndex = endTagMatch.Index; } // create element IHtmlElement element = htmlElementsFactory.Create(attributes, _encodedHtml, startContentIndex, endContentIndex, this); // add elements.Add(element); } } } catch (Exception ex) { return(elements); } return(elements); }