2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #ifndef HTMLParserIdioms_h
26 #define HTMLParserIdioms_h
28 #include <unicode/uchar.h>
29 #include <wtf/Forward.h>
30 #include <wtf/Optional.h>
31 #include <wtf/Vector.h>
38 // Space characters as defined by the HTML specification.
39 template<typename CharacterType> bool isHTMLSpace(CharacterType);
40 template<typename CharacterType> bool isComma(CharacterType);
41 template<typename CharacterType> bool isHTMLSpaceOrComma(CharacterType);
42 bool isHTMLLineBreak(UChar);
43 bool isNotHTMLSpace(UChar);
44 bool isHTMLSpaceButNotLineBreak(UChar);
46 // 2147483647 is 2^31 - 1.
47 static const unsigned maxHTMLNonNegativeInteger = 2147483647;
49 // Strip leading and trailing whitespace as defined by the HTML specification.
50 WEBCORE_EXPORT String stripLeadingAndTrailingHTMLSpaces(const String&);
52 // An implementation of the HTML specification's algorithm to convert a number to a string for number and range types.
53 String serializeForNumberType(const Decimal&);
54 String serializeForNumberType(double);
56 // Convert the specified string to a decimal/double. If the conversion fails, the return value is fallback value or NaN if not specified.
57 // Leading or trailing illegal characters cause failure, as does passing an empty string.
58 // The double* parameter may be 0 to check if the string can be parsed without getting the result.
59 Decimal parseToDecimalForNumberType(const String&);
60 Decimal parseToDecimalForNumberType(const String&, const Decimal& fallbackValue);
61 double parseToDoubleForNumberType(const String&);
62 double parseToDoubleForNumberType(const String&, double fallbackValue);
64 // http://www.whatwg.org/specs/web-apps/current-work/#rules-for-parsing-integers
65 WEBCORE_EXPORT Optional<int> parseHTMLInteger(const String&);
67 // http://www.whatwg.org/specs/web-apps/current-work/#rules-for-parsing-non-negative-integers
68 WEBCORE_EXPORT Optional<unsigned> parseHTMLNonNegativeInteger(const String&);
70 // https://html.spec.whatwg.org/#valid-non-negative-integer
71 Optional<int> parseValidHTMLNonNegativeInteger(StringView);
73 // https://html.spec.whatwg.org/#valid-floating-point-number
74 Optional<double> parseValidHTMLFloatingPointNumber(StringView);
76 // https://html.spec.whatwg.org/multipage/infrastructure.html#rules-for-parsing-floating-point-number-values
77 Vector<double> parseHTMLListOfOfFloatingPointNumberValues(StringView);
79 // https://html.spec.whatwg.org/multipage/semantics.html#attr-meta-http-equiv-refresh
80 bool parseMetaHTTPEquivRefresh(const StringView&, double& delay, String& url);
82 // https://html.spec.whatwg.org/multipage/infrastructure.html#cors-settings-attribute
83 String parseCORSSettingsAttribute(const AtomicString&);
85 bool threadSafeMatch(const QualifiedName&, const QualifiedName&);
87 // Inline implementations of some of the functions declared above.
89 template<typename CharacterType> inline bool isHTMLSpace(CharacterType character)
91 // Histogram from Apple's page load test combined with some ad hoc browsing some other test suites.
93 // 82%: 216330 non-space characters, all > U+0020
94 // 11%: 30017 plain space characters, U+0020
95 // 5%: 12099 newline characters, U+000A
96 // 2%: 5346 tab characters, U+0009
98 // No other characters seen. No U+000C or U+000D, and no other control characters.
99 // Accordingly, we check for non-spaces first, then space, then newline, then tab, then the other characters.
101 return character <= ' ' && (character == ' ' || character == '\n' || character == '\t' || character == '\r' || character == '\f');
104 inline bool isHTMLLineBreak(UChar character)
106 return character <= '\r' && (character == '\n' || character == '\r');
109 template<typename CharacterType> inline bool isComma(CharacterType character)
111 return character == ',';
114 template<typename CharacterType> inline bool isHTMLSpaceOrComma(CharacterType character)
116 return isComma(character) || isHTMLSpace(character);
119 inline bool isNotHTMLSpace(UChar character)
121 return !isHTMLSpace(character);
124 inline bool isHTMLSpaceButNotLineBreak(UChar character)
126 return isHTMLSpace(character) && !isHTMLLineBreak(character);
129 // https://html.spec.whatwg.org/multipage/infrastructure.html#limited-to-only-non-negative-numbers-greater-than-zero
130 inline unsigned limitToOnlyHTMLNonNegativeNumbersGreaterThanZero(unsigned value, unsigned defaultValue = 1)
132 return (value > 0 && value <= maxHTMLNonNegativeInteger) ? value : defaultValue;
135 inline unsigned limitToOnlyHTMLNonNegativeNumbersGreaterThanZero(const String& stringValue, unsigned defaultValue = 1)
137 ASSERT(defaultValue > 0);
138 ASSERT(defaultValue <= maxHTMLNonNegativeInteger);
139 auto optionalValue = parseHTMLNonNegativeInteger(stringValue);
140 unsigned value = optionalValue && optionalValue.value() ? optionalValue.value() : defaultValue;
142 ASSERT(value <= maxHTMLNonNegativeInteger);
146 // https://html.spec.whatwg.org/#reflecting-content-attributes-in-idl-attributes:idl-unsigned-long
147 inline unsigned limitToOnlyHTMLNonNegative(unsigned value, unsigned defaultValue = 0)
149 ASSERT(defaultValue <= maxHTMLNonNegativeInteger);
150 return value <= maxHTMLNonNegativeInteger ? value : defaultValue;
153 inline unsigned limitToOnlyHTMLNonNegative(const String& stringValue, unsigned defaultValue = 0)
155 ASSERT(defaultValue <= maxHTMLNonNegativeInteger);
156 unsigned value = parseHTMLNonNegativeInteger(stringValue).valueOr(defaultValue);
157 ASSERT(value <= maxHTMLNonNegativeInteger);