Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: third_party/WebKit/Source/core/html/HTMLDimension.cpp

Issue 2528673003: Rework the "rules for parsing dimension values" implementation (Closed)
Patch Set: Rebase; clarify comment. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 12 matching lines...) Expand all
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "core/html/HTMLDimension.h" 31 #include "core/html/HTMLDimension.h"
32 32
33 #include "core/html/parser/HTMLParserIdioms.h"
33 #include "wtf/MathExtras.h" 34 #include "wtf/MathExtras.h"
35 #include "wtf/text/ParsingUtilities.h"
34 #include "wtf/text/StringToNumber.h" 36 #include "wtf/text/StringToNumber.h"
35 #include "wtf/text/WTFString.h" 37 #include "wtf/text/WTFString.h"
36 38
37 namespace blink { 39 namespace blink {
38 40
39 template <typename CharacterType> 41 template <typename CharacterType>
40 static HTMLDimension parseDimension(const CharacterType* characters, 42 static HTMLDimension parseDimension(const CharacterType* characters,
41 size_t lastParsedIndex, 43 size_t lastParsedIndex,
42 size_t endOfCurrentToken) { 44 size_t endOfCurrentToken) {
43 HTMLDimension::HTMLDimensionType type = HTMLDimension::Absolute; 45 HTMLDimension::HTMLDimensionType type = HTMLDimension::Absolute;
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 parsedDimensions.append( 141 parsedDimensions.append(
140 parseDimension(trimmedString, lastParsedIndex, nextComma)); 142 parseDimension(trimmedString, lastParsedIndex, nextComma));
141 lastParsedIndex = nextComma + 1; 143 lastParsedIndex = nextComma + 1;
142 } 144 }
143 145
144 parsedDimensions.append( 146 parsedDimensions.append(
145 parseDimension(trimmedString, lastParsedIndex, trimmedString.length())); 147 parseDimension(trimmedString, lastParsedIndex, trimmedString.length()));
146 return parsedDimensions; 148 return parsedDimensions;
147 } 149 }
148 150
151 template <typename CharacterType>
152 static bool parseDimensionValue(const CharacterType* current,
153 const CharacterType* end,
154 HTMLDimension& dimension) {
155 skipWhile<CharacterType, isHTMLSpace>(current, end);
156 // Deviation: HTML allows '+' here.
157 const CharacterType* numberStart = current;
158 if (!skipExactly<CharacterType, isASCIIDigit>(current, end))
159 return false;
160 skipWhile<CharacterType, isASCIIDigit>(current, end);
161 if (skipExactly<CharacterType>(current, end, '.')) {
162 // Deviation: HTML requires a digit after the full stop to be able to treat
163 // the value as a percentage (if not, the '.' will considered "garbage",
164 // yielding a regular length.) Gecko and Edge does not.
165 skipWhile<CharacterType, isASCIIDigit>(current, end);
166 }
167 bool ok;
168 double value = charactersToDouble(numberStart, current - numberStart, &ok);
169 if (!ok)
170 return false;
171 HTMLDimension::HTMLDimensionType type = HTMLDimension::Absolute;
172 if (current < end) {
173 if (*current == '%') {
174 type = HTMLDimension::Percentage;
175 } else if (*current == '*') {
176 // Deviation: HTML does not recognize '*' in this context, and we don't
177 // treat it as a valid value. We do count it though, so this is purely
178 // for statistics. Note though that per the specced behavior, "<number>*"
179 // would be the same as "<number>" (i.e '*' would just be trailing
180 // garbage.)
181 type = HTMLDimension::Relative;
182 }
183 }
184 dimension = HTMLDimension(value, type);
185 return true;
186 }
187
188 // https://html.spec.whatwg.org/multipage/infrastructure.html#rules-for-parsing- dimension-values
189 bool parseDimensionValue(const String& input, HTMLDimension& dimension) {
190 if (input.isEmpty())
191 return false;
192 if (input.is8Bit()) {
193 return parseDimensionValue(input.characters8(),
194 input.characters8() + input.length(), dimension);
195 }
196 return parseDimensionValue(input.characters16(),
197 input.characters16() + input.length(), dimension);
198 }
199
149 } // namespace blink 200 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/html/HTMLDimension.h ('k') | third_party/WebKit/Source/core/html/HTMLDimensionTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698