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

Side by Side Diff: Source/core/html/parser/HTMLSrcsetParser.cpp

Issue 23694049: Improved srcset descriptor parsing. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: 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 unified diff | Download patch
« no previous file with comments | « LayoutTests/fast/hidpi/image-srcset-viewport-modifiers-expected.txt ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 23 matching lines...) Expand all
34 #include "core/html/parser/HTMLParserIdioms.h" 34 #include "core/html/parser/HTMLParserIdioms.h"
35 #include "core/platform/ParsingUtilities.h" 35 #include "core/platform/ParsingUtilities.h"
36 36
37 namespace WebCore { 37 namespace WebCore {
38 38
39 static bool compareByScaleFactor(const ImageCandidate& first, const ImageCandida te& second) 39 static bool compareByScaleFactor(const ImageCandidate& first, const ImageCandida te& second)
40 { 40 {
41 return first.scaleFactor() < second.scaleFactor(); 41 return first.scaleFactor() < second.scaleFactor();
42 } 42 }
43 43
44 template<typename CharType>
45 inline bool isComma(CharType character)
46 {
47 return character == ',';
48 }
49
50 template<typename CharType>
51 static bool parseDescriptors(const CharType* descriptorsStart, const CharType* d escriptorsEnd, float& imgScaleFactor)
52 {
53 const CharType* position = descriptorsStart;
54 bool isValid = true;
55 bool isScaleFactorFound = false;
56 while (position < descriptorsEnd) {
57 // 13.1. Let descriptor list be the result of splitting unparsed descrip tors on spaces.
58 skipWhile<CharType, isHTMLSpace<CharType> >(position, descriptorsEnd);
59 const CharType* currentDescriptorStart = position;
60 skipWhile<CharType, isNotHTMLSpace<CharType> >(position, descriptorsEnd) ;
61 const CharType* currentDescriptorEnd = position;
62
63 ++position;
64 ASSERT(currentDescriptorEnd > currentDescriptorStart);
65 --currentDescriptorEnd;
66 unsigned descriptorLength = currentDescriptorEnd - currentDescriptorStar t;
67 if (*currentDescriptorEnd == 'x') {
68 if (isScaleFactorFound)
69 return false;
70 imgScaleFactor = charactersToFloat(currentDescriptorStart, descripto rLength, &isValid);
71 isScaleFactorFound = true;
72 } else {
73 continue;
74 }
75 }
76 return isValid;
77 }
78
44 // http://www.whatwg.org/specs/web-apps/current-work/multipage/embedded-content- 1.html#processing-the-image-candidates 79 // http://www.whatwg.org/specs/web-apps/current-work/multipage/embedded-content- 1.html#processing-the-image-candidates
45 template<typename CharType> 80 template<typename CharType>
46 static void parseImageCandidatesFromSrcsetAttribute(const String& attribute, con st CharType* attributeStart, unsigned length, Vector<ImageCandidate>& imageCandi dates) 81 static void parseImageCandidatesFromSrcsetAttribute(const String& attribute, con st CharType* attributeStart, unsigned length, Vector<ImageCandidate>& imageCandi dates)
47 { 82 {
48 const CharType* position = attributeStart; 83 const CharType* position = attributeStart;
49 const CharType* attributeEnd = position + length; 84 const CharType* attributeEnd = position + length;
50 85
51 while (position < attributeEnd) { 86 while (position < attributeEnd) {
52 float imgScaleFactor = 1.0; 87 float imgScaleFactor = 1.0;
88
53 // 4. Splitting loop: Skip whitespace. 89 // 4. Splitting loop: Skip whitespace.
54 skipWhile<CharType, isHTMLSpace<CharType> >(position, attributeEnd); 90 skipWhile<CharType, isHTMLSpace<CharType> >(position, attributeEnd);
55 if (position == attributeEnd) 91 if (position == attributeEnd)
56 break; 92 break;
57 const CharType* imageURLStart = position; 93 const CharType* imageURLStart = position;
58 94
59 // If The current candidate is either totally empty or only contains spa ce, skipping. 95 // If The current candidate is either totally empty or only contains spa ce, skipping.
60 if (*position == ',') { 96 if (*position == ',') {
61 ++position; 97 ++position;
62 continue; 98 continue;
63 } 99 }
100
64 // 5. Collect a sequence of characters that are not space characters, an d let that be url. 101 // 5. Collect a sequence of characters that are not space characters, an d let that be url.
65 ++position;
66 skipUntil<CharType, isHTMLSpace<CharType> >(position, attributeEnd); 102 skipUntil<CharType, isHTMLSpace<CharType> >(position, attributeEnd);
67 const CharType* imageURLEnd = position; 103 const CharType* imageURLEnd = position;
68 104
69 if (position != attributeEnd && *(position - 1) == ',') { 105 if (position != attributeEnd && *(position - 1) == ',') {
70 --imageURLEnd; 106 --imageURLEnd;
71 } else { 107 } else {
72 // 7. Collect a sequence of characters that are not "," (U+002C) cha racters, and let that be descriptors. 108 // 7. Collect a sequence of characters that are not "," (U+002C) cha racters, and let that be descriptors.
73 skipWhile<CharType, isHTMLSpace<CharType> >(position, attributeEnd); 109 skipWhile<CharType, isHTMLSpace<CharType> >(position, attributeEnd);
74 const CharType* qualifierStart = position; 110 const CharType* descriptorsStart = position;
75 if (position != attributeEnd && *position != ',') { 111 skipUntil<CharType, isComma<CharType> >(position, attributeEnd);
76 // This part differs from the spec as the current implementation only supports pixel density descriptors for now. 112 const CharType* descriptorsEnd = position;
77 skipUntil<CharType, isHTMLSpaceOrComma<CharType> >(position, att ributeEnd); 113 if (!parseDescriptors(descriptorsStart, descriptorsEnd, imgScaleFact or))
78 const CharType* qualifierEnd = position; 114 continue;
79 ASSERT(qualifierEnd > qualifierStart);
80 // Make sure there are no other descriptors
81 skipWhile<CharType, isHTMLSpace<CharType> >(position, attributeE nd);
82 // If the first non-html-space character after the scale modifie r is not a comma,
83 // the current candidate is an invalid input.
84 if (position != attributeEnd && *position != ',') {
85 skipUntil<CharType>(position, attributeEnd, ',');
86 ++position;
87 continue;
88 }
89 // If the current qualifier is not an 'x', the resource is ignor ed
90 if (*(qualifierEnd - 1) != 'x')
91 continue;
92
93 bool validScaleFactor = false;
94 unsigned scaleFactorLengthWithoutUnit = qualifierEnd - qualifier Start - 1;
95 imgScaleFactor = charactersToFloat(qualifierStart, scaleFactorLe ngthWithoutUnit, &validScaleFactor);
96
97 if (!validScaleFactor)
98 continue;
99 }
100 } 115 }
101 116
102 imageCandidates.append(ImageCandidate(attribute, imageURLStart - attribu teStart, imageURLEnd - imageURLStart, imgScaleFactor)); 117 imageCandidates.append(ImageCandidate(attribute, imageURLStart - attribu teStart, imageURLEnd - imageURLStart, imgScaleFactor));
103 // 11. Return to the step labeled splitting loop. 118 // 11. Return to the step labeled splitting loop.
104 } 119 }
105 } 120 }
106 121
107 static void parseImageCandidatesFromSrcsetAttribute(const String& attribute, Vec tor<ImageCandidate>& imageCandidates) 122 static void parseImageCandidatesFromSrcsetAttribute(const String& attribute, Vec tor<ImageCandidate>& imageCandidates)
108 { 123 {
109 if (attribute.isNull()) 124 if (attribute.isNull())
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 Vector<ImageCandidate> imageCandidates; 177 Vector<ImageCandidate> imageCandidates;
163 imageCandidates.append(srcsetImageCandidate); 178 imageCandidates.append(srcsetImageCandidate);
164 179
165 if (!srcAttribute.isEmpty()) 180 if (!srcAttribute.isEmpty())
166 imageCandidates.append(ImageCandidate(srcAttribute, 0, srcAttribute.leng th(), 1.0)); 181 imageCandidates.append(ImageCandidate(srcAttribute, 0, srcAttribute.leng th(), 1.0));
167 182
168 return pickBestImageCandidate(deviceScaleFactor, imageCandidates).toString() ; 183 return pickBestImageCandidate(deviceScaleFactor, imageCandidates).toString() ;
169 } 184 }
170 185
171 } 186 }
OLDNEW
« no previous file with comments | « LayoutTests/fast/hidpi/image-srcset-viewport-modifiers-expected.txt ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698