Bug 1408308: Integrate Servo SourceSizeList in ResponsiveImageSelector. r=Manishearth

MozReview-Commit-ID: DMVfE56DyYm
This commit is contained in:
Emilio Cobos Álvarez 2017-11-13 06:44:12 +01:00
Родитель 0a20dc62d7
Коммит f52846b518
2 изменённых файлов: 46 добавлений и 28 удалений

Просмотреть файл

@ -5,6 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/ResponsiveImageSelector.h"
#include "mozilla/ServoStyleSet.h"
#include "nsIURI.h"
#include "nsIDocument.h"
#include "nsContentUtils.h"
@ -241,11 +242,18 @@ bool
ResponsiveImageSelector::SetSizesFromDescriptor(const nsAString & aSizes)
{
ClearSelectedCandidate();
mSizeQueries.Clear();
mSizeValues.Clear();
if (Document()->IsStyledByServo()) {
NS_ConvertUTF16toUTF8 sizes(aSizes);
mServoSourceSizeList.reset(Servo_SourceSizeList_Parse(&sizes));
return !!mServoSourceSizeList;
}
nsCSSParser cssParser;
mSizeQueries.Clear();
mSizeValues.Clear();
return cssParser.ParseSourceSizeList(aSizes, nullptr, 0,
mSizeQueries, mSizeValues);
}
@ -364,11 +372,11 @@ ResponsiveImageSelector::SelectImage(bool aReselect)
}
nsIDocument* doc = Document();
nsIPresShell *shell = doc ? doc->GetShell() : nullptr;
nsPresContext *pctx = shell ? shell->GetPresContext() : nullptr;
nsCOMPtr<nsIURI> baseURI = mOwnerNode ? mOwnerNode->GetBaseURI() : nullptr;
nsIPresShell* shell = doc->GetShell();
nsPresContext* pctx = shell ? shell->GetPresContext() : nullptr;
nsCOMPtr<nsIURI> baseURI = mOwnerNode->GetBaseURI();
if (!pctx || !doc || !baseURI) {
if (!pctx || !baseURI) {
return oldBest != -1;
}
@ -388,7 +396,7 @@ ResponsiveImageSelector::SelectImage(bool aReselect)
double computedWidth = -1;
for (int i = 0; i < numCandidates; i++) {
if (mCandidates[i].IsComputedFromWidth()) {
DebugOnly<bool> computeResult = \
DebugOnly<bool> computeResult =
ComputeFinalWidthForCurrentViewport(&computedWidth);
MOZ_ASSERT(computeResult,
"Computed candidates not allowed without sizes data");
@ -439,34 +447,38 @@ ResponsiveImageSelector::GetSelectedCandidateIndex()
bool
ResponsiveImageSelector::ComputeFinalWidthForCurrentViewport(double *aWidth)
{
unsigned int numSizes = mSizeQueries.Length();
nsIDocument* doc = Document();
nsIPresShell *presShell = doc ? doc->GetShell() : nullptr;
nsPresContext *pctx = presShell ? presShell->GetPresContext() : nullptr;
nsIPresShell* presShell = doc->GetShell();
nsPresContext* pctx = presShell ? presShell->GetPresContext() : nullptr;
if (!pctx) {
return false;
}
MOZ_ASSERT(numSizes == mSizeValues.Length(),
"mSizeValues length differs from mSizeQueries");
unsigned int i;
for (i = 0; i < numSizes; i++) {
if (mSizeQueries[i]->Matches(pctx, nullptr)) {
break;
}
}
nscoord effectiveWidth;
if (i == numSizes) {
// No match defaults to 100% viewport
nsCSSValue defaultWidth(100.0f, eCSSUnit_ViewportWidth);
effectiveWidth = nsRuleNode::CalcLengthWithInitialFont(pctx,
defaultWidth);
if (doc->IsStyledByServo()) {
effectiveWidth = presShell->StyleSet()->AsServo()->EvaluateSourceSizeList(
mServoSourceSizeList.get());
} else {
effectiveWidth = nsRuleNode::CalcLengthWithInitialFont(pctx,
mSizeValues[i]);
unsigned int numSizes = mSizeQueries.Length();
MOZ_ASSERT(numSizes == mSizeValues.Length(),
"mSizeValues length differs from mSizeQueries");
unsigned int i;
for (i = 0; i < numSizes; i++) {
if (mSizeQueries[i]->Matches(pctx, nullptr)) {
break;
}
}
if (i == numSizes) {
// No match defaults to 100% viewport
nsCSSValue defaultWidth(100.0f, eCSSUnit_ViewportWidth);
effectiveWidth = nsRuleNode::CalcLengthWithInitialFont(pctx,
defaultWidth);
} else {
effectiveWidth = nsRuleNode::CalcLengthWithInitialFont(pctx,
mSizeValues[i]);
}
}
*aWidth = nsPresContext::AppUnitsToDoubleCSSPixels(std::max(effectiveWidth, 0));

Просмотреть файл

@ -7,6 +7,8 @@
#ifndef mozilla_dom_responsiveimageselector_h__
#define mozilla_dom_responsiveimageselector_h__
#include "mozilla/UniquePtr.h"
#include "mozilla/ServoBindingTypes.h"
#include "nsAutoPtr.h"
#include "nsISupports.h"
#include "nsIContent.h"
@ -120,6 +122,10 @@ private:
// resolve the absolute URL at selection time
nsCOMPtr<nsIURI> mSelectedCandidateURL;
// Servo bits.
UniquePtr<RawServoSourceSizeList> mServoSourceSizeList;
// Gecko bits.
nsTArray< nsAutoPtr<nsMediaQuery> > mSizeQueries;
nsTArray<nsCSSValue> mSizeValues;
};