OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 #include "config.h" |
| 32 #include "core/css/parser/MediaQueryTokenizer.h" |
| 33 |
| 34 #include "core/css/parser/CSSInputStream.h" |
| 35 #include "core/html/parser/HTMLParserIdioms.h" |
| 36 #include "wtf/unicode/CharacterNames.h" |
| 37 #include <cfloat> |
| 38 |
| 39 namespace WebCore { |
| 40 |
| 41 // http://dev.w3.org/csswg/css-syntax/#name-start-code-point |
| 42 static bool isNameStart(UChar c) |
| 43 { |
| 44 if (isASCIIAlpha(c)) |
| 45 return true; |
| 46 if (c == '_') |
| 47 return true; |
| 48 return !isASCII(c); |
| 49 } |
| 50 |
| 51 // http://www.w3.org/TR/css-syntax-3/#name-code-point |
| 52 static bool isNameChar(UChar c) |
| 53 { |
| 54 return isNameStart(c) || isASCIIDigit(c) || c == '-'; |
| 55 } |
| 56 |
| 57 // http://www.w3.org/TR/css-syntax-3/#check-if-two-code-points-are-a-valid-escap
e |
| 58 static bool twoCharsAreValidEscape(UChar first, UChar second) |
| 59 { |
| 60 return ((first == '\\') && (second != '\n') && (second != kEndOfFileMarker))
; |
| 61 } |
| 62 |
| 63 MediaQueryTokenizer::MediaQueryTokenizer() |
| 64 { |
| 65 } |
| 66 |
| 67 void MediaQueryTokenizer::reconsume(UChar c) |
| 68 { |
| 69 m_input->pushBack(c); |
| 70 } |
| 71 |
| 72 UChar MediaQueryTokenizer::consume() |
| 73 { |
| 74 UChar current = m_input->currentInputChar(); |
| 75 m_input->advance(); |
| 76 return current; |
| 77 } |
| 78 |
| 79 void MediaQueryTokenizer::consume(unsigned offset) |
| 80 { |
| 81 m_input->advance(offset); |
| 82 } |
| 83 |
| 84 CSSToken MediaQueryTokenizer::whiteSpace(UChar cc) |
| 85 { |
| 86 // CSS Tokenization is currently lossy, but we could record |
| 87 // the exact whitespace instead of discarding it here. |
| 88 consumeUntilNotWhitespace(); |
| 89 return CSSToken(WhitespaceToken); |
| 90 } |
| 91 |
| 92 CSSToken MediaQueryTokenizer::leftParen(UChar cc) |
| 93 { |
| 94 return CSSToken(LeftParenToken); |
| 95 } |
| 96 |
| 97 CSSToken MediaQueryTokenizer::rightParen(UChar cc) |
| 98 { |
| 99 return CSSToken(RightParenToken); |
| 100 } |
| 101 |
| 102 CSSToken MediaQueryTokenizer::plusOrFullStop(UChar cc) |
| 103 { |
| 104 if (nextCharsAreNumber()) { |
| 105 reconsume(cc); |
| 106 return consumeNumericToken(); |
| 107 } |
| 108 return CSSToken(DelimToken, cc); |
| 109 } |
| 110 |
| 111 CSSToken MediaQueryTokenizer::comma(UChar cc) |
| 112 { |
| 113 return CSSToken(CommaToken); |
| 114 } |
| 115 |
| 116 CSSToken MediaQueryTokenizer::hyphenMinus(UChar cc) |
| 117 { |
| 118 if (nextCharsAreNumber()) { |
| 119 reconsume(cc); |
| 120 return consumeNumericToken(); |
| 121 } |
| 122 if (nextCharsAreIdentifier()) { |
| 123 reconsume(cc); |
| 124 return consumeIdentLikeToken(); |
| 125 } |
| 126 return CSSToken(DelimToken, cc); |
| 127 } |
| 128 |
| 129 CSSToken MediaQueryTokenizer::solidus(UChar cc) |
| 130 { |
| 131 return CSSToken(DelimToken, cc); |
| 132 } |
| 133 |
| 134 CSSToken MediaQueryTokenizer::colon(UChar cc) |
| 135 { |
| 136 return CSSToken(ColonToken); |
| 137 } |
| 138 |
| 139 CSSToken MediaQueryTokenizer::semiColon(UChar cc) |
| 140 { |
| 141 return CSSToken(SemicolonToken); |
| 142 } |
| 143 |
| 144 CSSToken MediaQueryTokenizer::reverseSolidus(UChar cc) |
| 145 { |
| 146 if (twoCharsAreValidEscape(cc, m_input->currentInputChar())) { |
| 147 reconsume(cc); |
| 148 return consumeIdentLikeToken(); |
| 149 } |
| 150 return CSSToken(DelimToken, cc); |
| 151 } |
| 152 |
| 153 CSSToken MediaQueryTokenizer::asciiDigit(UChar cc) |
| 154 { |
| 155 reconsume(cc); |
| 156 return consumeNumericToken(); |
| 157 } |
| 158 |
| 159 CSSToken MediaQueryTokenizer::nameStart(UChar cc) |
| 160 { |
| 161 reconsume(cc); |
| 162 return consumeIdentLikeToken(); |
| 163 } |
| 164 |
| 165 CSSToken MediaQueryTokenizer::endOfFile(UChar cc) |
| 166 { |
| 167 return CSSToken(EOFToken); |
| 168 } |
| 169 |
| 170 void MediaQueryTokenizer::tokenize(String string, Vector<CSSToken>& outTokens) |
| 171 { |
| 172 MediaQueryTokenizer tokenizer; |
| 173 // According to the spec, we should perform preprocessing here. |
| 174 // See: http://www.w3.org/TR/css-syntax-3/#input-preprocessing |
| 175 // |
| 176 // However, we can skip this step since: |
| 177 // * We're using HTML spaces (which accept \r and \f as a valid white space) |
| 178 // * Do not count white spaces |
| 179 // * consumeEscape replaces NULLs for replacement characters |
| 180 |
| 181 CSSInputStream input(string); |
| 182 while (true) { |
| 183 outTokens.append(tokenizer.nextToken(input)); |
| 184 if (outTokens.last().type() == EOFToken) |
| 185 return; |
| 186 } |
| 187 } |
| 188 |
| 189 CSSToken MediaQueryTokenizer::nextToken(CSSInputStream& input) |
| 190 { |
| 191 // Unlike the HTMLTokenizer, the CSS Syntax spec is written |
| 192 // as a stateless, (fixed-size) look-ahead tokenizer. |
| 193 // We could move to the stateful model and instead create |
| 194 // states for all the "next 3 codepoints are X" cases. |
| 195 // State-machine tokenizers are easier to write to handle |
| 196 // incremental tokenization of partial sources. |
| 197 // However, for now we follow the spec exactly. |
| 198 m_input = &input; |
| 199 UChar cc = consume(); |
| 200 CodePoint codePointFunc = 0; |
| 201 |
| 202 if (isASCII(cc)) |
| 203 codePointFunc = getCodePoints()->codePoints[cc]; |
| 204 else |
| 205 codePointFunc = &MediaQueryTokenizer::nameStart; |
| 206 |
| 207 if (codePointFunc) |
| 208 return ((this)->*(codePointFunc))(cc); |
| 209 |
| 210 return CSSToken(DelimToken, cc); |
| 211 } |
| 212 |
| 213 // This method merges the following spec sections for efficiency |
| 214 // http://www.w3.org/TR/css3-syntax/#consume-a-number |
| 215 // http://www.w3.org/TR/css3-syntax/#convert-a-string-to-a-number |
| 216 CSSToken MediaQueryTokenizer::consumeNumber() |
| 217 { |
| 218 ASSERT(nextCharsAreNumber()); |
| 219 NumericValueType type = IntegerValueType; |
| 220 double value = 0; |
| 221 int sign = 1; |
| 222 unsigned peekOffset = 0; |
| 223 int exponentSign = 1; |
| 224 unsigned exponentStartPos = 0; |
| 225 unsigned exponentEndPos = 0; |
| 226 unsigned fractionStartPos = 0; |
| 227 unsigned fractionEndPos = 0; |
| 228 unsigned long long integerPart; |
| 229 unsigned long long fractionPart; |
| 230 unsigned fractionDigits; |
| 231 unsigned long long exponentPart; |
| 232 if (m_input->currentInputChar() == '+') { |
| 233 ++peekOffset; |
| 234 } else if (m_input->peek(peekOffset) == '-') { |
| 235 sign = -1; |
| 236 ++peekOffset; |
| 237 } |
| 238 unsigned intStartPos = peekOffset; |
| 239 peekOffset = m_input->skipWhilePredicate<isASCIIDigit>(peekOffset); |
| 240 unsigned intEndPos = peekOffset; |
| 241 if (m_input->peek(peekOffset) == '.' && isASCIIDigit(m_input->peek(++peekOff
set))) { |
| 242 fractionStartPos = peekOffset; |
| 243 peekOffset = m_input->skipWhilePredicate<isASCIIDigit>(peekOffset); |
| 244 fractionEndPos = peekOffset; |
| 245 } |
| 246 if ((m_input->peek(peekOffset) == 'E' || m_input->peek(peekOffset) == 'e'))
{ |
| 247 int peekOffsetBeforeExponent = peekOffset; |
| 248 ++peekOffset; |
| 249 if (m_input->peek(peekOffset) == '+') { |
| 250 ++peekOffset; |
| 251 } else if (m_input->peek(peekOffset) =='-') { |
| 252 exponentSign = -1; |
| 253 ++peekOffset; |
| 254 } |
| 255 exponentStartPos = peekOffset; |
| 256 peekOffset = m_input->skipWhilePredicate<isASCIIDigit>(peekOffset); |
| 257 exponentEndPos = peekOffset; |
| 258 if (exponentEndPos == exponentStartPos) |
| 259 peekOffset = peekOffsetBeforeExponent; |
| 260 } |
| 261 integerPart = m_input->getNumber(intStartPos, intEndPos); |
| 262 fractionDigits = fractionEndPos - fractionStartPos; |
| 263 unsigned floatingFractionEndPos = fractionEndPos; |
| 264 if (fractionDigits > DBL_DIG) { |
| 265 // Limit the number of fraction digits, to avoid double (and fractionPar
t) from overflowing |
| 266 fractionDigits = DBL_DIG; |
| 267 floatingFractionEndPos = fractionStartPos + DBL_DIG; |
| 268 } |
| 269 fractionPart = m_input->getNumber(fractionStartPos, floatingFractionEndPos); |
| 270 exponentPart = m_input->getNumber(exponentStartPos, exponentEndPos); |
| 271 double fractionDivisor = pow((double)10.0, (double)(fractionDigits)); |
| 272 double exponent = pow(10, (float)exponentSign * (double)exponentPart); |
| 273 value = (double)sign * ((double)integerPart + (double)fractionPart / fractio
nDivisor) * exponent; |
| 274 |
| 275 m_input->advance(peekOffset); |
| 276 if (fractionDigits > 0) |
| 277 type = NumberValueType; |
| 278 |
| 279 return CSSToken(NumberToken, value, type); |
| 280 } |
| 281 |
| 282 // http://www.w3.org/TR/css3-syntax/#consume-a-numeric-token |
| 283 CSSToken MediaQueryTokenizer::consumeNumericToken() |
| 284 { |
| 285 CSSToken token = consumeNumber(); |
| 286 if (nextCharsAreIdentifier()) |
| 287 token.convertToDimensionWithUnit(consumeName()); |
| 288 else if (consumeIfNext('%')) |
| 289 token.convertToPercentage(); |
| 290 return token; |
| 291 } |
| 292 |
| 293 // http://www.w3.org/TR/css3-syntax/#consume-an-ident-like-token |
| 294 CSSToken MediaQueryTokenizer::consumeIdentLikeToken() |
| 295 { |
| 296 String name = consumeName(); |
| 297 if (consumeIfNext('(')) |
| 298 return CSSToken(FunctionToken, name); |
| 299 return CSSToken(IdentToken, name); |
| 300 } |
| 301 |
| 302 void MediaQueryTokenizer::consumeUntilNotWhitespace() |
| 303 { |
| 304 // Using HTML space here rather than CSS space since we don't do preprocessi
ng |
| 305 while (isHTMLSpace<UChar>(m_input->currentInputChar())) |
| 306 consume(); |
| 307 } |
| 308 |
| 309 bool MediaQueryTokenizer::consumeIfNext(UChar character) |
| 310 { |
| 311 if (m_input->currentInputChar() == character) { |
| 312 consume(); |
| 313 return true; |
| 314 } |
| 315 return false; |
| 316 } |
| 317 |
| 318 // http://www.w3.org/TR/css3-syntax/#consume-a-name |
| 319 String MediaQueryTokenizer::consumeName() |
| 320 { |
| 321 // FIXME: Is this as efficient as it can be? |
| 322 // The possibility of escape chars mandates a copy AFAICT. |
| 323 Vector<UChar> result; |
| 324 while (true) { |
| 325 if (isNameChar(m_input->currentInputChar())) { |
| 326 result.append(consume()); |
| 327 continue; |
| 328 } |
| 329 if (nextTwoCharsAreValidEscape()) { |
| 330 // "consume()" fixes a spec bug. |
| 331 // The first code point should be consumed before consuming the esca
ped code point. |
| 332 consume(); |
| 333 result.append(consumeEscape()); |
| 334 continue; |
| 335 } |
| 336 return String(result); |
| 337 } |
| 338 } |
| 339 |
| 340 // http://www.w3.org/TR/css-syntax-3/#consume-an-escaped-code-point |
| 341 UChar MediaQueryTokenizer::consumeEscape() |
| 342 { |
| 343 UChar cc = consume(); |
| 344 ASSERT(cc != '\n'); |
| 345 if (isASCIIHexDigit(cc)) { |
| 346 unsigned consumedHexDigits = 1; |
| 347 String hexChars; |
| 348 do { |
| 349 hexChars.append(cc); |
| 350 cc = consume(); |
| 351 consumedHexDigits++; |
| 352 } while (consumedHexDigits < 6 && isASCIIHexDigit(cc)); |
| 353 bool ok = false; |
| 354 UChar codePoint = hexChars.toUIntStrict(&ok, 16); |
| 355 if (!ok) |
| 356 return WTF::Unicode::replacementCharacter; |
| 357 return codePoint; |
| 358 } |
| 359 |
| 360 // Replaces NULLs with replacement characters, since we do not perform prepr
ocessing |
| 361 if (cc == kEndOfFileMarker) |
| 362 return WTF::Unicode::replacementCharacter; |
| 363 return cc; |
| 364 } |
| 365 |
| 366 bool MediaQueryTokenizer::nextTwoCharsAreValidEscape() |
| 367 { |
| 368 return twoCharsAreValidEscape(m_input->peek(1), m_input->peek(2)); |
| 369 } |
| 370 |
| 371 // http://www.w3.org/TR/css3-syntax/#starts-with-a-number |
| 372 bool MediaQueryTokenizer::nextCharsAreNumber() |
| 373 { |
| 374 UChar first = m_input->currentInputChar(); |
| 375 UChar second = m_input->peek(1); |
| 376 if (isASCIIDigit(first)) |
| 377 return true; |
| 378 if (first == '+' || first == '-') |
| 379 return ((isASCIIDigit(second)) || (second == '.' && isASCIIDigit(m_input
->peek(2)))); |
| 380 if (first =='.') |
| 381 return (isASCIIDigit(second)); |
| 382 return false; |
| 383 } |
| 384 |
| 385 // http://www.w3.org/TR/css3-syntax/#would-start-an-identifier |
| 386 bool MediaQueryTokenizer::nextCharsAreIdentifier() |
| 387 { |
| 388 UChar firstChar = m_input->currentInputChar(); |
| 389 if (isNameStart(firstChar) || nextTwoCharsAreValidEscape()) |
| 390 return true; |
| 391 |
| 392 if (firstChar == '-') { |
| 393 if (isNameStart(m_input->peek(1))) |
| 394 return true; |
| 395 return twoCharsAreValidEscape(m_input->peek(1), m_input->peek(2)); |
| 396 } |
| 397 |
| 398 return false; |
| 399 } |
| 400 |
| 401 } // namespace WebCore |
OLD | NEW |