Chromium Code Reviews| Index: Source/core/html/parser/HTMLSrcsetParser.cpp |
| diff --git a/Source/core/html/parser/HTMLSrcsetParser.cpp b/Source/core/html/parser/HTMLSrcsetParser.cpp |
| index 89b39e65d98b77ba0333b4a20cc6c342a01c2fdf..90f0867f4514bbcb662040fbdf041a6d9350e6b4 100644 |
| --- a/Source/core/html/parser/HTMLSrcsetParser.cpp |
| +++ b/Source/core/html/parser/HTMLSrcsetParser.cpp |
| @@ -321,6 +321,39 @@ static void parseImageCandidatesFromSrcsetAttribute(const String& attribute, Vec |
| parseImageCandidatesFromSrcsetAttribute<UChar>(attribute, attribute.characters16(), attribute.length(), imageCandidates, document); |
| } |
| +static int selectionLogic(Vector<ImageCandidate>& imageCandidates, float deviceScaleFactor, bool ignoreSrc) |
| +{ |
| + unsigned i = 0; |
| + unsigned next; |
| + float nextDensity; |
| + float currentDensity; |
|
Mike West
2014/10/22 13:41:39
Why do you define `next`, `nextDensity`, and `curr
|
| + float geometricMean; |
| + |
| + for (; i < imageCandidates.size() - 1; ++i) { |
| + next = i + 1; |
| + if (ignoreSrc) { |
| + if (imageCandidates[i].srcOrigin()) |
| + continue; |
| + if (imageCandidates[next].srcOrigin()) { |
| + ++next; |
|
Mike West
2014/10/22 13:41:39
Should this be a loop? Or do you really only want
|
| + if (next >= imageCandidates.size()) |
| + break; |
| + } |
| + } |
| + |
| + nextDensity = imageCandidates[next].density(); |
| + if (nextDensity < deviceScaleFactor) |
| + continue; |
| + |
| + currentDensity = imageCandidates[i].density(); |
| + geometricMean = sqrt(currentDensity * nextDensity); |
| + if (deviceScaleFactor >= geometricMean) |
| + return next; |
| + break; |
| + } |
| + return i; |
| +} |
| + |
| static ImageCandidate pickBestImageCandidate(float deviceScaleFactor, float sourceSize, Vector<ImageCandidate>& imageCandidates) |
| { |
| const float defaultDensityValue = 1.0; |
| @@ -340,16 +373,8 @@ static ImageCandidate pickBestImageCandidate(float deviceScaleFactor, float sour |
| std::stable_sort(imageCandidates.begin(), imageCandidates.end(), compareByDensity); |
| - unsigned i; |
| - for (i = 0; i < imageCandidates.size() - 1; ++i) { |
| - if ((imageCandidates[i].density() >= deviceScaleFactor) && (!ignoreSrc || !imageCandidates[i].srcOrigin())) |
| - break; |
| - } |
| + unsigned i = selectionLogic(imageCandidates, deviceScaleFactor, ignoreSrc); |
| - if (imageCandidates[i].srcOrigin() && ignoreSrc) { |
| - ASSERT(i > 0); |
| - --i; |
| - } |
| float winningDensity = imageCandidates[i].density(); |
|
Mike West
2014/10/22 13:41:40
Perhaps ASSERT that `i` ended up in a valid range?
|
| unsigned winner = i; |