示例#1
0
        /// <summary>
        ///     Sets an attribute value by name to 'value.ToString()'. If 'replace' is false, the attribute is only set if not
        ///     already set. If 'value' is null, the entry is removed from the list instead.
        /// </summary>
        /// <exception cref="ArgumentException"> Thrown if 'name' is null or empty. </exception>
        /// <param name="list"> The TagHelperAttributeList to merge an attribute value with. </param>
        /// <param name="name"> The key to set a value for. </param>
        /// <param name="value">
        ///     The value to set for the specified key, which will be converted to a string first. If null, any existing entry is
        ///     removed instead (if 'replace' is true).
        /// </param>
        /// <param name="replace">
        ///     (Optional) If true (the default) adds a new entry or replaces an existing entry, otherwise the request is ignored.
        ///     If this is false, nothing is removed, and any merge requests with existing keys will be ignored.
        /// </param>
        /// <param name="removeOnEmptyOrWhitespace">
        ///     (Optional) If true (default) any empty or whitespace-only values will remove the attribute. This is in addition to
        ///     'null', which always removes values.
        /// </param>
        /// <param name="replaceIfValueNotEmpty">
        ///     (Optional) If true, any existing entry is only replaced if the given value is not empty or whitespace only. Default
        ///     is false. This setting has no affect if the 'replace' parameter is false.
        /// </param>
        public static void MergeAttribute(this TagHelperAttributeList list, string name, object value, bool replace = true, bool removeOnEmptyOrWhitespace = true, bool replaceIfValueNotEmpty = false)
        {
            if (name == null)
            {
                throw new ArgumentException("Value cannot be null.", nameof(name));
            }
            else if (name is string && string.IsNullOrEmpty((string)(object)name))
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(name));
            }

            if (replace || value != null && !list.ContainsName(name))
            {
                var valueStr = value?.ToString().Trim();
                var isEmpty  = string.IsNullOrWhiteSpace(valueStr);
                if (valueStr == null || removeOnEmptyOrWhitespace && isEmpty)
                {
                    list.RemoveAll(name);
                }
                else if (!replaceIfValueNotEmpty || !isEmpty)
                {
                    list.Add(name, valueStr);
                }
            }
        }
示例#2
0
 public override void Process(TagHelperContext context, TagHelperOutput output)
 {
     if (ShouldAddActive())
     {
         TagHelperAttributeList attrs = output.Attributes;
         attrs.RemoveAll("active-if-route");
         attrs.Add("class", "active");
     }
 }
示例#3
0
        public void ApplyTo(TagHelperAttributeList target)
        {
            Guard.NotNull(target, nameof(target));

            if (_list.Count == 0)
            {
                target.RemoveAll("class");
            }
            else
            {
                target.SetAttribute("class", string.Join(' ', _list));
            }
        }
    public void RemoveAll_RemovesAllExpectedAttributes(
        IEnumerable <TagHelperAttribute> initialAttributes,
        string keyToRemove,
        IEnumerable <TagHelperAttribute> expectedAttributes,
        bool expectedRemoval)
    {
        // Arrange
        var attributes = new TagHelperAttributeList(initialAttributes);

        // Act
        var removed = attributes.RemoveAll(keyToRemove);

        // Assert
        Assert.Equal(expectedRemoval, removed);
        Assert.Equal(expectedAttributes, attributes, CaseSensitiveTagHelperAttributeComparer.Default);
    }
示例#5
0
        /// <summary>
        /// Moves 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 move.</param>
        /// <param name="target">The target <see cref="TagBuilder"/> instance to move 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 moving if source attribute's value is <see langword="null"/>.</param>
        /// <returns><see langword="true"/> if an attribute has been moved, <see langword="false"/> otherwise.</returns>
        public static bool MoveAttribute(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))
            {
                if (target.MergeAttribute(name, () => attribute.Value?.ToString(), replaceExisting, ignoreNull))
                {
                    attributes.RemoveAll(name);
                    return(true);
                }
            }

            return(false);
        }
 public static bool RemoveAll(this TagHelperAttributeList attributeList, params string[] attributeNames)
 {
     return(attributeNames.Aggregate(false, (current, name) => attributeList.RemoveAll(name) || current));
 }
        public void RemoveAll_RemovesAllExpectedAttributes(
            IEnumerable<TagHelperAttribute> initialAttributes,
            string keyToRemove,
            IEnumerable<TagHelperAttribute> expectedAttributes,
            bool expectedRemoval)
        {
            // Arrange
            var attributes = new TagHelperAttributeList(initialAttributes);

            // Act
            var removed = attributes.RemoveAll(keyToRemove);

            // Assert
            Assert.Equal(expectedRemoval, removed);
            Assert.Equal(expectedAttributes, attributes, CaseSensitiveTagHelperAttributeComparer.Default);
        }