예제 #1
0
        /// <summary>
        /// Gets all descendant elements in this <see cref="HtmlContainer"/> that match specified class name.
        /// Element class name comparison is case sensitive.
        /// </summary>
        public static IEnumerable <HtmlElement> GetElementsByClassName(this HtmlContainer container, string className)
        {
            container.GuardNotNull(nameof(container));
            className.GuardNotNull(nameof(className));

            return(container.GetDescendants()
                   .OfType <HtmlElement>()
                   .Where(e => e.MatchesClassName(className)));
        }
예제 #2
0
        /// <summary>
        /// Gets the first descendant element in this <see cref="HtmlContainer"/> that has specified id or null if it's not found.
        /// Element ID comparison is case sensitive.
        /// </summary>
        public static HtmlElement GetElementById(this HtmlContainer container, string id)
        {
            container.GuardNotNull(nameof(container));
            id.GuardNotNull(nameof(id));

            return(container.GetDescendants()
                   .OfType <HtmlElement>()
                   .FirstOrDefault(e => string.Equals(e.GetId(), id, StringComparison.Ordinal)));
        }
예제 #3
0
        /// <summary>
        /// Gets all descendant elements in this <see cref="HtmlContainer"/> that have specified tag name.
        /// Element tag name comparison is not case sensitive.
        /// </summary>
        public static IEnumerable <HtmlElement> GetElementsByTagName(this HtmlContainer container, string tagName)
        {
            container.GuardNotNull(nameof(container));
            tagName.GuardNotNull(nameof(tagName));

            // Mimic JS behavior
            if (string.Equals(tagName, "*", StringComparison.OrdinalIgnoreCase))
            {
                return(container.GetDescendants().OfType <HtmlElement>());
            }

            return(container.GetDescendants()
                   .OfType <HtmlElement>()
                   .Where(e => string.Equals(e.Name, tagName, StringComparison.OrdinalIgnoreCase)));
        }
예제 #4
0
        /// <summary>
        /// Gets all descendant elements in this <see cref="HtmlContainer"/> that match specified CSS selector.
        /// See https://w3.org/TR/selectors-3 for the list of supported selectors.
        /// </summary>
        public static IEnumerable <HtmlElement> GetElementsBySelector(this HtmlContainer container, string selector)
        {
            container.GuardNotNull(nameof(container));
            selector.GuardNotNull(nameof(selector));

            var selectorParseResult = SelectorGrammar.Selector.TryParse(selector);

            // JS doesn't fail on invalid selectors so neither should we
            if (!selectorParseResult.WasSuccessful)
            {
                return(Enumerable.Empty <HtmlElement>());
            }

            return(container.GetDescendants()
                   .OfType <HtmlElement>()
                   .Where(e => selectorParseResult.Value.Matches(e)));
        }
예제 #5
0
        /// <summary>
        /// Gets all descendants of this <see cref="HtmlContainer"/>.
        /// </summary>
        public static IEnumerable <HtmlNode> GetDescendants(this HtmlContainer container)
        {
            container.GuardNotNull(nameof(container));

            foreach (var child in container.Children)
            {
                yield return(child);

                if (child is HtmlContainer containerChild)
                {
                    foreach (var recursiveChild in containerChild.GetDescendants())
                    {
                        yield return(recursiveChild);
                    }
                }
            }
        }
예제 #6
0
        // TODO: refactor
        /// <summary>
        /// Gets the text representation of descendants of this <see cref="HtmlContainer"/>.
        /// </summary>
        public static string GetInnerText(this HtmlContainer container)
        {
            container.GuardNotNull(nameof(container));

            return(container.GetTextRepresentation());
        }