2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2011-10-11 09:50:08 +04:00
|
|
|
|
2012-12-31 21:25:46 +04:00
|
|
|
#include "mozilla/dom/HTMLImageElement.h"
|
2020-05-05 00:18:41 +03:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2012-12-31 22:48:55 +04:00
|
|
|
#include "mozilla/dom/HTMLImageElementBinding.h"
|
2019-05-31 17:33:33 +03:00
|
|
|
#include "mozilla/dom/BindContext.h"
|
2020-05-05 00:18:41 +03:00
|
|
|
#include "mozilla/dom/NameSpaceConstants.h"
|
|
|
|
#include "nsGenericHTMLElement.h"
|
2007-01-30 03:06:41 +03:00
|
|
|
#include "nsGkAtoms.h"
|
1998-09-03 05:03:33 +04:00
|
|
|
#include "nsStyleConsts.h"
|
2004-08-01 03:15:21 +04:00
|
|
|
#include "nsPresContext.h"
|
2004-01-26 22:22:05 +03:00
|
|
|
#include "nsMappedAttributes.h"
|
1999-05-07 04:08:21 +04:00
|
|
|
#include "nsSize.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
Bug 1149357: Properly update responsive images for density changes. r=dholbert
Before that we were not notifying the image frame in any way if we ended up not
doing a load, and we were instead relying on the reflow the viewport resize
caused to get the new density in ComputeSize from the content node (but nowhere
else, since that's the bug part 1 fixes).
This was generally unsound, since you can stash random media in a sizes=
attribute, which don't necessarily needs to cause a reflow.
Now we need to notify necessarily because nsImageFrame stores the adjusted
intrinsic size.
mCurrentDensity could also get out of sync as well, when the selected image
density changed, but we ended up returning early because our source hadn't
change in the first early exit.
This patch moves us to a model where we don't re-trigger loads for density
changes if the source doesn't change (unless we pass aAlwaysLoad when we need
to, per spec).
This matches our previous behavior (without the bugginess of not updating the
intrinsic size), and also Chromium, at least.
This changes behavior in one case, which is when we don't load the same source
node, but we have the same source URL, and the density does change. This could
happen with <picture> and two <source>s with same source and different media and
sizes. This makes our behavior consistent with the behavior we have when both
the source and the density doesn't change.
Blink and WebKit do trigger a second image load both when the source changes
without changing density and when density changes. I'll file a spec issue, since
per:
https://html.spec.whatwg.org/#reacting-to-environment-changes
We should be triggering the load when the density changes but the source
doesn't as well, but no UA does that.
I filed https://github.com/whatwg/html/issues/3709 with a little summary of the
situation and what I think the behavior should be (which is what this patch
implements). That being said, I'll update the impl if the spec people think
otherwise :).
MozReview-Commit-ID: Eqy16ygHRLo
2018-05-23 12:19:49 +03:00
|
|
|
#include "nsImageFrame.h"
|
1999-05-07 04:08:21 +04:00
|
|
|
#include "nsIScriptContext.h"
|
2001-02-19 15:55:42 +03:00
|
|
|
#include "nsContentUtils.h"
|
2014-05-25 02:20:39 +04:00
|
|
|
#include "nsContainerFrame.h"
|
2000-05-10 17:13:39 +04:00
|
|
|
#include "nsNodeInfoManager.h"
|
2013-09-25 15:21:18 +04:00
|
|
|
#include "mozilla/MouseEvents.h"
|
2001-05-22 02:40:10 +04:00
|
|
|
#include "nsContentPolicyUtils.h"
|
2010-08-09 20:15:47 +04:00
|
|
|
#include "nsFocusManager.h"
|
2020-02-13 00:31:48 +03:00
|
|
|
#include "mozilla/dom/DOMIntersectionObserver.h"
|
2013-06-19 18:24:37 +04:00
|
|
|
#include "mozilla/dom/HTMLFormElement.h"
|
2018-02-09 19:17:10 +03:00
|
|
|
#include "mozilla/dom/MutationEventBinding.h"
|
2019-09-20 23:51:25 +03:00
|
|
|
#include "mozilla/dom/UserActivation.h"
|
2014-07-02 00:36:06 +04:00
|
|
|
#include "nsAttrValueOrString.h"
|
2014-10-09 00:53:24 +04:00
|
|
|
#include "imgLoader.h"
|
2015-03-17 22:25:35 +03:00
|
|
|
#include "Image.h"
|
1999-12-18 23:29:29 +03:00
|
|
|
|
2014-05-03 01:32:20 +04:00
|
|
|
// Responsive images!
|
|
|
|
#include "mozilla/dom/HTMLSourceElement.h"
|
|
|
|
#include "mozilla/dom/ResponsiveImageSelector.h"
|
|
|
|
|
2012-10-12 20:11:22 +04:00
|
|
|
#include "imgINotificationObserver.h"
|
2012-10-12 16:43:01 +04:00
|
|
|
#include "imgRequestProxy.h"
|
2001-03-22 04:42:01 +03:00
|
|
|
|
2019-07-22 20:35:20 +03:00
|
|
|
#include "mozilla/CycleCollectedJSContext.h"
|
1998-09-03 05:03:33 +04:00
|
|
|
|
2014-03-18 08:48:21 +04:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2014-04-03 08:18:36 +04:00
|
|
|
#include "mozilla/EventStates.h"
|
2018-06-22 19:48:42 +03:00
|
|
|
#include "mozilla/MappedDeclarations.h"
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 20:46:42 +04:00
|
|
|
|
2007-02-18 13:43:12 +03:00
|
|
|
#include "nsLayoutUtils.h"
|
|
|
|
|
2016-05-11 04:38:05 +03:00
|
|
|
using namespace mozilla::net;
|
|
|
|
|
2013-06-19 18:24:37 +04:00
|
|
|
NS_IMPL_NS_NEW_HTML_ELEMENT(Image)
|
1998-09-03 05:03:33 +04:00
|
|
|
|
2014-10-02 07:59:56 +04:00
|
|
|
#ifdef DEBUG
|
2014-05-03 01:32:20 +04:00
|
|
|
// Is aSubject a previous sibling of aNode.
|
|
|
|
static bool IsPreviousSibling(nsINode* aSubject, nsINode* aNode) {
|
|
|
|
if (aSubject == aNode) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsINode* parent = aSubject->GetParentNode();
|
|
|
|
if (parent && parent == aNode->GetParentNode()) {
|
2018-01-23 16:30:18 +03:00
|
|
|
return parent->ComputeIndexOf(aSubject) < parent->ComputeIndexOf(aNode);
|
2014-05-03 01:32:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-02 07:59:56 +04:00
|
|
|
#endif
|
2014-05-03 01:32:20 +04:00
|
|
|
|
2012-12-31 21:25:46 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-10-02 07:30:49 +04:00
|
|
|
// Calls LoadSelectedImage on host element unless it has been superseded or
|
|
|
|
// canceled -- this is the synchronous section of "update the image data".
|
|
|
|
// https://html.spec.whatwg.org/multipage/embedded-content.html#update-the-image-data
|
2019-11-29 23:04:18 +03:00
|
|
|
class ImageLoadTask final : public MicroTaskRunnable {
|
2014-10-02 07:30:49 +04:00
|
|
|
public:
|
2017-06-12 22:34:10 +03:00
|
|
|
ImageLoadTask(HTMLImageElement* aElement, bool aAlwaysLoad,
|
2017-04-25 04:17:38 +03:00
|
|
|
bool aUseUrgentStartForChannel)
|
2019-07-22 20:35:20 +03:00
|
|
|
: MicroTaskRunnable(),
|
2017-06-12 22:34:10 +03:00
|
|
|
mElement(aElement),
|
2015-08-31 22:29:40 +03:00
|
|
|
mAlwaysLoad(aAlwaysLoad),
|
2017-04-25 04:17:38 +03:00
|
|
|
mUseUrgentStartForChannel(aUseUrgentStartForChannel) {
|
2015-06-06 16:21:56 +03:00
|
|
|
mDocument = aElement->OwnerDoc();
|
|
|
|
mDocument->BlockOnload();
|
|
|
|
}
|
2014-10-02 07:30:49 +04:00
|
|
|
|
2019-11-29 23:04:18 +03:00
|
|
|
void Run(AutoSlowOperation& aAso) override {
|
2014-10-02 07:30:49 +04:00
|
|
|
if (mElement->mPendingImageLoadTask == this) {
|
|
|
|
mElement->mPendingImageLoadTask = nullptr;
|
2017-04-25 04:17:38 +03:00
|
|
|
mElement->mUseUrgentStartForChannel = mUseUrgentStartForChannel;
|
2020-04-11 02:21:03 +03:00
|
|
|
// Defer loading this image if loading="lazy" was set after this microtask
|
|
|
|
// was queued.
|
|
|
|
// NOTE: Using ShouldLoadImage() will violate the HTML standard spec
|
|
|
|
// because ShouldLoadImage() checks the document active state which should
|
|
|
|
// have done just once before this queue is created as per the spec, so
|
|
|
|
// we just check the lazy loading state here.
|
|
|
|
if (!mElement->IsLazyLoading()) {
|
|
|
|
mElement->LoadSelectedImage(true, true, mAlwaysLoad);
|
|
|
|
}
|
2014-10-02 07:30:49 +04:00
|
|
|
}
|
2015-06-06 16:21:56 +03:00
|
|
|
mDocument->UnblockOnload(false);
|
2019-07-22 20:35:20 +03:00
|
|
|
}
|
|
|
|
|
2019-11-29 23:04:18 +03:00
|
|
|
bool Suppressed() override {
|
2019-07-22 20:35:20 +03:00
|
|
|
nsIGlobalObject* global = mElement->GetOwnerGlobal();
|
|
|
|
return global && global->IsInSyncOperation();
|
2014-10-02 07:30:49 +04:00
|
|
|
}
|
|
|
|
|
2016-03-30 19:39:59 +03:00
|
|
|
bool AlwaysLoad() { return mAlwaysLoad; }
|
|
|
|
|
2014-10-02 07:30:49 +04:00
|
|
|
private:
|
2020-02-20 19:19:15 +03:00
|
|
|
~ImageLoadTask() = default;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<HTMLImageElement> mElement;
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> mDocument;
|
2015-08-31 22:29:40 +03:00
|
|
|
bool mAlwaysLoad;
|
2017-04-25 04:17:38 +03:00
|
|
|
|
|
|
|
// True if we want to set nsIClassOfService::UrgentStart to the channel to
|
|
|
|
// get the response ASAP for better user responsiveness.
|
|
|
|
bool mUseUrgentStartForChannel;
|
2014-10-02 07:30:49 +04:00
|
|
|
};
|
|
|
|
|
2018-09-21 23:45:49 +03:00
|
|
|
HTMLImageElement::HTMLImageElement(
|
|
|
|
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
|
|
|
|
: nsGenericHTMLElement(std::move(aNodeInfo)),
|
2013-06-18 16:53:23 +04:00
|
|
|
mForm(nullptr),
|
2015-08-31 22:29:40 +03:00
|
|
|
mInDocResponsiveContent(false),
|
2016-09-26 21:17:38 +03:00
|
|
|
mCurrentDensity(1.0) {
|
2011-06-01 05:46:57 +04:00
|
|
|
// We start out broken
|
|
|
|
AddStatesSilently(NS_EVENT_STATE_BROKEN);
|
1998-09-03 05:03:33 +04:00
|
|
|
}
|
|
|
|
|
2012-12-31 21:25:46 +04:00
|
|
|
HTMLImageElement::~HTMLImageElement() { DestroyImageLoadingContent(); }
|
1998-09-03 05:03:33 +04:00
|
|
|
|
2014-10-14 00:36:01 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(HTMLImageElement, nsGenericHTMLElement,
|
|
|
|
mResponsiveSelector)
|
2010-01-12 16:08:43 +03:00
|
|
|
|
2017-09-01 02:29:22 +03:00
|
|
|
NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED(HTMLImageElement,
|
|
|
|
nsGenericHTMLElement,
|
|
|
|
nsIImageLoadingContent,
|
|
|
|
imgINotificationObserver)
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 20:46:42 +04:00
|
|
|
|
2012-12-31 21:25:46 +04:00
|
|
|
NS_IMPL_ELEMENT_CLONE(HTMLImageElement)
|
1998-09-03 05:03:33 +04:00
|
|
|
|
2020-04-28 14:22:32 +03:00
|
|
|
bool HTMLImageElement::IsInteractiveHTMLContent() const {
|
2015-03-23 12:02:33 +03:00
|
|
|
return HasAttr(kNameSpaceID_None, nsGkAtoms::usemap) ||
|
2020-04-28 14:22:32 +03:00
|
|
|
nsGenericHTMLElement::IsInteractiveHTMLContent();
|
2015-01-20 23:39:28 +03:00
|
|
|
}
|
|
|
|
|
2016-10-06 07:30:35 +03:00
|
|
|
void HTMLImageElement::AsyncEventRunning(AsyncEventDispatcher* aEvent) {
|
|
|
|
nsImageLoadingContent::AsyncEventRunning(aEvent);
|
|
|
|
}
|
|
|
|
|
2014-05-14 01:21:15 +04:00
|
|
|
void HTMLImageElement::GetCurrentSrc(nsAString& aValue) {
|
|
|
|
nsCOMPtr<nsIURI> currentURI;
|
|
|
|
GetCurrentURI(getter_AddRefs(currentURI));
|
|
|
|
if (currentURI) {
|
|
|
|
nsAutoCString spec;
|
|
|
|
currentURI->GetSpec(spec);
|
|
|
|
CopyUTF8toUTF16(spec, aValue);
|
|
|
|
} else {
|
|
|
|
SetDOMStringToNull(aValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-31 21:25:46 +04:00
|
|
|
bool HTMLImageElement::Draggable() const {
|
2008-08-27 16:07:27 +04:00
|
|
|
// images may be dragged unless the draggable attribute is false
|
2012-10-06 11:19:52 +04:00
|
|
|
return !AttrValueIs(kNameSpaceID_None, nsGkAtoms::draggable,
|
|
|
|
nsGkAtoms::_false, eIgnoreCase);
|
2008-08-27 16:07:27 +04:00
|
|
|
}
|
|
|
|
|
2012-12-31 22:48:55 +04:00
|
|
|
bool HTMLImageElement::Complete() {
|
2019-09-11 17:37:39 +03:00
|
|
|
// It is still not clear what value should img.complete return in various
|
|
|
|
// cases, see https://github.com/whatwg/html/issues/4884
|
|
|
|
|
2019-11-01 18:24:25 +03:00
|
|
|
if (!HasAttr(nsGkAtoms::srcset) && !HasNonEmptyAttr(nsGkAtoms::src)) {
|
|
|
|
return true;
|
2000-01-07 02:41:14 +03:00
|
|
|
}
|
|
|
|
|
2019-09-11 17:37:39 +03:00
|
|
|
if (!mCurrentRequest || mPendingRequest) {
|
2015-02-05 05:05:17 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t status;
|
2003-03-19 06:47:09 +03:00
|
|
|
mCurrentRequest->GetImageStatus(&status);
|
2003-05-03 06:32:43 +04:00
|
|
|
return (status &
|
|
|
|
(imgIRequest::STATUS_LOAD_COMPLETE | imgIRequest::STATUS_ERROR)) != 0;
|
2012-12-31 22:48:55 +04:00
|
|
|
}
|
|
|
|
|
2012-12-31 21:25:46 +04:00
|
|
|
CSSIntPoint HTMLImageElement::GetXY() {
|
2017-01-05 10:31:56 +03:00
|
|
|
nsIFrame* frame = GetPrimaryFrame(FlushType::Layout);
|
2012-04-24 23:04:37 +04:00
|
|
|
if (!frame) {
|
2013-06-12 11:00:08 +04:00
|
|
|
return CSSIntPoint(0, 0);
|
2012-04-24 23:04:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIFrame* layer = nsLayoutUtils::GetClosestLayer(frame->GetParent());
|
2013-06-12 11:00:08 +04:00
|
|
|
return CSSIntPoint::FromAppUnitsRounded(frame->GetOffsetTo(layer));
|
2012-04-24 23:04:37 +04:00
|
|
|
}
|
|
|
|
|
2013-06-02 19:38:25 +04:00
|
|
|
int32_t HTMLImageElement::X() { return GetXY().x; }
|
|
|
|
|
|
|
|
int32_t HTMLImageElement::Y() { return GetXY().y; }
|
|
|
|
|
2018-08-08 14:56:01 +03:00
|
|
|
void HTMLImageElement::GetDecoding(nsAString& aValue) {
|
|
|
|
GetEnumAttr(nsGkAtoms::decoding, kDecodingTableDefault->tag, aValue);
|
|
|
|
}
|
|
|
|
|
2020-01-14 00:10:43 +03:00
|
|
|
// https://whatpr.org/html/3752/urls-and-fetching.html#lazy-loading-attributes
|
|
|
|
static const nsAttrValue::EnumTable kLoadingTable[] = {
|
|
|
|
{"eager", HTMLImageElement::Loading::Eager},
|
|
|
|
{"lazy", HTMLImageElement::Loading::Lazy},
|
|
|
|
{nullptr, 0}};
|
|
|
|
|
|
|
|
void HTMLImageElement::GetLoading(nsAString& aValue) const {
|
|
|
|
GetEnumAttr(nsGkAtoms::loading, kLoadingTable[0].tag, aValue);
|
|
|
|
}
|
|
|
|
|
2020-02-13 00:29:32 +03:00
|
|
|
HTMLImageElement::Loading HTMLImageElement::LoadingState() const {
|
|
|
|
const nsAttrValue* val = mAttrs.GetAttr(nsGkAtoms::loading);
|
|
|
|
if (!val) {
|
|
|
|
return HTMLImageElement::Loading::Eager;
|
|
|
|
}
|
|
|
|
return static_cast<HTMLImageElement::Loading>(val->GetEnumValue());
|
|
|
|
}
|
|
|
|
|
2018-11-08 20:45:50 +03:00
|
|
|
already_AddRefed<Promise> HTMLImageElement::Decode(ErrorResult& aRv) {
|
|
|
|
return nsImageLoadingContent::QueueDecodeAsync(aRv);
|
|
|
|
}
|
|
|
|
|
2012-12-31 21:25:46 +04:00
|
|
|
bool HTMLImageElement::ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
|
|
|
|
const nsAString& aValue,
|
2017-11-02 06:35:52 +03:00
|
|
|
nsIPrincipal* aMaybeScriptedPrincipal,
|
2012-12-31 21:25:46 +04:00
|
|
|
nsAttrValue& aResult) {
|
2005-11-29 19:37:15 +03:00
|
|
|
if (aNamespaceID == kNameSpaceID_None) {
|
2006-12-26 20:47:52 +03:00
|
|
|
if (aAttribute == nsGkAtoms::align) {
|
2005-11-29 19:37:15 +03:00
|
|
|
return ParseAlignValue(aValue, aResult);
|
|
|
|
}
|
2011-07-17 21:40:22 +04:00
|
|
|
if (aAttribute == nsGkAtoms::crossorigin) {
|
2012-03-10 20:13:51 +04:00
|
|
|
ParseCORSValue(aValue, aResult);
|
|
|
|
return true;
|
2011-07-14 22:47:35 +04:00
|
|
|
}
|
2018-08-08 14:56:01 +03:00
|
|
|
if (aAttribute == nsGkAtoms::decoding) {
|
2020-01-14 00:10:43 +03:00
|
|
|
return aResult.ParseEnumValue(aValue, kDecodingTable,
|
|
|
|
/* aCaseSensitive = */ false,
|
2018-08-08 14:56:01 +03:00
|
|
|
kDecodingTableDefault);
|
|
|
|
}
|
2020-01-14 00:10:43 +03:00
|
|
|
if (aAttribute == nsGkAtoms::loading) {
|
|
|
|
return aResult.ParseEnumValue(aValue, kLoadingTable,
|
|
|
|
/* aCaseSensitive = */ false,
|
|
|
|
kLoadingTable);
|
|
|
|
}
|
2005-11-29 19:37:15 +03:00
|
|
|
if (ParseImageAttribute(aAttribute, aValue, aResult)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2005-11-29 19:37:15 +03:00
|
|
|
}
|
1998-09-03 05:03:33 +04:00
|
|
|
}
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2005-11-29 19:37:15 +03:00
|
|
|
return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
|
2017-11-02 06:35:52 +03:00
|
|
|
aMaybeScriptedPrincipal, aResult);
|
1998-09-03 05:03:33 +04:00
|
|
|
}
|
|
|
|
|
2013-11-19 23:21:29 +04:00
|
|
|
void HTMLImageElement::MapAttributesIntoRule(
|
|
|
|
const nsMappedAttributes* aAttributes, MappedDeclarations& aDecls) {
|
2019-09-26 01:33:47 +03:00
|
|
|
MapImageAlignAttributeInto(aAttributes, aDecls);
|
|
|
|
MapImageBorderAttributeInto(aAttributes, aDecls);
|
|
|
|
MapImageMarginAttributeInto(aAttributes, aDecls);
|
|
|
|
MapImageSizeAttributesInto(aAttributes, aDecls, MapAspectRatio::Yes);
|
|
|
|
MapCommonAttributesInto(aAttributes, aDecls);
|
1998-09-05 08:00:06 +04:00
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
nsChangeHint HTMLImageElement::GetAttributeChangeHint(const nsAtom* aAttribute,
|
2012-12-31 21:25:46 +04:00
|
|
|
int32_t aModType) const {
|
2004-10-24 21:10:32 +04:00
|
|
|
nsChangeHint retval =
|
|
|
|
nsGenericHTMLElement::GetAttributeChangeHint(aAttribute, aModType);
|
2006-12-26 20:47:52 +03:00
|
|
|
if (aAttribute == nsGkAtoms::usemap || aAttribute == nsGkAtoms::ismap) {
|
2016-07-06 08:06:14 +03:00
|
|
|
retval |= nsChangeHint_ReconstructFrame;
|
2013-08-21 16:44:14 +04:00
|
|
|
} else if (aAttribute == nsGkAtoms::alt) {
|
2018-06-26 00:20:54 +03:00
|
|
|
if (aModType == MutationEvent_Binding::ADDITION ||
|
|
|
|
aModType == MutationEvent_Binding::REMOVAL) {
|
2016-07-06 08:06:14 +03:00
|
|
|
retval |= nsChangeHint_ReconstructFrame;
|
2013-08-21 16:44:14 +04:00
|
|
|
}
|
2003-07-12 01:16:12 +04:00
|
|
|
}
|
2004-10-24 21:10:32 +04:00
|
|
|
return retval;
|
2003-07-12 01:16:12 +04:00
|
|
|
}
|
2003-04-17 00:54:20 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP_(bool)
|
2017-10-03 01:05:19 +03:00
|
|
|
HTMLImageElement::IsAttributeMapped(const nsAtom* aAttribute) const {
|
2004-02-26 00:04:50 +03:00
|
|
|
static const MappedAttributeEntry* const map[] = {
|
2003-07-12 01:16:12 +04:00
|
|
|
sCommonAttributeMap, sImageMarginSizeAttributeMap,
|
|
|
|
sImageBorderAttributeMap, sImageAlignAttributeMap};
|
2003-04-17 00:54:20 +04:00
|
|
|
|
2011-12-18 14:09:27 +04:00
|
|
|
return FindAttributeDependence(aAttribute, map);
|
1999-07-07 05:24:40 +04:00
|
|
|
}
|
|
|
|
|
2012-12-31 21:25:46 +04:00
|
|
|
nsMapRuleToAttributesFunc HTMLImageElement::GetAttributeMappingFunction()
|
|
|
|
const {
|
2005-01-12 22:45:38 +03:00
|
|
|
return &MapAttributesIntoRule;
|
1998-09-03 05:03:33 +04:00
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
nsresult HTMLImageElement::BeforeSetAttr(int32_t aNameSpaceID, nsAtom* aName,
|
2017-03-16 21:50:41 +03:00
|
|
|
const nsAttrValueOrString* aValue,
|
2013-06-18 16:53:23 +04:00
|
|
|
bool aNotify) {
|
|
|
|
if (aNameSpaceID == kNameSpaceID_None && mForm &&
|
|
|
|
(aName == nsGkAtoms::name || aName == nsGkAtoms::id)) {
|
|
|
|
// remove the image from the hashtable as needed
|
|
|
|
nsAutoString tmp;
|
|
|
|
GetAttr(kNameSpaceID_None, aName, tmp);
|
|
|
|
|
|
|
|
if (!tmp.IsEmpty()) {
|
2016-11-21 20:34:02 +03:00
|
|
|
mForm->RemoveImageElementFromTable(this, tmp);
|
2013-06-18 16:53:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsGenericHTMLElement::BeforeSetAttr(aNameSpaceID, aName, aValue,
|
|
|
|
aNotify);
|
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
nsresult HTMLImageElement::AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
|
2017-05-19 00:09:01 +03:00
|
|
|
const nsAttrValue* aValue,
|
2017-10-10 00:33:38 +03:00
|
|
|
const nsAttrValue* aOldValue,
|
|
|
|
nsIPrincipal* aMaybeScriptedPrincipal,
|
|
|
|
bool aNotify) {
|
2020-05-05 00:18:41 +03:00
|
|
|
if (aNameSpaceID != kNameSpaceID_None) {
|
|
|
|
return nsGenericHTMLElement::AfterSetAttr(aNameSpaceID, aName, aValue,
|
|
|
|
aOldValue,
|
|
|
|
aMaybeScriptedPrincipal, aNotify);
|
2020-02-13 00:31:48 +03:00
|
|
|
}
|
|
|
|
|
2020-05-05 00:18:41 +03:00
|
|
|
nsAttrValueOrString attrVal(aValue);
|
|
|
|
|
2017-06-02 01:38:45 +03:00
|
|
|
if (aValue) {
|
2017-10-10 00:33:38 +03:00
|
|
|
AfterMaybeChangeAttr(aNameSpaceID, aName, attrVal, aOldValue,
|
2020-05-05 00:18:41 +03:00
|
|
|
aMaybeScriptedPrincipal, aNotify);
|
2017-06-02 01:38:45 +03:00
|
|
|
}
|
|
|
|
|
2020-05-05 00:18:41 +03:00
|
|
|
if (mForm && (aName == nsGkAtoms::name || aName == nsGkAtoms::id) && aValue &&
|
2013-06-18 16:53:23 +04:00
|
|
|
!aValue->IsEmptyString()) {
|
|
|
|
// add the image to the hashtable as needed
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(aValue->Type() == nsAttrValue::eAtom,
|
|
|
|
"Expected atom value for name/id");
|
2013-06-18 16:53:23 +04:00
|
|
|
mForm->AddImageElementToTable(
|
|
|
|
this, nsDependentAtomString(aValue->GetAtomValue()));
|
|
|
|
}
|
|
|
|
|
2020-05-05 00:18:41 +03:00
|
|
|
bool forceReload = false;
|
2014-05-03 01:32:20 +04:00
|
|
|
|
2020-05-05 00:18:41 +03:00
|
|
|
if (aName == nsGkAtoms::loading) {
|
|
|
|
if (aValue &&
|
|
|
|
static_cast<HTMLImageElement::Loading>(aValue->GetEnumValue()) ==
|
|
|
|
Loading::Lazy &&
|
|
|
|
!ImageState().HasState(NS_EVENT_STATE_LOADING)) {
|
|
|
|
SetLazyLoading();
|
|
|
|
} else if (aOldValue &&
|
|
|
|
static_cast<HTMLImageElement::Loading>(
|
|
|
|
aOldValue->GetEnumValue()) == Loading::Lazy &&
|
|
|
|
!ImageState().HasState(NS_EVENT_STATE_LOADING)) {
|
|
|
|
StopLazyLoadingAndStartLoadIfNeeded();
|
|
|
|
}
|
|
|
|
} else if (aName == nsGkAtoms::src && !aValue) {
|
|
|
|
// NOTE: regular src value changes are handled in AfterMaybeChangeAttr, so
|
|
|
|
// this only needs to handle unsetting the src attribute.
|
2017-04-25 04:17:38 +03:00
|
|
|
// Mark channel as urgent-start before load image if the image load is
|
|
|
|
// initaiated by a user interaction.
|
2019-09-20 23:51:25 +03:00
|
|
|
mUseUrgentStartForChannel = UserActivation::IsHandlingUserInput();
|
2017-04-25 04:17:38 +03:00
|
|
|
|
2020-05-05 00:18:41 +03:00
|
|
|
// AfterMaybeChangeAttr handles setting src since it needs to catch
|
|
|
|
// img.src = img.src, so we only need to handle the unset case
|
2014-10-02 07:59:56 +04:00
|
|
|
if (InResponsiveMode()) {
|
2015-03-11 00:38:30 +03:00
|
|
|
if (mResponsiveSelector && mResponsiveSelector->Content() == this) {
|
2017-09-22 07:35:46 +03:00
|
|
|
mResponsiveSelector->SetDefaultSource(VoidString());
|
2014-10-02 07:30:49 +04:00
|
|
|
}
|
2015-08-31 22:29:40 +03:00
|
|
|
QueueImageLoadTask(true);
|
2014-10-02 07:30:49 +04:00
|
|
|
} else {
|
|
|
|
// Bug 1076583 - We still behave synchronously in the non-responsive case
|
2014-05-03 03:26:22 +04:00
|
|
|
CancelImageRequests(aNotify);
|
|
|
|
}
|
2020-05-05 00:18:41 +03:00
|
|
|
} else if (aName == nsGkAtoms::srcset) {
|
2017-04-25 04:17:38 +03:00
|
|
|
// Mark channel as urgent-start before load image if the image load is
|
|
|
|
// initaiated by a user interaction.
|
2019-09-20 23:51:25 +03:00
|
|
|
mUseUrgentStartForChannel = UserActivation::IsHandlingUserInput();
|
2017-04-25 04:17:38 +03:00
|
|
|
|
2017-10-03 07:30:34 +03:00
|
|
|
mSrcsetTriggeringPrincipal = aMaybeScriptedPrincipal;
|
|
|
|
|
2014-10-02 07:30:49 +04:00
|
|
|
PictureSourceSrcsetChanged(this, attrVal.String(), aNotify);
|
2020-05-05 00:18:41 +03:00
|
|
|
} else if (aName == nsGkAtoms::sizes) {
|
2017-04-25 04:17:38 +03:00
|
|
|
// Mark channel as urgent-start before load image if the image load is
|
2020-05-05 00:18:41 +03:00
|
|
|
// initiated by a user interaction.
|
2019-09-20 23:51:25 +03:00
|
|
|
mUseUrgentStartForChannel = UserActivation::IsHandlingUserInput();
|
2017-04-25 04:17:38 +03:00
|
|
|
|
2014-10-02 07:30:49 +04:00
|
|
|
PictureSourceSizesChanged(this, attrVal.String(), aNotify);
|
2020-05-05 00:18:41 +03:00
|
|
|
} else if (aName == nsGkAtoms::decoding) {
|
2018-08-08 14:56:01 +03:00
|
|
|
// Request sync or async image decoding.
|
|
|
|
SetSyncDecodingHint(
|
|
|
|
aValue && static_cast<ImageDecodingType>(aValue->GetEnumValue()) ==
|
|
|
|
ImageDecodingType::Sync);
|
2020-05-05 00:18:41 +03:00
|
|
|
} else if (aName == nsGkAtoms::referrerpolicy) {
|
2020-05-05 00:26:38 +03:00
|
|
|
ReferrerPolicy referrerPolicy = GetReferrerPolicyAsEnum();
|
2020-05-05 00:18:41 +03:00
|
|
|
// FIXME(emilio): Why only when not in responsive mode? Also see below for
|
|
|
|
// aNotify.
|
|
|
|
forceReload = aNotify && !InResponsiveMode() &&
|
|
|
|
referrerPolicy != ReferrerPolicy::_empty &&
|
|
|
|
referrerPolicy != ReferrerPolicyFromAttr(aOldValue);
|
|
|
|
} else if (aName == nsGkAtoms::crossorigin) {
|
|
|
|
// FIXME(emilio): The aNotify bit seems a bit suspicious, but it is useful
|
|
|
|
// to avoid extra sync loads, specially in non-responsive mode. Ideally we
|
|
|
|
// can unify the responsive and non-responsive code paths (bug 1076583), and
|
|
|
|
// simplify this a bit.
|
|
|
|
forceReload = aNotify && GetCORSMode() != AttrValueToCORSMode(aOldValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (forceReload) {
|
|
|
|
// Because we load image synchronously in non-responsive-mode, we need to do
|
|
|
|
// reload after the attribute has been set if the reload is triggered by
|
|
|
|
// cross origin / referrer policy changing.
|
|
|
|
//
|
|
|
|
// Mark channel as urgent-start before load image if the image load is
|
|
|
|
// initiated by a user interaction.
|
|
|
|
mUseUrgentStartForChannel = UserActivation::IsHandlingUserInput();
|
|
|
|
if (InResponsiveMode()) {
|
|
|
|
// Per spec, full selection runs when this changes, even though
|
|
|
|
// it doesn't directly affect the source selection
|
|
|
|
QueueImageLoadTask(true);
|
|
|
|
} else if (ShouldLoadImage()) {
|
|
|
|
// Bug 1076583 - We still use the older synchronous algorithm in
|
|
|
|
// non-responsive mode. Force a new load of the image with the
|
|
|
|
// new cross origin policy
|
|
|
|
ForceReload(aNotify, IgnoreErrors());
|
|
|
|
}
|
2013-10-10 18:54:02 +04:00
|
|
|
}
|
|
|
|
|
2013-06-18 16:53:23 +04:00
|
|
|
return nsGenericHTMLElement::AfterSetAttr(
|
2017-10-10 00:33:38 +03:00
|
|
|
aNameSpaceID, aName, aValue, aOldValue, aMaybeScriptedPrincipal, aNotify);
|
2013-06-18 16:53:23 +04:00
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
nsresult HTMLImageElement::OnAttrSetButNotChanged(
|
2017-06-02 01:38:45 +03:00
|
|
|
int32_t aNamespaceID, nsAtom* aName, const nsAttrValueOrString& aValue,
|
|
|
|
bool aNotify) {
|
2020-05-05 00:18:41 +03:00
|
|
|
AfterMaybeChangeAttr(aNamespaceID, aName, aValue, nullptr, nullptr, aNotify);
|
2017-06-02 01:38:45 +03:00
|
|
|
return nsGenericHTMLElement::OnAttrSetButNotChanged(aNamespaceID, aName,
|
|
|
|
aValue, aNotify);
|
2004-07-25 01:12:43 +04:00
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
void HTMLImageElement::AfterMaybeChangeAttr(
|
2017-06-09 19:46:54 +03:00
|
|
|
int32_t aNamespaceID, nsAtom* aName, const nsAttrValueOrString& aValue,
|
2017-10-10 00:33:38 +03:00
|
|
|
const nsAttrValue* aOldValue, nsIPrincipal* aMaybeScriptedPrincipal,
|
2020-05-05 00:18:41 +03:00
|
|
|
bool aNotify) {
|
|
|
|
if (aNamespaceID != kNameSpaceID_None || aName != nsGkAtoms::src) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-11 01:38:02 +04:00
|
|
|
// We need to force our image to reload. This must be done here, not in
|
|
|
|
// AfterSetAttr or BeforeSetAttr, because we want to do it even if the attr is
|
|
|
|
// being set to its existing value, which is normally optimized away as a
|
|
|
|
// no-op.
|
|
|
|
//
|
2014-10-02 07:30:49 +04:00
|
|
|
// If we are in responsive mode, we drop the forced reload behavior,
|
|
|
|
// but still trigger a image load task for img.src = img.src per
|
|
|
|
// spec.
|
2014-05-03 03:26:22 +04:00
|
|
|
//
|
2014-10-02 07:30:49 +04:00
|
|
|
// Both cases handle unsetting src in AfterSetAttr
|
2020-05-05 00:18:41 +03:00
|
|
|
// Mark channel as urgent-start before load image if the image load is
|
|
|
|
// initaiated by a user interaction.
|
|
|
|
mUseUrgentStartForChannel = UserActivation::IsHandlingUserInput();
|
2017-04-25 04:17:38 +03:00
|
|
|
|
2020-05-05 00:18:41 +03:00
|
|
|
mSrcTriggeringPrincipal = nsContentUtils::GetAttrTriggeringPrincipal(
|
|
|
|
this, aValue.String(), aMaybeScriptedPrincipal);
|
2017-10-03 06:28:32 +03:00
|
|
|
|
2020-05-05 00:18:41 +03:00
|
|
|
if (InResponsiveMode()) {
|
|
|
|
if (mResponsiveSelector && mResponsiveSelector->Content() == this) {
|
|
|
|
mResponsiveSelector->SetDefaultSource(aValue.String(),
|
|
|
|
mSrcTriggeringPrincipal);
|
2016-04-15 01:53:44 +03:00
|
|
|
}
|
2020-05-05 00:18:41 +03:00
|
|
|
QueueImageLoadTask(true);
|
|
|
|
} else if (aNotify && ShouldLoadImage()) {
|
|
|
|
// If aNotify is false, we are coming from the parser or some such place;
|
|
|
|
// we'll get bound after all the attributes have been set, so we'll do the
|
|
|
|
// sync image load from BindToTree. Skip the LoadImage call in that case.
|
|
|
|
|
|
|
|
// Note that this sync behavior is partially removed from the spec, bug
|
|
|
|
// 1076583
|
|
|
|
|
|
|
|
// A hack to get animations to reset. See bug 594771.
|
|
|
|
mNewRequestsWillNeedAnimationReset = true;
|
|
|
|
|
|
|
|
// Force image loading here, so that we'll try to load the image from
|
|
|
|
// network if it's set to be not cacheable.
|
|
|
|
// Potentially, false could be passed here rather than aNotify since
|
|
|
|
// UpdateState will be called by SetAttrAndNotify, but there are two
|
|
|
|
// obstacles to this: 1) LoadImage will end up calling
|
|
|
|
// UpdateState(aNotify), and we do not want it to call UpdateState(false)
|
|
|
|
// when aNotify is true, and 2) When this function is called by
|
|
|
|
// OnAttrSetButNotChanged, SetAttrAndNotify will not subsequently call
|
|
|
|
// UpdateState.
|
|
|
|
LoadImage(aValue.String(), true, aNotify, eImageLoadType_Normal,
|
|
|
|
mSrcTriggeringPrincipal);
|
|
|
|
|
|
|
|
mNewRequestsWillNeedAnimationReset = false;
|
2014-03-11 01:38:02 +04:00
|
|
|
}
|
2017-06-02 01:38:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void HTMLImageElement::GetEventTargetParent(EventChainPreVisitor& aVisitor) {
|
|
|
|
// We handle image element with attribute ismap in its corresponding frame
|
|
|
|
// element. Set mMultipleActionsPrevented here to prevent the click event
|
|
|
|
// trigger the behaviors in Element::PostHandleEventForLinks
|
|
|
|
WidgetMouseEvent* mouseEvent = aVisitor.mEvent->AsMouseEvent();
|
|
|
|
if (mouseEvent && mouseEvent->IsLeftClickEvent() && IsMap()) {
|
|
|
|
mouseEvent->mFlags.mMultipleActionsPrevented = true;
|
|
|
|
}
|
2018-04-05 20:42:41 +03:00
|
|
|
nsGenericHTMLElement::GetEventTargetParent(aVisitor);
|
2017-06-02 01:38:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HTMLImageElement::IsHTMLFocusable(bool aWithMouse, bool* aIsFocusable,
|
|
|
|
int32_t* aTabIndex) {
|
|
|
|
int32_t tabIndex = TabIndex();
|
|
|
|
|
2018-07-04 20:26:09 +03:00
|
|
|
if (IsInComposedDoc() && FindImageMap()) {
|
|
|
|
if (aTabIndex) {
|
|
|
|
// Use tab index on individual map areas
|
|
|
|
*aTabIndex = (sTabFocusModel & eTabFocus_linksMask) ? 0 : -1;
|
2017-06-02 01:38:45 +03:00
|
|
|
}
|
2018-07-04 20:26:09 +03:00
|
|
|
// Image map is not focusable itself, but flag as tabbable
|
|
|
|
// so that image map areas get walked into.
|
|
|
|
*aIsFocusable = false;
|
|
|
|
|
|
|
|
return false;
|
2017-06-02 01:38:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aTabIndex) {
|
|
|
|
// Can be in tab order if tabindex >=0 and form controls are tabbable.
|
|
|
|
*aTabIndex = (sTabFocusModel & eTabFocus_formElementsMask) ? tabIndex : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aIsFocusable =
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
(!aWithMouse || nsFocusManager::sMouseFocusesFormControl) &&
|
|
|
|
#endif
|
2020-04-04 23:03:09 +03:00
|
|
|
(tabIndex >= 0 || GetTabIndexAttrValue().isSome());
|
2017-06-02 01:38:45 +03:00
|
|
|
|
|
|
|
return false;
|
2003-03-19 06:47:09 +03:00
|
|
|
}
|
|
|
|
|
Bug 1555216 - Change the signature of BindToTree to be (BindContext&, nsINode& aParentNode). r=bzbarsky
BindContext was going to have way more information at first, but then I realized
that most of the things I wanted to know were basically a flag away using the
parent node.
Still I think it's worth it, now experimenting with BindToTree will only mean
adding a field to a struct that's included from a couple cpp files, instead of a
massive pain.
I also think this is clearer, and doing this highlights quite a few
inconsistencies in our code which I've left untouched, but commented with
FIXMEs.
Steps are:
$ for file in $(rg 'nsresult BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#nsresult BindToTree(Document\* aDocument, nsIContent\* aParent,#nsresult BindToTree(BindContext\&, nsINode\& aParent)#g' $file; done
$ for file in $(rg 'nsresult BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's# nsIContent\* aBindingParent) override#override#g' $file; done
$ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#::BindToTree(Document\* aDocument, nsIContent\* aParent,#::BindToTree(BindContext\& aContext, nsINode\& aParent)#g' $file; done
$ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#nsIContent\* aBindingParent)##g' $file; done
$ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#::BindToTree(aDocument, aParent, aBindingParent)#::BindToTree(aContext, aParent)#g' $file; done
$ ./mach clang-format
Then manual fixups.
Depends on D32948
Differential Revision: https://phabricator.services.mozilla.com/D32949
2019-05-29 07:27:04 +03:00
|
|
|
nsresult HTMLImageElement::BindToTree(BindContext& aContext, nsINode& aParent) {
|
|
|
|
nsresult rv = nsGenericHTMLElement::BindToTree(aContext, aParent);
|
2005-04-06 03:54:35 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
Bug 1555216 - Change the signature of BindToTree to be (BindContext&, nsINode& aParentNode). r=bzbarsky
BindContext was going to have way more information at first, but then I realized
that most of the things I wanted to know were basically a flag away using the
parent node.
Still I think it's worth it, now experimenting with BindToTree will only mean
adding a field to a struct that's included from a couple cpp files, instead of a
massive pain.
I also think this is clearer, and doing this highlights quite a few
inconsistencies in our code which I've left untouched, but commented with
FIXMEs.
Steps are:
$ for file in $(rg 'nsresult BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#nsresult BindToTree(Document\* aDocument, nsIContent\* aParent,#nsresult BindToTree(BindContext\&, nsINode\& aParent)#g' $file; done
$ for file in $(rg 'nsresult BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's# nsIContent\* aBindingParent) override#override#g' $file; done
$ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#::BindToTree(Document\* aDocument, nsIContent\* aParent,#::BindToTree(BindContext\& aContext, nsINode\& aParent)#g' $file; done
$ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#nsIContent\* aBindingParent)##g' $file; done
$ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#::BindToTree(aDocument, aParent, aBindingParent)#::BindToTree(aContext, aParent)#g' $file; done
$ ./mach clang-format
Then manual fixups.
Depends on D32948
Differential Revision: https://phabricator.services.mozilla.com/D32949
2019-05-29 07:27:04 +03:00
|
|
|
nsImageLoadingContent::BindToTree(aContext, aParent);
|
2012-08-14 02:11:50 +04:00
|
|
|
|
Bug 1555216 - Change the signature of BindToTree to be (BindContext&, nsINode& aParentNode). r=bzbarsky
BindContext was going to have way more information at first, but then I realized
that most of the things I wanted to know were basically a flag away using the
parent node.
Still I think it's worth it, now experimenting with BindToTree will only mean
adding a field to a struct that's included from a couple cpp files, instead of a
massive pain.
I also think this is clearer, and doing this highlights quite a few
inconsistencies in our code which I've left untouched, but commented with
FIXMEs.
Steps are:
$ for file in $(rg 'nsresult BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#nsresult BindToTree(Document\* aDocument, nsIContent\* aParent,#nsresult BindToTree(BindContext\&, nsINode\& aParent)#g' $file; done
$ for file in $(rg 'nsresult BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's# nsIContent\* aBindingParent) override#override#g' $file; done
$ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#::BindToTree(Document\* aDocument, nsIContent\* aParent,#::BindToTree(BindContext\& aContext, nsINode\& aParent)#g' $file; done
$ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#nsIContent\* aBindingParent)##g' $file; done
$ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#::BindToTree(aDocument, aParent, aBindingParent)#::BindToTree(aContext, aParent)#g' $file; done
$ ./mach clang-format
Then manual fixups.
Depends on D32948
Differential Revision: https://phabricator.services.mozilla.com/D32949
2019-05-29 07:27:04 +03:00
|
|
|
UpdateFormOwner();
|
2013-06-18 16:53:23 +04:00
|
|
|
|
2015-08-31 22:29:40 +03:00
|
|
|
if (HaveSrcsetOrInPicture()) {
|
2019-05-31 17:33:33 +03:00
|
|
|
if (IsInComposedDoc() && !mInDocResponsiveContent) {
|
|
|
|
aContext.OwnerDoc().AddResponsiveContent(this);
|
2015-08-31 22:29:40 +03:00
|
|
|
mInDocResponsiveContent = true;
|
2015-05-12 01:07:49 +03:00
|
|
|
}
|
2015-08-31 22:29:40 +03:00
|
|
|
|
2017-04-25 04:17:38 +03:00
|
|
|
// Mark channel as urgent-start before load image if the image load is
|
|
|
|
// initaiated by a user interaction.
|
2019-09-20 23:51:25 +03:00
|
|
|
mUseUrgentStartForChannel = UserActivation::IsHandlingUserInput();
|
2017-04-25 04:17:38 +03:00
|
|
|
|
2016-05-04 05:21:45 +03:00
|
|
|
// Run selection algorithm when an img element is inserted into a document
|
|
|
|
// in order to react to changes in the environment. See note of
|
|
|
|
// https://html.spec.whatwg.org/multipage/embedded-content.html#img-environment-changes
|
|
|
|
QueueImageLoadTask(false);
|
2014-10-02 07:59:56 +04:00
|
|
|
} else if (!InResponsiveMode() &&
|
2014-10-02 07:30:49 +04:00
|
|
|
HasAttr(kNameSpaceID_None, nsGkAtoms::src)) {
|
|
|
|
// We skip loading when our attributes were set from parser land,
|
|
|
|
// so trigger a aForce=false load now to check if things changed.
|
|
|
|
// This isn't necessary for responsive mode, since creating the
|
|
|
|
// image load task is asynchronous we don't need to take special
|
|
|
|
// care to avoid doing so when being filled by the parser.
|
|
|
|
|
2017-04-25 04:17:38 +03:00
|
|
|
// Mark channel as urgent-start before load image if the image load is
|
|
|
|
// initaiated by a user interaction.
|
2019-09-20 23:51:25 +03:00
|
|
|
mUseUrgentStartForChannel = UserActivation::IsHandlingUserInput();
|
2017-04-25 04:17:38 +03:00
|
|
|
|
2014-10-02 07:30:49 +04:00
|
|
|
// We still act synchronously for the non-responsive case (Bug
|
|
|
|
// 1076583), but still need to delay if it is unsafe to run
|
|
|
|
// script.
|
2014-05-03 03:26:22 +04:00
|
|
|
|
2009-11-20 07:39:11 +03:00
|
|
|
// If loading is temporarily disabled, don't even launch MaybeLoadImage.
|
|
|
|
// Otherwise MaybeLoadImage may run later when someone has reenabled
|
|
|
|
// loading.
|
2020-02-13 00:31:48 +03:00
|
|
|
if (LoadingEnabled() && ShouldLoadImage()) {
|
2009-11-20 07:39:11 +03:00
|
|
|
nsContentUtils::AddScriptRunner(
|
2017-06-12 22:34:10 +03:00
|
|
|
NewRunnableMethod<bool>("dom::HTMLImageElement::MaybeLoadImage", this,
|
|
|
|
&HTMLImageElement::MaybeLoadImage, false));
|
2009-11-20 07:39:11 +03:00
|
|
|
}
|
2009-05-20 12:00:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2019-05-29 01:47:08 +03:00
|
|
|
void HTMLImageElement::UnbindFromTree(bool aNullParent) {
|
2013-06-18 16:53:23 +04:00
|
|
|
if (mForm) {
|
|
|
|
if (aNullParent || !FindAncestorForm(mForm)) {
|
|
|
|
ClearForm(true);
|
|
|
|
} else {
|
|
|
|
UnsetFlags(MAYBE_ORPHAN_FORM_ELEMENT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-31 22:29:40 +03:00
|
|
|
if (mInDocResponsiveContent) {
|
2018-03-29 19:46:34 +03:00
|
|
|
OwnerDoc()->RemoveResponsiveContent(this);
|
|
|
|
mInDocResponsiveContent = false;
|
2015-08-31 22:29:40 +03:00
|
|
|
}
|
|
|
|
|
2019-05-29 01:47:08 +03:00
|
|
|
nsImageLoadingContent::UnbindFromTree(aNullParent);
|
|
|
|
nsGenericHTMLElement::UnbindFromTree(aNullParent);
|
2012-08-14 02:11:50 +04:00
|
|
|
}
|
|
|
|
|
2013-06-18 16:53:23 +04:00
|
|
|
void HTMLImageElement::UpdateFormOwner() {
|
|
|
|
if (!mForm) {
|
|
|
|
mForm = FindAncestorForm();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mForm && !HasFlag(ADDED_TO_FORM)) {
|
|
|
|
// Now we need to add ourselves to the form
|
|
|
|
nsAutoString nameVal, idVal;
|
|
|
|
GetAttr(kNameSpaceID_None, nsGkAtoms::name, nameVal);
|
|
|
|
GetAttr(kNameSpaceID_None, nsGkAtoms::id, idVal);
|
|
|
|
|
|
|
|
SetFlags(ADDED_TO_FORM);
|
|
|
|
|
|
|
|
mForm->AddImageElement(this);
|
|
|
|
|
|
|
|
if (!nameVal.IsEmpty()) {
|
|
|
|
mForm->AddImageElementToTable(this, nameVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!idVal.IsEmpty()) {
|
|
|
|
mForm->AddImageElementToTable(this, idVal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-13 13:03:10 +03:00
|
|
|
void HTMLImageElement::MaybeLoadImage(bool aAlwaysForceLoad) {
|
2014-05-03 03:26:22 +04:00
|
|
|
// Our base URI may have changed, or we may have had responsive parameters
|
|
|
|
// change while not bound to the tree. Re-parse src/srcset and call LoadImage,
|
|
|
|
// which is a no-op if it resolves to the same effective URI without aForce.
|
|
|
|
|
2009-05-20 12:00:04 +04:00
|
|
|
// Note, check LoadingEnabled() after LoadImage call.
|
2014-05-03 03:26:22 +04:00
|
|
|
|
2016-10-13 13:03:10 +03:00
|
|
|
LoadSelectedImage(aAlwaysForceLoad, /* aNotify */ true, aAlwaysForceLoad);
|
2014-05-03 03:26:22 +04:00
|
|
|
|
2014-10-02 07:30:49 +04:00
|
|
|
if (!LoadingEnabled()) {
|
2011-10-17 18:59:28 +04:00
|
|
|
CancelImageRequests(true);
|
2003-07-13 02:32:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-31 21:25:46 +04:00
|
|
|
EventStates HTMLImageElement::IntrinsicState() const {
|
2005-09-18 22:05:40 +04:00
|
|
|
return nsGenericHTMLElement::IntrinsicState() |
|
|
|
|
nsImageLoadingContent::ImageState();
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
void HTMLImageElement::NodeInfoChanged(Document* aOldDoc) {
|
2017-03-25 01:28:19 +03:00
|
|
|
nsGenericHTMLElement::NodeInfoChanged(aOldDoc);
|
2020-02-13 00:31:48 +03:00
|
|
|
|
|
|
|
if (mLazyLoading) {
|
|
|
|
aOldDoc->GetLazyLoadImageObserver()->Unobserve(*this);
|
|
|
|
mLazyLoading = false;
|
|
|
|
SetLazyLoading();
|
|
|
|
}
|
|
|
|
|
2016-10-13 13:03:10 +03:00
|
|
|
// Force reload image if adoption steps are run.
|
|
|
|
// If loading is temporarily disabled, don't even launch script runner.
|
|
|
|
// Otherwise script runner may run later when someone has reenabled loading.
|
2020-02-13 00:31:48 +03:00
|
|
|
StartLoadingIfNeeded();
|
2016-11-18 10:54:21 +03:00
|
|
|
}
|
|
|
|
|
2013-01-22 14:53:13 +04:00
|
|
|
// static
|
|
|
|
already_AddRefed<HTMLImageElement> HTMLImageElement::Image(
|
|
|
|
const GlobalObject& aGlobal, const Optional<uint32_t>& aWidth,
|
2013-08-23 09:17:08 +04:00
|
|
|
const Optional<uint32_t>& aHeight, ErrorResult& aError) {
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> win = do_QueryInterface(aGlobal.GetAsSupports());
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc;
|
2013-01-22 14:53:13 +04:00
|
|
|
if (!win || !(doc = win->GetExtantDoc())) {
|
|
|
|
aError.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
1999-05-07 04:08:21 +04:00
|
|
|
}
|
|
|
|
|
2013-01-22 14:53:13 +04:00
|
|
|
RefPtr<mozilla::dom::NodeInfo> nodeInfo = doc->NodeInfoManager()->GetNodeInfo(
|
|
|
|
nsGkAtoms::img, nullptr, kNameSpaceID_XHTML, ELEMENT_NODE);
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2020-03-17 17:53:08 +03:00
|
|
|
auto* nim = nodeInfo->NodeInfoManager();
|
|
|
|
RefPtr<HTMLImageElement> img = new (nim) HTMLImageElement(nodeInfo.forget());
|
2001-09-07 08:00:37 +04:00
|
|
|
|
2013-01-22 14:53:13 +04:00
|
|
|
if (aWidth.WasPassed()) {
|
2013-04-04 11:03:50 +04:00
|
|
|
img->SetWidth(aWidth.Value(), aError);
|
2013-01-22 14:53:13 +04:00
|
|
|
if (aError.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2013-01-22 14:53:13 +04:00
|
|
|
if (aHeight.WasPassed()) {
|
2013-04-04 11:03:50 +04:00
|
|
|
img->SetHeight(aHeight.Value(), aError);
|
2013-01-22 14:53:13 +04:00
|
|
|
if (aError.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
1999-05-07 04:08:21 +04:00
|
|
|
}
|
|
|
|
|
2013-01-22 14:53:13 +04:00
|
|
|
return img.forget();
|
1999-05-07 04:08:21 +04:00
|
|
|
}
|
|
|
|
|
2019-03-13 03:30:11 +03:00
|
|
|
uint32_t HTMLImageElement::Height() {
|
|
|
|
RefPtr<imgRequestProxy> currentRequest(mCurrentRequest);
|
|
|
|
return GetWidthHeightForImage(currentRequest).height;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t HTMLImageElement::Width() {
|
|
|
|
RefPtr<imgRequestProxy> currentRequest(mCurrentRequest);
|
|
|
|
return GetWidthHeightForImage(currentRequest).width;
|
|
|
|
}
|
|
|
|
|
2012-12-31 22:48:55 +04:00
|
|
|
uint32_t HTMLImageElement::NaturalHeight() {
|
2018-11-20 04:20:17 +03:00
|
|
|
uint32_t height = nsImageLoadingContent::NaturalHeight();
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2014-05-03 04:06:45 +04:00
|
|
|
if (mResponsiveSelector) {
|
|
|
|
double density = mResponsiveSelector->GetSelectedImageDensity();
|
2015-07-06 04:03:00 +03:00
|
|
|
MOZ_ASSERT(density >= 0.0);
|
2014-05-03 04:06:45 +04:00
|
|
|
height = NSToIntRound(double(height) / density);
|
|
|
|
}
|
|
|
|
|
2014-05-03 04:05:19 +04:00
|
|
|
return height;
|
2000-09-08 06:06:56 +04:00
|
|
|
}
|
|
|
|
|
2012-12-31 22:48:55 +04:00
|
|
|
uint32_t HTMLImageElement::NaturalWidth() {
|
2018-11-20 04:20:17 +03:00
|
|
|
uint32_t width = nsImageLoadingContent::NaturalWidth();
|
2000-12-23 13:56:31 +03:00
|
|
|
|
2014-05-03 04:06:45 +04:00
|
|
|
if (mResponsiveSelector) {
|
|
|
|
double density = mResponsiveSelector->GetSelectedImageDensity();
|
2015-07-06 04:03:00 +03:00
|
|
|
MOZ_ASSERT(density >= 0.0);
|
2014-05-03 04:06:45 +04:00
|
|
|
width = NSToIntRound(double(width) / density);
|
|
|
|
}
|
|
|
|
|
2014-05-03 04:05:19 +04:00
|
|
|
return width;
|
2012-12-31 22:48:55 +04:00
|
|
|
}
|
|
|
|
|
2018-08-09 02:58:44 +03:00
|
|
|
nsresult HTMLImageElement::CopyInnerTo(HTMLImageElement* aDest) {
|
2015-07-22 20:21:25 +03:00
|
|
|
bool destIsStatic = aDest->OwnerDoc()->IsStaticDocument();
|
|
|
|
if (destIsStatic) {
|
2018-08-09 02:58:44 +03:00
|
|
|
CreateStaticImageClone(aDest);
|
2009-12-11 07:02:13 +03:00
|
|
|
}
|
2015-07-22 20:21:25 +03:00
|
|
|
|
2018-08-09 02:58:44 +03:00
|
|
|
nsresult rv = nsGenericHTMLElement::CopyInnerTo(aDest);
|
2015-07-22 20:21:25 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!destIsStatic) {
|
2018-08-09 02:58:44 +03:00
|
|
|
// In SetAttr (called from nsGenericHTMLElement::CopyInnerTo), aDest skipped
|
2015-07-22 20:21:25 +03:00
|
|
|
// doing the image load because we passed in false for aNotify. But we
|
|
|
|
// really do want it to do the load, so set it up to happen once the cloning
|
|
|
|
// reaches a stable state.
|
2018-08-09 02:58:44 +03:00
|
|
|
if (!aDest->InResponsiveMode() &&
|
|
|
|
aDest->HasAttr(kNameSpaceID_None, nsGkAtoms::src) &&
|
2020-02-13 00:31:48 +03:00
|
|
|
aDest->ShouldLoadImage()) {
|
2017-04-25 04:17:38 +03:00
|
|
|
// Mark channel as urgent-start before load image if the image load is
|
|
|
|
// initaiated by a user interaction.
|
2019-09-20 23:51:25 +03:00
|
|
|
mUseUrgentStartForChannel = UserActivation::IsHandlingUserInput();
|
2017-04-25 04:17:38 +03:00
|
|
|
|
2015-07-22 20:21:25 +03:00
|
|
|
nsContentUtils::AddScriptRunner(NewRunnableMethod<bool>(
|
2017-06-12 22:34:10 +03:00
|
|
|
"dom::HTMLImageElement::MaybeLoadImage", aDest,
|
|
|
|
&HTMLImageElement::MaybeLoadImage, false));
|
2015-07-22 20:21:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2009-12-11 07:02:13 +03:00
|
|
|
}
|
2011-07-14 22:47:35 +04:00
|
|
|
|
2012-12-31 21:25:46 +04:00
|
|
|
CORSMode HTMLImageElement::GetCORSMode() {
|
2012-03-10 20:13:52 +04:00
|
|
|
return AttrValueToCORSMode(GetParsedAttr(nsGkAtoms::crossorigin));
|
2011-07-14 22:47:35 +04:00
|
|
|
}
|
2012-12-31 21:25:46 +04:00
|
|
|
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
JSObject* HTMLImageElement::WrapNode(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return HTMLImageElement_Binding::Wrap(aCx, this, aGivenProto);
|
2012-12-31 22:48:55 +04:00
|
|
|
}
|
|
|
|
|
2013-06-18 16:53:23 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
HTMLFormElement* HTMLImageElement::GetForm() const { return mForm; }
|
|
|
|
#endif
|
|
|
|
|
2018-03-14 23:42:25 +03:00
|
|
|
void HTMLImageElement::SetForm(HTMLFormElement* aForm) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aForm, "Don't pass null here");
|
2013-06-18 16:53:23 +04:00
|
|
|
NS_ASSERTION(!mForm,
|
|
|
|
"We don't support switching from one non-null form to another.");
|
|
|
|
|
2018-03-14 23:42:25 +03:00
|
|
|
mForm = aForm;
|
2013-06-18 16:53:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void HTMLImageElement::ClearForm(bool aRemoveFromForm) {
|
|
|
|
NS_ASSERTION((mForm != nullptr) == HasFlag(ADDED_TO_FORM),
|
|
|
|
"Form control should have had flag set correctly");
|
|
|
|
|
|
|
|
if (!mForm) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aRemoveFromForm) {
|
|
|
|
nsAutoString nameVal, idVal;
|
|
|
|
GetAttr(kNameSpaceID_None, nsGkAtoms::name, nameVal);
|
|
|
|
GetAttr(kNameSpaceID_None, nsGkAtoms::id, idVal);
|
|
|
|
|
|
|
|
mForm->RemoveImageElement(this);
|
|
|
|
|
|
|
|
if (!nameVal.IsEmpty()) {
|
2016-11-21 20:34:02 +03:00
|
|
|
mForm->RemoveImageElementFromTable(this, nameVal);
|
2013-06-18 16:53:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!idVal.IsEmpty()) {
|
2016-11-21 20:34:02 +03:00
|
|
|
mForm->RemoveImageElementFromTable(this, idVal);
|
2013-06-18 16:53:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UnsetFlags(ADDED_TO_FORM);
|
|
|
|
mForm = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-08-31 22:29:40 +03:00
|
|
|
void HTMLImageElement::QueueImageLoadTask(bool aAlwaysLoad) {
|
2014-10-02 07:30:49 +04:00
|
|
|
// If loading is temporarily disabled, we don't want to queue tasks
|
|
|
|
// that may then run when loading is re-enabled.
|
2020-02-13 00:31:48 +03:00
|
|
|
if (!LoadingEnabled() || !ShouldLoadImage()) {
|
2014-10-02 07:30:49 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-30 19:39:59 +03:00
|
|
|
// Ensure that we don't overwrite a previous load request that requires
|
|
|
|
// a complete load to occur.
|
|
|
|
bool alwaysLoad = aAlwaysLoad;
|
|
|
|
if (mPendingImageLoadTask) {
|
|
|
|
alwaysLoad = alwaysLoad || mPendingImageLoadTask->AlwaysLoad();
|
|
|
|
}
|
2017-04-25 04:17:38 +03:00
|
|
|
RefPtr<ImageLoadTask> task =
|
|
|
|
new ImageLoadTask(this, alwaysLoad, mUseUrgentStartForChannel);
|
2015-06-11 05:36:12 +03:00
|
|
|
// The task checks this to determine if it was the last
|
|
|
|
// queued event, and so earlier tasks are implicitly canceled.
|
|
|
|
mPendingImageLoadTask = task;
|
2019-07-22 20:35:20 +03:00
|
|
|
CycleCollectedJSContext::Get()->DispatchToMicroTask(task.forget());
|
2014-10-02 07:30:49 +04:00
|
|
|
}
|
|
|
|
|
2014-10-02 07:59:56 +04:00
|
|
|
bool HTMLImageElement::HaveSrcsetOrInPicture() {
|
2016-04-17 01:07:56 +03:00
|
|
|
if (HasAttr(kNameSpaceID_None, nsGkAtoms::srcset)) {
|
2014-10-02 07:59:56 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-19 14:43:42 +03:00
|
|
|
Element* parent = nsINode::GetParentElement();
|
2015-03-03 14:08:59 +03:00
|
|
|
return (parent && parent->IsHTMLElement(nsGkAtoms::picture));
|
2014-10-02 07:59:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HTMLImageElement::InResponsiveMode() {
|
|
|
|
// When we lose srcset or leave a <picture> element, the fallback to img.src
|
|
|
|
// will happen from the microtask, and we should behave responsively in the
|
|
|
|
// interim
|
|
|
|
return mResponsiveSelector || mPendingImageLoadTask ||
|
|
|
|
HaveSrcsetOrInPicture();
|
|
|
|
}
|
|
|
|
|
Bug 1149357: Properly update responsive images for density changes. r=dholbert
Before that we were not notifying the image frame in any way if we ended up not
doing a load, and we were instead relying on the reflow the viewport resize
caused to get the new density in ComputeSize from the content node (but nowhere
else, since that's the bug part 1 fixes).
This was generally unsound, since you can stash random media in a sizes=
attribute, which don't necessarily needs to cause a reflow.
Now we need to notify necessarily because nsImageFrame stores the adjusted
intrinsic size.
mCurrentDensity could also get out of sync as well, when the selected image
density changed, but we ended up returning early because our source hadn't
change in the first early exit.
This patch moves us to a model where we don't re-trigger loads for density
changes if the source doesn't change (unless we pass aAlwaysLoad when we need
to, per spec).
This matches our previous behavior (without the bugginess of not updating the
intrinsic size), and also Chromium, at least.
This changes behavior in one case, which is when we don't load the same source
node, but we have the same source URL, and the density does change. This could
happen with <picture> and two <source>s with same source and different media and
sizes. This makes our behavior consistent with the behavior we have when both
the source and the density doesn't change.
Blink and WebKit do trigger a second image load both when the source changes
without changing density and when density changes. I'll file a spec issue, since
per:
https://html.spec.whatwg.org/#reacting-to-environment-changes
We should be triggering the load when the density changes but the source
doesn't as well, but no UA does that.
I filed https://github.com/whatwg/html/issues/3709 with a little summary of the
situation and what I think the behavior should be (which is what this patch
implements). That being said, I'll update the impl if the spec people think
otherwise :).
MozReview-Commit-ID: Eqy16ygHRLo
2018-05-23 12:19:49 +03:00
|
|
|
bool HTMLImageElement::SelectedSourceMatchesLast(nsIURI* aSelectedSource) {
|
2016-09-26 21:17:38 +03:00
|
|
|
// If there was no selected source previously, we don't want to short-circuit
|
|
|
|
// the load. Similarly for if there is no newly selected source.
|
|
|
|
if (!mLastSelectedSource || !aSelectedSource) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool equal = false;
|
Bug 1149357: Properly update responsive images for density changes. r=dholbert
Before that we were not notifying the image frame in any way if we ended up not
doing a load, and we were instead relying on the reflow the viewport resize
caused to get the new density in ComputeSize from the content node (but nowhere
else, since that's the bug part 1 fixes).
This was generally unsound, since you can stash random media in a sizes=
attribute, which don't necessarily needs to cause a reflow.
Now we need to notify necessarily because nsImageFrame stores the adjusted
intrinsic size.
mCurrentDensity could also get out of sync as well, when the selected image
density changed, but we ended up returning early because our source hadn't
change in the first early exit.
This patch moves us to a model where we don't re-trigger loads for density
changes if the source doesn't change (unless we pass aAlwaysLoad when we need
to, per spec).
This matches our previous behavior (without the bugginess of not updating the
intrinsic size), and also Chromium, at least.
This changes behavior in one case, which is when we don't load the same source
node, but we have the same source URL, and the density does change. This could
happen with <picture> and two <source>s with same source and different media and
sizes. This makes our behavior consistent with the behavior we have when both
the source and the density doesn't change.
Blink and WebKit do trigger a second image load both when the source changes
without changing density and when density changes. I'll file a spec issue, since
per:
https://html.spec.whatwg.org/#reacting-to-environment-changes
We should be triggering the load when the density changes but the source
doesn't as well, but no UA does that.
I filed https://github.com/whatwg/html/issues/3709 with a little summary of the
situation and what I think the behavior should be (which is what this patch
implements). That being said, I'll update the impl if the spec people think
otherwise :).
MozReview-Commit-ID: Eqy16ygHRLo
2018-05-23 12:19:49 +03:00
|
|
|
return NS_SUCCEEDED(mLastSelectedSource->Equals(aSelectedSource, &equal)) &&
|
|
|
|
equal;
|
2016-09-26 21:17:38 +03:00
|
|
|
}
|
|
|
|
|
2015-08-31 22:29:40 +03:00
|
|
|
nsresult HTMLImageElement::LoadSelectedImage(bool aForce, bool aNotify,
|
|
|
|
bool aAlwaysLoad) {
|
Bug 1149357: Properly update responsive images for density changes. r=dholbert
Before that we were not notifying the image frame in any way if we ended up not
doing a load, and we were instead relying on the reflow the viewport resize
caused to get the new density in ComputeSize from the content node (but nowhere
else, since that's the bug part 1 fixes).
This was generally unsound, since you can stash random media in a sizes=
attribute, which don't necessarily needs to cause a reflow.
Now we need to notify necessarily because nsImageFrame stores the adjusted
intrinsic size.
mCurrentDensity could also get out of sync as well, when the selected image
density changed, but we ended up returning early because our source hadn't
change in the first early exit.
This patch moves us to a model where we don't re-trigger loads for density
changes if the source doesn't change (unless we pass aAlwaysLoad when we need
to, per spec).
This matches our previous behavior (without the bugginess of not updating the
intrinsic size), and also Chromium, at least.
This changes behavior in one case, which is when we don't load the same source
node, but we have the same source URL, and the density does change. This could
happen with <picture> and two <source>s with same source and different media and
sizes. This makes our behavior consistent with the behavior we have when both
the source and the density doesn't change.
Blink and WebKit do trigger a second image load both when the source changes
without changing density and when density changes. I'll file a spec issue, since
per:
https://html.spec.whatwg.org/#reacting-to-environment-changes
We should be triggering the load when the density changes but the source
doesn't as well, but no UA does that.
I filed https://github.com/whatwg/html/issues/3709 with a little summary of the
situation and what I think the behavior should be (which is what this patch
implements). That being said, I'll update the impl if the spec people think
otherwise :).
MozReview-Commit-ID: Eqy16ygHRLo
2018-05-23 12:19:49 +03:00
|
|
|
double currentDensity = 1.0; // default to 1.0 for the src attribute case
|
|
|
|
|
|
|
|
// Helper to update state when only density may have changed (i.e., the source
|
|
|
|
// to load hasn't changed, and we don't do any request at all). We need (apart
|
|
|
|
// from updating our internal state) to tell the image frame because its
|
|
|
|
// intrinsic size may have changed.
|
|
|
|
//
|
|
|
|
// In the case we actually trigger a new load, that load will trigger a call
|
|
|
|
// to nsImageFrame::NotifyNewCurrentRequest, which takes care of that for us.
|
|
|
|
auto UpdateDensityOnly = [&]() -> void {
|
|
|
|
if (mCurrentDensity == currentDensity) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mCurrentDensity = currentDensity;
|
|
|
|
if (nsImageFrame* f = do_QueryFrame(GetPrimaryFrame())) {
|
|
|
|
f->ResponsiveContentDensityChanged();
|
|
|
|
}
|
|
|
|
};
|
2014-05-03 03:26:22 +04:00
|
|
|
|
2014-10-02 07:30:49 +04:00
|
|
|
if (aForce) {
|
Bug 1149357: Properly update responsive images for density changes. r=dholbert
Before that we were not notifying the image frame in any way if we ended up not
doing a load, and we were instead relying on the reflow the viewport resize
caused to get the new density in ComputeSize from the content node (but nowhere
else, since that's the bug part 1 fixes).
This was generally unsound, since you can stash random media in a sizes=
attribute, which don't necessarily needs to cause a reflow.
Now we need to notify necessarily because nsImageFrame stores the adjusted
intrinsic size.
mCurrentDensity could also get out of sync as well, when the selected image
density changed, but we ended up returning early because our source hadn't
change in the first early exit.
This patch moves us to a model where we don't re-trigger loads for density
changes if the source doesn't change (unless we pass aAlwaysLoad when we need
to, per spec).
This matches our previous behavior (without the bugginess of not updating the
intrinsic size), and also Chromium, at least.
This changes behavior in one case, which is when we don't load the same source
node, but we have the same source URL, and the density does change. This could
happen with <picture> and two <source>s with same source and different media and
sizes. This makes our behavior consistent with the behavior we have when both
the source and the density doesn't change.
Blink and WebKit do trigger a second image load both when the source changes
without changing density and when density changes. I'll file a spec issue, since
per:
https://html.spec.whatwg.org/#reacting-to-environment-changes
We should be triggering the load when the density changes but the source
doesn't as well, but no UA does that.
I filed https://github.com/whatwg/html/issues/3709 with a little summary of the
situation and what I think the behavior should be (which is what this patch
implements). That being said, I'll update the impl if the spec people think
otherwise :).
MozReview-Commit-ID: Eqy16ygHRLo
2018-05-23 12:19:49 +03:00
|
|
|
// In responsive mode we generally want to re-run the full selection
|
|
|
|
// algorithm whenever starting a new load, per spec.
|
|
|
|
//
|
|
|
|
// This also causes us to re-resolve the URI as appropriate.
|
|
|
|
const bool sourceChanged = UpdateResponsiveSource();
|
|
|
|
|
|
|
|
if (mResponsiveSelector) {
|
|
|
|
currentDensity = mResponsiveSelector->GetSelectedImageDensity();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sourceChanged && !aAlwaysLoad) {
|
|
|
|
UpdateDensityOnly();
|
2015-08-31 22:29:40 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
Bug 1149357: Properly update responsive images for density changes. r=dholbert
Before that we were not notifying the image frame in any way if we ended up not
doing a load, and we were instead relying on the reflow the viewport resize
caused to get the new density in ComputeSize from the content node (but nowhere
else, since that's the bug part 1 fixes).
This was generally unsound, since you can stash random media in a sizes=
attribute, which don't necessarily needs to cause a reflow.
Now we need to notify necessarily because nsImageFrame stores the adjusted
intrinsic size.
mCurrentDensity could also get out of sync as well, when the selected image
density changed, but we ended up returning early because our source hadn't
change in the first early exit.
This patch moves us to a model where we don't re-trigger loads for density
changes if the source doesn't change (unless we pass aAlwaysLoad when we need
to, per spec).
This matches our previous behavior (without the bugginess of not updating the
intrinsic size), and also Chromium, at least.
This changes behavior in one case, which is when we don't load the same source
node, but we have the same source URL, and the density does change. This could
happen with <picture> and two <source>s with same source and different media and
sizes. This makes our behavior consistent with the behavior we have when both
the source and the density doesn't change.
Blink and WebKit do trigger a second image load both when the source changes
without changing density and when density changes. I'll file a spec issue, since
per:
https://html.spec.whatwg.org/#reacting-to-environment-changes
We should be triggering the load when the density changes but the source
doesn't as well, but no UA does that.
I filed https://github.com/whatwg/html/issues/3709 with a little summary of the
situation and what I think the behavior should be (which is what this patch
implements). That being said, I'll update the impl if the spec people think
otherwise :).
MozReview-Commit-ID: Eqy16ygHRLo
2018-05-23 12:19:49 +03:00
|
|
|
} else if (mResponsiveSelector) {
|
|
|
|
currentDensity = mResponsiveSelector->GetSelectedImageDensity();
|
2014-10-02 07:30:49 +04:00
|
|
|
}
|
|
|
|
|
Bug 1149357: Properly update responsive images for density changes. r=dholbert
Before that we were not notifying the image frame in any way if we ended up not
doing a load, and we were instead relying on the reflow the viewport resize
caused to get the new density in ComputeSize from the content node (but nowhere
else, since that's the bug part 1 fixes).
This was generally unsound, since you can stash random media in a sizes=
attribute, which don't necessarily needs to cause a reflow.
Now we need to notify necessarily because nsImageFrame stores the adjusted
intrinsic size.
mCurrentDensity could also get out of sync as well, when the selected image
density changed, but we ended up returning early because our source hadn't
change in the first early exit.
This patch moves us to a model where we don't re-trigger loads for density
changes if the source doesn't change (unless we pass aAlwaysLoad when we need
to, per spec).
This matches our previous behavior (without the bugginess of not updating the
intrinsic size), and also Chromium, at least.
This changes behavior in one case, which is when we don't load the same source
node, but we have the same source URL, and the density does change. This could
happen with <picture> and two <source>s with same source and different media and
sizes. This makes our behavior consistent with the behavior we have when both
the source and the density doesn't change.
Blink and WebKit do trigger a second image load both when the source changes
without changing density and when density changes. I'll file a spec issue, since
per:
https://html.spec.whatwg.org/#reacting-to-environment-changes
We should be triggering the load when the density changes but the source
doesn't as well, but no UA does that.
I filed https://github.com/whatwg/html/issues/3709 with a little summary of the
situation and what I think the behavior should be (which is what this patch
implements). That being said, I'll update the impl if the spec people think
otherwise :).
MozReview-Commit-ID: Eqy16ygHRLo
2018-05-23 12:19:49 +03:00
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
2016-09-26 21:17:38 +03:00
|
|
|
nsCOMPtr<nsIURI> selectedSource;
|
2014-05-03 03:26:22 +04:00
|
|
|
if (mResponsiveSelector) {
|
|
|
|
nsCOMPtr<nsIURI> url = mResponsiveSelector->GetSelectedImageURL();
|
2017-10-03 07:30:34 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> triggeringPrincipal =
|
|
|
|
mResponsiveSelector->GetSelectedImageTriggeringPrincipal();
|
2016-09-26 21:17:38 +03:00
|
|
|
selectedSource = url;
|
Bug 1149357: Properly update responsive images for density changes. r=dholbert
Before that we were not notifying the image frame in any way if we ended up not
doing a load, and we were instead relying on the reflow the viewport resize
caused to get the new density in ComputeSize from the content node (but nowhere
else, since that's the bug part 1 fixes).
This was generally unsound, since you can stash random media in a sizes=
attribute, which don't necessarily needs to cause a reflow.
Now we need to notify necessarily because nsImageFrame stores the adjusted
intrinsic size.
mCurrentDensity could also get out of sync as well, when the selected image
density changed, but we ended up returning early because our source hadn't
change in the first early exit.
This patch moves us to a model where we don't re-trigger loads for density
changes if the source doesn't change (unless we pass aAlwaysLoad when we need
to, per spec).
This matches our previous behavior (without the bugginess of not updating the
intrinsic size), and also Chromium, at least.
This changes behavior in one case, which is when we don't load the same source
node, but we have the same source URL, and the density does change. This could
happen with <picture> and two <source>s with same source and different media and
sizes. This makes our behavior consistent with the behavior we have when both
the source and the density doesn't change.
Blink and WebKit do trigger a second image load both when the source changes
without changing density and when density changes. I'll file a spec issue, since
per:
https://html.spec.whatwg.org/#reacting-to-environment-changes
We should be triggering the load when the density changes but the source
doesn't as well, but no UA does that.
I filed https://github.com/whatwg/html/issues/3709 with a little summary of the
situation and what I think the behavior should be (which is what this patch
implements). That being said, I'll update the impl if the spec people think
otherwise :).
MozReview-Commit-ID: Eqy16ygHRLo
2018-05-23 12:19:49 +03:00
|
|
|
if (!aAlwaysLoad && SelectedSourceMatchesLast(selectedSource)) {
|
|
|
|
UpdateDensityOnly();
|
2016-09-26 21:17:38 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-12-11 02:53:00 +03:00
|
|
|
if (url) {
|
2017-10-03 07:30:34 +03:00
|
|
|
rv = LoadImage(url, aForce, aNotify, eImageLoadType_Imageset,
|
|
|
|
triggeringPrincipal);
|
2014-12-11 02:53:00 +03:00
|
|
|
}
|
2014-05-03 03:26:22 +04:00
|
|
|
} else {
|
|
|
|
nsAutoString src;
|
|
|
|
if (!GetAttr(kNameSpaceID_None, nsGkAtoms::src, src)) {
|
|
|
|
CancelImageRequests(aNotify);
|
|
|
|
rv = NS_OK;
|
|
|
|
} else {
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = OwnerDoc();
|
Bug 1149357: Properly update responsive images for density changes. r=dholbert
Before that we were not notifying the image frame in any way if we ended up not
doing a load, and we were instead relying on the reflow the viewport resize
caused to get the new density in ComputeSize from the content node (but nowhere
else, since that's the bug part 1 fixes).
This was generally unsound, since you can stash random media in a sizes=
attribute, which don't necessarily needs to cause a reflow.
Now we need to notify necessarily because nsImageFrame stores the adjusted
intrinsic size.
mCurrentDensity could also get out of sync as well, when the selected image
density changed, but we ended up returning early because our source hadn't
change in the first early exit.
This patch moves us to a model where we don't re-trigger loads for density
changes if the source doesn't change (unless we pass aAlwaysLoad when we need
to, per spec).
This matches our previous behavior (without the bugginess of not updating the
intrinsic size), and also Chromium, at least.
This changes behavior in one case, which is when we don't load the same source
node, but we have the same source URL, and the density does change. This could
happen with <picture> and two <source>s with same source and different media and
sizes. This makes our behavior consistent with the behavior we have when both
the source and the density doesn't change.
Blink and WebKit do trigger a second image load both when the source changes
without changing density and when density changes. I'll file a spec issue, since
per:
https://html.spec.whatwg.org/#reacting-to-environment-changes
We should be triggering the load when the density changes but the source
doesn't as well, but no UA does that.
I filed https://github.com/whatwg/html/issues/3709 with a little summary of the
situation and what I think the behavior should be (which is what this patch
implements). That being said, I'll update the impl if the spec people think
otherwise :).
MozReview-Commit-ID: Eqy16ygHRLo
2018-05-23 12:19:49 +03:00
|
|
|
StringToURI(src, doc, getter_AddRefs(selectedSource));
|
|
|
|
if (!aAlwaysLoad && SelectedSourceMatchesLast(selectedSource)) {
|
|
|
|
UpdateDensityOnly();
|
|
|
|
return NS_OK;
|
2016-09-26 21:17:38 +03:00
|
|
|
}
|
|
|
|
|
2014-08-20 01:49:38 +04:00
|
|
|
// If we have a srcset attribute or are in a <picture> element,
|
|
|
|
// we always use the Imageset load type, even if we parsed no
|
|
|
|
// valid responsive sources from either, per spec.
|
|
|
|
rv = LoadImage(src, aForce, aNotify,
|
|
|
|
HaveSrcsetOrInPicture() ? eImageLoadType_Imageset
|
2017-10-03 06:28:32 +03:00
|
|
|
: eImageLoadType_Normal,
|
|
|
|
mSrcTriggeringPrincipal);
|
2014-05-03 03:26:22 +04:00
|
|
|
}
|
|
|
|
}
|
2016-09-26 21:17:38 +03:00
|
|
|
mLastSelectedSource = selectedSource;
|
|
|
|
mCurrentDensity = currentDensity;
|
2014-05-03 03:26:22 +04:00
|
|
|
|
2014-10-02 07:30:49 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
CancelImageRequests(aNotify);
|
|
|
|
}
|
2014-05-03 03:26:22 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-05-03 01:32:20 +04:00
|
|
|
void HTMLImageElement::PictureSourceSrcsetChanged(nsIContent* aSourceNode,
|
|
|
|
const nsAString& aNewValue,
|
|
|
|
bool aNotify) {
|
2016-04-17 01:07:56 +03:00
|
|
|
MOZ_ASSERT(aSourceNode == this || IsPreviousSibling(aSourceNode, this),
|
2014-10-02 07:30:49 +04:00
|
|
|
"Should not be getting notifications for non-previous-siblings");
|
|
|
|
|
|
|
|
nsIContent* currentSrc =
|
|
|
|
mResponsiveSelector ? mResponsiveSelector->Content() : nullptr;
|
2014-05-03 01:32:20 +04:00
|
|
|
|
|
|
|
if (aSourceNode == currentSrc) {
|
2014-10-02 07:30:49 +04:00
|
|
|
// We're currently using this node as our responsive selector
|
|
|
|
// source.
|
2017-10-03 07:30:34 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
if (aSourceNode == this) {
|
|
|
|
principal = mSrcsetTriggeringPrincipal;
|
2018-03-22 00:39:04 +03:00
|
|
|
} else if (auto* source = HTMLSourceElement::FromNode(aSourceNode)) {
|
2017-10-06 01:59:15 +03:00
|
|
|
principal = source->GetSrcsetTriggeringPrincipal();
|
2017-10-03 07:30:34 +03:00
|
|
|
}
|
|
|
|
mResponsiveSelector->SetCandidatesFromSourceSet(aNewValue, principal);
|
2014-05-03 01:32:20 +04:00
|
|
|
}
|
2014-10-02 07:30:49 +04:00
|
|
|
|
2015-12-18 04:48:47 +03:00
|
|
|
if (!mInDocResponsiveContent && IsInComposedDoc()) {
|
2018-03-29 19:46:34 +03:00
|
|
|
OwnerDoc()->AddResponsiveContent(this);
|
|
|
|
mInDocResponsiveContent = true;
|
2015-08-31 22:29:40 +03:00
|
|
|
}
|
|
|
|
|
2014-10-02 07:30:49 +04:00
|
|
|
// This always triggers the image update steps per the spec, even if
|
|
|
|
// we are not using this source.
|
2015-08-31 22:29:40 +03:00
|
|
|
QueueImageLoadTask(true);
|
2014-05-03 03:26:22 +04:00
|
|
|
}
|
|
|
|
|
2014-05-03 01:32:20 +04:00
|
|
|
void HTMLImageElement::PictureSourceSizesChanged(nsIContent* aSourceNode,
|
|
|
|
const nsAString& aNewValue,
|
|
|
|
bool aNotify) {
|
2014-10-02 07:30:49 +04:00
|
|
|
MOZ_ASSERT(aSourceNode == this || IsPreviousSibling(aSourceNode, this),
|
|
|
|
"Should not be getting notifications for non-previous-siblings");
|
|
|
|
|
|
|
|
nsIContent* currentSrc =
|
|
|
|
mResponsiveSelector ? mResponsiveSelector->Content() : nullptr;
|
2014-05-03 03:26:22 +04:00
|
|
|
|
2014-05-03 01:32:20 +04:00
|
|
|
if (aSourceNode == currentSrc) {
|
2014-10-02 07:30:49 +04:00
|
|
|
// We're currently using this node as our responsive selector
|
|
|
|
// source.
|
2014-05-03 01:32:20 +04:00
|
|
|
mResponsiveSelector->SetSizesFromDescriptor(aNewValue);
|
|
|
|
}
|
2014-10-02 07:30:49 +04:00
|
|
|
|
|
|
|
// This always triggers the image update steps per the spec, even if
|
|
|
|
// we are not using this source.
|
2015-08-31 22:29:40 +03:00
|
|
|
QueueImageLoadTask(true);
|
2014-05-03 01:32:20 +04:00
|
|
|
}
|
2014-05-03 03:26:22 +04:00
|
|
|
|
2014-10-09 01:01:19 +04:00
|
|
|
void HTMLImageElement::PictureSourceMediaOrTypeChanged(nsIContent* aSourceNode,
|
|
|
|
bool aNotify) {
|
2014-10-02 07:30:49 +04:00
|
|
|
MOZ_ASSERT(IsPreviousSibling(aSourceNode, this),
|
|
|
|
"Should not be getting notifications for non-previous-siblings");
|
|
|
|
|
|
|
|
// This always triggers the image update steps per the spec, even if
|
|
|
|
// we are not switching to/from this source
|
2015-08-31 22:29:40 +03:00
|
|
|
QueueImageLoadTask(true);
|
2014-05-03 01:32:20 +04:00
|
|
|
}
|
|
|
|
|
2014-10-02 07:30:49 +04:00
|
|
|
void HTMLImageElement::PictureSourceAdded(nsIContent* aSourceNode) {
|
2016-05-04 05:21:45 +03:00
|
|
|
MOZ_ASSERT(aSourceNode == this || IsPreviousSibling(aSourceNode, this),
|
|
|
|
"Should not be getting notifications for non-previous-siblings");
|
|
|
|
|
2015-08-31 22:29:40 +03:00
|
|
|
QueueImageLoadTask(true);
|
2014-05-03 01:32:20 +04:00
|
|
|
}
|
|
|
|
|
2014-10-02 07:30:49 +04:00
|
|
|
void HTMLImageElement::PictureSourceRemoved(nsIContent* aSourceNode) {
|
2016-04-22 17:36:54 +03:00
|
|
|
MOZ_ASSERT(aSourceNode == this || IsPreviousSibling(aSourceNode, this),
|
|
|
|
"Should not be getting notifications for non-previous-siblings");
|
|
|
|
|
2015-08-31 22:29:40 +03:00
|
|
|
QueueImageLoadTask(true);
|
2014-10-02 07:30:49 +04:00
|
|
|
}
|
2014-05-03 01:32:20 +04:00
|
|
|
|
2014-10-02 07:30:49 +04:00
|
|
|
bool HTMLImageElement::UpdateResponsiveSource() {
|
2015-08-31 22:29:40 +03:00
|
|
|
bool hadSelector = !!mResponsiveSelector;
|
|
|
|
|
2014-10-02 07:30:49 +04:00
|
|
|
nsIContent* currentSource =
|
|
|
|
mResponsiveSelector ? mResponsiveSelector->Content() : nullptr;
|
2016-04-17 01:07:56 +03:00
|
|
|
Element* parent = nsINode::GetParentElement();
|
2014-10-02 07:30:49 +04:00
|
|
|
|
|
|
|
nsINode* candidateSource = nullptr;
|
2015-03-03 14:08:59 +03:00
|
|
|
if (parent && parent->IsHTMLElement(nsGkAtoms::picture)) {
|
2014-10-02 07:30:49 +04:00
|
|
|
// Walk source nodes previous to ourselves
|
|
|
|
candidateSource = parent->GetFirstChild();
|
|
|
|
} else {
|
|
|
|
candidateSource = this;
|
2014-05-03 01:32:20 +04:00
|
|
|
}
|
|
|
|
|
2014-10-02 07:30:49 +04:00
|
|
|
while (candidateSource) {
|
|
|
|
if (candidateSource == currentSource) {
|
|
|
|
// found no better source before current, re-run selection on
|
|
|
|
// that and keep it if it's still usable.
|
2015-08-31 22:29:40 +03:00
|
|
|
bool changed = mResponsiveSelector->SelectImage(true);
|
2014-10-02 07:30:49 +04:00
|
|
|
if (mResponsiveSelector->NumCandidates()) {
|
2015-05-12 01:07:49 +03:00
|
|
|
bool isUsableCandidate = true;
|
|
|
|
|
|
|
|
// an otherwise-usable source element may still have a media query that
|
|
|
|
// may not match any more.
|
|
|
|
if (candidateSource->IsHTMLElement(nsGkAtoms::source) &&
|
2017-12-07 21:13:50 +03:00
|
|
|
!SourceElementMatches(candidateSource->AsElement())) {
|
2015-05-12 01:07:49 +03:00
|
|
|
isUsableCandidate = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isUsableCandidate) {
|
2015-08-31 22:29:40 +03:00
|
|
|
return changed;
|
2015-05-12 01:07:49 +03:00
|
|
|
}
|
2014-10-02 07:30:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// no longer valid
|
|
|
|
mResponsiveSelector = nullptr;
|
|
|
|
if (candidateSource == this) {
|
|
|
|
// No further possibilities
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (candidateSource == this) {
|
|
|
|
// We are the last possible source
|
2017-12-07 21:13:50 +03:00
|
|
|
if (!TryCreateResponsiveSelector(candidateSource->AsElement())) {
|
2014-10-02 07:30:49 +04:00
|
|
|
// Failed to find any source
|
|
|
|
mResponsiveSelector = nullptr;
|
|
|
|
}
|
2014-05-03 01:32:20 +04:00
|
|
|
break;
|
2015-03-03 14:08:59 +03:00
|
|
|
} else if (candidateSource->IsHTMLElement(nsGkAtoms::source) &&
|
2017-12-07 21:13:50 +03:00
|
|
|
TryCreateResponsiveSelector(candidateSource->AsElement())) {
|
2014-10-02 07:30:49 +04:00
|
|
|
// This led to a valid source, stop
|
2014-05-03 01:32:20 +04:00
|
|
|
break;
|
|
|
|
}
|
2014-10-02 07:30:49 +04:00
|
|
|
candidateSource = candidateSource->GetNextSibling();
|
2014-05-03 01:32:20 +04:00
|
|
|
}
|
|
|
|
|
2014-10-02 07:30:49 +04:00
|
|
|
if (!candidateSource) {
|
|
|
|
// Ran out of siblings without finding ourself, e.g. XBL magic.
|
|
|
|
mResponsiveSelector = nullptr;
|
|
|
|
}
|
2015-08-31 22:29:40 +03:00
|
|
|
|
2016-01-06 23:40:09 +03:00
|
|
|
// If we reach this point, either:
|
|
|
|
// - there was no selector originally, and there is not one now
|
|
|
|
// - there was no selector originally, and there is one now
|
|
|
|
// - there was a selector, and there is a different one now
|
|
|
|
// - there was a selector, and there is not one now
|
|
|
|
return hadSelector || mResponsiveSelector;
|
2014-05-03 01:32:20 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/*static */
|
|
|
|
bool HTMLImageElement::SupportedPictureSourceType(const nsAString& aType) {
|
2016-03-18 17:16:00 +03:00
|
|
|
nsAutoString type;
|
|
|
|
nsAutoString params;
|
|
|
|
|
|
|
|
nsContentUtils::SplitMimeType(aType, type, params);
|
|
|
|
if (type.IsEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return imgLoader::SupportImageWithMimeType(
|
|
|
|
NS_ConvertUTF16toUTF8(type).get(),
|
2014-12-11 02:53:00 +03:00
|
|
|
AcceptedMimeTypes::IMAGES_AND_DOCUMENTS);
|
|
|
|
}
|
|
|
|
|
2017-12-07 21:13:50 +03:00
|
|
|
bool HTMLImageElement::SourceElementMatches(Element* aSourceElement) {
|
|
|
|
MOZ_ASSERT(aSourceElement->IsHTMLElement(nsGkAtoms::source));
|
2015-05-12 01:07:49 +03:00
|
|
|
|
|
|
|
DebugOnly<Element*> parent(nsINode::GetParentElement());
|
|
|
|
MOZ_ASSERT(parent && parent->IsHTMLElement(nsGkAtoms::picture));
|
2017-12-07 21:13:50 +03:00
|
|
|
MOZ_ASSERT(IsPreviousSibling(aSourceElement, this));
|
2015-05-12 01:07:49 +03:00
|
|
|
|
|
|
|
// Check media and type
|
2017-12-07 21:13:50 +03:00
|
|
|
auto* src = static_cast<HTMLSourceElement*>(aSourceElement);
|
2015-05-12 01:07:49 +03:00
|
|
|
if (!src->MatchesCurrentMedia()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString type;
|
2017-12-07 21:13:50 +03:00
|
|
|
if (src->GetAttr(kNameSpaceID_None, nsGkAtoms::type, type) &&
|
2015-05-12 01:07:49 +03:00
|
|
|
!SupportedPictureSourceType(type)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-07 21:13:50 +03:00
|
|
|
bool HTMLImageElement::TryCreateResponsiveSelector(Element* aSourceElement) {
|
2017-10-03 07:30:34 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
|
2014-05-03 01:32:20 +04:00
|
|
|
// Skip if this is not a <source> with matching media query
|
2017-12-07 21:13:50 +03:00
|
|
|
bool isSourceTag = aSourceElement->IsHTMLElement(nsGkAtoms::source);
|
2014-05-03 01:32:20 +04:00
|
|
|
if (isSourceTag) {
|
2017-12-07 21:13:50 +03:00
|
|
|
if (!SourceElementMatches(aSourceElement)) {
|
2014-10-09 00:53:24 +04:00
|
|
|
return false;
|
|
|
|
}
|
2018-03-22 00:39:04 +03:00
|
|
|
auto* source = HTMLSourceElement::FromNode(aSourceElement);
|
2017-10-06 01:59:15 +03:00
|
|
|
principal = source->GetSrcsetTriggeringPrincipal();
|
2017-12-07 21:13:50 +03:00
|
|
|
} else if (aSourceElement->IsHTMLElement(nsGkAtoms::img)) {
|
2014-05-03 01:32:20 +04:00
|
|
|
// Otherwise this is the <img> tag itself
|
2017-12-07 21:13:50 +03:00
|
|
|
MOZ_ASSERT(aSourceElement == this);
|
2017-10-03 07:30:34 +03:00
|
|
|
principal = mSrcsetTriggeringPrincipal;
|
2014-05-03 01:32:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Skip if has no srcset or an empty srcset
|
|
|
|
nsString srcset;
|
2017-12-07 21:13:50 +03:00
|
|
|
if (!aSourceElement->GetAttr(kNameSpaceID_None, nsGkAtoms::srcset, srcset)) {
|
2014-05-03 01:32:20 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (srcset.IsEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to parse
|
2017-12-07 21:13:50 +03:00
|
|
|
RefPtr<ResponsiveImageSelector> sel =
|
|
|
|
new ResponsiveImageSelector(aSourceElement);
|
2017-10-03 07:30:34 +03:00
|
|
|
if (!sel->SetCandidatesFromSourceSet(srcset, principal)) {
|
2014-05-03 01:32:20 +04:00
|
|
|
// No possible candidates, don't need to bother parsing sizes
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-03 03:26:22 +04:00
|
|
|
|
2016-04-16 23:50:02 +03:00
|
|
|
nsAutoString sizes;
|
2017-12-07 21:13:50 +03:00
|
|
|
aSourceElement->GetAttr(kNameSpaceID_None, nsGkAtoms::sizes, sizes);
|
2016-04-16 23:50:02 +03:00
|
|
|
sel->SetSizesFromDescriptor(sizes);
|
2014-05-03 01:32:20 +04:00
|
|
|
|
|
|
|
// If this is the <img> tag, also pull in src as the default source
|
|
|
|
if (!isSourceTag) {
|
2017-12-07 21:13:50 +03:00
|
|
|
MOZ_ASSERT(aSourceElement == this);
|
2014-05-03 01:32:20 +04:00
|
|
|
nsAutoString src;
|
|
|
|
if (GetAttr(kNameSpaceID_None, nsGkAtoms::src, src) && !src.IsEmpty()) {
|
2017-10-03 07:30:34 +03:00
|
|
|
sel->SetDefaultSource(src, mSrcTriggeringPrincipal);
|
2014-05-03 01:32:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mResponsiveSelector = sel;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
bool HTMLImageElement::SelectSourceForTagWithAttrs(
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* aDocument, bool aIsSourceTag, const nsAString& aSrcAttr,
|
2014-12-11 02:53:00 +03:00
|
|
|
const nsAString& aSrcsetAttr, const nsAString& aSizesAttr,
|
|
|
|
const nsAString& aTypeAttr, const nsAString& aMediaAttr,
|
|
|
|
nsAString& aResult) {
|
|
|
|
MOZ_ASSERT(aIsSourceTag || (aTypeAttr.IsEmpty() && aMediaAttr.IsEmpty()),
|
|
|
|
"Passing type or media attrs makes no sense without aIsSourceTag");
|
|
|
|
MOZ_ASSERT(!aIsSourceTag || aSrcAttr.IsEmpty(),
|
|
|
|
"Passing aSrcAttr makes no sense with aIsSourceTag set");
|
|
|
|
|
2016-04-17 01:07:56 +03:00
|
|
|
if (aSrcsetAttr.IsEmpty()) {
|
2014-12-11 02:53:00 +03:00
|
|
|
if (!aIsSourceTag) {
|
|
|
|
// For an <img> with no srcset, we would always select the src attr.
|
|
|
|
aResult.Assign(aSrcAttr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Otherwise, a <source> without srcset is never selected
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Would not consider source tags with unsupported media or type
|
|
|
|
if (aIsSourceTag &&
|
|
|
|
((!aMediaAttr.IsVoid() && !HTMLSourceElement::WouldMatchMediaForDocument(
|
|
|
|
aMediaAttr, aDocument)) ||
|
|
|
|
(!aTypeAttr.IsVoid() && !SupportedPictureSourceType(aTypeAttr)))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using srcset or picture <source>, build a responsive selector for this tag.
|
|
|
|
RefPtr<ResponsiveImageSelector> sel = new ResponsiveImageSelector(aDocument);
|
|
|
|
|
|
|
|
sel->SetCandidatesFromSourceSet(aSrcsetAttr);
|
2016-04-17 01:07:56 +03:00
|
|
|
if (!aSizesAttr.IsEmpty()) {
|
2014-12-11 02:53:00 +03:00
|
|
|
sel->SetSizesFromDescriptor(aSizesAttr);
|
|
|
|
}
|
|
|
|
if (!aIsSourceTag) {
|
|
|
|
sel->SetDefaultSource(aSrcAttr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sel->GetSelectedImageURLSpec(aResult)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aIsSourceTag) {
|
|
|
|
// <img> tag with no match would definitively load nothing.
|
|
|
|
aResult.Truncate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// <source> tags with no match would leave source yet-undetermined.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-03 01:32:20 +04:00
|
|
|
void HTMLImageElement::DestroyContent() {
|
|
|
|
mResponsiveSelector = nullptr;
|
2015-12-04 01:03:12 +03:00
|
|
|
|
|
|
|
nsGenericHTMLElement::DestroyContent();
|
2014-05-03 03:26:22 +04:00
|
|
|
}
|
|
|
|
|
2015-05-12 01:07:49 +03:00
|
|
|
void HTMLImageElement::MediaFeatureValuesChanged() {
|
2015-08-31 22:29:40 +03:00
|
|
|
QueueImageLoadTask(false);
|
2015-05-12 01:07:49 +03:00
|
|
|
}
|
|
|
|
|
2020-02-13 00:31:48 +03:00
|
|
|
bool HTMLImageElement::ShouldLoadImage() const {
|
|
|
|
return OwnerDoc()->ShouldLoadImages() && !mLazyLoading;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTMLImageElement::SetLazyLoading() {
|
|
|
|
if (mLazyLoading) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-13 14:03:12 +03:00
|
|
|
if (!StaticPrefs::dom_image_lazy_loading_enabled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-13 00:31:48 +03:00
|
|
|
// If scripting is disabled don't do lazy load.
|
|
|
|
// https://whatpr.org/html/3752/images.html#updating-the-image-data
|
|
|
|
if (!OwnerDoc()->IsScriptEnabled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// There (maybe) is a race condition that we have no LazyLoadImageObserver
|
|
|
|
// when the root document has been removed from the docshell.
|
|
|
|
// In the case we don't need to worry about lazy-loading.
|
2020-03-05 00:34:20 +03:00
|
|
|
OwnerDoc()->EnsureLazyLoadImageObserver().Observe(*this);
|
|
|
|
mLazyLoading = true;
|
|
|
|
UpdateImageState(true);
|
2020-02-13 00:31:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void HTMLImageElement::StartLoadingIfNeeded() {
|
|
|
|
if (LoadingEnabled() && ShouldLoadImage()) {
|
|
|
|
// Use script runner for the case the adopt is from appendChild.
|
|
|
|
// Bug 1076583 - We still behave synchronously in the non-responsive case
|
|
|
|
nsContentUtils::AddScriptRunner(
|
|
|
|
(InResponsiveMode())
|
|
|
|
? NewRunnableMethod<bool>(
|
|
|
|
"dom::HTMLImageElement::QueueImageLoadTask", this,
|
|
|
|
&HTMLImageElement::QueueImageLoadTask, true)
|
|
|
|
: NewRunnableMethod<bool>("dom::HTMLImageElement::MaybeLoadImage",
|
|
|
|
this, &HTMLImageElement::MaybeLoadImage,
|
|
|
|
true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTMLImageElement::StopLazyLoadingAndStartLoadIfNeeded() {
|
|
|
|
if (!mLazyLoading) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mLazyLoading = false;
|
2020-03-05 00:34:20 +03:00
|
|
|
OwnerDoc()->GetLazyLoadImageObserver()->Unobserve(*this);
|
2020-02-13 00:31:48 +03:00
|
|
|
StartLoadingIfNeeded();
|
|
|
|
}
|
|
|
|
|
2012-12-31 21:25:46 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|