mirror of
https://github.com/roytam1/palemoon27.git
synced 2026-05-26 13:23:07 +00:00
76fdf7909d
- Bug 1222098 - Devicemotion event timestamp should return values from Android sensor API and not Gecko. r:smaug (0fb05341d5)
- Bug 1266701 - some header missing in dom/events, r=jwatt (59a8a30af3)
- namespace (c5796648b6)
- Bug 1243555: Remove unnecessary nsDocShell static_cast in EventListenerManager::HandleEventInternal(). r=smaug (21c559122c)
- Bug 1254629 - Let query events fail when content root is wrong; r=masayuki (64454249aa)
- Bug 1224233 - fix crashy usage of IMENotificationSender::Run using on-screen keyboard on Windows, r=smaug (ddcdf13ad0)
- Bug 1252058 IMEContentObserver::IMENotificationSender shouldn't post notifications when IMEContentObserver which is the owner of it stopped observing contents r=smaug (9f4a14f13a)
- Bug 1259671 part.1 Rename InternalScrollPortEvent::orientType to InternalScrollPortEvent::OrientType r=smaug (eaefa4472f)
- Bug 1259671 part.2 Rename InternalScrollPortEvent::vertical to InternalScrollPortEvent::eVertical r=smaug (91bcdcd1df)
- Bug 1259671 part.3 Rename InternalScrollPortEvent::horizontal to InternalScrollPortEvent::eHorizontal r=smaug (f3c206dc4c)
- Bug 1259671 part.4 Rename InternalScrollPortEvent::both to InternalScrollPortEvent::eBoth r=smaug (ac34dc95c1)
- Bug 1259671 part.5 Rename InternalScrollPortEvent::orient to InternalScrollPortEvent::mOrient r=smaug (6736862c75)
- Bug 1262324 (part 1) - Remove "locale.all" prefix from Unix charsets. r=emk. (b337137c3d)
- Bug 1255655 - Const-ify dom encodings and similar arrays. r=baku. (93d79e84a7)
- Bug 1262324 (part 2) - Introduce nsUConvProp. r=emk. (bad497dee6)
- Bug 1257877 - Remove UTF-16 support from TextEncoder. r=hsivonen (ad647b12a3)
- Bug 1120813 - Add support for the MS932 label of Shift_JIS. r=emk, r=Ms2ger (8a35fd93cf)
- Bug 1250930 - Use correct global when creating a key in GenerateAsymmetricKeyTask r=bz (ba65e0ae34)
- Bug 842818 - Allow calling WebCryptoTask::DispatchWithPromise() from workers r=keeler (e99301ba3e)
- Bug 1251390. Make timer queries available at the appropriate time. r=jgilbert (f1a0dcf7d3)
- Bug 1259696 - Check read buffer mode when doing CopyTexImage. r=jgilbert (fe51211b56)
- Bug 1241042 - Get correct frag varying from angle validator. r=jmuizelaar (29a4298a7e)
- Bug 1244611 - "Using named uniform buffer objects in the fragment shader fails". r=jgilbert (2e05e31d09)
- Bug 1263018 - Only update active prog info if linking active prog. - r=jrmuizel (1603ebdd8c)
- Bug 1257593 - Handle webgl FramebufferTexture2D() with an unbound texture. r=jgilbert (4d75981cb7)
- Bug 1257593 followup, fix bustage from believing a reviewer and then sending a different patch to try (7766b0cdb4)
- Bug 1258061 - Clarify FramebufferTexture2D handling for tex2D and cubeMap; r=jgilbert (0635d9412a)
- Bug 1259449 - Require GLFeature::sync for WebGL's disjoint_timer_query. - r=jrmuizel (b6017134a3)
- Bug 1247804 - Enable seamless cubemaps where available. - r=jrmuizel (e7fd3ec5ed)
- Bug 1247977: More information when we hit the OpenGL error in FakeBlackTexture. r=jgilbert (f6f2c82e33)
- Bug 1255655 - Const-ify sExtensionNames. r=mattwoodrow. (c0c60b34c7)
- Bug 1262757 - Use StaticRefPtr for the global context in GLContextProviderWGL, r=jrmuizel (0cc29a3385)
- Bug 1259811 - Require FBO support for GLContexts. - r=jrmuizel (ec3c1a6045)
- Bug 1234441 - Allow malformed ESSL version string. - r=jrmuizel (8fda1569ab)
- Bug 1199923 - Work around Intel Linux driver lying about max texture size - r=jgilbert (546b7dfe6a)
- Bug 1262265 - Cleanup GLContext symbol init. - r=jrmuizel (8da8ce647a)
- Bug 1232334 - [1.2] Only set context on successful attach. r=snorp (ca29c322bf)
- Bug 1261320 - Check DataSurface is vaild before using, r=milan (be9aebba47)
- Bug 1245868 - repalce pass by value with pass by pointer in IsHeadRequest. r=jst (f9d7f6185f)
- fix tests (3ccf9d1715)
- Bug 1232941 - Register Observer and listen to NS_XPCOM_SHUTDOWN_OBSERVER_ID for GATT, r=shuang (aa367807fa)
- Bug 1239979: Init and uninit all Bluetooth profile managers, r=shuang (78c1ebbd13)
- Bug 1239979: Uninitialized Bluetooth profile managers explictly to release refs, r=shuang (83b5389539)
- Bug 1262630. Replace workers::GetGlobalObjectForGlobal with xpc::NativeGlobal. r=khuey (9f639580be)
- Bug 1255817 part 6. Fix up some comments in CallbackObject that refer to things that no longer exist. r=bholley (575bf90c98)
- Bug 1259545. Remove the JS_SaveFrameChain bits in CallbackObject error reporting, since they are no longer needed. r=bholley (16181ddb38)
- Bug 1260511 part 2. Change XPConnect's handling of exceptions thrown from JS components so that if an nsresult integer is thrown we convert it into an actual exception object before handing it out to content instead of propagating out the numeric value. r=khuey (dc02854e3f)
- Bug 1260511 part 3. In dom::Throw, ignore the pending xpconnect exception if we were given a non-default message string (because we don't want to lose that string). Also, make sure to always clear the pending xpconnect exception there. r=khuey (978a1e9132)
- Bug 1250106 - Correctly set charging status and remaining time when battery level is rounded to 1.0 r=bz (662e18648c)
- Bug 1253641 - DOMException's CC participant should traverse mData. r=khuey. (0a48e3f8e7)
- Bug 1261115 - when Console is running in the main thread the existence of mWindow should always be ensured, r=smaug (caa1efd087)
- Bug 1257208 - Use the nsTextNode concrete type in several places in DirectionalityUtils.cpp instead of nsINode and nsIContent; r=peterv (69529fd8d4)
- Bug 1260982 - BlobFileImpl::GetType() should work also in workers, r=smaug (4b01d269bc)
- Bug 1262104 - Remove a non-used CTOR for BlobImplFile, r=ehsan (37ee0ec6d4)
- Bug 1259477 - Port test_document_register.html to mochitest-plain so that it can be turned on in e10s mode; r=mrbkap (411c220cda)
- Bug 1222128 - Turn test_bug1011748.html into a browser mochitest to make it run properly in e10s mode r=bzbarsky (45403d3d15)
- Bug 1259588 - new File("") throws TypeError exception, r=baku (05f6e7292d)
- Bug 1264710 - Catch IDB exceptions in IndexedDBHelper. r=fabrice (0d3c860a89)
- Bug 1263553 - Move MultipartBlobImpl into mozilla::dom namespace, r=smaug (e286c6cfe1)
- Bug 1263551 - Remove unused method in MultipartBlobImpl, r=smaug (54c2da9a12)
- Bug 1252687 - get rid of static nsStrings in PerformanceObserver.cpp r=bz (319f2697d4)
- Bug 1148535 - Check if the density descriptor in srcset consists of a valid floating-point number. r=jdm (79ac8d8dea)
- Bug 1257742 - Part 1: Follow the update-source-set rule to append default source into source set; r=jdm (853c69cc8c)
- Bug 1257742 - Part 2: Allow both width and static density candidates showing in same selector; r=jdm (4f1e00225d)
- Bug 1257742 - Part 3: Support using floating point in sizes descriptor; r=jdm (c1a7e36bb3)
- Bug 1257742 - Part 4: Update web-platform test expectation; r=jdm (e41044b88e)
- Bug 1262942 - Remove unnecessary warning message in ResponsiveImageSelector; r=jdm (ffb757204d)
- Bug 1158412 - Remove assertion for document prescontex and add crash test; r=jdm (947ccdfbfc)
- Bug 1237633 - Part 1: Percentages are not allowed in a <source-size-value>. r=jdm (614b560097)
- Bug 1237633 - Part 2: Avoid fatal assertion when a responsive image's size specifier is invalid. r=johns, r=jdm (4e90829d97)
- Bug 495546 - Add crashtest. (c2765ecbf4)
- fix some tests (d9b393b168)
- Bug 1256419. Null-check our nsDOMWindowList before trying to get its length. r=smaug (6c14430e5d)
- Bug 1162775: Make contentAreaDropListener use dataTransfer.files to get the files dropped. r=smaug (d2850f2008)
- Bug 1220679 - replace AutoSafeJSContext with AutoJSAPI. r=bz. (64538bdd44)
- reinstantiate assert, present in up to esr68 (221cec538a)
- Bug 1209329 - Improve comments about about: URIs in nsContentUtils::InternalStorageAllowedForPrincipal, r=bholley (71152e5639)
- Bug 1246250 - Deal with failure to create a blob actor. r=khuey (102686ac28)
- Bug 1265902 - part 1 - be more efficient when using nsContentUtils::GetSurfaceData(); r=mccr8 (ba2a52abd3)
- Bug 1258857 - Add empty items to an IPC transferable object for every flavor of the source object that did not have any data associated to it r=enndeakin (0a02b61566)
- Bug 1265902 - part 2 - don't construct unnecessary string temporaries in TransferablesToIPCTransferables; r=mccr8 (aec10c8fc6)
- remove unknown blob handling, not found in Tycho-dev repo, nor esr60 or TFF (eb6a24720a)
- Bug 1155486 - Convert nsDOMAttributeMap::mLocalName to void* to ensure that we can never dereference it; r=baku (34e2864340)
- Bug 1250926 - Remove unused SCRIPTABLE_FLAGS defines from nsDOMClassInfo; r=peterv (76917fb76b)
- No bug. Helper tool to partially autogenerate portions of the release notes. (3818e5534b)
- Bug 1203423 - Move call to AddClone outside nsMutationReceiver constructor; r=smaug (35c94ad785)
- Bug 1254096 - Update CaretPositionFromPoint() for type=number, r=ehsan (df31edca8f)
- Bug 1265771 P1 Only store active documents in the global observer list. r=bz (01502e91e5)
- Bug 1265771 P2 Expand navigate-window.https.html wpt test to cover uncontrolled windows. r=bz (3333906720)
- Bug 1265771 P3 Expand browser_force_refresh.js to verify Clients.matchAll() behavior on refresh. r=bz (01394ec8f1)
- stop hiding things for _LIBCPP_VERSION (5de86e8bbf)
789 lines
22 KiB
C++
789 lines
22 KiB
C++
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
#include "mozilla/dom/ResponsiveImageSelector.h"
|
|
#include "nsIURI.h"
|
|
#include "nsIDocument.h"
|
|
#include "nsContentUtils.h"
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsCSSParser.h"
|
|
#include "nsCSSProps.h"
|
|
#include "nsIMediaList.h"
|
|
#include "nsRuleNode.h"
|
|
#include "nsRuleData.h"
|
|
|
|
// For IsPictureEnabled() -- the candidate parser needs to be aware of sizes
|
|
// support being enabled
|
|
#include "HTMLPictureElement.h"
|
|
|
|
using namespace mozilla;
|
|
using namespace mozilla::dom;
|
|
|
|
namespace mozilla {
|
|
namespace dom {
|
|
|
|
NS_IMPL_CYCLE_COLLECTION(ResponsiveImageSelector, mOwnerNode)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(ResponsiveImageSelector, AddRef)
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(ResponsiveImageSelector, Release)
|
|
|
|
static bool
|
|
ParseInteger(const nsAString& aString, int32_t& aInt)
|
|
{
|
|
nsContentUtils::ParseHTMLIntegerResultFlags parseResult;
|
|
aInt = nsContentUtils::ParseHTMLInteger(aString, &parseResult);
|
|
return !(parseResult &
|
|
( nsContentUtils::eParseHTMLInteger_Error |
|
|
nsContentUtils::eParseHTMLInteger_DidNotConsumeAllInput |
|
|
nsContentUtils::eParseHTMLInteger_IsPercent |
|
|
nsContentUtils::eParseHTMLInteger_NonStandard ));
|
|
}
|
|
|
|
static bool
|
|
ParseFloat(const nsAString& aString, double& aDouble)
|
|
{
|
|
// Check if it is a valid floating-point number first since the result of
|
|
// nsString.ToDouble() is more lenient than the spec,
|
|
// https://html.spec.whatwg.org/#valid-floating-point-number
|
|
nsAString::const_iterator iter, end;
|
|
aString.BeginReading(iter);
|
|
aString.EndReading(end);
|
|
|
|
if (iter == end) {
|
|
return false;
|
|
}
|
|
|
|
if (*iter == char16_t('-') && ++iter == end) {
|
|
return false;
|
|
}
|
|
|
|
if (nsCRT::IsAsciiDigit(*iter)) {
|
|
for (; iter != end && nsCRT::IsAsciiDigit(*iter) ; ++iter);
|
|
} else if (*iter == char16_t('.')) {
|
|
// Do nothing, jumps to fraction part
|
|
} else {
|
|
return false;
|
|
}
|
|
|
|
// Fraction
|
|
if (*iter == char16_t('.')) {
|
|
++iter;
|
|
if (iter == end || !nsCRT::IsAsciiDigit(*iter)) {
|
|
// U+002E FULL STOP character (.) must be followed by one or more ASCII digits
|
|
return false;
|
|
}
|
|
|
|
for (; iter != end && nsCRT::IsAsciiDigit(*iter) ; ++iter);
|
|
}
|
|
|
|
if (iter != end && (*iter == char16_t('e') || *iter == char16_t('E'))) {
|
|
++iter;
|
|
if (*iter == char16_t('-') || *iter == char16_t('+')) {
|
|
++iter;
|
|
}
|
|
|
|
if (iter == end || !nsCRT::IsAsciiDigit(*iter)) {
|
|
// Should have one or more ASCII digits
|
|
return false;
|
|
}
|
|
|
|
for (; iter != end && nsCRT::IsAsciiDigit(*iter) ; ++iter);
|
|
}
|
|
|
|
if (iter != end) {
|
|
return false;
|
|
}
|
|
|
|
nsresult rv;
|
|
aDouble = PromiseFlatString(aString).ToDouble(&rv);
|
|
return NS_SUCCEEDED(rv);
|
|
}
|
|
|
|
ResponsiveImageSelector::ResponsiveImageSelector(nsIContent *aContent)
|
|
: mOwnerNode(aContent),
|
|
mSelectedCandidateIndex(-1)
|
|
{
|
|
}
|
|
|
|
ResponsiveImageSelector::ResponsiveImageSelector(nsIDocument *aDocument)
|
|
: mOwnerNode(aDocument),
|
|
mSelectedCandidateIndex(-1)
|
|
{
|
|
}
|
|
|
|
ResponsiveImageSelector::~ResponsiveImageSelector()
|
|
{}
|
|
|
|
// http://www.whatwg.org/specs/web-apps/current-work/#processing-the-image-candidates
|
|
bool
|
|
ResponsiveImageSelector::SetCandidatesFromSourceSet(const nsAString & aSrcSet)
|
|
{
|
|
ClearSelectedCandidate();
|
|
|
|
nsCOMPtr<nsIURI> docBaseURI = mOwnerNode ? mOwnerNode->GetBaseURI() : nullptr;
|
|
|
|
if (!docBaseURI) {
|
|
MOZ_ASSERT(false,
|
|
"Should not be parsing SourceSet without a document");
|
|
return false;
|
|
}
|
|
|
|
mCandidates.Clear();
|
|
|
|
nsAString::const_iterator iter, end;
|
|
aSrcSet.BeginReading(iter);
|
|
aSrcSet.EndReading(end);
|
|
|
|
// Read URL / descriptor pairs
|
|
while (iter != end) {
|
|
nsAString::const_iterator url, urlEnd, descriptor;
|
|
|
|
// Skip whitespace and commas.
|
|
// Extra commas at this point are a non-fatal syntax error.
|
|
for (; iter != end && (nsContentUtils::IsHTMLWhitespace(*iter) ||
|
|
*iter == char16_t(',')); ++iter);
|
|
|
|
if (iter == end) {
|
|
break;
|
|
}
|
|
|
|
url = iter;
|
|
|
|
// Find end of url
|
|
for (;iter != end && !nsContentUtils::IsHTMLWhitespace(*iter); ++iter);
|
|
|
|
// Omit trailing commas from URL.
|
|
// Multiple commas are a non-fatal error.
|
|
while (iter != url) {
|
|
if (*(--iter) != char16_t(',')) {
|
|
iter++;
|
|
break;
|
|
}
|
|
}
|
|
|
|
const nsDependentSubstring &urlStr = Substring(url, iter);
|
|
|
|
MOZ_ASSERT(url != iter, "Shouldn't have empty URL at this point");
|
|
|
|
ResponsiveImageCandidate candidate;
|
|
if (candidate.ConsumeDescriptors(iter, end)) {
|
|
candidate.SetURLSpec(urlStr);
|
|
AppendCandidateIfUnique(candidate);
|
|
}
|
|
}
|
|
|
|
bool parsedCandidates = mCandidates.Length() > 0;
|
|
|
|
// Re-add default to end of list
|
|
MaybeAppendDefaultCandidate();
|
|
|
|
return parsedCandidates;
|
|
}
|
|
|
|
uint32_t
|
|
ResponsiveImageSelector::NumCandidates(bool aIncludeDefault)
|
|
{
|
|
uint32_t candidates = mCandidates.Length();
|
|
|
|
// If present, the default candidate is the last item
|
|
if (!aIncludeDefault && candidates &&
|
|
(mCandidates[candidates - 1].Type() ==
|
|
ResponsiveImageCandidate::eCandidateType_Default)) {
|
|
candidates--;
|
|
}
|
|
|
|
return candidates;
|
|
}
|
|
|
|
nsIContent*
|
|
ResponsiveImageSelector::Content()
|
|
{
|
|
return mOwnerNode->IsContent() ? mOwnerNode->AsContent() : nullptr;
|
|
}
|
|
|
|
nsIDocument*
|
|
ResponsiveImageSelector::Document()
|
|
{
|
|
return mOwnerNode->OwnerDoc();
|
|
}
|
|
|
|
void
|
|
ResponsiveImageSelector::SetDefaultSource(const nsAString& aURLString)
|
|
{
|
|
ClearSelectedCandidate();
|
|
|
|
// Check if the last element of our candidates is a default
|
|
int32_t candidates = mCandidates.Length();
|
|
if (candidates && (mCandidates[candidates - 1].Type() ==
|
|
ResponsiveImageCandidate::eCandidateType_Default)) {
|
|
mCandidates.RemoveElementAt(candidates - 1);
|
|
}
|
|
|
|
mDefaultSourceURL = aURLString;
|
|
|
|
// Add new default to end of list
|
|
MaybeAppendDefaultCandidate();
|
|
}
|
|
|
|
void
|
|
ResponsiveImageSelector::ClearSelectedCandidate()
|
|
{
|
|
mSelectedCandidateIndex = -1;
|
|
mSelectedCandidateURL = nullptr;
|
|
}
|
|
|
|
bool
|
|
ResponsiveImageSelector::SetSizesFromDescriptor(const nsAString & aSizes)
|
|
{
|
|
ClearSelectedCandidate();
|
|
mSizeQueries.Clear();
|
|
mSizeValues.Clear();
|
|
|
|
nsCSSParser cssParser;
|
|
|
|
if (!cssParser.ParseSourceSizeList(aSizes, nullptr, 0,
|
|
mSizeQueries, mSizeValues, true)) {
|
|
return false;
|
|
}
|
|
|
|
return mSizeQueries.Length() > 0;
|
|
}
|
|
|
|
void
|
|
ResponsiveImageSelector::AppendCandidateIfUnique(const ResponsiveImageCandidate & aCandidate)
|
|
{
|
|
int numCandidates = mCandidates.Length();
|
|
|
|
// With the exception of Default, which should not be added until we are done
|
|
// building the list.
|
|
if (aCandidate.Type() == ResponsiveImageCandidate::eCandidateType_Default) {
|
|
return;
|
|
}
|
|
|
|
// Discard candidates with identical parameters, they will never match
|
|
for (int i = 0; i < numCandidates; i++) {
|
|
if (mCandidates[i].HasSameParameter(aCandidate)) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
mCandidates.AppendElement(aCandidate);
|
|
}
|
|
|
|
void
|
|
ResponsiveImageSelector::MaybeAppendDefaultCandidate()
|
|
{
|
|
if (mDefaultSourceURL.IsEmpty()) {
|
|
return;
|
|
}
|
|
|
|
int numCandidates = mCandidates.Length();
|
|
|
|
// https://html.spec.whatwg.org/multipage/embedded-content.html#update-the-source-set
|
|
// step 4.1.3:
|
|
// If child has a src attribute whose value is not the empty string and source
|
|
// set does not contain an image source with a density descriptor value of 1,
|
|
// and no image source with a width descriptor, append child's src attribute
|
|
// value to source set.
|
|
for (int i = 0; i < numCandidates; i++) {
|
|
if (mCandidates[i].IsComputedFromWidth()) {
|
|
return;
|
|
} else if (mCandidates[i].Density(this) == 1.0) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
ResponsiveImageCandidate defaultCandidate;
|
|
defaultCandidate.SetParameterDefault();
|
|
defaultCandidate.SetURLSpec(mDefaultSourceURL);
|
|
// We don't use MaybeAppend since we want to keep this even if it can never
|
|
// match, as it may if the source set changes.
|
|
mCandidates.AppendElement(defaultCandidate);
|
|
}
|
|
|
|
already_AddRefed<nsIURI>
|
|
ResponsiveImageSelector::GetSelectedImageURL()
|
|
{
|
|
SelectImage();
|
|
|
|
nsCOMPtr<nsIURI> url = mSelectedCandidateURL;
|
|
return url.forget();
|
|
}
|
|
|
|
bool
|
|
ResponsiveImageSelector::GetSelectedImageURLSpec(nsAString& aResult)
|
|
{
|
|
SelectImage();
|
|
|
|
if (mSelectedCandidateIndex == -1) {
|
|
return false;
|
|
}
|
|
|
|
aResult.Assign(mCandidates[mSelectedCandidateIndex].URLString());
|
|
return true;
|
|
}
|
|
|
|
double
|
|
ResponsiveImageSelector::GetSelectedImageDensity()
|
|
{
|
|
int bestIndex = GetSelectedCandidateIndex();
|
|
if (bestIndex < 0) {
|
|
return 1.0;
|
|
}
|
|
|
|
return mCandidates[bestIndex].Density(this);
|
|
}
|
|
|
|
bool
|
|
ResponsiveImageSelector::SelectImage(bool aReselect)
|
|
{
|
|
if (!aReselect && mSelectedCandidateIndex != -1) {
|
|
// Already have selection
|
|
return false;
|
|
}
|
|
|
|
int oldBest = mSelectedCandidateIndex;
|
|
ClearSelectedCandidate();
|
|
|
|
int numCandidates = mCandidates.Length();
|
|
if (!numCandidates) {
|
|
return oldBest != -1;
|
|
}
|
|
|
|
nsIDocument* doc = Document();
|
|
nsIPresShell *shell = doc ? doc->GetShell() : nullptr;
|
|
nsPresContext *pctx = shell ? shell->GetPresContext() : nullptr;
|
|
nsCOMPtr<nsIURI> baseURI = mOwnerNode ? mOwnerNode->GetBaseURI() : nullptr;
|
|
|
|
if (!pctx || !doc || !baseURI) {
|
|
return oldBest != -1;
|
|
}
|
|
|
|
double displayDensity = pctx->CSSPixelsToDevPixels(1.0f);
|
|
|
|
// Per spec, "In a UA-specific manner, choose one image source"
|
|
// - For now, select the lowest density greater than displayDensity, otherwise
|
|
// the greatest density available
|
|
|
|
// If the list contains computed width candidates, compute the current
|
|
// effective image width.
|
|
double computedWidth = -1;
|
|
for (int i = 0; i < numCandidates; i++) {
|
|
if (mCandidates[i].IsComputedFromWidth()) {
|
|
DebugOnly<bool> computeResult = \
|
|
ComputeFinalWidthForCurrentViewport(&computedWidth);
|
|
MOZ_ASSERT(computeResult,
|
|
"Computed candidates not allowed without sizes data");
|
|
break;
|
|
}
|
|
}
|
|
|
|
int bestIndex = -1;
|
|
double bestDensity = -1.0;
|
|
for (int i = 0; i < numCandidates; i++) {
|
|
double candidateDensity = \
|
|
(computedWidth == -1) ? mCandidates[i].Density(this)
|
|
: mCandidates[i].Density(computedWidth);
|
|
// - If bestIndex is below display density, pick anything larger.
|
|
// - Otherwise, prefer if less dense than bestDensity but still above
|
|
// displayDensity.
|
|
if (bestIndex == -1 ||
|
|
(bestDensity < displayDensity && candidateDensity > bestDensity) ||
|
|
(candidateDensity >= displayDensity && candidateDensity < bestDensity)) {
|
|
bestIndex = i;
|
|
bestDensity = candidateDensity;
|
|
}
|
|
}
|
|
|
|
MOZ_ASSERT(bestIndex >= 0 && bestIndex < numCandidates);
|
|
|
|
// Resolve URL
|
|
nsresult rv;
|
|
const nsAString& urlStr = mCandidates[bestIndex].URLString();
|
|
nsCOMPtr<nsIURI> candidateURL;
|
|
rv = nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(candidateURL),
|
|
urlStr, doc, baseURI);
|
|
|
|
mSelectedCandidateURL = NS_SUCCEEDED(rv) ? candidateURL : nullptr;
|
|
mSelectedCandidateIndex = bestIndex;
|
|
|
|
return mSelectedCandidateIndex != oldBest;
|
|
}
|
|
|
|
int
|
|
ResponsiveImageSelector::GetSelectedCandidateIndex()
|
|
{
|
|
SelectImage();
|
|
|
|
return mSelectedCandidateIndex;
|
|
}
|
|
|
|
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;
|
|
|
|
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);
|
|
} else {
|
|
effectiveWidth = nsRuleNode::CalcLengthWithInitialFont(pctx,
|
|
mSizeValues[i]);
|
|
}
|
|
|
|
*aWidth = nsPresContext::AppUnitsToDoubleCSSPixels(std::max(effectiveWidth, 0));
|
|
return true;
|
|
}
|
|
|
|
ResponsiveImageCandidate::ResponsiveImageCandidate()
|
|
{
|
|
mType = eCandidateType_Invalid;
|
|
mValue.mDensity = 1.0;
|
|
}
|
|
|
|
ResponsiveImageCandidate::ResponsiveImageCandidate(const nsAString& aURLString,
|
|
double aDensity)
|
|
: mURLString(aURLString)
|
|
{
|
|
mType = eCandidateType_Density;
|
|
mValue.mDensity = aDensity;
|
|
}
|
|
|
|
|
|
void
|
|
ResponsiveImageCandidate::SetURLSpec(const nsAString& aURLString)
|
|
{
|
|
mURLString = aURLString;
|
|
}
|
|
|
|
void
|
|
ResponsiveImageCandidate::SetParameterAsComputedWidth(int32_t aWidth)
|
|
{
|
|
mType = eCandidateType_ComputedFromWidth;
|
|
mValue.mWidth = aWidth;
|
|
}
|
|
|
|
void
|
|
ResponsiveImageCandidate::SetParameterDefault()
|
|
{
|
|
MOZ_ASSERT(mType == eCandidateType_Invalid, "double setting candidate type");
|
|
|
|
mType = eCandidateType_Default;
|
|
// mValue shouldn't actually be used for this type, but set it to default
|
|
// anyway
|
|
mValue.mDensity = 1.0;
|
|
}
|
|
|
|
void
|
|
ResponsiveImageCandidate::SetParameterInvalid()
|
|
{
|
|
mType = eCandidateType_Invalid;
|
|
// mValue shouldn't actually be used for this type, but set it to default
|
|
// anyway
|
|
mValue.mDensity = 1.0;
|
|
}
|
|
|
|
void
|
|
ResponsiveImageCandidate::SetParameterAsDensity(double aDensity)
|
|
{
|
|
MOZ_ASSERT(mType == eCandidateType_Invalid, "double setting candidate type");
|
|
|
|
mType = eCandidateType_Density;
|
|
mValue.mDensity = aDensity;
|
|
}
|
|
|
|
// Represents all supported descriptors for a ResponsiveImageCandidate, though
|
|
// there is no candidate type that uses all of these. This should generally
|
|
// match the mValue union of ResponsiveImageCandidate.
|
|
struct ResponsiveImageDescriptors {
|
|
ResponsiveImageDescriptors()
|
|
: mInvalid(false) {};
|
|
|
|
Maybe<double> mDensity;
|
|
Maybe<int32_t> mWidth;
|
|
// We don't support "h" descriptors yet and they are not spec'd, but the
|
|
// current spec does specify that they can be silently ignored (whereas
|
|
// entirely unknown descriptors cause us to invalidate the candidate)
|
|
Maybe<int32_t> mFutureCompatHeight;
|
|
// If this descriptor set is bogus, e.g. a value was added twice (and thus
|
|
// dropped) or an unknown descriptor was added.
|
|
bool mInvalid;
|
|
|
|
void AddDescriptor(const nsAString& aDescriptor);
|
|
bool Valid();
|
|
// Use the current set of descriptors to configure a candidate
|
|
void FillCandidate(ResponsiveImageCandidate &aCandidate);
|
|
};
|
|
|
|
// Try to parse a single descriptor from a string. If value already set or
|
|
// unknown, sets invalid flag.
|
|
// This corresponds to the descriptor "Descriptor parser" step in:
|
|
// https://html.spec.whatwg.org/#parse-a-srcset-attribute
|
|
void
|
|
ResponsiveImageDescriptors::AddDescriptor(const nsAString& aDescriptor)
|
|
{
|
|
if (aDescriptor.IsEmpty()) {
|
|
return;
|
|
}
|
|
|
|
// All currently supported descriptors end with an identifying character.
|
|
nsAString::const_iterator descStart, descType;
|
|
aDescriptor.BeginReading(descStart);
|
|
aDescriptor.EndReading(descType);
|
|
descType--;
|
|
const nsDependentSubstring& valueStr = Substring(descStart, descType);
|
|
if (*descType == char16_t('w')) {
|
|
int32_t possibleWidth;
|
|
// If the value is not a valid non-negative integer, it doesn't match this
|
|
// descriptor, fall through.
|
|
if (ParseInteger(valueStr, possibleWidth) && possibleWidth >= 0) {
|
|
if (possibleWidth != 0 && HTMLPictureElement::IsPictureEnabled() &&
|
|
mWidth.isNothing() && mDensity.isNothing()) {
|
|
mWidth.emplace(possibleWidth);
|
|
} else {
|
|
// Valid width descriptor, but width or density were already seen, sizes
|
|
// support isn't enabled, or it parsed to 0, which is an error per spec
|
|
mInvalid = true;
|
|
}
|
|
|
|
return;
|
|
}
|
|
} else if (*descType == char16_t('h')) {
|
|
int32_t possibleHeight;
|
|
// If the value is not a valid non-negative integer, it doesn't match this
|
|
// descriptor, fall through.
|
|
if (ParseInteger(valueStr, possibleHeight) && possibleHeight >= 0) {
|
|
if (possibleHeight != 0 && mFutureCompatHeight.isNothing() &&
|
|
mDensity.isNothing()) {
|
|
mFutureCompatHeight.emplace(possibleHeight);
|
|
} else {
|
|
// Valid height descriptor, but height or density were already seen, or
|
|
// it parsed to zero, which is an error per spec
|
|
mInvalid = true;
|
|
}
|
|
|
|
return;
|
|
}
|
|
} else if (*descType == char16_t('x')) {
|
|
// If the value is not a valid floating point number, it doesn't match this
|
|
// descriptor, fall through.
|
|
double possibleDensity = 0.0;
|
|
if (ParseFloat(valueStr, possibleDensity)) {
|
|
if (possibleDensity >= 0.0 &&
|
|
mWidth.isNothing() &&
|
|
mDensity.isNothing() &&
|
|
mFutureCompatHeight.isNothing()) {
|
|
mDensity.emplace(possibleDensity);
|
|
} else {
|
|
// Valid density descriptor, but height or width or density were already
|
|
// seen, or it parsed to less than zero, which is an error per spec
|
|
mInvalid = true;
|
|
}
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Matched no known descriptor, mark this descriptor set invalid
|
|
mInvalid = true;
|
|
}
|
|
|
|
bool
|
|
ResponsiveImageDescriptors::Valid()
|
|
{
|
|
return !mInvalid && !(mFutureCompatHeight.isSome() && mWidth.isNothing());
|
|
}
|
|
|
|
void
|
|
ResponsiveImageDescriptors::FillCandidate(ResponsiveImageCandidate &aCandidate)
|
|
{
|
|
if (!Valid()) {
|
|
aCandidate.SetParameterInvalid();
|
|
} else if (mWidth.isSome()) {
|
|
MOZ_ASSERT(mDensity.isNothing()); // Shouldn't be valid
|
|
|
|
aCandidate.SetParameterAsComputedWidth(*mWidth);
|
|
} else if (mDensity.isSome()) {
|
|
MOZ_ASSERT(mWidth.isNothing()); // Shouldn't be valid
|
|
|
|
aCandidate.SetParameterAsDensity(*mDensity);
|
|
} else {
|
|
// A valid set of descriptors with no density nor width (e.g. an empty set)
|
|
// becomes 1.0 density, per spec
|
|
aCandidate.SetParameterAsDensity(1.0);
|
|
}
|
|
}
|
|
|
|
bool
|
|
ResponsiveImageCandidate::ConsumeDescriptors(nsAString::const_iterator& aIter,
|
|
const nsAString::const_iterator& aIterEnd)
|
|
{
|
|
nsAString::const_iterator &iter = aIter;
|
|
const nsAString::const_iterator &end = aIterEnd;
|
|
|
|
bool inParens = false;
|
|
|
|
ResponsiveImageDescriptors descriptors;
|
|
|
|
// Parse descriptor list.
|
|
// This corresponds to the descriptor parsing loop from:
|
|
// https://html.spec.whatwg.org/#parse-a-srcset-attribute
|
|
|
|
// Skip initial whitespace
|
|
for (; iter != end && nsContentUtils::IsHTMLWhitespace(*iter); ++iter);
|
|
|
|
nsAString::const_iterator currentDescriptor = iter;
|
|
|
|
for (;; iter++) {
|
|
if (iter == end) {
|
|
descriptors.AddDescriptor(Substring(currentDescriptor, iter));
|
|
break;
|
|
} else if (inParens) {
|
|
if (*iter == char16_t(')')) {
|
|
inParens = false;
|
|
}
|
|
} else {
|
|
if (*iter == char16_t(',')) {
|
|
// End of descriptors, flush current descriptor and advance past comma
|
|
// before breaking
|
|
descriptors.AddDescriptor(Substring(currentDescriptor, iter));
|
|
iter++;
|
|
break;
|
|
} else if (nsContentUtils::IsHTMLWhitespace(*iter)) {
|
|
// End of current descriptor, consume it, skip spaces
|
|
// ("After descriptor" state in spec) before continuing
|
|
descriptors.AddDescriptor(Substring(currentDescriptor, iter));
|
|
for (; iter != end && nsContentUtils::IsHTMLWhitespace(*iter); ++iter);
|
|
if (iter == end) {
|
|
break;
|
|
}
|
|
currentDescriptor = iter;
|
|
// Leave one whitespace so the loop advances to this position next iteration
|
|
iter--;
|
|
} else if (*iter == char16_t('(')) {
|
|
inParens = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
descriptors.FillCandidate(*this);
|
|
|
|
return Type() != eCandidateType_Invalid;
|
|
}
|
|
|
|
bool
|
|
ResponsiveImageCandidate::HasSameParameter(const ResponsiveImageCandidate & aOther) const
|
|
{
|
|
if (aOther.mType != mType) {
|
|
return false;
|
|
}
|
|
|
|
if (mType == eCandidateType_Default) {
|
|
return true;
|
|
}
|
|
|
|
if (mType == eCandidateType_Density) {
|
|
return aOther.mValue.mDensity == mValue.mDensity;
|
|
}
|
|
|
|
if (mType == eCandidateType_Invalid) {
|
|
MOZ_ASSERT(false, "Comparing invalid candidates?");
|
|
return true;
|
|
} else if (mType == eCandidateType_ComputedFromWidth) {
|
|
return aOther.mValue.mWidth == mValue.mWidth;
|
|
}
|
|
|
|
MOZ_ASSERT(false, "Somebody forgot to check for all uses of this enum");
|
|
return false;
|
|
}
|
|
|
|
const nsAString&
|
|
ResponsiveImageCandidate::URLString() const
|
|
{
|
|
return mURLString;
|
|
}
|
|
|
|
double
|
|
ResponsiveImageCandidate::Density(ResponsiveImageSelector *aSelector) const
|
|
{
|
|
if (mType == eCandidateType_ComputedFromWidth) {
|
|
double width;
|
|
if (!aSelector->ComputeFinalWidthForCurrentViewport(&width)) {
|
|
return 1.0;
|
|
}
|
|
return Density(width);
|
|
}
|
|
|
|
// Other types don't need matching width
|
|
MOZ_ASSERT(mType == eCandidateType_Default || mType == eCandidateType_Density,
|
|
"unhandled candidate type");
|
|
return Density(-1);
|
|
}
|
|
|
|
double
|
|
ResponsiveImageCandidate::Density(double aMatchingWidth) const
|
|
{
|
|
if (mType == eCandidateType_Invalid) {
|
|
MOZ_ASSERT(false, "Getting density for uninitialized candidate");
|
|
return 1.0;
|
|
}
|
|
|
|
if (mType == eCandidateType_Default) {
|
|
return 1.0;
|
|
}
|
|
|
|
if (mType == eCandidateType_Density) {
|
|
return mValue.mDensity;
|
|
} else if (mType == eCandidateType_ComputedFromWidth) {
|
|
if (aMatchingWidth < 0) {
|
|
MOZ_ASSERT(false, "Don't expect to have a negative matching width at this point");
|
|
return 1.0;
|
|
}
|
|
double density = double(mValue.mWidth) / aMatchingWidth;
|
|
MOZ_ASSERT(density > 0.0);
|
|
return density;
|
|
}
|
|
|
|
MOZ_ASSERT(false, "Unknown candidate type");
|
|
return 1.0;
|
|
}
|
|
|
|
bool
|
|
ResponsiveImageCandidate::IsComputedFromWidth() const
|
|
{
|
|
if (mType == eCandidateType_ComputedFromWidth) {
|
|
return true;
|
|
}
|
|
|
|
MOZ_ASSERT(mType == eCandidateType_Default || mType == eCandidateType_Density,
|
|
"Unknown candidate type");
|
|
return false;
|
|
}
|
|
|
|
} // namespace dom
|
|
} // namespace mozilla
|