public static bool ContainsClass(this TagHelperAttributeList attributes, string className)
        {
            if (string.IsNullOrWhiteSpace(className))
            {
                throw new ArgumentOutOfRangeException(nameof(className), "The class name must not be null or white space.");
            }

            if (!attributes.TryGetAttribute("class", out TagHelperAttribute classAttribute))
            {
                return(false);
            }

            string classes = classAttribute.Value.ToString();

            for (int i = 0; i + className.Length <= classes.Length; i++)
            {
                // Find className
                i = classes.IndexOf(className, i);
                if (i == -1)
                {
                    return(false);
                }

                // Ensure word boundaries
                if ((i == 0 || char.IsWhiteSpace(classes[i - 1])) &&
                    (i + className.Length == classes.Length || char.IsWhiteSpace(classes[i + className.Length])))
                {
                    return(true);
                }
            }

            return(false);
        }
示例#2
0
        private bool HasStyleSheetLinkType(TagHelperAttributeList attributes)
        {
            if (!attributes.TryGetAttribute(RelAttributeName, out var relAttribute) ||
                relAttribute.Value == null)
            {
                return(false);
            }

            var attributeValue = relAttribute.Value;
            var stringValue    = attributeValue as string;

            if (attributeValue is IHtmlContent contentValue)
            {
                contentValue.WriteTo(StringWriter, HtmlEncoder);
                stringValue = StringWriter.ToString();

                // Reset writer
                StringWriter.GetStringBuilder().Clear();
            }
            else if (stringValue == null)
            {
                stringValue = Convert.ToString(attributeValue, CultureInfo.InvariantCulture);
            }

            var hasRelStylesheet = string.Equals("stylesheet", stringValue, StringComparison.Ordinal);

            return(hasRelStylesheet);
        }
示例#3
0
        /// <summary>
        /// Copies the html attribute <paramref name="name"/> from source <paramref name="attributes"/> to <see cref="TagBuilder"/> <paramref name="target"/>.
        /// </summary>
        /// <param name="attributes">The source to find attribute to copy.</param>
        /// <param name="target">The target <see cref="TagBuilder"/> instance to copy found attribute to.</param>
        /// <param name="name">Name of attribute to find in source.</param>
        /// <param name="replaceExisting">Whether to replace existing attribute in <paramref name="target"/>.</param>
        /// <param name="ignoreNull">Whether to skip copying if source attribute's value is <see langword="null"/>.</param>
        /// <returns><see langword="true"/> if an attribute has been copied, <see langword="false"/> otherwise.</returns>
        public static bool CopyAttribute(this TagHelperAttributeList attributes, TagBuilder target, string name, bool replaceExisting = true, bool ignoreNull = true)
        {
            Guard.NotEmpty(name, nameof(name));
            Guard.NotNull(target, nameof(target));

            if (attributes.TryGetAttribute(name, out var attribute))
            {
                return(target.MergeAttribute(name, () => attribute.Value?.ToString(), replaceExisting, ignoreNull));
            }

            return(false);
        }
        /// <summary>
        /// TODO document
        /// </summary>
        /// <param name="attributes"></param>
        /// <param name="attribute"></param>
        /// <param name="value"></param>
        public static void AppendToAttribute(this TagHelperAttributeList attributes, string attribute, string value)
        {
            var hasAttribute = attributes.TryGetAttribute(attribute, out var classAttribute);

            var existingValue = "";

            if (hasAttribute)
            {
                existingValue = classAttribute.Value.ToString() + " ";
            }

            existingValue += value;

            attributes.SetAttribute(attribute, existingValue);
        }
示例#5
0
        private static void AddInAttributeValue(TagHelperAttributeList attrList, string name, char separator, string value, bool prepend = false)
        {
            if (!attrList.TryGetAttribute(name, out var attribute))
            {
                attrList.Add(new TagHelperAttribute(name, value));
            }
            else
            {
                var arr      = attribute.Value.ToString().Trim().Tokenize(new[] { separator }, StringSplitOptions.RemoveEmptyEntries);
                var arrValue = value.Trim().Tokenize(new[] { separator }, StringSplitOptions.RemoveEmptyEntries);

                arr = prepend ? arrValue.Union(arr) : arr.Union(arrValue);

                attrList.SetAttribute(name, string.Join(separator, arr));
            }
        }
示例#6
0
    public void TryGetAttribute_ReturnsExpectedValueAndAttribute(
        IEnumerable <TagHelperAttribute> initialAttributes,
        string nameToLookup,
        TagHelperAttribute expectedAttribute,
        bool expectedResult)
    {
        // Arrange
        var attributes = new TagHelperAttributeList(initialAttributes);
        TagHelperAttribute attribute;

        // Act
        var result = attributes.TryGetAttribute(nameToLookup, out attribute);

        // Assert
        Assert.Equal(expectedResult, result);
        Assert.Equal(expectedAttribute, attribute, CaseSensitiveTagHelperAttributeComparer.Default);
    }
示例#7
0
        /// <summary>
        /// Adds a class to a tag, if it is not already there (case sensitive)
        /// </summary>
        /// <param name="attributes">The attribute list to add the class to</param>
        /// <param name="htmlClass">The class to add to the tag</param>
        /// <remarks>Keep checking to see if this or similar is added to the framework</remarks>
        public static void AddClass(this TagHelperAttributeList attributes, string htmlClass)
        {
            attributes.TryGetAttribute("class", out var classAttribute);
            var classes = classAttribute?.Value?.ToString();

            if (string.IsNullOrWhiteSpace(classes))
            {
                attributes.SetAttribute("class", htmlClass);
                return;
            }

            var classList = classes.Split(' ', StringSplitOptions.RemoveEmptyEntries);

            if (!classList.Contains(htmlClass, StringComparer.Ordinal))
            {
                attributes.SetAttribute("class", new HtmlString($"{classes} {htmlClass}"));
            }
        }
        public static bool AddClass(this TagHelperAttributeList attributes, string className)
        {
            if (string.IsNullOrWhiteSpace(className))
            {
                throw new ArgumentNullException(nameof(className));
            }

            if (!attributes.TryGetAttribute("class", out TagHelperAttribute classAttribute))
            {
                attributes.Add(new TagHelperAttribute("class", className));
                return(true);
            }

            if (attributes.ContainsClass(className))
            {
                return(false);
            }

            string classes = classAttribute.Value.ToString();

            attributes.SetAttribute("class", string.Join(" ", className, classes));
            return(true);
        }
示例#9
0
        public void TryGetAttribute_ReturnsExpectedValueAndAttribute(
            IEnumerable<TagHelperAttribute> initialAttributes,
            string nameToLookup,
            TagHelperAttribute expectedAttribute,
            bool expectedResult)
        {
            // Arrange
            var attributes = new TagHelperAttributeList(initialAttributes);
            TagHelperAttribute attribute;

            // Act
            var result = attributes.TryGetAttribute(nameToLookup, out attribute);

            // Assert
            Assert.Equal(expectedResult, result);
            Assert.Equal(expectedAttribute, attribute, CaseSensitiveTagHelperAttributeComparer.Default);
        }
示例#10
0
        // --------------------------------------------------------------------------------------------------------------------

        /// <summary>
        ///     A TagHelperAttributeList extension method that returns an attribute by name, or a default value or null if not found.
        /// </summary>
        /// <param name="list"> The list to act on. </param>
        /// <param name="name"> The name. </param>
        /// <param name="defaultIfEmpty"> (Optional) The default if empty. </param>
        /// <returns> A TagHelperAttribute. </returns>
        public static TagHelperAttribute Value(this TagHelperAttributeList list, string name, TagHelperAttribute defaultIfEmpty = null)
        {
            return(list.TryGetAttribute(name, out var value) ? value : defaultIfEmpty);
        }
示例#11
0
 /// <summary>
 ///     A TagHelperAttributeList extension method that returns an attribute by name, or a default value or null if not found.
 /// </summary>
 /// <param name="list"> The list to act on. </param>
 /// <param name="name"> The name. </param>
 /// <param name="defaultIfEmpty"> The default if empty. </param>
 /// <returns> A string. </returns>
 public static string Value(this TagHelperAttributeList list, string name, string defaultIfEmpty)
 {
     return(list.TryGetAttribute(name, out var value) ? value.Render() : defaultIfEmpty);
 }