/// <summary> /// 5.4.6. Consume a component value /// </summary> /// <returns></returns> private ComponentValue ConsumeComponentValue() { //Consume the next input token. cssToken token = ConsumeNextToken(); //If the current input token is a <{-token>, <[-token>, or <(-token>, consume a simple block and return it. if (token.Type == enumTokenType.curly_bracket_left || token.Type == enumTokenType.square_bracket_left || token.Type == enumTokenType.round_bracket_left) { SimpleBlock simpleblock = ConsumeSimpleBlock(); simpleblock.startindex = token.startIndex; return(simpleblock); } //Otherwise, if the current input token is a <function-token>, consume a function and return it. else if (token.Type == enumTokenType.function) { Function func = ConsumeFunction(); func.startindex = token.startIndex; return(func); } else { //Otherwise, return the current input token. PreservedToken preservedtoken = new PreservedToken(); preservedtoken.token = token; preservedtoken.startindex = token.startIndex; preservedtoken.endindex = token.endIndex; return(preservedtoken); } }
/// <summary> /// 5.4.3. Consume a qualified rule /// </summary> /// <returns></returns> private QualifiedRule ConsumeQualifiedRule() { //To consume a qualified rule: //Create a new qualified rule with its prelude initially set to an empty list, and its value initially set to nothing. QualifiedRule rule = new QualifiedRule(); // set operation is performed by CTOR. int startindex = -1; cssToken token = null; while (true) { //Repeatedly consume the next input token: token = ConsumeNextToken(); if (startindex == -1) { startindex = token.startIndex; } //<EOF-token> if (token.Type == enumTokenType.EOF) { //This is a parse error. Return nothing. return(null); } //<{-token> else if (token.Type == enumTokenType.curly_bracket_left) { //Consume a simple block and assign it to the qualified rule’s block. Return the qualified rule. SimpleBlock block = ConsumeSimpleBlock(); block.startindex = token.startIndex; rule.block = block; rule.startindex = startindex; rule.endindex = block.endindex; return(rule); } //simple block with an associated token of <{-token> //???????TODO: this must be an mistake in the syntax paper.. //Assign the block to the qualified rule’s block. Return the qualified rule. //anything else else { //Reconsume the current input token. Consume a component value. Append the returned value to the qualified rule’s prelude. ReconsumeToken(); ComponentValue value = ConsumeComponentValue(); rule.prelude.Add(value); } } }
/// <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); } } }
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)); }
private cssToken ConsumeNextToken() { currentToken = tokenizer.ConsumeNextToken(); return(currentToken); }
/// <summary> /// 5.4.2. Consume an at-rule /// </summary> /// <returns></returns> private AtRule ConsumeAtRule() { //To consume an at-rule: //Create a new at-rule with its name set to the value of the current input token, its prelude initially set to an empty list, and its value initially set to nothing. AtRule rule = new AtRule(); int startindex = -1; cssToken token = null; while (true) { //Repeatedly consume the next input token: token = ConsumeNextToken(); if (startindex == -1) { startindex = token.startIndex; } //<semicolon-token> //<EOF-token> if (token.Type == enumTokenType.semicolon || token.Type == enumTokenType.EOF) { //Return the at-rule. rule.startindex = startindex; rule.endindex = token.endIndex; if (token.Type == enumTokenType.EOF) { rule.endindex = rule.endindex - 1; } return(rule); } //<{-token> else if (token.Type == enumTokenType.curly_bracket_left) { //Consume a simple block and assign it to the at-rule’s block. Return the at-rule. SimpleBlock simpleblock = ConsumeSimpleBlock(); simpleblock.startindex = token.startIndex; rule.block = simpleblock; rule.startindex = startindex; rule.endindex = simpleblock.endindex; return(rule); } //simple block with an associated token of <{-token> //Assign the block to the at-rule’s block. Return the at-rule. ///TODO: ???? check what does this means??? //anything else else { //Reconsume the current input token. Consume a component value. Append the returned value to the at-rule’s prelude. ReconsumeToken(); ComponentValue value = ConsumeComponentValue(); rule.prelude.Add(value); } } }