Example #1
0
        /// <summary>
        /// 5.4.8. Consume a function
        /// </summary>
        /// <returns></returns>
        private Function ConsumeFunction()
        {
            //Create a function with a name equal to the value of the current input token, and with a value which is initially an empty list.

            /// before calling this method, the calling method must already check that current token is a function token.
            function_token token = currentToken as function_token;

            Function func = new Function();

            func.name       = token.Value;
            func.startindex = token.startIndex;

            cssToken nexttoken = null;

            while (true)
            {
                //Repeatedly consume the next input token and process it as follows:
                nexttoken = ConsumeNextToken();

                //<EOF-token>
                //<)-token>
                if (nexttoken.Type == enumTokenType.EOF || nexttoken.Type == enumTokenType.round_bracket_right)
                {
                    //Return the function.
                    func.endindex = nexttoken.endIndex;
                    if (nexttoken.Type == enumTokenType.EOF)
                    {
                        func.endindex = func.endindex - 1;
                    }
                    return(func);
                }

                //anything else
                else
                {
                    //Reconsume the current input token. Consume a component value and append the returned value to the function’s value.
                    ReconsumeToken();
                    ComponentValue value = ConsumeComponentValue();
                    func.value.Add(value);
                }
            }
        }
Example #2
0
        public static List <UrlInfo> GetUrlInfos(string CssText)
        {
            List <UrlInfo> urllist = new List <UrlInfo>();

            if (string.IsNullOrEmpty(CssText))
            {
                return(urllist);
            }

            Kooboo.Dom.CSS.Tokenizer toknizer = new Kooboo.Dom.CSS.Tokenizer(CssText);

            while (true)
            {
                cssToken token = toknizer.ConsumeNextToken();

                if (token == null || token.Type == enumTokenType.EOF)
                {
                    break;
                }
                else
                {
                    if (token.Type == enumTokenType.url)
                    {
                        url_token urltoken    = token as url_token;
                        string    resourceurl = urltoken.value;
                        if (!string.IsNullOrEmpty(resourceurl))
                        {
                            urllist.Add(new UrlInfo()
                            {
                                StartIndex = urltoken.startIndex, EndIndex = urltoken.endIndex, RawUrl = resourceurl, isUrlToken = true
                            });
                        }
                    }
                    else if (token.Type == enumTokenType.at_keyword)
                    {
                        at_keyword_token keywordtoken = token as at_keyword_token;
                        if (keywordtoken.value.ToLower() == "import")
                        {
                            cssToken nexttoken = toknizer.ConsumeNextToken();
                            while (nexttoken.Type == enumTokenType.whitespace)
                            {
                                nexttoken = toknizer.ConsumeNextToken();
                            }

                            if (nexttoken.Type == enumTokenType.String)
                            {
                                string_token stringtoken = nexttoken as string_token;

                                if (!string.IsNullOrEmpty(stringtoken.value))
                                {
                                    urllist.Add(new UrlInfo()
                                    {
                                        StartIndex = stringtoken.startIndex, EndIndex = stringtoken.endIndex, RawUrl = stringtoken.value, isImportRule = true
                                    });
                                }
                            }
                            else if (nexttoken.Type == enumTokenType.url)
                            {
                                url_token urltoken    = nexttoken as url_token;
                                string    resourceurl = urltoken.value;
                                if (!string.IsNullOrEmpty(resourceurl))
                                {
                                    urllist.Add(new UrlInfo()
                                    {
                                        StartIndex = urltoken.startIndex, EndIndex = urltoken.endIndex, RawUrl = resourceurl, isUrlToken = true, isImportRule = true
                                    });
                                }
                            }

                            else if (nexttoken.Type == enumTokenType.function)
                            {
                                function_token functoken = nexttoken as function_token;

                                string functionvalue = functoken.Value;
                                if (functionvalue.ToLower() == "url")
                                {
                                    nexttoken = toknizer.ConsumeNextToken();
                                    while (nexttoken.Type == enumTokenType.whitespace)
                                    {
                                        nexttoken = toknizer.ConsumeNextToken();
                                    }
                                    if (nexttoken == null || nexttoken.Type == enumTokenType.EOF)
                                    {
                                        break;
                                    }

                                    if (nexttoken.Type == enumTokenType.String)
                                    {
                                        string_token stringtoken = nexttoken as string_token;

                                        if (!string.IsNullOrEmpty(stringtoken.value))
                                        {
                                            urllist.Add(new UrlInfo()
                                            {
                                                StartIndex = stringtoken.startIndex, EndIndex = stringtoken.endIndex, RawUrl = stringtoken.value, isImportRule = true
                                            });
                                        }
                                    }
                                    else if (nexttoken.Type == enumTokenType.url)
                                    {
                                        url_token urltoken    = nexttoken as url_token;
                                        string    resourceurl = urltoken.value;
                                        if (!string.IsNullOrEmpty(resourceurl))
                                        {
                                            urllist.Add(new UrlInfo()
                                            {
                                                StartIndex = urltoken.startIndex, EndIndex = urltoken.endIndex, RawUrl = resourceurl, isUrlToken = true, isImportRule = true
                                            });
                                        }
                                    }
                                }
                            }
                        }
                    }

                    else if (token.Type == enumTokenType.function)
                    {
                        function_token functoken = token as function_token;

                        string functionvalue = functoken.Value;
                        if (functionvalue.ToLower() == "url")
                        {
                            cssToken nexttoken = toknizer.ConsumeNextToken();
                            while (nexttoken.Type == enumTokenType.whitespace)
                            {
                                nexttoken = toknizer.ConsumeNextToken();
                            }
                            if (nexttoken == null || nexttoken.Type == enumTokenType.EOF)
                            {
                                break;
                            }

                            if (nexttoken.Type == enumTokenType.String)
                            {
                                string_token stringtoken = nexttoken as string_token;

                                if (!string.IsNullOrEmpty(stringtoken.value))
                                {
                                    urllist.Add(new UrlInfo()
                                    {
                                        StartIndex = stringtoken.startIndex, EndIndex = stringtoken.endIndex, RawUrl = stringtoken.value
                                    });
                                }
                            }
                        }
                    }
                }
            }

            return(cleanSpecialMark(urllist));
        }