OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2010 Apple 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 | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 11 matching lines...) Expand all Loading... |
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
23 */ | 23 */ |
24 | 24 |
25 #include "config.h" | 25 #include "config.h" |
26 #include "core/html/parser/HTMLParserIdioms.h" | 26 #include "core/html/parser/HTMLParserIdioms.h" |
27 | 27 |
28 #include <limits> | 28 #include <limits> |
29 #include "core/dom/QualifiedName.h" | 29 #include "core/dom/QualifiedName.h" |
30 #include "core/html/parser/HTMLIdentifier.h" | 30 #include "core/html/parser/HTMLIdentifier.h" |
31 #include "core/platform/Decimal.h" | 31 #include "core/platform/Decimal.h" |
| 32 #include "core/platform/text/DecodeEscapeSequences.h" |
32 #include "wtf/MathExtras.h" | 33 #include "wtf/MathExtras.h" |
33 #include "wtf/text/AtomicString.h" | 34 #include "wtf/text/AtomicString.h" |
34 #include "wtf/text/StringBuilder.h" | 35 #include "wtf/text/StringBuilder.h" |
35 | 36 |
36 namespace WebCore { | 37 namespace WebCore { |
37 | 38 |
38 template <typename CharType> | 39 template <typename CharType> |
39 static String stripLeadingAndTrailingHTMLSpaces(String string, CharType characte
rs, unsigned length) | 40 static String stripLeadingAndTrailingHTMLSpaces(String string, CharType characte
rs, unsigned length) |
40 { | 41 { |
41 unsigned numLeadingSpaces = 0; | 42 unsigned numLeadingSpaces = 0; |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 bool threadSafeMatch(const QualifiedName& a, const QualifiedName& b) | 290 bool threadSafeMatch(const QualifiedName& a, const QualifiedName& b) |
290 { | 291 { |
291 return threadSafeEqual(a.localName().impl(), b.localName().impl()); | 292 return threadSafeEqual(a.localName().impl(), b.localName().impl()); |
292 } | 293 } |
293 | 294 |
294 bool threadSafeMatch(const HTMLIdentifier& localName, const QualifiedName& qName
) | 295 bool threadSafeMatch(const HTMLIdentifier& localName, const QualifiedName& qName
) |
295 { | 296 { |
296 return threadSafeEqual(localName.asStringImpl(), qName.localName().impl()); | 297 return threadSafeEqual(localName.asStringImpl(), qName.localName().impl()); |
297 } | 298 } |
298 | 299 |
| 300 struct ImageWithScale { |
| 301 String imageURL; |
| 302 float scaleFactor; |
| 303 bool operator==(const ImageWithScale& image) const |
| 304 { |
| 305 return scaleFactor == image.scaleFactor && imageURL == image.imageURL; |
| 306 } |
| 307 }; |
| 308 typedef Vector<ImageWithScale> ImageCandidates; |
| 309 |
| 310 static inline bool compareByScaleFactor(const ImageWithScale& first, const Image
WithScale& second) |
| 311 { |
| 312 return first.scaleFactor < second.scaleFactor; |
299 } | 313 } |
| 314 |
| 315 String bestFitSourceForImageAttributes(float deviceScaleFactor, const String& sr
cAttribute, const String& srcSetAttribute) |
| 316 { |
| 317 ImageCandidates imageCandidates; |
| 318 |
| 319 const String srcSetAttributeValue = srcSetAttribute.simplifyWhiteSpace(isHTM
LSpace); |
| 320 Vector<String> srcSetTokens; |
| 321 |
| 322 srcSetAttributeValue.split(',', srcSetTokens); |
| 323 for (size_t i = 0; i < srcSetTokens.size(); ++i) { |
| 324 Vector<String> data; |
| 325 float imgScaleFactor = 1.0; |
| 326 bool validScaleFactor = false; |
| 327 |
| 328 srcSetTokens[i].stripWhiteSpace().split(' ', data); |
| 329 // There must be at least one candidate descriptor, and the last one mus
t |
| 330 // be a scale factor. Since we don't support descriptors other than scal
e, |
| 331 // it's better to discard any rule with such descriptors rather than acc
ept |
| 332 // only the scale data. |
| 333 if (data.size() != 2) |
| 334 continue; |
| 335 if (!data.last().endsWith('x')) |
| 336 continue; |
| 337 |
| 338 imgScaleFactor = data.last().substring(0, data.last().length() - 1).toFl
oat(&validScaleFactor); |
| 339 if (!validScaleFactor) |
| 340 continue; |
| 341 |
| 342 ImageWithScale image; |
| 343 image.imageURL = decodeEscapeSequences<URLEscapeSequence>(data[0], UTF8E
ncoding()); |
| 344 image.scaleFactor = imgScaleFactor; |
| 345 |
| 346 imageCandidates.append(image); |
| 347 } |
| 348 |
| 349 const String src = srcAttribute.simplifyWhiteSpace(isHTMLSpace); |
| 350 if (!src.isEmpty()) { |
| 351 ImageWithScale image; |
| 352 image.imageURL = decodeEscapeSequences<URLEscapeSequence>(src, UTF8Encod
ing()); |
| 353 image.scaleFactor = 1.0; |
| 354 |
| 355 imageCandidates.append(image); |
| 356 } |
| 357 |
| 358 if (imageCandidates.isEmpty()) |
| 359 return String(); |
| 360 |
| 361 std::stable_sort(imageCandidates.begin(), imageCandidates.end(), compareBySc
aleFactor); |
| 362 |
| 363 for (size_t i = 0; i < imageCandidates.size() - 1; ++i) { |
| 364 if (imageCandidates[i].scaleFactor >= deviceScaleFactor) |
| 365 return imageCandidates[i].imageURL; |
| 366 } |
| 367 return imageCandidates.last().imageURL; |
| 368 } |
| 369 |
| 370 } |
OLD | NEW |