Beispiel #1
0
        private bool TrimAttribute(HtmlElement element, HtmlAttribute attribute)
        {
            var tag  = element.Name.ToLowerInvariant();
            var attr = attribute.Name.ToLowerInvariant();

            if (attribute.Value != null)
            {
                if (IsUriTypeAttribute(element.Name, attribute.Name))
                {
                    attribute.Value = attribute.Value.Trim();
                    return(false);
                }
                if (attr == "class")
                {
                    attribute.Value = attribute.Value.Trim();
                    //if (options.sortClassName)
                    //{
                    //    attrValue = options.sortClassName(attrValue);
                    //}
                    // else
                    {
                        attribute.Value = CharHelper.CollapseWhitespaces(attribute.Value);
                    }
                    return(attribute.Value == string.Empty);
                }
                else if (attr == "style" && element.Descriptor != null && settings.MinifyCssAttributes)
                {
                    attribute.Value = MinifyJsOrCss(attribute.Value, false);
                    return(attribute.Value == string.Empty);
                }
            }

            if (settings.ShortBooleanAttribute && attribute.Value == "true")
            {
                attribute.Value = null;
            }

            if (settings.RemoveEmptyAttributes)
            {
                if ((attribute.Value != null || (attribute.Value == null && AttributesRemovedIfEmpty.ContainsKey(attr))) && attribute.Value.IsNullOrWhiteSpace())
                {
                    attribute.Value = string.Empty;

                    return((tag == "input" && attr == "value") || AttributesRemovedIfEmpty.ContainsKey(attr));
                }
            }

            if (!settings.AttributesCaseSensitive && xmlNamespaceCount == 0)
            {
                attribute.Name = attr;
            }

            return(false);
        }
Beispiel #2
0
        bool TrimAttribute(HtmlElement element, HtmlAttribute attribute)
        {
            var tag = element.Name.ToLowerInvariant();
            var attributeNameLower = attribute.Name.ToLowerInvariant();
            var attr = attributeNameLower;

            if (settings.RemoveAttributes.Contains(attribute.Name))
            {
                return(true);
            }

            if (attribute.Value != null)
            {
                if (settings.RemoveJavaScript)
                {
                    if (ScriptAttributes.ContainsKey(attr) || attribute.Value.Trim().StartsWith("javascript:", StringComparison.OrdinalIgnoreCase))
                    {
                        return(true);
                    }
                }

                if (IsUriTypeAttribute(element.Name, attribute.Name))
                {
                    attribute.Value = attribute.Value.Trim();
                    return(false);
                }
                if (attr == "class")
                {
                    attribute.Value = attribute.Value.Trim();
                    //if (options.sortClassName)
                    //{
                    //    attrValue = options.sortClassName(attrValue);
                    //}
                    // else
                    {
                        attribute.Value = CharHelper.CollapseWhitespaces(attribute.Value);
                    }
                    return(attribute.Value == string.Empty);
                }

                if (attr == "style" && element.Descriptor != null && settings.MinifyCssAttributes)
                {
                    attribute.Value = MinifyCssAttribute(attribute.Value);
                    return(attribute.Value == string.Empty);
                }

                if (settings.MinifyJsAttributes && ScriptAttributes.ContainsKey(attr))
                {
                    attribute.Value = MinifyJsAttribute(attribute.Value);
                    return(attribute.Value == string.Empty);
                }
            }

            if (settings.ShortBooleanAttribute && attribute.Value == "true" && attributeNameLower != "value" && attributeNameLower != "aria-hidden")
            {
                attribute.Value = null;
            }

            if (settings.RemoveEmptyAttributes)
            {
                if ((attribute.Value != null || (attribute.Value == null && AttributesRemovedIfEmpty.ContainsKey(attr))) && attribute.Value.IsNullOrWhiteSpace())
                {
                    attribute.Value = string.Empty;

                    return((tag == "input" && attr == "value") || AttributesRemovedIfEmpty.ContainsKey(attr));
                }
            }

            if (!settings.AttributesCaseSensitive && xmlNamespaceCount == 0)
            {
                attribute.Name = attr;
            }

            return(false);
        }