| 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Copyright (C) 2016 Apple 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 | #include "config.h" |
| 31 | #include "CSSParserTokenRange.h" |
| 32 | |
| 33 | #include "StyleSheetContents.h" |
| 34 | #include <wtf/NeverDestroyed.h> |
| 35 | #include <wtf/text/StringBuilder.h> |
| 36 | |
| 37 | namespace WebCore { |
| 38 | |
| 39 | CSSParserToken& CSSParserTokenRange::eofToken() |
| 40 | { |
| 41 | static NeverDestroyed<CSSParserToken> eofToken(EOFToken); |
| 42 | return eofToken.get(); |
| 43 | } |
| 44 | |
| 45 | CSSParserTokenRange CSSParserTokenRange::makeSubRange(const CSSParserToken* first, const CSSParserToken* last) const |
| 46 | { |
| 47 | if (first == &eofToken()) |
| 48 | first = m_last; |
| 49 | if (last == &eofToken()) |
| 50 | last = m_last; |
| 51 | ASSERT(first <= last); |
| 52 | return CSSParserTokenRange(first, last); |
| 53 | } |
| 54 | |
| 55 | CSSParserTokenRange CSSParserTokenRange::consumeBlock() |
| 56 | { |
| 57 | ASSERT(peek().getBlockType() == CSSParserToken::BlockStart); |
| 58 | const CSSParserToken* start = &peek() + 1; |
| 59 | unsigned nestingLevel = 0; |
| 60 | do { |
| 61 | const CSSParserToken& token = consume(); |
| 62 | if (token.getBlockType() == CSSParserToken::BlockStart) |
| 63 | nestingLevel++; |
| 64 | else if (token.getBlockType() == CSSParserToken::BlockEnd) |
| 65 | nestingLevel--; |
| 66 | } while (nestingLevel && m_first < m_last); |
| 67 | |
| 68 | if (nestingLevel) |
| 69 | return makeSubRange(start, m_first); // Ended at EOF |
| 70 | return makeSubRange(start, m_first - 1); |
| 71 | } |
| 72 | |
| 73 | CSSParserTokenRange CSSParserTokenRange::consumeBlockCheckingForEditability(StyleSheetContents* styleSheet) |
| 74 | { |
| 75 | ASSERT(peek().getBlockType() == CSSParserToken::BlockStart); |
| 76 | const auto* start = &peek() + 1; |
| 77 | unsigned nestingLevel = 0; |
| 78 | do { |
| 79 | const auto& token = consume(); |
| 80 | if (token.getBlockType() == CSSParserToken::BlockStart) |
| 81 | nestingLevel++; |
| 82 | else if (token.getBlockType() == CSSParserToken::BlockEnd) |
| 83 | nestingLevel--; |
| 84 | |
| 85 | if (styleSheet && !styleSheet->usesStyleBasedEditability() && token.type() == IdentToken && equalLettersIgnoringASCIICase(token.value(), "-webkit-user-modify" )) |
| 86 | styleSheet->parserSetUsesStyleBasedEditability(); |
| 87 | } while (nestingLevel && m_first < m_last); |
| 88 | |
| 89 | if (nestingLevel) |
| 90 | return makeSubRange(start, m_first); // Ended at EOF |
| 91 | return makeSubRange(start, m_first - 1); |
| 92 | } |
| 93 | |
| 94 | void CSSParserTokenRange::consumeComponentValue() |
| 95 | { |
| 96 | // FIXME: This is going to do multiple passes over large sections of a stylesheet. |
| 97 | // We should consider optimising this by precomputing where each block ends. |
| 98 | unsigned nestingLevel = 0; |
| 99 | do { |
| 100 | const CSSParserToken& token = consume(); |
| 101 | if (token.getBlockType() == CSSParserToken::BlockStart) |
| 102 | nestingLevel++; |
| 103 | else if (token.getBlockType() == CSSParserToken::BlockEnd) |
| 104 | nestingLevel--; |
| 105 | } while (nestingLevel && m_first < m_last); |
| 106 | } |
| 107 | |
| 108 | String CSSParserTokenRange::serialize() const |
| 109 | { |
| 110 | // We're supposed to insert comments between certain pairs of token types |
| 111 | // as per spec, but since this is currently only used for @supports CSSOM |
| 112 | // we just get these cases wrong and avoid the additional complexity. |
| 113 | StringBuilder builder; |
| 114 | for (const CSSParserToken* it = m_first; it < m_last; ++it) |
| 115 | it->serialize(builder); |
| 116 | return builder.toString(); |
| 117 | } |
| 118 | |
| 119 | } // namespace WebCore |
| 120 | |