HTMLAreaElement's coords attributes parsing does not comply with the HTML specification
[WebKit-https.git] / Source / WebCore / html / parser / HTMLParserIdioms.cpp
index 52fc8b0..ad332ee 100644 (file)
 #include "HTMLParserIdioms.h"
 
 #include "Decimal.h"
-#include "HTMLIdentifier.h"
 #include "QualifiedName.h"
+#include "URL.h"
 #include <limits>
 #include <wtf/MathExtras.h>
-#include <wtf/text/AtomicString.h>
+#include <wtf/NeverDestroyed.h>
+#include <wtf/dtoa.h>
 #include <wtf/text/StringBuilder.h>
-#include <wtf/text/StringHash.h>
 
 namespace WebCore {
 
@@ -73,7 +73,7 @@ String stripLeadingAndTrailingHTMLSpaces(const String& string)
     if (string.is8Bit())
         return stripLeadingAndTrailingHTMLSpaces(string, string.characters8(), length);
 
-    return stripLeadingAndTrailingHTMLSpaces(string, string.characters(), length);
+    return stripLeadingAndTrailingHTMLSpaces(string, string.characters16(), length);
 }
 
 String serializeForNumberType(const Decimal& number)
@@ -154,149 +154,147 @@ double parseToDoubleForNumberType(const String& string)
 }
 
 template <typename CharacterType>
-static bool parseHTMLIntegerInternal(const CharacterType* position, const CharacterType* end, int& value)
+static Optional<int> parseHTMLIntegerInternal(const CharacterType* position, const CharacterType* end)
 {
-    // Step 3
-    int sign = 1;
-
-    // Step 4
-    while (position < end) {
-        if (!isHTMLSpace(*position))
-            break;
+    while (position < end && isHTMLSpace(*position))
         ++position;
-    }
 
-    // Step 5
     if (position == end)
-        return false;
-    ASSERT(position < end);
+        return Nullopt;
 
-    // Step 6
+    bool isNegative = false;
     if (*position == '-') {
-        sign = -1;
+        isNegative = true;
         ++position;
     } else if (*position == '+')
         ++position;
-    if (position == end)
-        return false;
-    ASSERT(position < end);
 
-    // Step 7
-    if (!isASCIIDigit(*position))
-        return false;
+    if (position == end || !isASCIIDigit(*position))
+        return Nullopt;
 
-    // Step 8
-    StringBuilder digits;
-    while (position < end) {
-        if (!isASCIIDigit(*position))
-            break;
-        digits.append(*position++);
-    }
+    constexpr int intMax = std::numeric_limits<int>::max();
+    constexpr int base = 10;
+    constexpr int maxMultiplier = intMax / base;
+
+    unsigned result = 0;
+    do {
+        int digitValue = *position - '0';
 
-    // Step 9
-    bool ok;
-    if (digits.is8Bit())
-        value = sign * charactersToIntStrict(digits.characters8(), digits.length(), &ok);
-    else
-        value = sign * charactersToIntStrict(digits.characters16(), digits.length(), &ok);
-    return ok;
+        if (result > maxMultiplier || (result == maxMultiplier && digitValue > (intMax % base) + isNegative))
+            return Nullopt;
+
+        result = base * result + digitValue;
+        ++position;
+    } while (position < end && isASCIIDigit(*position));
+
+    return isNegative ? -result : result;
 }
 
-// http://www.whatwg.org/specs/web-apps/current-work/#rules-for-parsing-integers
-bool parseHTMLInteger(const String& input, int& value)
+// https://html.spec.whatwg.org/multipage/infrastructure.html#rules-for-parsing-integers
+Optional<int> parseHTMLInteger(const String& input)
 {
-    // Step 1
-    // Step 2
     unsigned length = input.length();
-    if (length && input.is8Bit()) {
-        const LChar* start = input.characters8();
-        return parseHTMLIntegerInternal(start, start + length, value);
+    if (!length)
+        return Nullopt;
+
+    if (LIKELY(input.is8Bit())) {
+        auto* start = input.characters8();
+        return parseHTMLIntegerInternal(start, start + length);
     }
 
-    const UChar* start = input.characters();
-    return parseHTMLIntegerInternal(start, start + length, value);
+    auto* start = input.characters16();
+    return parseHTMLIntegerInternal(start, start + length);
+}
+
+// https://html.spec.whatwg.org/multipage/infrastructure.html#rules-for-parsing-non-negative-integers
+Optional<int> parseHTMLNonNegativeInteger(const String& input)
+{
+    Optional<int> signedValue = parseHTMLInteger(input);
+    if (!signedValue || signedValue.value() < 0)
+        return Nullopt;
+
+    return signedValue;
 }
 
 template <typename CharacterType>
-static bool parseHTMLNonNegativeIntegerInternal(const CharacterType* position, const CharacterType* end, unsigned& value)
+static inline bool isHTMLSpaceOrDelimiter(CharacterType character)
 {
-    // Step 3
-    while (position < end) {
-        if (!isHTMLSpace(*position))
-            break;
-        ++position;
-    }
+    return isHTMLSpace(character) || character == ',' || character == ';';
+}
 
-    // Step 4
-    if (position == end)
-        return false;
-    ASSERT(position < end);
+template <typename CharacterType>
+static inline bool isNumberStart(CharacterType character)
+{
+    return isASCIIDigit(character) || character == '.' || character == '-';
+}
+
+// https://html.spec.whatwg.org/multipage/infrastructure.html#rules-for-parsing-floating-point-number-values
+template <typename CharacterType>
+static Vector<double> parseHTMLListOfOfFloatingPointNumberValuesInternal(const CharacterType* position, const CharacterType* end)
+{
+    Vector<double> numbers;
 
-    // Step 5
-    if (*position == '+')
+    // This skips past any leading delimiters.
+    while (position < end && isHTMLSpaceOrDelimiter(*position))
         ++position;
 
-    // Step 6
-    if (position == end)
-        return false;
-    ASSERT(position < end);
+    while (position < end) {
+        // This skips past leading garbage.
+        while (position < end && !(isHTMLSpaceOrDelimiter(*position) || isNumberStart(*position)))
+            ++position;
 
-    // Step 7
-    if (!isASCIIDigit(*position))
-        return false;
+        const CharacterType* numberStart = position;
+        while (position < end && !isHTMLSpaceOrDelimiter(*position))
+            ++position;
 
-    // Step 8
-    StringBuilder digits;
-    while (position < end) {
-        if (!isASCIIDigit(*position))
-            break;
-        digits.append(*position++);
+        size_t parsedLength = 0;
+        double number = parseDouble(numberStart, position - numberStart, parsedLength);
+        numbers.append(parsedLength > 0 && std::isfinite(number) ? number : 0);
+
+        // This skips past the delimiter.
+        while (position < end && isHTMLSpaceOrDelimiter(*position))
+            ++position;
     }
 
-    // Step 9
-    bool ok;
-    if (digits.is8Bit())
-        value = charactersToUIntStrict(digits.characters8(), digits.length(), &ok);
-    else
-        value = charactersToUIntStrict(digits.characters16(), digits.length(), &ok);
-    return ok;
+    return numbers;
 }
 
-
-// http://www.whatwg.org/specs/web-apps/current-work/#rules-for-parsing-non-negative-integers
-bool parseHTMLNonNegativeInteger(const String& input, unsigned& value)
+Vector<double> parseHTMLListOfOfFloatingPointNumberValues(const String& input)
 {
-    // Step 1
-    // Step 2
     unsigned length = input.length();
-    if (length && input.is8Bit()) {
-        const LChar* start = input.characters8();
-        return parseHTMLNonNegativeIntegerInternal(start, start + length, value);
+    if (!length)
+        return { };
+
+    if (LIKELY(input.is8Bit())) {
+        auto* start = input.characters8();
+        return parseHTMLListOfOfFloatingPointNumberValuesInternal(start, start + length);
     }
-    
-    const UChar* start = input.characters();
-    return parseHTMLNonNegativeIntegerInternal(start, start + length, value);
+
+    auto* start = input.characters16();
+    return parseHTMLListOfOfFloatingPointNumberValuesInternal(start, start + length);
 }
 
-static bool threadSafeEqual(const StringImpl* a, const StringImpl* b)
+static bool threadSafeEqual(const StringImpl& a, const StringImpl& b)
 {
-    if (a == b)
+    if (&a == &b)
         return true;
-    if (a->hash() != b->hash())
+    if (a.hash() != b.hash())
         return false;
-    return equalNonNull(a, b);
+    return equal(a, b);
 }
 
 bool threadSafeMatch(const QualifiedName& a, const QualifiedName& b)
 {
-    return threadSafeEqual(a.localName().impl(), b.localName().impl());
+    return threadSafeEqual(*a.localName().impl(), *b.localName().impl());
 }
 
-#if ENABLE(THREADED_HTML_PARSER)
-bool threadSafeMatch(const HTMLIdentifier& localName, const QualifiedName& qName)
+String parseCORSSettingsAttribute(const AtomicString& value)
 {
-    return threadSafeEqual(localName.asStringImpl(), qName.localName().impl());
+    if (value.isNull())
+        return String();
+    if (equalIgnoringASCIICase(value, "use-credentials"))
+        return ASCIILiteral("use-credentials");
+    return ASCIILiteral("anonymous");
 }
-#endif
 
 }