Fix srcset's image candidate algorithm when DPR exceeds all candidates
[WebKit-https.git] / Source / WebCore / html / parser / HTMLParserIdioms.cpp
index 9756572..baa7ce8 100644 (file)
@@ -26,6 +26,9 @@
 #include "HTMLParserIdioms.h"
 
 #include "Decimal.h"
+#include "HTMLIdentifier.h"
+#include "KURL.h"
+#include "QualifiedName.h"
 #include <limits>
 #include <wtf/MathExtras.h>
 #include <wtf/text/AtomicString.h>
@@ -133,7 +136,7 @@ double parseToDoubleForNumberType(const String& string, double fallbackValue)
         return fallbackValue;
 
     // NaN and infinity are considered valid by String::toDouble, but not valid here.
-    if (!isfinite(value))
+    if (!std::isfinite(value))
         return fallbackValue;
 
     // Numbers are considered finite IEEE 754 single-precision floating point values.
@@ -150,14 +153,9 @@ double parseToDoubleForNumberType(const String& string)
     return parseToDoubleForNumberType(string, std::numeric_limits<double>::quiet_NaN());
 }
 
-// http://www.whatwg.org/specs/web-apps/current-work/#rules-for-parsing-integers
-bool parseHTMLInteger(const String& input, int& value)
+template <typename CharacterType>
+static bool parseHTMLIntegerInternal(const CharacterType* position, const CharacterType* end, int& value)
 {
-    // Step 1
-    // Step 2
-    const UChar* position = input.characters();
-    const UChar* end = position + input.length();
-
     // Step 3
     int sign = 1;
 
@@ -197,18 +195,31 @@ bool parseHTMLInteger(const String& input, int& value)
 
     // Step 9
     bool ok;
-    value = sign * charactersToIntStrict(digits.characters(), digits.length(), &ok);
+    if (digits.is8Bit())
+        value = sign * charactersToIntStrict(digits.characters8(), digits.length(), &ok);
+    else
+        value = sign * charactersToIntStrict(digits.characters16(), digits.length(), &ok);
     return ok;
 }
 
-// http://www.whatwg.org/specs/web-apps/current-work/#rules-for-parsing-non-negative-integers
-bool parseHTMLNonNegativeInteger(const String& input, unsigned int& value)
+// http://www.whatwg.org/specs/web-apps/current-work/#rules-for-parsing-integers
+bool parseHTMLInteger(const String& input, int& value)
 {
     // Step 1
     // Step 2
-    const UChar* position = input.characters();
-    const UChar* end = position + input.length();
+    unsigned length = input.length();
+    if (!length || input.is8Bit()) {
+        const LChar* start = input.characters8();
+        return parseHTMLIntegerInternal(start, start + length, value);
+    }
 
+    const UChar* start = input.characters16();
+    return parseHTMLIntegerInternal(start, start + length, value);
+}
+
+template <typename CharacterType>
+static bool parseHTMLNonNegativeIntegerInternal(const CharacterType* position, const CharacterType* end, unsigned& value)
+{
     // Step 3
     while (position < end) {
         if (!isHTMLSpace(*position))
@@ -244,8 +255,118 @@ bool parseHTMLNonNegativeInteger(const String& input, unsigned int& value)
 
     // Step 9
     bool ok;
-    value = charactersToUIntStrict(digits.characters(), digits.length(), &ok);
+    if (digits.is8Bit())
+        value = charactersToUIntStrict(digits.characters8(), digits.length(), &ok);
+    else
+        value = charactersToUIntStrict(digits.characters16(), digits.length(), &ok);
     return ok;
 }
 
+
+// http://www.whatwg.org/specs/web-apps/current-work/#rules-for-parsing-non-negative-integers
+bool parseHTMLNonNegativeInteger(const String& input, unsigned& value)
+{
+    // Step 1
+    // Step 2
+    unsigned length = input.length();
+    if (length && input.is8Bit()) {
+        const LChar* start = input.characters8();
+        return parseHTMLNonNegativeIntegerInternal(start, start + length, value);
+    }
+    
+    const UChar* start = input.characters();
+    return parseHTMLNonNegativeIntegerInternal(start, start + length, value);
+}
+
+static bool threadSafeEqual(const StringImpl* a, const StringImpl* b)
+{
+    if (a == b)
+        return true;
+    if (a->hash() != b->hash())
+        return false;
+    return equalNonNull(a, b);
+}
+
+bool threadSafeMatch(const QualifiedName& a, const QualifiedName& b)
+{
+    return threadSafeEqual(a.localName().impl(), b.localName().impl());
+}
+
+#if ENABLE(THREADED_HTML_PARSER)
+bool threadSafeMatch(const HTMLIdentifier& localName, const QualifiedName& qName)
+{
+    return threadSafeEqual(localName.asStringImpl(), qName.localName().impl());
+}
+#endif
+
+struct ImageWithScale {
+    String imageURL;
+    float scaleFactor;
+    bool operator==(const ImageWithScale& image) const
+    {
+        return scaleFactor == image.scaleFactor && imageURL == image.imageURL;
+    }
+};
+typedef Vector<ImageWithScale> ImageCandidates;
+
+static inline bool compareByScaleFactor(const ImageWithScale& first, const ImageWithScale& second)
+{
+    return first.scaleFactor < second.scaleFactor;
+}
+
+String bestFitSourceForImageAttributes(float deviceScaleFactor, const String& srcAttribute, const String& srcSetAttribute)
+{
+    ImageCandidates imageCandidates;
+
+    const String srcSetAttributeValue = srcSetAttribute.simplifyWhiteSpace(isHTMLSpace);
+    Vector<String> srcSetTokens;
+
+    srcSetAttributeValue.split(',', srcSetTokens);
+    for (size_t i = 0; i < srcSetTokens.size(); ++i) {
+        Vector<String> data;
+        float imgScaleFactor = 1.0;
+        bool validScaleFactor = false;
+
+        srcSetTokens[i].stripWhiteSpace().split(' ', data);
+        // There must be at least one candidate descriptor, and the last one must
+        // be a scale factor. Since we don't support descriptors other than scale,
+        // it's better to discard any rule with such descriptors rather than accept
+        // only the scale data.
+        if (data.size() != 2)
+            continue;
+        if (!data.last().endsWith('x'))
+            continue;
+
+        imgScaleFactor = data.last().substring(0, data.last().length() - 1).toFloat(&validScaleFactor);
+        if (!validScaleFactor)
+            continue;
+
+        ImageWithScale image;
+        image.imageURL = decodeURLEscapeSequences(data[0]);
+        image.scaleFactor = imgScaleFactor;
+
+        imageCandidates.append(image);
+    }
+
+    const String src =  srcAttribute.simplifyWhiteSpace(isHTMLSpace);
+    if (!src.isEmpty()) {
+        ImageWithScale image;
+        image.imageURL = decodeURLEscapeSequences(src);
+        image.scaleFactor = 1.0;
+
+        imageCandidates.append(image);
+    }
+
+    if (imageCandidates.isEmpty())
+        return String();
+
+    std::stable_sort(imageCandidates.begin(), imageCandidates.end(), compareByScaleFactor);
+
+    for (size_t i = 0; i < imageCandidates.size(); ++i) {
+        if (imageCandidates[i].scaleFactor >= deviceScaleFactor)
+            return imageCandidates[i].imageURL;
+    }
+    return imageCandidates.last().imageURL;
+}
+
 }