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

Unified Diff: Source/core/html/parser/HTMLSrcsetParser.cpp

Issue 23861003: Enable srcset support in HTMLImageElement (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Rewrote HTMLSrcsetParser, making it more efficient and readable. Addressed abarth's review comments. Created 7 years, 3 months 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 side-by-side diff with in-line comments
Download patch
Index: Source/core/html/parser/HTMLSrcsetParser.cpp
diff --git a/Source/core/html/parser/HTMLSrcsetParser.cpp b/Source/core/html/parser/HTMLSrcsetParser.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..71debbf0ddd9c528536dbf1dfeb7211543521b32
--- /dev/null
+++ b/Source/core/html/parser/HTMLSrcsetParser.cpp
@@ -0,0 +1,142 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
abarth-chromium 2013/09/19 18:09:52 Same problem with the license block.
+
+#include "config.h"
+#include "core/html/parser/HTMLSrcsetParser.h"
+
+#include "core/html/parser/HTMLParserIdioms.h"
+#include "core/platform/ParsingUtilities.h"
+
+namespace WebCore {
+
+static inline bool compareByScaleFactor(const ImageCandidate& first, const ImageCandidate& second)
abarth-chromium 2013/09/19 18:09:52 The |static| and |inline| keywords are redundant h
+{
+ return first.scaleFactor() < second.scaleFactor();
+}
+
+// http://www.whatwg.org/specs/web-apps/current-work/multipage/embedded-content-1.html#processing-the-image-candidates
+template<typename CharType>
+static void parseImageCandidatesFromSrcsetAttribute(const String& attribute, const CharType* attributeStart, unsigned length, Vector<ImageCandidate>& imageCandidates)
+{
+ const CharType* position = attributeStart;
+ const CharType* attributeEnd = position + length;
+
+ while (position < attributeEnd) {
+ float imgScaleFactor = 1.0;
+ // 4. Splitting loop: Skip whitespace.
+ skipWhile<CharType, isHTMLSpace<CharType> >(position, attributeEnd);
+ if (position == attributeEnd)
+ break;
+ const CharType* imageURLStart = position;
+
+ // If The current candidate is either totally empty or only contains space, skipping.
+ if (*position == ',') {
+ ++position;
+ continue;
+ }
+ // 5. Collect a sequence of characters that are not space characters, and let that be url.
+ ++position;
+ skipWhile<CharType, isNotHTMLSpace<CharType> >(position, attributeEnd);
abarth-chromium 2013/09/19 18:09:52 skipUntil<CharType, isHTMLSpaceOrComma<CharType> >
Yoav Weiss 2013/09/20 07:31:36 skipUntil<CharType, isHTMLSpace<CharType> > The a
+ const CharType* imageURLEnd = position;
+
+ if (position != attributeEnd && *(position - 1) == ',') {
+ --imageURLEnd;
+ } else {
+ // 7. Collect a sequence of characters that are not "," (U+002C) characters, and let that be descriptors.
+ skipWhile<CharType, isHTMLSpace<CharType> >(position, attributeEnd);
+ const CharType* qualifierStart = position;
+ if (position != attributeEnd && *position != ',') {
+ // This part differs from the spec as the current implementation only supports pixel density descriptors for now.
+ skipUntil<CharType, isHTMLSpaceOrComma<CharType> >(position, attributeEnd);
+ const CharType* qualifierEnd = position;
+ // Make sure there are no other descriptors
+ skipWhile<CharType, isHTMLSpace<CharType> >(position, attributeEnd);
+ // If the first non-html-space character after the scale modifier is not a comma,
+ // the current candidate is an invalid input.
+ if (position != attributeEnd && *position != ',') {
+ skipUntil<CharType>(position, attributeEnd, ',');
+ ++position;
+ continue;
+ }
+ // If the current qualifier is not an 'x', the resource is ignored
+ if (*(qualifierEnd - 1) != 'x')
abarth-chromium 2013/09/19 18:09:52 How do we know qualifierEnd - 1 is a valid address
Yoav Weiss 2013/09/20 07:31:36 When going into the skipUntil at line 51, we know
abarth-chromium 2013/09/20 16:08:07 I think an ASSERT is probably enough.
+ continue;
+
+ bool validScaleFactor = false;
+ unsigned scaleFactorLengthWithoutUnit = qualifierEnd - qualifierStart - 1;
+ imgScaleFactor = charactersToFloat(qualifierStart, scaleFactorLengthWithoutUnit, &validScaleFactor);
+
+ if (!validScaleFactor)
+ continue;
+ }
+ }
+
+ imageCandidates.append(ImageCandidate(attribute, imageURLStart - attributeStart, imageURLEnd - imageURLStart, imgScaleFactor));
+ // 11. Return to the step labeled splitting loop.
+ }
+}
+
+static void parseImageCandidatesFromSrcsetAttribute(const String& attribute, Vector<ImageCandidate>& imageCandidates)
+{
+ if (attribute.is8Bit())
abarth-chromium 2013/09/19 18:09:52 What if attribute.isNull() ? Calling is8Bit() on
+ parseImageCandidatesFromSrcsetAttribute<LChar>(attribute, attribute.characters8(), attribute.length(), imageCandidates);
+ else
+ parseImageCandidatesFromSrcsetAttribute<UChar>(attribute, attribute.characters16(), attribute.length(), imageCandidates);
+}
+
+static ImageCandidate pickBestImageCandidate(float deviceScaleFactor, Vector<ImageCandidate>& imageCandidates)
+{
+ std::stable_sort(imageCandidates.begin(), imageCandidates.end(), compareByScaleFactor);
+
+ unsigned i;
+ for (i = 0; i < imageCandidates.size() - 1; ++i) {
+ if (imageCandidates[i].scaleFactor() >= deviceScaleFactor)
+ break;
+ }
+ return imageCandidates[i];
abarth-chromium 2013/09/19 18:09:52 This will crash if imageCandidates is empty. Rath
+}
+
+ImageCandidate bestFitSourceForSrcsetAttribute(float deviceScaleFactor, const String& srcsetAttribute)
+{
+ Vector<ImageCandidate> imageCandidates;
+
+ parseImageCandidatesFromSrcsetAttribute(srcsetAttribute, imageCandidates);
+ if (imageCandidates.isEmpty())
+ return ImageCandidate();
+ return pickBestImageCandidate(deviceScaleFactor, imageCandidates);
+}
+
+String bestFitSourceForImageAttributes(float deviceScaleFactor, const String& srcAttribute, const String& srcsetAttribute)
+{
+ if (srcsetAttribute.isNull())
+ return srcAttribute;
abarth-chromium 2013/09/19 18:09:52 Or just: return String()
cbiesinger 2013/09/19 23:01:44 But that's not equivalent, right? If srcset is emp
abarth-chromium 2013/09/20 16:08:07 In this case, you're testing for isNull(). There'
abarth-chromium 2013/09/20 16:08:58 Oh, you're totally right. Please ignore me.
+
+ Vector<ImageCandidate> imageCandidates;
+
+ parseImageCandidatesFromSrcsetAttribute(srcsetAttribute, imageCandidates);
+
+ if (!srcAttribute.isEmpty())
+ imageCandidates.append(ImageCandidate(srcAttribute, 0, srcAttribute.length(), 1.0));
+
+ if (imageCandidates.isEmpty())
+ return String();
+
+ return pickBestImageCandidate(deviceScaleFactor, imageCandidates).toString();
+}
+
+String bestFitSourceForImageAttributes(float deviceScaleFactor, const String& srcAttribute, ImageCandidate& srcsetImageCandidate)
+{
+ if (srcsetImageCandidate.isEmpty())
+ return srcAttribute;
+
+ Vector<ImageCandidate> imageCandidates;
+ imageCandidates.append(srcsetImageCandidate);
+
+ if (!srcAttribute.isEmpty())
+ imageCandidates.append(ImageCandidate(srcAttribute, 0, srcAttribute.length(), 1.0));
+
+ return pickBestImageCandidate(deviceScaleFactor, imageCandidates).toString();
+}
+
+}

Powered by Google App Engine
This is Rietveld 408576698