Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Whitelist SVG attributes from the uppercase letter warning #1749

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading