Example #1
0
        public StyleValidationResult ValidateProperty(string name, string value)
        {
            var result = new StyleValidationResult()
            {
                status = StyleValidationStatus.Ok
            };

            // Bypass custom styles
            if (name.StartsWith("--"))
            {
                return(result);
            }

            string syntax;

            if (!StylePropertyCache.TryGetSyntax(name, out syntax))
            {
                string closestName = StylePropertyCache.FindClosestPropertyName(name);
                result.status  = StyleValidationStatus.Error;
                result.message = $"Unknown property '{name}'";
                if (!string.IsNullOrEmpty(closestName))
                {
                    result.message = $"{result.message} (did you mean '{closestName}'?)";
                }

                return(result);
            }

            var syntaxTree = m_SyntaxParser.Parse(syntax);

            if (syntaxTree == null)
            {
                result.status  = StyleValidationStatus.Error;
                result.message = $"Invalid '{name}' property syntax '{syntax}'";
                return(result);
            }

            var matchResult = m_StyleMatcher.Match(syntaxTree, value);

            if (!matchResult.success)
            {
                result.errorValue = matchResult.errorValue;
                switch (matchResult.errorCode)
                {
                case MatchResultErrorCode.Syntax:
                    result.status = StyleValidationStatus.Error;
                    if (IsUnitMissing(syntax, value))
                    {
                        result.hint = "Property expects a unit. Did you forget to add px or %?";
                    }
                    else if (IsUnsupportedColor(syntax))
                    {
                        result.hint = $"Unsupported color '{value}'.";
                    }
                    result.message = $"Expected ({syntax}) but found '{matchResult.errorValue}'";
                    break;

                case MatchResultErrorCode.EmptyValue:
                    result.status  = StyleValidationStatus.Error;
                    result.message = $"Expected ({syntax}) but found empty value";
                    break;

                case MatchResultErrorCode.ExpectedEndOfValue:
                    result.status  = StyleValidationStatus.Warning;
                    result.message = $"Expected end of value but found '{matchResult.errorValue}'";
                    break;

                default:
                    Debug.LogAssertion($"Unexpected error code '{matchResult.errorCode}'");
                    break;
                }
            }

            return(result);
        }
        public StyleValidationResult ValidateProperty(string name, string value)
        {
            StyleValidationResult styleValidationResult = new StyleValidationResult
            {
                status = StyleValidationStatus.Ok
            };
            bool flag = name.StartsWith("--");
            StyleValidationResult result;

            if (flag)
            {
                result = styleValidationResult;
            }
            else
            {
                string text;
                bool   flag2 = !StylePropertyCache.TryGetSyntax(name, out text);
                if (flag2)
                {
                    string text2 = StylePropertyCache.FindClosestPropertyName(name);
                    styleValidationResult.status  = StyleValidationStatus.Error;
                    styleValidationResult.message = "Unknown property '" + name + "'";
                    bool flag3 = !string.IsNullOrEmpty(text2);
                    if (flag3)
                    {
                        styleValidationResult.message = styleValidationResult.message + " (did you mean '" + text2 + "'?)";
                    }
                    result = styleValidationResult;
                }
                else
                {
                    Expression expression = this.m_SyntaxParser.Parse(text);
                    bool       flag4      = expression == null;
                    if (flag4)
                    {
                        styleValidationResult.status  = StyleValidationStatus.Error;
                        styleValidationResult.message = string.Concat(new string[]
                        {
                            "Invalid '",
                            name,
                            "' property syntax '",
                            text,
                            "'"
                        });
                        result = styleValidationResult;
                    }
                    else
                    {
                        MatchResult matchResult = this.m_StyleMatcher.Match(expression, value);
                        bool        flag5       = !matchResult.success;
                        if (flag5)
                        {
                            styleValidationResult.errorValue = matchResult.errorValue;
                            switch (matchResult.errorCode)
                            {
                            case MatchResultErrorCode.Syntax:
                            {
                                styleValidationResult.status = StyleValidationStatus.Error;
                                bool flag6 = this.IsUnitMissing(text, value);
                                if (flag6)
                                {
                                    styleValidationResult.hint = "Property expects a unit. Did you forget to add px or %?";
                                }
                                else
                                {
                                    bool flag7 = this.IsUnsupportedColor(text);
                                    if (flag7)
                                    {
                                        styleValidationResult.hint = "Unsupported color '" + value + "'.";
                                    }
                                }
                                styleValidationResult.message = string.Concat(new string[]
                                    {
                                        "Expected (",
                                        text,
                                        ") but found '",
                                        matchResult.errorValue,
                                        "'"
                                    });
                                break;
                            }

                            case MatchResultErrorCode.EmptyValue:
                                styleValidationResult.status  = StyleValidationStatus.Error;
                                styleValidationResult.message = "Expected (" + text + ") but found empty value";
                                break;

                            case MatchResultErrorCode.ExpectedEndOfValue:
                                styleValidationResult.status  = StyleValidationStatus.Warning;
                                styleValidationResult.message = "Expected end of value but found '" + matchResult.errorValue + "'";
                                break;

                            default:
                                Debug.LogAssertion(string.Format("Unexpected error code '{0}'", matchResult.errorCode));
                                break;
                            }
                        }
                        result = styleValidationResult;
                    }
                }
            }
            return(result);
        }