예제 #1
0
 private static int CodePoint(char[] a)
 {
     if (a != null)
     {
         if (a.Length == 1 && JavaUtil.IsValidCodePoint(a[0]))
         {
             return(a[0]);
         }
         else
         {
             if (a.Length == 2 && Char.IsHighSurrogate(a[0]) && Char.IsLowSurrogate(a[1]))
             {
                 return(JavaUtil.ToCodePoint(a[0], a[1]));
             }
         }
     }
     return(-1);
 }
        /// <summary>Gets the next token.</summary>
        /// <returns>the next token</returns>
        private CssDeclarationValueTokenizer.Token GetNextToken()
        {
            StringBuilder buff = new StringBuilder();
            char          curChar;

            if (index >= src.Length - 1)
            {
                return(null);
            }
            if (inString)
            {
                bool          isEscaped = false;
                StringBuilder pendingUnicodeSequence = new StringBuilder();
                while (++index < src.Length)
                {
                    curChar = src[index];
                    if (isEscaped)
                    {
                        if (IsHexDigit(curChar) && pendingUnicodeSequence.Length < 6)
                        {
                            pendingUnicodeSequence.Append(curChar);
                        }
                        else
                        {
                            if (pendingUnicodeSequence.Length != 0)
                            {
                                int codePoint = Convert.ToInt32(pendingUnicodeSequence.ToString(), 16);
                                if (JavaUtil.IsValidCodePoint(codePoint))
                                {
                                    buff.AppendCodePoint(codePoint);
                                }
                                else
                                {
                                    buff.Append("\uFFFD");
                                }
                                pendingUnicodeSequence.Length = 0;
                                if (curChar == stringQuote)
                                {
                                    inString = false;
                                    return(new CssDeclarationValueTokenizer.Token(buff.ToString(), CssDeclarationValueTokenizer.TokenType.STRING
                                                                                  ));
                                }
                                else
                                {
                                    if (!iText.IO.Util.TextUtil.IsWhiteSpace(curChar))
                                    {
                                        buff.Append(curChar);
                                    }
                                }
                                isEscaped = false;
                            }
                            else
                            {
                                buff.Append(curChar);
                                isEscaped = false;
                            }
                        }
                    }
                    else
                    {
                        if (curChar == stringQuote)
                        {
                            inString = false;
                            return(new CssDeclarationValueTokenizer.Token(buff.ToString(), CssDeclarationValueTokenizer.TokenType.STRING
                                                                          ));
                        }
                        else
                        {
                            if (curChar == '\\')
                            {
                                isEscaped = true;
                            }
                            else
                            {
                                buff.Append(curChar);
                            }
                        }
                    }
                }
            }
            else
            {
                while (++index < src.Length)
                {
                    curChar = src[index];
                    if (curChar == '(')
                    {
                        ++functionDepth;
                        buff.Append(curChar);
                    }
                    else
                    {
                        if (curChar == ')')
                        {
                            --functionDepth;
                            buff.Append(curChar);
                            if (functionDepth == 0)
                            {
                                return(new CssDeclarationValueTokenizer.Token(buff.ToString(), CssDeclarationValueTokenizer.TokenType.FUNCTION
                                                                              ));
                            }
                        }
                        else
                        {
                            if (curChar == '"' || curChar == '\'')
                            {
                                stringQuote = curChar;
                                inString    = true;
                                return(new CssDeclarationValueTokenizer.Token(buff.ToString(), CssDeclarationValueTokenizer.TokenType.FUNCTION
                                                                              ));
                            }
                            else
                            {
                                if (curChar == ',' && !inString && functionDepth == 0)
                                {
                                    return(new CssDeclarationValueTokenizer.Token(",", CssDeclarationValueTokenizer.TokenType.COMMA));
                                }
                                else
                                {
                                    if (iText.IO.Util.TextUtil.IsWhiteSpace(curChar))
                                    {
                                        if (functionDepth > 0)
                                        {
                                            buff.Append(curChar);
                                        }
                                        return(new CssDeclarationValueTokenizer.Token(buff.ToString(), functionDepth > 0 ? CssDeclarationValueTokenizer.TokenType
                                                                                      .FUNCTION : CssDeclarationValueTokenizer.TokenType.UNKNOWN));
                                    }
                                    else
                                    {
                                        buff.Append(curChar);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(new CssDeclarationValueTokenizer.Token(buff.ToString(), CssDeclarationValueTokenizer.TokenType.FUNCTION
                                                          ));
        }