| 1 | // Copyright 2016 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 "CSSFunctionValue.h" |
| 33 | #include "CSSParserContext.h" |
| 34 | #include "CSSParserTokenRange.h" |
| 35 | #include "CSSPrimitiveValue.h" |
| 36 | #include "CSSShadowValue.h" |
| 37 | #include "CSSValuePool.h" |
| 38 | #include "Length.h" // For ValueRange |
| 39 | |
| 40 | namespace WebCore { |
| 41 | |
| 42 | // When these functions are successful, they will consume all the relevant |
| 43 | // tokens from the range and also consume any whitespace which follows. When |
| 44 | // the start of the range doesn't match the type we're looking for, the range |
| 45 | // will not be modified. |
| 46 | namespace CSSPropertyParserHelpers { |
| 47 | |
| 48 | // FIXME: These should probably just be consumeComma and consumeSlash. |
| 49 | bool consumeCommaIncludingWhitespace(CSSParserTokenRange&); |
| 50 | bool consumeSlashIncludingWhitespace(CSSParserTokenRange&); |
| 51 | // consumeFunction expects the range starts with a FunctionToken. |
| 52 | CSSParserTokenRange consumeFunction(CSSParserTokenRange&); |
| 53 | |
| 54 | enum class UnitlessQuirk { |
| 55 | Allow, |
| 56 | Forbid |
| 57 | }; |
| 58 | |
| 59 | RefPtr<CSSPrimitiveValue> consumeInteger(CSSParserTokenRange&, double minimumValue = -std::numeric_limits<double>::max()); |
| 60 | bool consumePositiveIntegerRaw(CSSParserTokenRange&, int& result); |
| 61 | RefPtr<CSSPrimitiveValue> consumePositiveInteger(CSSParserTokenRange&); |
| 62 | bool (CSSParserTokenRange&, double& result); |
| 63 | RefPtr<CSSPrimitiveValue> (CSSParserTokenRange&, ValueRange); |
| 64 | RefPtr<CSSPrimitiveValue> consumeFontWeightNumber(CSSParserTokenRange&); |
| 65 | RefPtr<CSSPrimitiveValue> consumeLength(CSSParserTokenRange&, CSSParserMode, ValueRange, UnitlessQuirk = UnitlessQuirk::Forbid); |
| 66 | RefPtr<CSSPrimitiveValue> consumePercent(CSSParserTokenRange&, ValueRange); |
| 67 | RefPtr<CSSPrimitiveValue> consumeLengthOrPercent(CSSParserTokenRange&, CSSParserMode, ValueRange, UnitlessQuirk = UnitlessQuirk::Forbid); |
| 68 | RefPtr<CSSPrimitiveValue> consumeAngle(CSSParserTokenRange&, CSSParserMode, UnitlessQuirk = UnitlessQuirk::Forbid); |
| 69 | RefPtr<CSSPrimitiveValue> consumeTime(CSSParserTokenRange&, CSSParserMode, ValueRange, UnitlessQuirk = UnitlessQuirk::Forbid); |
| 70 | RefPtr<CSSPrimitiveValue> consumeResolution(CSSParserTokenRange&); |
| 71 | |
| 72 | RefPtr<CSSPrimitiveValue> consumeIdent(CSSParserTokenRange&); |
| 73 | RefPtr<CSSPrimitiveValue> consumeIdentRange(CSSParserTokenRange&, CSSValueID lower, CSSValueID upper); |
| 74 | template<CSSValueID, CSSValueID...> inline bool identMatches(CSSValueID id); |
| 75 | template<CSSValueID... allowedIdents> RefPtr<CSSPrimitiveValue> consumeIdent(CSSParserTokenRange&); |
| 76 | |
| 77 | RefPtr<CSSPrimitiveValue> consumeCustomIdent(CSSParserTokenRange&); |
| 78 | RefPtr<CSSPrimitiveValue> consumeString(CSSParserTokenRange&); |
| 79 | StringView consumeUrlAsStringView(CSSParserTokenRange&); |
| 80 | RefPtr<CSSPrimitiveValue> consumeUrl(CSSParserTokenRange&); |
| 81 | |
| 82 | RefPtr<CSSPrimitiveValue> consumeColor(CSSParserTokenRange&, CSSParserMode, bool acceptQuirkyColors = false); |
| 83 | |
| 84 | RefPtr<CSSPrimitiveValue> consumePosition(CSSParserTokenRange&, CSSParserMode, UnitlessQuirk); |
| 85 | bool consumePosition(CSSParserTokenRange&, CSSParserMode, UnitlessQuirk, RefPtr<CSSPrimitiveValue>& resultX, RefPtr<CSSPrimitiveValue>& resultY); |
| 86 | bool consumeOneOrTwoValuedPosition(CSSParserTokenRange&, CSSParserMode, UnitlessQuirk, RefPtr<CSSPrimitiveValue>& resultX, RefPtr<CSSPrimitiveValue>& resultY); |
| 87 | |
| 88 | enum class ConsumeGeneratedImage { |
| 89 | Allow, |
| 90 | Forbid |
| 91 | }; |
| 92 | |
| 93 | RefPtr<CSSValue> consumeImage(CSSParserTokenRange&, CSSParserContext, ConsumeGeneratedImage = ConsumeGeneratedImage::Allow); |
| 94 | RefPtr<CSSValue> consumeImageOrNone(CSSParserTokenRange&, CSSParserContext); |
| 95 | |
| 96 | enum class AllowedFilterFunctions { |
| 97 | PixelFilters, |
| 98 | ColorFilters |
| 99 | }; |
| 100 | |
| 101 | RefPtr<CSSValue> consumeFilter(CSSParserTokenRange&, const CSSParserContext&, AllowedFilterFunctions); |
| 102 | RefPtr<CSSShadowValue> consumeSingleShadow(CSSParserTokenRange&, CSSParserMode, bool allowInset, bool allowSpread); |
| 103 | |
| 104 | // Template implementations are at the bottom of the file for readability. |
| 105 | |
| 106 | template<typename... emptyBaseCase> inline bool identMatches(CSSValueID) { return false; } |
| 107 | template<CSSValueID head, CSSValueID... tail> inline bool identMatches(CSSValueID id) |
| 108 | { |
| 109 | return id == head || identMatches<tail...>(id); |
| 110 | } |
| 111 | |
| 112 | // FIXME-NEWPARSER - converted to a RefPtr return type from a raw ptr. |
| 113 | template<CSSValueID... names> RefPtr<CSSPrimitiveValue> consumeIdent(CSSParserTokenRange& range) |
| 114 | { |
| 115 | if (range.peek().type() != IdentToken || !identMatches<names...>(range.peek().id())) |
| 116 | return nullptr; |
| 117 | return CSSValuePool::singleton().createIdentifierValue(range.consumeIncludingWhitespace().id()); |
| 118 | } |
| 119 | |
| 120 | static inline bool isCSSWideKeyword(const CSSValueID& id) |
| 121 | { |
| 122 | return id == CSSValueInitial || id == CSSValueInherit || id == CSSValueUnset || id == CSSValueRevert || id == CSSValueDefault; |
| 123 | } |
| 124 | |
| 125 | } // namespace CSSPropertyParserHelpers |
| 126 | |
| 127 | } // namespace WebCore |
| 128 | |