Headless.QueryFactory.BuildAttributeQuery C# (CSharp) Method

BuildAttributeQuery() private method

private BuildAttributeQuery ( string attributeName, string attributeValue, bool ignoreCase ) : string
attributeName string
attributeValue string
ignoreCase bool
return string
        public static string BuildAttributeQuery(string attributeName, string attributeValue, bool ignoreCase)
        {
            if (string.IsNullOrWhiteSpace(attributeName))
            {
                throw new ArgumentException(Resources.Guard_NoValueProvided, "attributeName");
            }

            if (attributeValue == null)
            {
                attributeValue = string.Empty;
            }

            string attributeQuery;

            // Attribute names are folded to lowercase when the HTML is read
            var queryAttributeName = attributeName.ToLowerInvariant();

            if (ignoreCase)
            {
                // This literal value can be converted to lower case here rather than within the execution of the XPath query
                var queryAttributeValue = attributeValue.ToLowerInvariant();

                // The query execution will push the value of the node being checked to lowercase for the
                // specified attribute name
                attributeQuery = "[" + CaseQuery("@" + queryAttributeName, true) + "='" + queryAttributeValue + "']";
            }
            else
            {
                attributeQuery = "[@" + queryAttributeName + "='" + attributeValue + "']";
            }

            return attributeQuery;
        }

Usage Example

        /// <inheritdoc />
        /// <exception cref="ArgumentException">
        ///     The <paramref name="attributeName" /> parameter is <c>null</c>, empty or only
        ///     contains white-space.
        /// </exception>
        public IEnumerable <T> AllByAttribute(string attributeName, string attributeValue, bool ignoreCase)
        {
            if (string.IsNullOrWhiteSpace(attributeName))
            {
                throw new ArgumentException(Resources.Guard_NoValueProvided, "attributeName");
            }

            var tagSelector    = BuildElementQuery();
            var attributeQuery = QueryFactory.BuildAttributeQuery(attributeName, attributeValue, ignoreCase);

            return(Execute(tagSelector + attributeQuery));
        }