示例#1
0
        public static TagHelperAttributeList AddOrAppendAttribute(
            this TagHelperAttributeList tagAttributesList,
            string name,
            string attrValue,
            bool appendFirst                  = false,
            bool checkForDuplicates           = false,
            Func <string, bool> dontAddIfPred = null)
        {
            if (name.IsNulle())
            {
                throw new ArgumentNullException();
            }
            if (tagAttributesList == null)
            {
                throw new ArgumentNullException();
            }

            if (attrValue == null)
            {
                return(tagAttributesList);
            }

            TagHelperAttribute attr = tagAttributesList.IsNulle()
                                ? null
                                : tagAttributesList.FirstN(t => t.Name == name);

            if (attr != null)
            {
                string attrValStr = attr.Value?.ToString();
                if (attrValStr.IsNulle())
                {
                    attr = null;
                }
                else
                {
                    if (dontAddIfPred != null && dontAddIfPred.Invoke(attrValStr))
                    {
                        return(tagAttributesList);
                    }

                    attrValue = appendFirst
                                                ? $"{attrValue} {attrValStr}"
                                                : $"{attrValStr} {attrValue}";
                }
            }

            attrValue = attrValue.TrimIfNeeded();

            if (checkForDuplicates && attr != null)
            {
                // so only have to do this check if attribute already exists, that single check will GREATLY improv perf, removing a needless case
                attrValue = attrValue
                            .SplitAndRemoveWhiteSpaceEntries(_whiteSpSeparators)
                            .Distinct()
                            .JoinToString(" ");
            }

            tagAttributesList.SetAttribute(name, attrValue);

            return(tagAttributesList);
        }