OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CSSPropertyParserHelpers_h | 5 #ifndef CSSPropertyParserHelpers_h |
6 #define CSSPropertyParserHelpers_h | 6 #define CSSPropertyParserHelpers_h |
7 | 7 |
8 #include "core/css/CSSCustomIdentValue.h" | 8 #include "core/css/CSSCustomIdentValue.h" |
9 #include "core/css/CSSIdentifierValue.h" | 9 #include "core/css/CSSIdentifierValue.h" |
10 #include "core/css/CSSPrimitiveValue.h" | 10 #include "core/css/CSSPrimitiveValue.h" |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 if (range.Peek().GetType() != kIdentToken || | 119 if (range.Peek().GetType() != kIdentToken || |
120 !IdentMatches<names...>(range.Peek().Id())) | 120 !IdentMatches<names...>(range.Peek().Id())) |
121 return nullptr; | 121 return nullptr; |
122 return CSSIdentifierValue::Create(range.ConsumeIncludingWhitespace().Id()); | 122 return CSSIdentifierValue::Create(range.ConsumeIncludingWhitespace().Id()); |
123 } | 123 } |
124 | 124 |
125 // ConsumeCommaSeparatedList takes a callback function to call on each item in | 125 // ConsumeCommaSeparatedList takes a callback function to call on each item in |
126 // the list, followed by the arguments to pass to this callback. | 126 // the list, followed by the arguments to pass to this callback. |
127 // The first argument to the callback must be the CSSParserTokenRange | 127 // The first argument to the callback must be the CSSParserTokenRange |
128 template <typename Func, typename... Args> | 128 template <typename Func, typename... Args> |
129 CSSValueList* ConsumeCommaSeparatedList(Func callback, | 129 CSSValue* ConsumeCommaSeparatedList(bool optimise, |
130 CSSParserTokenRange& range, | 130 Func callback, |
131 Args... args) { | 131 CSSParserTokenRange& range, |
132 CSSValueList* list = CSSValueList::CreateCommaSeparated(); | 132 Args... args) { |
| 133 CSSValue* result = nullptr; |
| 134 if (!optimise) |
| 135 result = CSSValueList::CreateCommaSeparated(); |
133 do { | 136 do { |
134 CSSValue* value = callback(range, args...); | 137 CSSValue* value = callback(range, args...); |
135 if (!value) | 138 if (!value) |
136 return nullptr; | 139 return nullptr; |
137 list->Append(*value); | 140 |
| 141 if (optimise) { |
| 142 if (!result) { |
| 143 // To conserve memory we don't wrap a single value in a list. |
| 144 result = value; |
| 145 continue; |
| 146 } |
| 147 if (!result->IsBaseValueList()) { |
| 148 CSSValue* first_value = result; |
| 149 result = CSSValueList::CreateCommaSeparated(); |
| 150 ToCSSValueList(result)->Append(*first_value); |
| 151 } |
| 152 } |
| 153 ToCSSValueList(result)->Append(*value); |
138 } while (ConsumeCommaIncludingWhitespace(range)); | 154 } while (ConsumeCommaIncludingWhitespace(range)); |
139 DCHECK(list->length()); | 155 |
140 return list; | 156 DCHECK(!result->IsBaseValueList() || ToCSSValueList(result)->length()); |
| 157 return result; |
141 } | 158 } |
142 | 159 |
143 } // namespace CSSPropertyParserHelpers | 160 } // namespace CSSPropertyParserHelpers |
144 | 161 |
145 } // namespace blink | 162 } // namespace blink |
146 | 163 |
147 #endif // CSSPropertyParserHelpers_h | 164 #endif // CSSPropertyParserHelpers_h |
OLD | NEW |