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
40namespace 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.
46namespace CSSPropertyParserHelpers {
47
48// FIXME: These should probably just be consumeComma and consumeSlash.
49bool consumeCommaIncludingWhitespace(CSSParserTokenRange&);
50bool consumeSlashIncludingWhitespace(CSSParserTokenRange&);
51// consumeFunction expects the range starts with a FunctionToken.
52CSSParserTokenRange consumeFunction(CSSParserTokenRange&);
53
54enum class UnitlessQuirk {
55 Allow,
56 Forbid
57};
58
59RefPtr<CSSPrimitiveValue> consumeInteger(CSSParserTokenRange&, double minimumValue = -std::numeric_limits<double>::max());
60bool consumePositiveIntegerRaw(CSSParserTokenRange&, int& result);
61RefPtr<CSSPrimitiveValue> consumePositiveInteger(CSSParserTokenRange&);
62bool consumeNumberRaw(CSSParserTokenRange&, double& result);
63RefPtr<CSSPrimitiveValue> consumeNumber(CSSParserTokenRange&, ValueRange);
64RefPtr<CSSPrimitiveValue> consumeFontWeightNumber(CSSParserTokenRange&);
65RefPtr<CSSPrimitiveValue> consumeLength(CSSParserTokenRange&, CSSParserMode, ValueRange, UnitlessQuirk = UnitlessQuirk::Forbid);
66RefPtr<CSSPrimitiveValue> consumePercent(CSSParserTokenRange&, ValueRange);
67RefPtr<CSSPrimitiveValue> consumeLengthOrPercent(CSSParserTokenRange&, CSSParserMode, ValueRange, UnitlessQuirk = UnitlessQuirk::Forbid);
68RefPtr<CSSPrimitiveValue> consumeAngle(CSSParserTokenRange&, CSSParserMode, UnitlessQuirk = UnitlessQuirk::Forbid);
69RefPtr<CSSPrimitiveValue> consumeTime(CSSParserTokenRange&, CSSParserMode, ValueRange, UnitlessQuirk = UnitlessQuirk::Forbid);
70RefPtr<CSSPrimitiveValue> consumeResolution(CSSParserTokenRange&);
71
72RefPtr<CSSPrimitiveValue> consumeIdent(CSSParserTokenRange&);
73RefPtr<CSSPrimitiveValue> consumeIdentRange(CSSParserTokenRange&, CSSValueID lower, CSSValueID upper);
74template<CSSValueID, CSSValueID...> inline bool identMatches(CSSValueID id);
75template<CSSValueID... allowedIdents> RefPtr<CSSPrimitiveValue> consumeIdent(CSSParserTokenRange&);
76
77RefPtr<CSSPrimitiveValue> consumeCustomIdent(CSSParserTokenRange&);
78RefPtr<CSSPrimitiveValue> consumeString(CSSParserTokenRange&);
79StringView consumeUrlAsStringView(CSSParserTokenRange&);
80RefPtr<CSSPrimitiveValue> consumeUrl(CSSParserTokenRange&);
81
82RefPtr<CSSPrimitiveValue> consumeColor(CSSParserTokenRange&, CSSParserMode, bool acceptQuirkyColors = false);
83
84RefPtr<CSSPrimitiveValue> consumePosition(CSSParserTokenRange&, CSSParserMode, UnitlessQuirk);
85bool consumePosition(CSSParserTokenRange&, CSSParserMode, UnitlessQuirk, RefPtr<CSSPrimitiveValue>& resultX, RefPtr<CSSPrimitiveValue>& resultY);
86bool consumeOneOrTwoValuedPosition(CSSParserTokenRange&, CSSParserMode, UnitlessQuirk, RefPtr<CSSPrimitiveValue>& resultX, RefPtr<CSSPrimitiveValue>& resultY);
87
88enum class ConsumeGeneratedImage {
89 Allow,
90 Forbid
91};
92
93RefPtr<CSSValue> consumeImage(CSSParserTokenRange&, CSSParserContext, ConsumeGeneratedImage = ConsumeGeneratedImage::Allow);
94RefPtr<CSSValue> consumeImageOrNone(CSSParserTokenRange&, CSSParserContext);
95
96enum class AllowedFilterFunctions {
97 PixelFilters,
98 ColorFilters
99};
100
101RefPtr<CSSValue> consumeFilter(CSSParserTokenRange&, const CSSParserContext&, AllowedFilterFunctions);
102RefPtr<CSSShadowValue> consumeSingleShadow(CSSParserTokenRange&, CSSParserMode, bool allowInset, bool allowSpread);
103
104// Template implementations are at the bottom of the file for readability.
105
106template<typename... emptyBaseCase> inline bool identMatches(CSSValueID) { return false; }
107template<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.
113template<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
120static 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