public static string GetAttributeValue(this HtmlNode node, MarkerAttributes markerAttribute)
 {
     if (null == node)
     {
         return(string.Empty);
     }
     if (MarkerAttributes.Tag == markerAttribute)
     {
         return(node.OriginalName);
     }
     return(NodeWithAttributes(node) ? node.Attributes.First(attribute => attribute.Name.ToLower() == ConvertMarkerToStringNameOfAttribute(markerAttribute)).Value : string.Empty);
 }
        public static bool NodeMatchesTheCondition(this HtmlNode nodeForCondition, MarkerAttributes markerAttribute, List <string> markerValues)
        {
            if (null == nodeForCondition)
            {
                return(false);
            }

            if (MarkerAttributes.OtherAttribute == markerAttribute)
            {
                return(markerValues.Any(markerValue => nodeForCondition.Attributes.Any(attribute => markerValue == attribute.Value)));
            }

            var attributeValue = nodeForCondition.GetAttributeValue(markerAttribute);

            return(markerValues.Any(markerValue => attributeValue.Contains(markerValue)));
        }
 static string ConvertMarkerToStringNameOfAttribute(this MarkerAttributes markerAttribute)
 {
     return(markerAttribute.ToString().ToLower());
 }
 public static bool HasAttribute(this HtmlNode node, MarkerAttributes markerAttribute)
 {
     return(MarkerAttributes.OtherAttribute == markerAttribute ? node.HasAttributes : HasAttribute(node, ConvertMarkerToStringNameOfAttribute(markerAttribute)));
 }
        public static IEnumerable <HtmlNode> GetNodesThatMatchTheCondition(this HtmlNode node, NodeRelationships relationship, MarkerAttributes markerAttribute)
        {
            // TODO: refactor this!
            switch (relationship)
            {
            case NodeRelationships.Self:
                var result = node.HasAttribute(markerAttribute) ? new List <HtmlNode> {
                    node
                } : new List <HtmlNode> {
                    null
                };
                return(result);

            case NodeRelationships.Sibling:
                // TODO: write better code!
                return(new List <HtmlNode> {
                    null
                });

            case NodeRelationships.Parent:
                return(node.ParentNode.HasAttribute(markerAttribute) ? new List <HtmlNode> {
                    node.ParentNode
                } : new List <HtmlNode> {
                    null
                });

            case NodeRelationships.Ancestor:
                return(node.Ancestors().Any(ancestor => ancestor.HasAttribute(markerAttribute))
                        ? node.Ancestors().Where(ancestor => ancestor.HasAttribute(markerAttribute)).ToList()
                        : new List <HtmlNode> {
                    null
                });

            case NodeRelationships.Child:
                return(node.ChildNodes.Any(childNode => childNode.HasAttribute(markerAttribute))
                        ? node.ChildNodes.Where(childNode => childNode.HasAttribute(markerAttribute)).ToList()
                        : new List <HtmlNode> {
                    null
                });

            //return node.SelectNodes("*").Any(childNode => childNode.HasAttribute(markerAttribute))
            //    ? node.SelectNodes("*").Where(childNode => childNode.HasAttribute(markerAttribute)).ToList()
            //    : new List<HtmlNode> { null };
            case NodeRelationships.Descendant:
                //return node.Descendants().Any(descendant => descendant.HasAttribute(markerAttribute))
                //    ? node.Descendants().Where(childNode => childNode.HasAttribute(markerAttribute)).ToList()
                //    : new List<HtmlNode> { null };
                var result2 = node.Descendants().Any(descendant => descendant.HasAttribute(markerAttribute))
                        ? node.Descendants().Where(childNode => childNode.HasAttribute(markerAttribute)).ToList()
                        : new List <HtmlNode> {
                    null
                };
                //var result2 = node.SelectNodes("*").Any(descendant => descendant.HasAttribute(markerAttribute))
                //    ? node.SelectNodes("*").Where(childNode => childNode.HasAttribute(markerAttribute)).ToList()
                //    : new List<HtmlNode> { null };
                return(result2);

            default:
                return(new List <HtmlNode> {
                    null
                });
            }
        }