Exemplo n.º 1
0
        private static HtmlStyleAttribute DoStylePostOperation(HtmlStyleParserContext context, HtmlStyleAttribute currentStyleAttr)
        {
            if (currentStyleAttr != null)
            {
                switch (context.Stage)
                {
                case HtmlStyleParsingStage.KeyStage:
                    currentStyleAttr.Key        = context.ChangeStage(HtmlStyleParsingStage.ValueStage);
                    currentStyleAttr.Expression = ParseValue(currentStyleAttr.Value);
                    break;

                case HtmlStyleParsingStage.ValueStage:
                    currentStyleAttr.Value      = context.ChangeStage(HtmlStyleParsingStage.None);
                    currentStyleAttr.Expression = ParseValue(currentStyleAttr.Value);
                    break;
                }
            }

            return(currentStyleAttr);
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="style"></param>
        /// <returns></returns>
        public static HtmlStyleAttributeCollection Parse(string style)
        {
            HtmlStyleAttributeCollection result = new HtmlStyleAttributeCollection();

            if (style.IsNullOrEmpty())
            {
                return(result);
            }

            using (HtmlStyleParserContext context = new HtmlStyleParserContext())
            {
                HtmlStyleAttribute currentStyleAttr = new HtmlStyleAttribute();

                while (context.Index < style.Length)
                {
                    switch (context.Stage)
                    {
                    case HtmlStyleParsingStage.None:
                    {
                        if (SkipNotLetter(context, style))
                        {
                            context.Stage    = HtmlStyleParsingStage.KeyStage;
                            currentStyleAttr = new HtmlStyleAttribute();
                        }
                        break;
                    }

                    case HtmlStyleParsingStage.KeyStage:
                    {
                        if (style[context.Index] != ':')
                        {
                            context.Writer.Write(style[context.Index++]);
                        }
                        else
                        {
                            currentStyleAttr.Key = context.ChangeStage(HtmlStyleParsingStage.ValueStage);
                            context.Index++;
                        }

                        break;
                    }

                    case HtmlStyleParsingStage.ValueStage:
                    {
                        if (style[context.Index] != ';')
                        {
                            context.Writer.Write(style[context.Index++]);
                        }
                        else
                        {
                            currentStyleAttr.Value      = context.ChangeStage(HtmlStyleParsingStage.None);
                            currentStyleAttr.Expression = ParseValue(currentStyleAttr.Value);
                            result.AddNotExistsItem(currentStyleAttr);

                            currentStyleAttr = null;
                            context.Index++;
                        }

                        break;
                    }
                    }
                }

                currentStyleAttr = DoStylePostOperation(context, currentStyleAttr);

                if (currentStyleAttr != null)
                {
                    result.AddNotExistsItem(currentStyleAttr);
                }
            }

            return(result);
        }