diff options
Diffstat (limited to 'Source/WebCore/html/parser/HTMLParserIdioms.h')
-rw-r--r-- | Source/WebCore/html/parser/HTMLParserIdioms.h | 93 |
1 files changed, 75 insertions, 18 deletions
diff --git a/Source/WebCore/html/parser/HTMLParserIdioms.h b/Source/WebCore/html/parser/HTMLParserIdioms.h index 7c5914248..29a6c6ed3 100644 --- a/Source/WebCore/html/parser/HTMLParserIdioms.h +++ b/Source/WebCore/html/parser/HTMLParserIdioms.h @@ -22,30 +22,32 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef HTMLParserIdioms_h -#define HTMLParserIdioms_h +#pragma once -#include "QualifiedName.h" +#include <unicode/uchar.h> #include <wtf/Forward.h> -#include <wtf/text/WTFString.h> +#include <wtf/Optional.h> +#include <wtf/Vector.h> +#include <wtf/text/StringView.h> namespace WebCore { class Decimal; +class QualifiedName; // Space characters as defined by the HTML specification. -bool isHTMLSpace(UChar); +template<typename CharacterType> bool isHTMLSpace(CharacterType); +template<typename CharacterType> bool isComma(CharacterType); +template<typename CharacterType> bool isHTMLSpaceOrComma(CharacterType); bool isHTMLLineBreak(UChar); bool isNotHTMLSpace(UChar); -bool isHTMLSpaceButNotLineBreak(UChar character); +bool isHTMLSpaceButNotLineBreak(UChar); + +// 2147483647 is 2^31 - 1. +static const unsigned maxHTMLNonNegativeInteger = 2147483647; // Strip leading and trailing whitespace as defined by the HTML specification. -String stripLeadingAndTrailingHTMLSpaces(const String&); -template<size_t inlineCapacity> -String stripLeadingAndTrailingHTMLSpaces(const Vector<UChar, inlineCapacity>& vector) -{ - return stripLeadingAndTrailingHTMLSpaces(StringImpl::create8BitIfPossible(vector)); -} +WEBCORE_EXPORT String stripLeadingAndTrailingHTMLSpaces(const String&); // An implementation of the HTML specification's algorithm to convert a number to a string for number and range types. String serializeForNumberType(const Decimal&); @@ -60,14 +62,33 @@ double parseToDoubleForNumberType(const String&); double parseToDoubleForNumberType(const String&, double fallbackValue); // http://www.whatwg.org/specs/web-apps/current-work/#rules-for-parsing-integers -bool parseHTMLInteger(const String&, int&); +WEBCORE_EXPORT std::optional<int> parseHTMLInteger(StringView); // http://www.whatwg.org/specs/web-apps/current-work/#rules-for-parsing-non-negative-integers -bool parseHTMLNonNegativeInteger(const String&, unsigned int&); +WEBCORE_EXPORT std::optional<unsigned> parseHTMLNonNegativeInteger(StringView); + +// https://html.spec.whatwg.org/#valid-non-negative-integer +std::optional<int> parseValidHTMLNonNegativeInteger(StringView); + +// https://html.spec.whatwg.org/#valid-floating-point-number +std::optional<double> parseValidHTMLFloatingPointNumber(StringView); + +// https://html.spec.whatwg.org/multipage/infrastructure.html#rules-for-parsing-floating-point-number-values +Vector<double> parseHTMLListOfOfFloatingPointNumberValues(StringView); + +// https://html.spec.whatwg.org/multipage/semantics.html#attr-meta-http-equiv-refresh +bool parseMetaHTTPEquivRefresh(const StringView&, double& delay, String& url); + +// https://html.spec.whatwg.org/multipage/infrastructure.html#cors-settings-attribute +String parseCORSSettingsAttribute(const AtomicString&); + +bool threadSafeMatch(const QualifiedName&, const QualifiedName&); + +AtomicString parseHTMLHashNameReference(StringView); // Inline implementations of some of the functions declared above. -inline bool isHTMLSpace(UChar character) +template<typename CharacterType> inline bool isHTMLSpace(CharacterType character) { // Histogram from Apple's page load test combined with some ad hoc browsing some other test suites. // @@ -87,6 +108,16 @@ inline bool isHTMLLineBreak(UChar character) return character <= '\r' && (character == '\n' || character == '\r'); } +template<typename CharacterType> inline bool isComma(CharacterType character) +{ + return character == ','; +} + +template<typename CharacterType> inline bool isHTMLSpaceOrComma(CharacterType character) +{ + return isComma(character) || isHTMLSpace(character); +} + inline bool isNotHTMLSpace(UChar character) { return !isHTMLSpace(character); @@ -97,10 +128,36 @@ inline bool isHTMLSpaceButNotLineBreak(UChar character) return isHTMLSpace(character) && !isHTMLLineBreak(character); } -bool threadSafeMatch(const QualifiedName&, const QualifiedName&); +// https://html.spec.whatwg.org/multipage/infrastructure.html#limited-to-only-non-negative-numbers-greater-than-zero +inline unsigned limitToOnlyHTMLNonNegativeNumbersGreaterThanZero(unsigned value, unsigned defaultValue = 1) +{ + return (value > 0 && value <= maxHTMLNonNegativeInteger) ? value : defaultValue; +} -String bestFitSourceForImageAttributes(float deviceScaleFactor, const String& srcAttribute, const String& sourceSetAttribute); +inline unsigned limitToOnlyHTMLNonNegativeNumbersGreaterThanZero(StringView stringValue, unsigned defaultValue = 1) +{ + ASSERT(defaultValue > 0); + ASSERT(defaultValue <= maxHTMLNonNegativeInteger); + auto optionalValue = parseHTMLNonNegativeInteger(stringValue); + unsigned value = optionalValue && optionalValue.value() ? optionalValue.value() : defaultValue; + ASSERT(value > 0); + ASSERT(value <= maxHTMLNonNegativeInteger); + return value; +} + +// https://html.spec.whatwg.org/#reflecting-content-attributes-in-idl-attributes:idl-unsigned-long +inline unsigned limitToOnlyHTMLNonNegative(unsigned value, unsigned defaultValue = 0) +{ + ASSERT(defaultValue <= maxHTMLNonNegativeInteger); + return value <= maxHTMLNonNegativeInteger ? value : defaultValue; +} +inline unsigned limitToOnlyHTMLNonNegative(StringView stringValue, unsigned defaultValue = 0) +{ + ASSERT(defaultValue <= maxHTMLNonNegativeInteger); + unsigned value = parseHTMLNonNegativeInteger(stringValue).value_or(defaultValue); + ASSERT(value <= maxHTMLNonNegativeInteger); + return value; } -#endif +} // namespace WebCore |