Skip to content

Commit

Permalink
Whitelist SVG attributes from the uppercase letter warning
Browse files Browse the repository at this point in the history
  • Loading branch information
exyi committed Dec 15, 2023
1 parent 0bfb665 commit c87e4a4
Showing 1 changed file with 37 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -451,28 +451,43 @@ attribute.ValueNode is DothtmlValueBindingNode binding &&
if (property is IGroupedPropertyDescriptor groupedProperty &&
property.UsedInCapabilities.Any(c => c.PropertyType.IsEqualTo(typeof(HtmlCapability))))
{
var pGroup = groupedProperty.PropertyGroup;
var name = groupedProperty.GroupMemberName;
var prefix = attribute.AttributeFullName.Substring(0, attribute.AttributeFullName.Length - name.Length);
// If the HTML attribute is used with a prefix such as `Item`, it might be clearer if the first character is uppercased
// e.g. ItemClass reads better than Itemclass
// we supress the warning in such case
var allowFirstCharacterUppercase = prefix.Length > 0 && char.IsLetter(prefix[prefix.Length - 1]);
if (pGroup.Name.EndsWith("Attributes") &&
name.Substring(allowFirstCharacterUppercase ? 1 : 0).ToLowerInvariant() != name.Substring(allowFirstCharacterUppercase ? 1 : 0))
{
// properties with at most two typos
var similarNameProperties =
control.Metadata.AllProperties
.Where(p => StringSimilarity.DamerauLevenshteinDistance(p.Name.ToLowerInvariant(), (prefix + name).ToLowerInvariant()) <= 2)
.Select(p => p.Name)
.ToArray();
var similarPropertyHelp =
similarNameProperties.Any() ? $" Did you mean {string.Join(", ", similarNameProperties)}, or another DotVVM property?" : " Did you intent to use a DotVVM property instead?";
attribute.AttributeNameNode.AddWarning(
$"HTML attribute name '{name}' should not contain uppercase letters." + similarPropertyHelp
);
}
AddHtmlAttributeWarning(control, attribute, groupedProperty);
}
}

// some SVG attributes contain uppercase letters, we don't want to warn about those
static HashSet<string> uppercaseHtmlAttributeList = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "attributeName", "baseFrequency", "calcMode", "clipPathUnits", "diffuseConstant", "edgeMode", "edgeMode", "filterUnits", "gradientTransform", "gradientTransform", "gradientUnits", "gradientUnits", "kernelMatrix", "kernelUnitLength", "kernelUnitLength", "kernelUnitLength", "keyPoints", "keySplines", "keyTimes", "lengthAdjust", "limitingConeAngle", "markerHeight", "markerUnits", "markerWidth", "maskContentUnits", "maskUnits", "numOctaves", "pathLength", "patternContentUnits", "patternTransform", "patternUnits", "pointsAtX", "pointsAtY", "pointsAtZ", "preserveAlpha", "preserveAspectRatio", "primitiveUnits", "refX", "refX", "refY", "refY", "repeatCount", "repeatDur", "requiredExtensions", "specularConstant", "specularExponent", "specularExponent", "spreadMethod", "spreadMethod", "startOffset", "stdDeviation", "stdDeviation", "stitchTiles", "surfaceScale", "surfaceScale", "systemLanguage", "tableValues", "targetX", "targetY", "textLength", "textLength", "viewBox", "xChannelSelector", "yChannelSelector", "zoomAndPan" };
private static void AddHtmlAttributeWarning(IAbstractControl control, DothtmlAttributeNode attribute, IGroupedPropertyDescriptor groupedProperty)
{
var pGroup = groupedProperty.PropertyGroup;
var name = groupedProperty.GroupMemberName;


var prefix = attribute.AttributeFullName.Substring(0, attribute.AttributeFullName.Length - name.Length);
// If the HTML attribute is used with a prefix such as `Item`, it might be clearer if the first character is uppercased
// e.g. ItemClass reads better than Itemclass
// we supress the warning in such case
var allowFirstCharacterUppercase = prefix.Length > 0 && char.IsLetter(prefix[prefix.Length - 1]);


// Ignore SVG attributes (unless they also start with an uppercase letter)
if ((allowFirstCharacterUppercase || !char.IsUpper(name[0])) && uppercaseHtmlAttributeList.Contains(name))
return;

if (pGroup.Name.EndsWith("Attributes") &&
name.Substring(allowFirstCharacterUppercase ? 1 : 0).ToLowerInvariant() != name.Substring(allowFirstCharacterUppercase ? 1 : 0))
{
// properties with at most two typos
var similarNameProperties =
control.Metadata.AllProperties
.Where(p => StringSimilarity.DamerauLevenshteinDistance(p.Name.ToLowerInvariant(), (prefix + name).ToLowerInvariant()) <= 2)
.Select(p => p.Name)
.ToArray();
var similarPropertyHelp =
similarNameProperties.Any() ? $" Did you mean {string.Join(", ", similarNameProperties)}, or another DotVVM property?" : " Did you intent to use a DotVVM property instead?";
attribute.AttributeNameNode.AddWarning(
$"HTML attribute name '{name}' should not contain uppercase letters." + similarPropertyHelp
);
}
}

Expand Down

0 comments on commit c87e4a4

Please sign in to comment.