Exemplo n.º 1
0
        /// <summary>
        /// 分析样式的值,分解出其中的表达式和参数部分
        /// </summary>
        /// <param name="styleValue"></param>
        /// <returns></returns>
        public static HtmlStyleValueExpression ParseValue(string styleValue)
        {
            HtmlStyleValueExpression result = new HtmlStyleValueExpression();

            result.Expression = styleValue;

            if (styleValue.IsNotEmpty())
            {
                using (HtmlStyleValueParserContext context = new HtmlStyleValueParserContext())
                {
                    while (context.Index < styleValue.Length)
                    {
                        switch (context.Stage)
                        {
                        case HtmlStyleValueParsingStage.None:
                        {
                            if (SkipNotLetterAndDigit(context, styleValue))
                            {
                                context.Stage = HtmlStyleValueParsingStage.ExpressionStage;
                            }
                            break;
                        }

                        case HtmlStyleValueParsingStage.ExpressionStage:
                        {
                            if (styleValue[context.Index] != '(')
                            {
                                context.Writer.Write(styleValue[context.Index++]);
                            }
                            else
                            {
                                context.ParenthesesLevel++;
                                result.Expression = context.ChangeStage(HtmlStyleValueParsingStage.ValueStage);

                                context.Index++;
                            }
                            break;
                        }

                        case HtmlStyleValueParsingStage.ValueStage:
                        {
                            if (styleValue[context.Index] != ')')
                            {
                                context.Writer.Write(styleValue[context.Index++]);
                            }
                            else
                            {
                                context.ParenthesesLevel--;

                                if (context.ParenthesesLevel == 0)
                                {
                                    result.Value = context.ChangeStage(HtmlStyleValueParsingStage.None);
                                }

                                context.Index++;
                            }

                            break;
                        }
                        }
                    }

                    DoStyleValuePostOperation(context, result);
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        private static HtmlStyleValueExpression DoStyleValuePostOperation(HtmlStyleValueParserContext context, HtmlStyleValueExpression styleExpression)
        {
            if (styleExpression != null)
            {
                switch (context.Stage)
                {
                case HtmlStyleValueParsingStage.ExpressionStage:
                    styleExpression.Expression = context.ChangeStage(HtmlStyleValueParsingStage.None);
                    break;

                case HtmlStyleValueParsingStage.ValueStage:
                    styleExpression.Value = context.ChangeStage(HtmlStyleValueParsingStage.None);
                    break;
                }
            }

            return(styleExpression);
        }