public override AnalysisActions Analyze(RapidXamlElement element, ExtraAnalysisDetails extraDetails)
        {
            var hdrAttr = element.GetAttributes(Attributes.Text).FirstOrDefault();

            if (hdrAttr != null && hdrAttr.HasStringValue)
            {
                var value = hdrAttr.StringValue;

                // TODO: ISSUE#163 change this to an RXT200 when can handle localization of Xamarin.Forms apps
                if (!string.IsNullOrWhiteSpace(value) && char.IsLetterOrDigit(value[0]))
                {
                    return(AnalysisActions.HighlightAttributeWithoutAction(
                               errorType: RapidXamlErrorType.Warning,
                               code: "RXT201",
                               description: StringRes.UI_XamlAnalysisGenericHardCodedStringDescription.WithParams(Elements.SwipeItem, Attributes.Text, value),
                               attribute: hdrAttr));
                }
            }

            return(AnalysisActions.None);
        }
Esempio n. 2
0
        public override AnalysisActions Analyze(RapidXamlElement element, ExtraAnalysisDetails extraDetails)
        {
            AnalysisActions result = AnalysisActions.None;

            if (!element.HasAttribute(Attributes.Keyboard))
            {
                string nonDefaultSuggestion = null;
                var    xaml = element.OriginalString.ToLowerInvariant();
                if (xaml.Contains("email"))
                {
                    nonDefaultSuggestion = "Email";
                }
                else if (xaml.Contains("phone") || xaml.Contains("cell") || xaml.Contains("mobile"))
                {
                    nonDefaultSuggestion = "Telephone";
                }
                else if (xaml.Contains("url"))
                {
                    nonDefaultSuggestion = "Url";
                }

                result.AddAttribute(
                    RapidXamlErrorType.Suggestion,
                    "RXT300",
                    description: StringRes.UI_XamlAnalysisEntryWithoutKeyboardDescription,
                    actionText: StringRes.UI_UndoContextAddEntryKeyboard,
                    addAttributeName: Attributes.Keyboard,
                    addAttributeValue: "Default",
                    extendedMessage: StringRes.UI_XamlAnalysisEntryWithoutKeyboardExtendedMessage);

                if (!string.IsNullOrEmpty(nonDefaultSuggestion))
                {
                    result.OrAddAttribute(
                        actionText: StringRes.UI_AddEntryKeyboard.WithParams(nonDefaultSuggestion),
                        addAttributeName: Attributes.Keyboard,
                        addAttributeValue: nonDefaultSuggestion);
                }
            }

            var txtAttr = element.GetAttributes(Attributes.Text).FirstOrDefault();

            if (txtAttr != null && txtAttr.HasStringValue)
            {
                var value = txtAttr.StringValue;

                // TODO: ISSUE#163 change this to an RXT200 when can handle localization of Xamarin.Forms apps
                if (!string.IsNullOrWhiteSpace(value) && char.IsLetterOrDigit(value[0]))
                {
                    result.HighlightAttributeWithoutAction(
                        errorType: RapidXamlErrorType.Warning,
                        code: "RXT201",
                        description: StringRes.UI_XamlAnalysisGenericHardCodedStringDescription.WithParams(Elements.Entry, Attributes.Text, value),
                        attribute: txtAttr);
                }
            }

            var phAttr = element.GetAttributes(Attributes.Placeholder).FirstOrDefault();

            if (phAttr != null && phAttr.HasStringValue)
            {
                var value = phAttr.StringValue;

                // TODO: ISSUE#163 change this to an RXT200 when can handle localization of Xamarin.Forms apps
                if (!string.IsNullOrWhiteSpace(value) && char.IsLetterOrDigit(value[0]))
                {
                    result.HighlightAttributeWithoutAction(
                        errorType: RapidXamlErrorType.Warning,
                        code: "RXT201",
                        description: StringRes.UI_XamlAnalysisGenericHardCodedStringDescription.WithParams(Elements.Entry, Attributes.Placeholder, value),
                        attribute: phAttr);
                }
            }

            var isPwdAttr = element.GetAttributes(Attributes.IsPassword).FirstOrDefault();

            if (isPwdAttr != null && isPwdAttr.HasStringValue)
            {
                var value = isPwdAttr.StringValue;

                if (value.Equals("true", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (!element.ContainsAttribute(Attributes.MaxLength))
                    {
                        result.AddAttribute(
                            errorType: RapidXamlErrorType.Suggestion,
                            code: "RXT301",
                            description: StringRes.UI_XamlAnalysisPasswordWithoutMaxLengthDescription,
                            extendedMessage: StringRes.UI_XamlAnalysisPasswordWithoutMaxLengthExtendedMessage,
                            actionText: StringRes.UI_UndoContextAddMaxLangthProperty,
                            addAttributeName: Attributes.MaxLength,
                            addAttributeValue: "100");
                    }
                }
            }

            return(result);
        }
        // Note. Not handled: Col & Row defs of different formats
        // Also, should not return an option to switch to the shorter format if using Min/Max Width/Height properties.
        public AnalysisActions Analyze(RapidXamlElement element, ExtraAnalysisDetails extraDetails)
        {
            var colDef = element.GetAttributes("ColumnDefinitions")?.FirstOrDefault();
            var rowDef = element.GetAttributes("RowDefinitions")?.FirstOrDefault();

            bool isOldFormat = true;

            var newColDefXaml = new System.Text.StringBuilder();
            var newRowDefXaml = new System.Text.StringBuilder();

            if (colDef != null)
            {
                if (colDef.HasStringValue)
                {
                    isOldFormat = false;

                    var shortValue = colDef.StringValue;

                    var colValues = shortValue.Split(',');

                    newColDefXaml.AppendLine("<Grid.ColumnDefinitions>");
                    foreach (var colValue in colValues)
                    {
                        newColDefXaml.AppendLine($"<ColumnDefinition Width=\"{colValue.Trim()}\" />");
                    }

                    newColDefXaml.AppendLine("</Grid.ColumnDefinitions>");
                }
                else
                {
                    foreach (var oldColDef in colDef.Children)
                    {
                        if (oldColDef.Name == "ColumnDefinition")
                        {
                            // Allow for width being optional and defaulting to Star
                            var width = oldColDef.GetAttributes("Width")?.First()?.StringValue ?? "*";

                            if (newColDefXaml.Length > 0)
                            {
                                newColDefXaml.Append(", ");
                            }

                            newColDefXaml.Append(width);
                        }
                    }
                }
            }

            if (rowDef != null)
            {
                if (rowDef.HasStringValue)
                {
                    isOldFormat = false;

                    var shortValue = rowDef.StringValue;

                    var rowValues = shortValue.Split(',');

                    newRowDefXaml.AppendLine("<Grid.RowDefinitions>");
                    foreach (var rowValue in rowValues)
                    {
                        newRowDefXaml.AppendLine($"<RowDefinition Height=\"{rowValue.Trim()}\" />");
                    }

                    newRowDefXaml.AppendLine("</Grid.RowDefinitions>");
                }
                else
                {
                    foreach (var oldRowDef in rowDef.Children)
                    {
                        if (oldRowDef.Name == "RowDefinition")
                        {
                            // Allow for width being optional and defaulting to Star
                            var height = oldRowDef.GetAttributes("Height")?.First()?.StringValue ?? "*";

                            if (newRowDefXaml.Length > 0)
                            {
                                newRowDefXaml.Append(", ");
                            }

                            newRowDefXaml.Append(height);
                        }
                    }
                }
            }

            if (colDef != null || rowDef != null)
            {
                if (isOldFormat)
                {
                    // Offer option to use shorter (new) syntax.
                    return(AnalysisActions
                           .RemoveAttribute(
                               RapidXamlErrorType.Warning,
                               "WINUI673A",
                               description: "Can change to other syntax.",
                               actionText: "Use shorter definition syntax",
                               attribute: colDef,
                               moreInfoUrl: "https://github.com/microsoft/microsoft-ui-xaml/issues/673")
                           .AndAddAttribute("ColumnDefinitions", newColDefXaml.ToString().TrimEnd(',', ' '))
                           .AndRemoveAttribute(rowDef)
                           .AndAddAttribute("RowDefinitions", newRowDefXaml.ToString().TrimEnd(',', ' ')));
                }
                else
                {
                    // Offer option to use longer (old) syntax.
                    return(AnalysisActions
                           .RemoveAttribute(
                               RapidXamlErrorType.Warning,
                               "WINUI673B",
                               description: "Can change to other syntax.",
                               actionText: "Use old definition syntax",
                               attribute: colDef,
                               moreInfoUrl: "https://github.com/microsoft/microsoft-ui-xaml/issues/673")
                           .AndAddChildString(newColDefXaml.ToString())
                           .AndRemoveAttribute(rowDef)
                           .AndAddChildString(newRowDefXaml.ToString()));
                }
            }

            // If not returned an action
            return(AnalysisActions.None);
        }