| 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 | #pragma once |
| 31 | |
| 32 | #include "CSSParserSelector.h" |
| 33 | #include "CSSParserTokenRange.h" |
| 34 | #include <memory> |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | struct CSSParserContext; |
| 39 | class CSSSelectorList; |
| 40 | class StyleSheetContents; |
| 41 | |
| 42 | // FIXME: We should consider building CSSSelectors directly instead of using |
| 43 | // the intermediate CSSParserSelector. |
| 44 | class CSSSelectorParser { |
| 45 | public: |
| 46 | static CSSSelectorList parseSelector(CSSParserTokenRange, const CSSParserContext&, StyleSheetContents*); |
| 47 | |
| 48 | static bool consumeANPlusB(CSSParserTokenRange&, std::pair<int, int>&); |
| 49 | |
| 50 | private: |
| 51 | CSSSelectorParser(const CSSParserContext&, StyleSheetContents*); |
| 52 | |
| 53 | // These will all consume trailing comments if successful |
| 54 | |
| 55 | CSSSelectorList consumeComplexSelectorList(CSSParserTokenRange&); |
| 56 | CSSSelectorList consumeCompoundSelectorList(CSSParserTokenRange&); |
| 57 | |
| 58 | std::unique_ptr<CSSParserSelector> consumeComplexSelector(CSSParserTokenRange&); |
| 59 | std::unique_ptr<CSSParserSelector> consumeCompoundSelector(CSSParserTokenRange&); |
| 60 | // This doesn't include element names, since they're handled specially |
| 61 | std::unique_ptr<CSSParserSelector> consumeSimpleSelector(CSSParserTokenRange&); |
| 62 | |
| 63 | bool consumeName(CSSParserTokenRange&, AtomicString& name, AtomicString& namespacePrefix); |
| 64 | |
| 65 | // These will return nullptr when the selector is invalid |
| 66 | std::unique_ptr<CSSParserSelector> consumeId(CSSParserTokenRange&); |
| 67 | std::unique_ptr<CSSParserSelector> consumeClass(CSSParserTokenRange&); |
| 68 | std::unique_ptr<CSSParserSelector> consumePseudo(CSSParserTokenRange&); |
| 69 | std::unique_ptr<CSSParserSelector> consumeAttribute(CSSParserTokenRange&); |
| 70 | |
| 71 | CSSSelector::RelationType consumeCombinator(CSSParserTokenRange&); |
| 72 | CSSSelector::Match consumeAttributeMatch(CSSParserTokenRange&); |
| 73 | CSSSelector::AttributeMatchType consumeAttributeFlags(CSSParserTokenRange&); |
| 74 | |
| 75 | const AtomicString& defaultNamespace() const; |
| 76 | const AtomicString& determineNamespace(const AtomicString& prefix); |
| 77 | void prependTypeSelectorIfNeeded(const AtomicString& namespacePrefix, const AtomicString& elementName, CSSParserSelector*); |
| 78 | static std::unique_ptr<CSSParserSelector> addSimpleSelectorToCompound(std::unique_ptr<CSSParserSelector> compoundSelector, std::unique_ptr<CSSParserSelector> simpleSelector); |
| 79 | static std::unique_ptr<CSSParserSelector> splitCompoundAtImplicitShadowCrossingCombinator(std::unique_ptr<CSSParserSelector> compoundSelector, const CSSParserContext&); |
| 80 | |
| 81 | const CSSParserContext& m_context; |
| 82 | RefPtr<StyleSheetContents> m_styleSheet; // FIXME: Should be const |
| 83 | |
| 84 | bool m_failedParsing = false; |
| 85 | bool m_disallowPseudoElements = false; |
| 86 | |
| 87 | class DisallowPseudoElementsScope { |
| 88 | WTF_MAKE_NONCOPYABLE(DisallowPseudoElementsScope); |
| 89 | public: |
| 90 | DisallowPseudoElementsScope(CSSSelectorParser* parser) |
| 91 | : m_parser(parser), m_wasDisallowed(m_parser->m_disallowPseudoElements) |
| 92 | { |
| 93 | m_parser->m_disallowPseudoElements = true; |
| 94 | } |
| 95 | |
| 96 | ~DisallowPseudoElementsScope() |
| 97 | { |
| 98 | m_parser->m_disallowPseudoElements = m_wasDisallowed; |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | CSSSelectorParser* m_parser; |
| 103 | bool m_wasDisallowed; |
| 104 | }; |
| 105 | }; |
| 106 | |
| 107 | } // namespace WebCore |
| 108 | |
| 109 | |