private IList <HTMLElement> FindAll(string html, string tag, HTMLAttributes attributes, bool allElements) { var elm = InternalFindAll(html, tag, attributes, 0); var list = new List <HTMLElement>(); if (elm.Item1 == -1) { return(list); } if (!allElements) { list.Add(elm.Item2); return(list); } do { list.Add(elm.Item2); elm = InternalFindAll(html, tag, attributes, elm.Item1); } while (elm.Item1 != -1); return(list); }
private (int, HTMLElement) InternalFindAll(string html, string tag, HTMLAttributes attributes, int position) { var initPos = GetInitPosition(html, tag, attributes, position); if (initPos.Item1 == -1) { return(-1, null); } while (initPos.Item1 > -1 && initPos.Item2 == 0) { initPos = GetInitPosition(html, tag, attributes, initPos.Item1); } if (initPos.Item1 == -1) { return(-1, null); } var endPos = GetEndPosition(html, tag, initPos.Item2); return(endPos, new HTMLElement { TagName = tag, Attributes = initPos.Item3, Content = html.Substring(initPos.Item2, endPos - initPos.Item2) }); }
public bool ExistsAllValues(HTMLAttributes parameters) { if (parameters == null) { return(false); } if (!ExistsAllKeys(parameters)) { return(false); } var finded = true; foreach (var keyValuePair in parameters) { var valueExpected = parameters[keyValuePair.Key].Split(" "); finded &= this[keyValuePair.Key].Split(" ").Intersect(valueExpected).Count() == valueExpected.Length; if (!finded) { break; } } return(finded); }
public bool ExistsAllKeys(HTMLAttributes parameters) { if (parameters == null) { return(false); } return(Keys.Intersect(parameters.Keys).Count() == parameters.Keys.Count); }
private static HTMLAttributes GetAttributes(string html) { var matchs = Regex.Matches(html, GetRegexAttribute(), RegexOptions.IgnoreCase); if (matchs.Count == 0) { return(null); } var attrs = new HTMLAttributes(); foreach (Match match in matchs) { attrs.Add(match.Groups[MATCH_GROUP_NAME].Value, match.Groups[MATCH_GROUP_VALUE].Value); } return(attrs); }
private (int, int, HTMLAttributes) GetInitPosition(string html, string tag, HTMLAttributes attributes, int initPosition) { var initTag = $"<{tag}"; var initPos = html.IndexOf(initTag, initPosition); if (initPos == -1) { return(-1, 0, null); } var termPos = html.IndexOf(">", initPos) + 1; var tagFull = html.Substring(initPos, termPos - initPos); var tagAttributes = GetAttributes(tagFull); if (tagAttributes.HasAttributeInTag(attributes)) { return(initPos, termPos, tagAttributes); } return(termPos, 0, null); }
public IList <HTMLElement> FindAll(string html, string tag, HTMLAttributes attributes) => FindAll(html, tag, attributes, true);
public HTMLElement Find(string html, string tag, HTMLAttributes attributes) => FindAll(html, tag, attributes, false).FirstOrDefault();