Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Apple Inc. All rights reserved. | 2 * Copyright (C) 2013 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2013 Google Inc. All rights reserved. | 3 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
| 7 * met: | 7 * met: |
| 8 * | 8 * |
| 9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 StringBuilder errorMessage; | 189 StringBuilder errorMessage; |
| 190 errorMessage.append("Failed parsing 'srcset' attribute value since "); | 190 errorMessage.append("Failed parsing 'srcset' attribute value since "); |
| 191 errorMessage.append(message); | 191 errorMessage.append(message); |
| 192 document->frame()->console().addMessage(ConsoleMessage::create(OtherMess ageSource, ErrorMessageLevel, errorMessage.toString())); | 192 document->frame()->console().addMessage(ConsoleMessage::create(OtherMess ageSource, ErrorMessageLevel, errorMessage.toString())); |
| 193 } | 193 } |
| 194 } | 194 } |
| 195 | 195 |
| 196 template<typename CharType> | 196 template<typename CharType> |
| 197 static bool parseDescriptors(const CharType* attribute, Vector<DescriptorToken>& descriptors, DescriptorParsingResult& result, Document* document) | 197 static bool parseDescriptors(const CharType* attribute, Vector<DescriptorToken>& descriptors, DescriptorParsingResult& result, Document* document) |
| 198 { | 198 { |
| 199 for (Vector<DescriptorToken>::iterator it = descriptors.begin(); it != descr iptors.end(); ++it) { | 199 for (DescriptorToken& descriptor : descriptors) { |
| 200 if (it->length == 0) | 200 if (descriptor.length == 0) |
| 201 continue; | 201 continue; |
| 202 CharType c = attribute[it->lastIndex()]; | 202 CharType c = attribute[descriptor.lastIndex()]; |
| 203 bool isValid = false; | 203 bool isValid = false; |
| 204 if (RuntimeEnabledFeatures::pictureSizesEnabled() && c == 'w') { | 204 if (RuntimeEnabledFeatures::pictureSizesEnabled() && c == 'w') { |
| 205 if (result.hasDensity() || result.hasWidth()) { | 205 if (result.hasDensity() || result.hasWidth()) { |
| 206 srcsetError(document, "it has multiple 'w' descriptors or a mix of 'x' and 'w' descriptors."); | 206 srcsetError(document, "it has multiple 'w' descriptors or a mix of 'x' and 'w' descriptors."); |
| 207 return false; | 207 return false; |
| 208 } | 208 } |
| 209 int resourceWidth = it->toInt(attribute, isValid); | 209 int resourceWidth = descriptor.toInt(attribute, isValid); |
| 210 if (!isValid || resourceWidth <= 0) { | 210 if (!isValid || resourceWidth <= 0) { |
| 211 srcsetError(document, "its 'w' descriptor is invalid."); | 211 srcsetError(document, "its 'w' descriptor is invalid."); |
| 212 return false; | 212 return false; |
| 213 } | 213 } |
| 214 result.setResourceWidth(resourceWidth); | 214 result.setResourceWidth(resourceWidth); |
| 215 } else if (RuntimeEnabledFeatures::pictureSizesEnabled() && c == 'h') { | 215 } else if (RuntimeEnabledFeatures::pictureSizesEnabled() && c == 'h') { |
| 216 // This is here only for future compat purposes. | 216 // This is here only for future compat purposes. |
| 217 // The value of the 'h' descriptor is not used. | 217 // The value of the 'h' descriptor is not used. |
| 218 if (result.hasDensity() || result.hasHeight()) { | 218 if (result.hasDensity() || result.hasHeight()) { |
| 219 srcsetError(document, "it has multiple 'h' descriptors or a mix of 'x' and 'h' descriptors."); | 219 srcsetError(document, "it has multiple 'h' descriptors or a mix of 'x' and 'h' descriptors."); |
| 220 return false; | 220 return false; |
| 221 } | 221 } |
| 222 int resourceHeight = it->toInt(attribute, isValid); | 222 int resourceHeight = descriptor.toInt(attribute, isValid); |
| 223 if (!isValid || resourceHeight <= 0) { | 223 if (!isValid || resourceHeight <= 0) { |
| 224 srcsetError(document, "its 'h' descriptor is invalid."); | 224 srcsetError(document, "its 'h' descriptor is invalid."); |
| 225 return false; | 225 return false; |
| 226 } | 226 } |
| 227 result.setResourceHeight(resourceHeight); | 227 result.setResourceHeight(resourceHeight); |
| 228 } else if (c == 'x') { | 228 } else if (c == 'x') { |
| 229 if (result.hasDensity() || result.hasHeight() || result.hasWidth()) { | 229 if (result.hasDensity() || result.hasHeight() || result.hasWidth()) { |
| 230 srcsetError(document, "it has multiple 'x' descriptors or a mix of 'x' and 'w'/'h' descriptors."); | 230 srcsetError(document, "it has multiple 'x' descriptors or a mix of 'x' and 'w'/'h' descriptors."); |
| 231 return false; | 231 return false; |
| 232 } | 232 } |
| 233 float density = it->toFloat(attribute, isValid); | 233 float density = descriptor.toFloat(attribute, isValid); |
| 234 if (!isValid || density < 0) { | 234 if (!isValid || density < 0) { |
| 235 srcsetError(document, "its 'x' descriptor is invalid."); | 235 srcsetError(document, "its 'x' descriptor is invalid."); |
| 236 return false; | 236 return false; |
| 237 } | 237 } |
| 238 result.setDensity(density); | 238 result.setDensity(density); |
| 239 } else { | 239 } else { |
| 240 srcsetError(document, "it has an unknown descriptor."); | 240 srcsetError(document, "it has an unknown descriptor."); |
| 241 return false; | 241 return false; |
| 242 } | 242 } |
| 243 } | 243 } |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 258 | 258 |
| 259 // http://picture.responsiveimages.org/#parse-srcset-attr | 259 // http://picture.responsiveimages.org/#parse-srcset-attr |
| 260 template<typename CharType> | 260 template<typename CharType> |
| 261 static void parseImageCandidatesFromSrcsetAttribute(const String& attribute, con st CharType* attributeStart, unsigned length, Vector<ImageCandidate>& imageCandi dates, Document* document) | 261 static void parseImageCandidatesFromSrcsetAttribute(const String& attribute, con st CharType* attributeStart, unsigned length, Vector<ImageCandidate>& imageCandi dates, Document* document) |
| 262 { | 262 { |
| 263 const CharType* position = attributeStart; | 263 const CharType* position = attributeStart; |
| 264 const CharType* attributeEnd = position + length; | 264 const CharType* attributeEnd = position + length; |
| 265 | 265 |
| 266 while (position < attributeEnd) { | 266 while (position < attributeEnd) { |
| 267 // 4. Splitting loop: Collect a sequence of characters that are space ch aracters or U+002C COMMA characters. | 267 // 4. Splitting loop: Collect a sequence of characters that are space ch aracters or U+002C COMMA characters. |
| 268 skipWhile<CharType, isHTMLSpaceOrComma<CharType> >(position, attributeEn d); | 268 skipWhile<CharType, isHTMLSpaceOrComma<CharType>>(position, attributeEnd ); |
| 269 if (position == attributeEnd) { | 269 if (position == attributeEnd) { |
| 270 // Contrary to spec language - descriptor parsing happens on each ca ndidate, so when we reach the attributeEnd, we can exit. | 270 // Contrary to spec language - descriptor parsing happens on each ca ndidate, so when we reach the attributeEnd, we can exit. |
| 271 break; | 271 break; |
| 272 } | 272 } |
| 273 const CharType* imageURLStart = position; | 273 const CharType* imageURLStart = position; |
| 274 | 274 |
| 275 // 6. Collect a sequence of characters that are not space characters, an d let that be url. | 275 // 6. Collect a sequence of characters that are not space characters, an d let that be url. |
| 276 skipUntil<CharType, isHTMLSpace<CharType> >(position, attributeEnd); | 276 skipUntil<CharType, isHTMLSpace<CharType>>(position, attributeEnd); |
| 277 const CharType* imageURLEnd = position; | 277 const CharType* imageURLEnd = position; |
| 278 | 278 |
| 279 DescriptorParsingResult result; | 279 DescriptorParsingResult result; |
| 280 | 280 |
| 281 // 8. If url ends with a U+002C COMMA character (,) | 281 // 8. If url ends with a U+002C COMMA character (,) |
| 282 if (isComma(*(position - 1))) { | 282 if (isComma(*(position - 1))) { |
| 283 // Remove all trailing U+002C COMMA characters from url. | 283 // Remove all trailing U+002C COMMA characters from url. |
| 284 imageURLEnd = position - 1; | 284 imageURLEnd = position - 1; |
| 285 reverseSkipWhile<CharType, isComma>(imageURLEnd, imageURLStart); | 285 reverseSkipWhile<CharType, isComma>(imageURLEnd, imageURLStart); |
| 286 ++imageURLEnd; | 286 ++imageURLEnd; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 369 } | 369 } |
| 370 | 370 |
| 371 static ImageCandidate pickBestImageCandidate(float deviceScaleFactor, float sour ceSize, Vector<ImageCandidate>& imageCandidates, Document* document = nullptr) | 371 static ImageCandidate pickBestImageCandidate(float deviceScaleFactor, float sour ceSize, Vector<ImageCandidate>& imageCandidates, Document* document = nullptr) |
| 372 { | 372 { |
| 373 const float defaultDensityValue = 1.0; | 373 const float defaultDensityValue = 1.0; |
| 374 bool ignoreSrc = false; | 374 bool ignoreSrc = false; |
| 375 if (imageCandidates.isEmpty()) | 375 if (imageCandidates.isEmpty()) |
| 376 return ImageCandidate(); | 376 return ImageCandidate(); |
| 377 | 377 |
| 378 // http://picture.responsiveimages.org/#normalize-source-densities | 378 // http://picture.responsiveimages.org/#normalize-source-densities |
| 379 for (Vector<ImageCandidate>::iterator it = imageCandidates.begin(); it != im ageCandidates.end(); ++it) { | 379 for (auto& image : imageCandidates) { |
|
Mike West
2014/11/04 15:25:52
s/auto/ImageCandidate/
riju_
2014/11/04 17:50:53
Done.
| |
| 380 if (it->resourceWidth() > 0) { | 380 if (image.resourceWidth() > 0) { |
| 381 it->setDensity((float)it->resourceWidth() / sourceSize); | 381 image.setDensity((float)image.resourceWidth() / sourceSize); |
| 382 ignoreSrc = true; | 382 ignoreSrc = true; |
| 383 } else if (it->density() < 0) { | 383 } else if (image.density() < 0) { |
| 384 it->setDensity(defaultDensityValue); | 384 image.setDensity(defaultDensityValue); |
| 385 } | 385 } |
| 386 } | 386 } |
| 387 | 387 |
| 388 std::stable_sort(imageCandidates.begin(), imageCandidates.end(), compareByDe nsity); | 388 std::stable_sort(imageCandidates.begin(), imageCandidates.end(), compareByDe nsity); |
| 389 | 389 |
| 390 unsigned winner = selectionLogic(imageCandidates, deviceScaleFactor, ignoreS rc); | 390 unsigned winner = selectionLogic(imageCandidates, deviceScaleFactor, ignoreS rc); |
| 391 ASSERT(winner < imageCandidates.size()); | 391 ASSERT(winner < imageCandidates.size()); |
| 392 winner = avoidDownloadIfHigherDensityResourceIsInCache(imageCandidates, winn er, document); | 392 winner = avoidDownloadIfHigherDensityResourceIsInCache(imageCandidates, winn er, document); |
| 393 | 393 |
| 394 float winningDensity = imageCandidates[winner].density(); | 394 float winningDensity = imageCandidates[winner].density(); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 435 Vector<ImageCandidate> imageCandidates; | 435 Vector<ImageCandidate> imageCandidates; |
| 436 imageCandidates.append(srcsetImageCandidate); | 436 imageCandidates.append(srcsetImageCandidate); |
| 437 | 437 |
| 438 if (!srcAttribute.isEmpty()) | 438 if (!srcAttribute.isEmpty()) |
| 439 imageCandidates.append(ImageCandidate(srcAttribute, 0, srcAttribute.leng th(), DescriptorParsingResult(), ImageCandidate::SrcOrigin)); | 439 imageCandidates.append(ImageCandidate(srcAttribute, 0, srcAttribute.leng th(), DescriptorParsingResult(), ImageCandidate::SrcOrigin)); |
| 440 | 440 |
| 441 return pickBestImageCandidate(deviceScaleFactor, sourceSize, imageCandidates ).toString(); | 441 return pickBestImageCandidate(deviceScaleFactor, sourceSize, imageCandidates ).toString(); |
| 442 } | 442 } |
| 443 | 443 |
| 444 } | 444 } |
| OLD | NEW |