2015-05-03 19:32:37 +00: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 11:12:37 +00: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 05:50:08 +00:00
|
|
|
|
2012-12-31 17:25:46 +00:00
|
|
|
#include "mozilla/dom/HTMLImageElement.h"
|
2012-12-31 18:48:55 +00:00
|
|
|
#include "mozilla/dom/HTMLImageElementBinding.h"
|
2007-01-30 00:06:41 +00:00
|
|
|
#include "nsGkAtoms.h"
|
1998-09-03 01:03:33 +00:00
|
|
|
#include "nsStyleConsts.h"
|
2004-07-31 23:15:21 +00:00
|
|
|
#include "nsPresContext.h"
|
2004-01-26 19:22:05 +00:00
|
|
|
#include "nsMappedAttributes.h"
|
1999-05-07 00:08:21 +00:00
|
|
|
#include "nsSize.h"
|
2015-03-17 19:25:35 +00:00
|
|
|
#include "nsDocument.h"
|
1999-05-07 00:08:21 +00:00
|
|
|
#include "nsIDocument.h"
|
2013-08-21 12:44:14 +00:00
|
|
|
#include "nsIDOMMutationEvent.h"
|
1999-05-07 00:08:21 +00:00
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
#include "nsIURL.h"
|
1999-06-18 17:34:08 +00:00
|
|
|
#include "nsIIOService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
2001-02-19 12:55:42 +00:00
|
|
|
#include "nsContentUtils.h"
|
2014-05-24 22:20:39 +00:00
|
|
|
#include "nsContainerFrame.h"
|
2000-05-10 13:13:39 +00:00
|
|
|
#include "nsNodeInfoManager.h"
|
2013-09-25 11:21:18 +00:00
|
|
|
#include "mozilla/MouseEvents.h"
|
2001-05-21 22:40:10 +00:00
|
|
|
#include "nsContentPolicyUtils.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
2010-08-09 16:15:47 +00:00
|
|
|
#include "nsFocusManager.h"
|
2013-06-19 14:24:37 +00:00
|
|
|
#include "mozilla/dom/HTMLFormElement.h"
|
2014-07-01 20:36:06 +00:00
|
|
|
#include "nsAttrValueOrString.h"
|
2014-10-08 20:53:24 +00:00
|
|
|
#include "imgLoader.h"
|
2015-03-17 19:25:35 +00:00
|
|
|
#include "Image.h"
|
1999-12-18 20:29:29 +00:00
|
|
|
|
2014-05-02 21:32:20 +00:00
|
|
|
// Responsive images!
|
|
|
|
#include "mozilla/dom/HTMLSourceElement.h"
|
|
|
|
#include "mozilla/dom/ResponsiveImageSelector.h"
|
|
|
|
|
2001-03-22 01:42:01 +00:00
|
|
|
#include "imgIContainer.h"
|
|
|
|
#include "imgILoader.h"
|
2012-10-12 16:11:22 +00:00
|
|
|
#include "imgINotificationObserver.h"
|
2012-10-12 12:43:01 +00:00
|
|
|
#include "imgRequestProxy.h"
|
2001-03-22 01:42:01 +00:00
|
|
|
|
|
|
|
#include "nsILoadGroup.h"
|
1998-09-03 01:03:33 +00:00
|
|
|
|
2004-07-20 06:11:27 +00:00
|
|
|
#include "nsRuleData.h"
|
2001-05-31 22:19:43 +00:00
|
|
|
|
2004-07-24 21:12:43 +00:00
|
|
|
#include "nsIDOMHTMLMapElement.h"
|
2014-03-18 04:48:21 +00:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2014-04-03 04:18:36 +00:00
|
|
|
#include "mozilla/EventStates.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 16:46:42 +00:00
|
|
|
|
2007-02-18 10:43:12 +00:00
|
|
|
#include "nsLayoutUtils.h"
|
|
|
|
|
2014-05-02 23:26:22 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
static const char *kPrefSrcsetEnabled = "dom.image.srcset.enabled";
|
|
|
|
|
2013-06-19 14:24:37 +00:00
|
|
|
NS_IMPL_NS_NEW_HTML_ELEMENT(Image)
|
1998-09-03 01:03:33 +00:00
|
|
|
|
2014-10-02 03:59:56 +00:00
|
|
|
#ifdef DEBUG
|
2014-05-02 21:32:20 +00: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()) {
|
|
|
|
return parent->IndexOf(aSubject) < parent->IndexOf(aNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-02 03:59:56 +00:00
|
|
|
#endif
|
2014-05-02 21:32:20 +00:00
|
|
|
|
2012-12-31 17:25:46 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-10-02 03:30:49 +00: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
|
|
|
|
class ImageLoadTask : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit ImageLoadTask(HTMLImageElement *aElement) :
|
|
|
|
mElement(aElement)
|
2015-06-06 13:21:56 +00:00
|
|
|
{
|
|
|
|
mDocument = aElement->OwnerDoc();
|
|
|
|
mDocument->BlockOnload();
|
|
|
|
}
|
2014-10-02 03:30:49 +00:00
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
if (mElement->mPendingImageLoadTask == this) {
|
|
|
|
mElement->mPendingImageLoadTask = nullptr;
|
|
|
|
mElement->LoadSelectedImage(true, true);
|
|
|
|
}
|
2015-06-06 13:21:56 +00:00
|
|
|
mDocument->UnblockOnload(false);
|
2014-10-02 03:30:49 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~ImageLoadTask() {}
|
|
|
|
nsRefPtr<HTMLImageElement> mElement;
|
2015-06-06 13:21:56 +00:00
|
|
|
nsCOMPtr<nsIDocument> mDocument;
|
2014-10-02 03:30:49 +00:00
|
|
|
};
|
|
|
|
|
2014-06-20 02:01:40 +00:00
|
|
|
HTMLImageElement::HTMLImageElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
|
2004-05-18 20:58:12 +00:00
|
|
|
: nsGenericHTMLElement(aNodeInfo)
|
2013-06-18 12:53:23 +00:00
|
|
|
, mForm(nullptr)
|
1998-09-03 01:03:33 +00:00
|
|
|
{
|
2011-06-01 01:46:57 +00:00
|
|
|
// We start out broken
|
|
|
|
AddStatesSilently(NS_EVENT_STATE_BROKEN);
|
1998-09-03 01:03:33 +00:00
|
|
|
}
|
|
|
|
|
2012-12-31 17:25:46 +00:00
|
|
|
HTMLImageElement::~HTMLImageElement()
|
1998-09-03 01:03:33 +00:00
|
|
|
{
|
2006-06-01 18:35:21 +00:00
|
|
|
DestroyImageLoadingContent();
|
1998-09-03 01:03:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-31 17:25:46 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(HTMLImageElement, Element)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(HTMLImageElement, Element)
|
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 16:46:42 +00:00
|
|
|
|
2014-10-13 20:36:01 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(HTMLImageElement,
|
|
|
|
nsGenericHTMLElement,
|
|
|
|
mResponsiveSelector)
|
2010-01-12 13:08:43 +00:00
|
|
|
|
2012-12-31 17:25:46 +00:00
|
|
|
// QueryInterface implementation for HTMLImageElement
|
2013-06-18 12:53:23 +00:00
|
|
|
NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(HTMLImageElement)
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_INTERFACE_TABLE_INHERITED(HTMLImageElement,
|
|
|
|
nsIDOMHTMLImageElement,
|
|
|
|
nsIImageLoadingContent,
|
|
|
|
imgIOnloadBlocker,
|
|
|
|
imgINotificationObserver)
|
2013-08-07 20:23:08 +00:00
|
|
|
NS_INTERFACE_TABLE_TAIL_INHERITING(nsGenericHTMLElement)
|
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 16:46:42 +00:00
|
|
|
|
|
|
|
|
2012-12-31 17:25:46 +00:00
|
|
|
NS_IMPL_ELEMENT_CLONE(HTMLImageElement)
|
1998-09-03 01:03:33 +00:00
|
|
|
|
2000-12-23 10:56:31 +00:00
|
|
|
|
2012-12-31 17:25:46 +00:00
|
|
|
NS_IMPL_STRING_ATTR(HTMLImageElement, Name, name)
|
|
|
|
NS_IMPL_STRING_ATTR(HTMLImageElement, Align, align)
|
|
|
|
NS_IMPL_STRING_ATTR(HTMLImageElement, Alt, alt)
|
|
|
|
NS_IMPL_STRING_ATTR(HTMLImageElement, Border, border)
|
|
|
|
NS_IMPL_INT_ATTR(HTMLImageElement, Hspace, hspace)
|
|
|
|
NS_IMPL_BOOL_ATTR(HTMLImageElement, IsMap, ismap)
|
|
|
|
NS_IMPL_URI_ATTR(HTMLImageElement, LongDesc, longdesc)
|
2014-03-19 21:18:57 +00:00
|
|
|
NS_IMPL_STRING_ATTR(HTMLImageElement, Sizes, sizes)
|
2012-12-31 17:25:46 +00:00
|
|
|
NS_IMPL_STRING_ATTR(HTMLImageElement, Lowsrc, lowsrc)
|
|
|
|
NS_IMPL_URI_ATTR(HTMLImageElement, Src, src)
|
2014-03-19 21:18:24 +00:00
|
|
|
NS_IMPL_STRING_ATTR(HTMLImageElement, Srcset, srcset)
|
2012-12-31 17:25:46 +00:00
|
|
|
NS_IMPL_STRING_ATTR(HTMLImageElement, UseMap, usemap)
|
|
|
|
NS_IMPL_INT_ATTR(HTMLImageElement, Vspace, vspace)
|
2000-01-04 03:06:13 +00:00
|
|
|
|
2015-01-20 20:39:28 +00:00
|
|
|
bool
|
2015-03-17 20:42:14 +00:00
|
|
|
HTMLImageElement::IsInteractiveHTMLContent(bool aIgnoreTabindex) const
|
2015-01-20 20:39:28 +00:00
|
|
|
{
|
2015-03-23 09:02:33 +00:00
|
|
|
return HasAttr(kNameSpaceID_None, nsGkAtoms::usemap) ||
|
|
|
|
nsGenericHTMLElement::IsInteractiveHTMLContent(aIgnoreTabindex);
|
2015-01-20 20:39:28 +00:00
|
|
|
}
|
|
|
|
|
2014-05-02 23:26:22 +00:00
|
|
|
bool
|
|
|
|
HTMLImageElement::IsSrcsetEnabled()
|
|
|
|
{
|
|
|
|
return Preferences::GetBool(kPrefSrcsetEnabled, false);
|
|
|
|
}
|
|
|
|
|
2014-05-13 21:21:15 +00:00
|
|
|
nsresult
|
|
|
|
HTMLImageElement::GetCurrentSrc(nsAString& aValue)
|
|
|
|
{
|
|
|
|
if (!IsSrcsetEnabled()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> currentURI;
|
|
|
|
GetCurrentURI(getter_AddRefs(currentURI));
|
|
|
|
if (currentURI) {
|
|
|
|
nsAutoCString spec;
|
|
|
|
currentURI->GetSpec(spec);
|
|
|
|
CopyUTF8toUTF16(spec, aValue);
|
|
|
|
} else {
|
|
|
|
SetDOMStringToNull(aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-04 23:49:57 +00:00
|
|
|
void
|
2015-02-13 01:27:39 +00:00
|
|
|
HTMLImageElement::GetItemValueText(DOMString& aValue)
|
2012-06-04 23:49:57 +00:00
|
|
|
{
|
|
|
|
GetSrc(aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-31 17:25:46 +00:00
|
|
|
HTMLImageElement::SetItemValueText(const nsAString& aValue)
|
2012-06-04 23:49:57 +00:00
|
|
|
{
|
|
|
|
SetSrc(aValue);
|
|
|
|
}
|
|
|
|
|
2012-10-06 07:19:52 +00:00
|
|
|
bool
|
2012-12-31 17:25:46 +00:00
|
|
|
HTMLImageElement::Draggable() const
|
2008-08-27 12:07:27 +00:00
|
|
|
{
|
|
|
|
// images may be dragged unless the draggable attribute is false
|
2012-10-06 07:19:52 +00:00
|
|
|
return !AttrValueIs(kNameSpaceID_None, nsGkAtoms::draggable,
|
|
|
|
nsGkAtoms::_false, eIgnoreCase);
|
2008-08-27 12:07:27 +00:00
|
|
|
}
|
|
|
|
|
2012-12-31 18:48:55 +00:00
|
|
|
bool
|
|
|
|
HTMLImageElement::Complete()
|
2000-01-06 23:41:14 +00:00
|
|
|
{
|
2003-03-19 03:47:09 +00:00
|
|
|
if (!mCurrentRequest) {
|
2012-12-31 18:48:55 +00:00
|
|
|
return true;
|
2000-01-06 23:41:14 +00:00
|
|
|
}
|
|
|
|
|
2015-02-05 02:05:17 +00:00
|
|
|
if (mPendingRequest) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t status;
|
2003-03-19 03:47:09 +00:00
|
|
|
mCurrentRequest->GetImageStatus(&status);
|
2012-12-31 18:48:55 +00:00
|
|
|
return
|
2003-05-03 02:32:43 +00:00
|
|
|
(status &
|
|
|
|
(imgIRequest::STATUS_LOAD_COMPLETE | imgIRequest::STATUS_ERROR)) != 0;
|
2012-12-31 18:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
HTMLImageElement::GetComplete(bool* aComplete)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aComplete, "Null out param!");
|
|
|
|
|
|
|
|
*aComplete = Complete();
|
2003-03-19 03:47:09 +00:00
|
|
|
|
2000-07-12 03:35:13 +00:00
|
|
|
return NS_OK;
|
2000-01-06 23:41:14 +00:00
|
|
|
}
|
|
|
|
|
2013-06-12 07:00:08 +00:00
|
|
|
CSSIntPoint
|
2012-12-31 17:25:46 +00:00
|
|
|
HTMLImageElement::GetXY()
|
2012-04-24 19:04:37 +00:00
|
|
|
{
|
|
|
|
nsIFrame* frame = GetPrimaryFrame(Flush_Layout);
|
|
|
|
if (!frame) {
|
2013-06-12 07:00:08 +00:00
|
|
|
return CSSIntPoint(0, 0);
|
2012-04-24 19:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIFrame* layer = nsLayoutUtils::GetClosestLayer(frame->GetParent());
|
2013-06-12 07:00:08 +00:00
|
|
|
return CSSIntPoint::FromAppUnitsRounded(frame->GetOffsetTo(layer));
|
2012-04-24 19:04:37 +00:00
|
|
|
}
|
|
|
|
|
2013-06-02 15:38:25 +00:00
|
|
|
int32_t
|
|
|
|
HTMLImageElement::X()
|
|
|
|
{
|
|
|
|
return GetXY().x;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
HTMLImageElement::Y()
|
|
|
|
{
|
|
|
|
return GetXY().y;
|
|
|
|
}
|
|
|
|
|
2012-04-24 19:04:37 +00:00
|
|
|
NS_IMETHODIMP
|
2012-12-31 17:25:46 +00:00
|
|
|
HTMLImageElement::GetX(int32_t* aX)
|
2012-04-24 19:04:37 +00:00
|
|
|
{
|
2013-06-02 15:38:25 +00:00
|
|
|
*aX = X();
|
2012-04-24 19:04:37 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-12-31 17:25:46 +00:00
|
|
|
HTMLImageElement::GetY(int32_t* aY)
|
2012-04-24 19:04:37 +00:00
|
|
|
{
|
2013-06-02 15:38:25 +00:00
|
|
|
*aY = Y();
|
2012-04-24 19:04:37 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-01-03 06:05:32 +00:00
|
|
|
NS_IMETHODIMP
|
2012-12-31 17:25:46 +00:00
|
|
|
HTMLImageElement::GetHeight(uint32_t* aHeight)
|
2002-01-03 06:05:32 +00:00
|
|
|
{
|
2012-12-31 18:48:55 +00:00
|
|
|
*aHeight = Height();
|
2004-01-09 23:54:21 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2002-01-03 06:05:32 +00:00
|
|
|
}
|
|
|
|
|
2000-06-07 01:23:55 +00:00
|
|
|
NS_IMETHODIMP
|
2012-12-31 17:25:46 +00:00
|
|
|
HTMLImageElement::SetHeight(uint32_t aHeight)
|
2000-06-07 01:23:55 +00:00
|
|
|
{
|
2013-04-04 07:03:50 +00:00
|
|
|
ErrorResult rv;
|
|
|
|
SetHeight(aHeight, rv);
|
2015-04-27 13:18:51 +00:00
|
|
|
return rv.StealNSResult();
|
2000-01-04 03:06:13 +00:00
|
|
|
}
|
|
|
|
|
2000-06-07 01:23:55 +00:00
|
|
|
NS_IMETHODIMP
|
2012-12-31 17:25:46 +00:00
|
|
|
HTMLImageElement::GetWidth(uint32_t* aWidth)
|
2000-06-07 01:23:55 +00:00
|
|
|
{
|
2012-12-31 18:48:55 +00:00
|
|
|
*aWidth = Width();
|
2004-01-09 23:54:21 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2000-06-07 01:23:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-12-31 17:25:46 +00:00
|
|
|
HTMLImageElement::SetWidth(uint32_t aWidth)
|
2000-06-07 01:23:55 +00:00
|
|
|
{
|
2013-04-04 07:03:50 +00:00
|
|
|
ErrorResult rv;
|
|
|
|
SetWidth(aWidth, rv);
|
2015-04-27 13:18:51 +00:00
|
|
|
return rv.StealNSResult();
|
2000-06-07 01:23:55 +00:00
|
|
|
}
|
1998-09-03 01:03:33 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2012-12-31 17:25:46 +00:00
|
|
|
HTMLImageElement::ParseAttribute(int32_t aNamespaceID,
|
|
|
|
nsIAtom* aAttribute,
|
|
|
|
const nsAString& aValue,
|
|
|
|
nsAttrValue& aResult)
|
1998-09-03 01:03:33 +00:00
|
|
|
{
|
2005-11-29 16:37:15 +00:00
|
|
|
if (aNamespaceID == kNameSpaceID_None) {
|
2006-12-26 17:47:52 +00:00
|
|
|
if (aAttribute == nsGkAtoms::align) {
|
2005-11-29 16:37:15 +00:00
|
|
|
return ParseAlignValue(aValue, aResult);
|
|
|
|
}
|
2011-07-17 17:40:22 +00:00
|
|
|
if (aAttribute == nsGkAtoms::crossorigin) {
|
2012-03-10 16:13:51 +00:00
|
|
|
ParseCORSValue(aValue, aResult);
|
|
|
|
return true;
|
2011-07-14 18:47:35 +00:00
|
|
|
}
|
2005-11-29 16:37:15 +00:00
|
|
|
if (ParseImageAttribute(aAttribute, aValue, aResult)) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2005-11-29 16:37:15 +00:00
|
|
|
}
|
1998-09-03 01:03:33 +00:00
|
|
|
}
|
2000-12-23 10:56:31 +00:00
|
|
|
|
2005-11-29 16:37:15 +00:00
|
|
|
return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
|
|
|
|
aResult);
|
1998-09-03 01:03:33 +00:00
|
|
|
}
|
|
|
|
|
2013-11-19 19:21:29 +00:00
|
|
|
void
|
|
|
|
HTMLImageElement::MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
|
|
|
|
nsRuleData* aData)
|
1998-09-03 01:03:33 +00:00
|
|
|
{
|
2003-07-11 21:16:12 +00:00
|
|
|
nsGenericHTMLElement::MapImageAlignAttributeInto(aAttributes, aData);
|
2001-05-31 22:19:43 +00:00
|
|
|
nsGenericHTMLElement::MapImageBorderAttributeInto(aAttributes, aData);
|
|
|
|
nsGenericHTMLElement::MapImageMarginAttributeInto(aAttributes, aData);
|
2003-07-11 21:16:12 +00:00
|
|
|
nsGenericHTMLElement::MapImageSizeAttributesInto(aAttributes, aData);
|
2001-05-31 22:19:43 +00:00
|
|
|
nsGenericHTMLElement::MapCommonAttributesInto(aAttributes, aData);
|
1998-09-05 04:00:06 +00:00
|
|
|
}
|
|
|
|
|
2004-10-24 17:10:32 +00:00
|
|
|
nsChangeHint
|
2012-12-31 17:25:46 +00:00
|
|
|
HTMLImageElement::GetAttributeChangeHint(const nsIAtom* aAttribute,
|
|
|
|
int32_t aModType) const
|
1999-07-07 01:24:40 +00:00
|
|
|
{
|
2004-10-24 17:10:32 +00:00
|
|
|
nsChangeHint retval =
|
|
|
|
nsGenericHTMLElement::GetAttributeChangeHint(aAttribute, aModType);
|
2006-12-26 17:47:52 +00:00
|
|
|
if (aAttribute == nsGkAtoms::usemap ||
|
|
|
|
aAttribute == nsGkAtoms::ismap) {
|
2004-10-24 17:10:32 +00:00
|
|
|
NS_UpdateHint(retval, NS_STYLE_HINT_FRAMECHANGE);
|
2013-08-21 12:44:14 +00:00
|
|
|
} else if (aAttribute == nsGkAtoms::alt) {
|
|
|
|
if (aModType == nsIDOMMutationEvent::ADDITION ||
|
|
|
|
aModType == nsIDOMMutationEvent::REMOVAL) {
|
|
|
|
NS_UpdateHint(retval, NS_STYLE_HINT_FRAMECHANGE);
|
|
|
|
}
|
2003-07-11 21:16:12 +00:00
|
|
|
}
|
2004-10-24 17:10:32 +00:00
|
|
|
return retval;
|
2003-07-11 21:16:12 +00:00
|
|
|
}
|
2003-04-16 20:54:20 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
NS_IMETHODIMP_(bool)
|
2012-12-31 17:25:46 +00:00
|
|
|
HTMLImageElement::IsAttributeMapped(const nsIAtom* aAttribute) const
|
2003-07-11 21:16:12 +00:00
|
|
|
{
|
2004-02-25 21:04:50 +00:00
|
|
|
static const MappedAttributeEntry* const map[] = {
|
2003-04-16 20:54:20 +00:00
|
|
|
sCommonAttributeMap,
|
2003-07-11 21:16:12 +00:00
|
|
|
sImageMarginSizeAttributeMap,
|
|
|
|
sImageBorderAttributeMap,
|
|
|
|
sImageAlignAttributeMap
|
2003-04-16 20:54:20 +00:00
|
|
|
};
|
|
|
|
|
2011-12-18 10:09:27 +00:00
|
|
|
return FindAttributeDependence(aAttribute, map);
|
1999-07-07 01:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-12 19:45:38 +00:00
|
|
|
nsMapRuleToAttributesFunc
|
2012-12-31 17:25:46 +00:00
|
|
|
HTMLImageElement::GetAttributeMappingFunction() const
|
1998-09-05 04:00:06 +00:00
|
|
|
{
|
2005-01-12 19:45:38 +00:00
|
|
|
return &MapAttributesIntoRule;
|
1998-09-03 01:03:33 +00:00
|
|
|
}
|
|
|
|
|
1998-09-05 04:00:06 +00:00
|
|
|
|
2013-06-18 12:53:23 +00:00
|
|
|
nsresult
|
|
|
|
HTMLImageElement::BeforeSetAttr(int32_t aNameSpaceID, nsIAtom* aName,
|
2015-08-01 05:14:06 +00:00
|
|
|
nsAttrValueOrString* aValue,
|
2013-06-18 12:53:23 +00: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()) {
|
2013-06-18 12:54:27 +00:00
|
|
|
mForm->RemoveImageElementFromTable(this, tmp,
|
2013-06-19 14:24:37 +00:00
|
|
|
HTMLFormElement::AttributeUpdated);
|
2013-06-18 12:53:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsGenericHTMLElement::BeforeSetAttr(aNameSpaceID, aName,
|
|
|
|
aValue, aNotify);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
HTMLImageElement::AfterSetAttr(int32_t aNameSpaceID, nsIAtom* aName,
|
|
|
|
const nsAttrValue* aValue, bool aNotify)
|
|
|
|
{
|
|
|
|
if (aNameSpaceID == kNameSpaceID_None && mForm &&
|
|
|
|
(aName == nsGkAtoms::name || aName == nsGkAtoms::id) &&
|
|
|
|
aValue && !aValue->IsEmptyString()) {
|
|
|
|
// add the image to the hashtable as needed
|
2015-02-09 22:34:50 +00:00
|
|
|
MOZ_ASSERT(aValue->Type() == nsAttrValue::eAtom,
|
|
|
|
"Expected atom value for name/id");
|
2013-06-18 12:53:23 +00:00
|
|
|
mForm->AddImageElementToTable(this,
|
|
|
|
nsDependentAtomString(aValue->GetAtomValue()));
|
|
|
|
}
|
|
|
|
|
2014-05-02 23:26:22 +00:00
|
|
|
// Handle src/srcset/crossorigin updates. 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 image load from BindToTree.
|
2014-05-02 21:32:20 +00:00
|
|
|
|
2014-07-01 20:36:06 +00:00
|
|
|
nsAttrValueOrString attrVal(aValue);
|
|
|
|
|
2014-05-02 23:26:22 +00:00
|
|
|
if (aName == nsGkAtoms::src &&
|
2014-10-02 03:30:49 +00:00
|
|
|
aNameSpaceID == kNameSpaceID_None &&
|
|
|
|
!aValue) {
|
|
|
|
// SetAttr handles setting src since it needs to catch img.src =
|
|
|
|
// img.src, so we only need to handle the unset case
|
2014-10-02 03:59:56 +00:00
|
|
|
if (InResponsiveMode()) {
|
2015-03-10 21:38:30 +00:00
|
|
|
if (mResponsiveSelector &&
|
|
|
|
mResponsiveSelector->Content() == this) {
|
2014-12-10 23:53:00 +00:00
|
|
|
mResponsiveSelector->SetDefaultSource(NullString());
|
2014-10-02 03:30:49 +00:00
|
|
|
}
|
|
|
|
QueueImageLoadTask();
|
|
|
|
} else {
|
|
|
|
// Bug 1076583 - We still behave synchronously in the non-responsive case
|
2014-05-02 23:26:22 +00:00
|
|
|
CancelImageRequests(aNotify);
|
|
|
|
}
|
|
|
|
} else if (aName == nsGkAtoms::srcset &&
|
|
|
|
aNameSpaceID == kNameSpaceID_None &&
|
|
|
|
IsSrcsetEnabled()) {
|
2014-10-02 03:30:49 +00:00
|
|
|
PictureSourceSrcsetChanged(this, attrVal.String(), aNotify);
|
2014-05-02 21:32:20 +00:00
|
|
|
} else if (aName == nsGkAtoms::sizes &&
|
|
|
|
aNameSpaceID == kNameSpaceID_None &&
|
|
|
|
HTMLPictureElement::IsPictureEnabled()) {
|
2014-10-02 03:30:49 +00:00
|
|
|
PictureSourceSizesChanged(this, attrVal.String(), aNotify);
|
2014-05-02 23:26:22 +00:00
|
|
|
} else if (aName == nsGkAtoms::crossorigin &&
|
|
|
|
aNameSpaceID == kNameSpaceID_None &&
|
|
|
|
aNotify) {
|
2014-10-02 03:30:49 +00:00
|
|
|
// Force a new load of the image with the new cross origin policy.
|
2014-10-02 03:59:56 +00:00
|
|
|
if (InResponsiveMode()) {
|
2014-10-02 03:30:49 +00:00
|
|
|
// per spec, full selection runs when this changes, even though
|
|
|
|
// it doesn't directly affect the source selection
|
|
|
|
QueueImageLoadTask();
|
|
|
|
} else {
|
|
|
|
// Bug 1076583 - We still use the older synchronous algorithm in
|
2014-08-19 21:49:38 +00:00
|
|
|
// non-responsive mode. Force a new load of the image with the
|
|
|
|
// new cross origin policy.
|
|
|
|
ForceReload(aNotify);
|
2014-05-02 23:26:22 +00:00
|
|
|
}
|
2013-10-10 14:54:02 +00:00
|
|
|
}
|
|
|
|
|
2013-06-18 12:53:23 +00:00
|
|
|
return nsGenericHTMLElement::AfterSetAttr(aNameSpaceID, aName,
|
|
|
|
aValue, aNotify);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-09 23:54:21 +00:00
|
|
|
nsresult
|
2014-03-18 04:48:19 +00:00
|
|
|
HTMLImageElement::PreHandleEvent(EventChainPreVisitor& aVisitor)
|
1998-09-03 01:03:33 +00:00
|
|
|
{
|
2000-12-23 10:56:31 +00:00
|
|
|
// If we are a map and get a mouse click, don't let it be handled by
|
|
|
|
// the Generic Element as this could cause a click event to fire
|
|
|
|
// twice, once by the image frame for the map and once by the Anchor
|
|
|
|
// element. (bug 39723)
|
2013-10-28 09:03:19 +00:00
|
|
|
WidgetMouseEvent* mouseEvent = aVisitor.mEvent->AsMouseEvent();
|
|
|
|
if (mouseEvent && mouseEvent->IsLeftClickEvent()) {
|
2011-09-29 06:19:26 +00:00
|
|
|
bool isMap = false;
|
2000-06-14 15:02:18 +00:00
|
|
|
GetIsMap(&isMap);
|
|
|
|
if (isMap) {
|
2006-03-07 17:08:51 +00:00
|
|
|
aVisitor.mEventStatus = nsEventStatus_eConsumeNoDefault;
|
2000-06-14 15:02:18 +00:00
|
|
|
}
|
|
|
|
}
|
2006-03-07 17:08:51 +00:00
|
|
|
return nsGenericHTMLElement::PreHandleEvent(aVisitor);
|
1998-09-14 23:02:32 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2012-12-31 17:25:46 +00:00
|
|
|
HTMLImageElement::IsHTMLFocusable(bool aWithMouse,
|
|
|
|
bool *aIsFocusable, int32_t *aTabIndex)
|
2004-07-24 21:12:43 +00:00
|
|
|
{
|
2012-10-06 07:19:51 +00:00
|
|
|
int32_t tabIndex = TabIndex();
|
2004-07-24 21:12:43 +00:00
|
|
|
|
2004-08-10 10:22:36 +00:00
|
|
|
if (IsInDoc()) {
|
2004-07-24 21:12:43 +00:00
|
|
|
nsAutoString usemap;
|
|
|
|
GetUseMap(usemap);
|
2004-10-11 16:14:27 +00:00
|
|
|
// XXXbz which document should this be using? sXBL/XBL2 issue! I
|
2011-10-18 10:53:36 +00:00
|
|
|
// think that OwnerDoc() is right, since we don't want to
|
2004-10-11 16:14:27 +00:00
|
|
|
// assume stuff about the document we're bound to.
|
2011-10-18 11:19:44 +00:00
|
|
|
if (OwnerDoc()->FindImageMap(usemap)) {
|
2004-07-24 21:12:43 +00:00
|
|
|
if (aTabIndex) {
|
|
|
|
// Use tab index on individual map areas
|
|
|
|
*aTabIndex = (sTabFocusModel & eTabFocus_linksMask)? 0 : -1;
|
|
|
|
}
|
2004-07-30 13:02:31 +00:00
|
|
|
// Image map is not focusable itself, but flag as tabbable
|
|
|
|
// so that image map areas get walked into.
|
2011-10-17 14:59:28 +00:00
|
|
|
*aIsFocusable = false;
|
2008-04-30 00:11:04 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2004-07-24 21:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aTabIndex) {
|
|
|
|
// Can be in tab order if tabindex >=0 and form controls are tabbable.
|
|
|
|
*aTabIndex = (sTabFocusModel & eTabFocus_formElementsMask)? tabIndex : -1;
|
|
|
|
}
|
|
|
|
|
2014-10-02 03:30:49 +00:00
|
|
|
*aIsFocusable =
|
2010-06-21 12:37:34 +00:00
|
|
|
#ifdef XP_MACOSX
|
2010-08-09 16:15:47 +00:00
|
|
|
(!aWithMouse || nsFocusManager::sMouseFocusesFormControl) &&
|
2010-06-21 12:37:34 +00:00
|
|
|
#endif
|
|
|
|
(tabIndex >= 0 || HasAttr(kNameSpaceID_None, nsGkAtoms::tabindex));
|
2008-04-15 18:40:38 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2004-07-24 21:12:43 +00:00
|
|
|
}
|
|
|
|
|
2004-01-09 23:54:21 +00:00
|
|
|
nsresult
|
2012-12-31 17:25:46 +00:00
|
|
|
HTMLImageElement::SetAttr(int32_t aNameSpaceID, nsIAtom* aName,
|
|
|
|
nsIAtom* aPrefix, const nsAString& aValue,
|
|
|
|
bool aNotify)
|
2003-03-19 03:47:09 +00:00
|
|
|
{
|
2014-03-10 21:38:02 +00: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 03:30:49 +00: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-02 23:26:22 +00:00
|
|
|
//
|
2014-10-02 03:30:49 +00:00
|
|
|
// Both cases handle unsetting src in AfterSetAttr
|
|
|
|
if (aNameSpaceID == kNameSpaceID_None &&
|
2014-03-10 21:38:02 +00:00
|
|
|
aName == nsGkAtoms::src) {
|
|
|
|
|
2014-10-02 03:30:49 +00:00
|
|
|
// This is for dom.disable_image_src_set, which predates "srcset"
|
|
|
|
// as an attribute. See Bug 773429
|
2014-03-10 21:38:02 +00:00
|
|
|
if (nsContentUtils::IsImageSrcSetDisabled()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-02 03:59:56 +00:00
|
|
|
if (InResponsiveMode()) {
|
2014-10-02 03:30:49 +00:00
|
|
|
if (mResponsiveSelector &&
|
|
|
|
mResponsiveSelector->Content() == this) {
|
|
|
|
mResponsiveSelector->SetDefaultSource(aValue);
|
|
|
|
}
|
|
|
|
QueueImageLoadTask();
|
|
|
|
} else if (aNotify) {
|
|
|
|
// 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
|
2014-03-10 21:38:02 +00:00
|
|
|
|
2014-10-02 03:30:49 +00:00
|
|
|
// A hack to get animations to reset. See bug 594771.
|
|
|
|
mNewRequestsWillNeedAnimationReset = true;
|
2014-03-10 21:38:02 +00:00
|
|
|
|
2014-10-02 03:30:49 +00:00
|
|
|
// Force image loading here, so that we'll try to load the image from
|
|
|
|
// network if it's set to be not cacheable... If we change things so that
|
|
|
|
// the state gets in Element's attr-setting happen around this
|
|
|
|
// LoadImage call, we could start passing false instead of aNotify
|
|
|
|
// here.
|
2014-08-19 21:49:38 +00:00
|
|
|
LoadImage(aValue, true, aNotify, eImageLoadType_Normal);
|
2014-10-02 03:30:49 +00:00
|
|
|
|
|
|
|
mNewRequestsWillNeedAnimationReset = false;
|
|
|
|
}
|
2014-03-10 21:38:02 +00:00
|
|
|
}
|
|
|
|
|
2004-02-10 19:36:43 +00:00
|
|
|
return nsGenericHTMLElement::SetAttr(aNameSpaceID, aName, aPrefix, aValue,
|
|
|
|
aNotify);
|
2003-03-19 03:47:09 +00:00
|
|
|
}
|
|
|
|
|
2005-04-05 23:54:35 +00:00
|
|
|
nsresult
|
2012-12-31 17:25:46 +00:00
|
|
|
HTMLImageElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
|
|
|
|
nsIContent* aBindingParent,
|
|
|
|
bool aCompileEventHandlers)
|
2003-07-12 22:32:20 +00:00
|
|
|
{
|
2005-04-05 23:54:35 +00:00
|
|
|
nsresult rv = nsGenericHTMLElement::BindToTree(aDocument, aParent,
|
|
|
|
aBindingParent,
|
|
|
|
aCompileEventHandlers);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-13 22:11:50 +00:00
|
|
|
nsImageLoadingContent::BindToTree(aDocument, aParent, aBindingParent,
|
|
|
|
aCompileEventHandlers);
|
|
|
|
|
2013-06-18 12:53:23 +00:00
|
|
|
if (aParent) {
|
|
|
|
UpdateFormOwner();
|
|
|
|
}
|
|
|
|
|
2015-03-03 11:08:59 +00:00
|
|
|
bool addedToPicture = aParent && aParent->IsHTMLElement(nsGkAtoms::picture) &&
|
2014-05-02 21:32:20 +00:00
|
|
|
HTMLPictureElement::IsPictureEnabled();
|
2014-10-02 03:30:49 +00:00
|
|
|
if (addedToPicture) {
|
2015-05-11 22:07:49 +00:00
|
|
|
if (aDocument) {
|
|
|
|
aDocument->AddResponsiveContent(this);
|
|
|
|
}
|
2014-10-02 03:30:49 +00:00
|
|
|
QueueImageLoadTask();
|
2014-10-02 03:59:56 +00:00
|
|
|
} else if (!InResponsiveMode() &&
|
2014-10-02 03:30:49 +00: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.
|
|
|
|
|
2011-06-01 01:46:57 +00:00
|
|
|
// FIXME: Bug 660963 it would be nice if we could just have
|
|
|
|
// ClearBrokenState update our state and do it fast...
|
2009-05-20 08:00:04 +00:00
|
|
|
ClearBrokenState();
|
2011-06-01 01:46:57 +00:00
|
|
|
RemoveStatesSilently(NS_EVENT_STATE_BROKEN);
|
2014-05-02 23:26:22 +00:00
|
|
|
|
2014-10-02 03:30:49 +00: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-02 23:26:22 +00:00
|
|
|
|
2009-11-20 04:39:11 +00:00
|
|
|
// If loading is temporarily disabled, don't even launch MaybeLoadImage.
|
|
|
|
// Otherwise MaybeLoadImage may run later when someone has reenabled
|
|
|
|
// loading.
|
|
|
|
if (LoadingEnabled()) {
|
|
|
|
nsContentUtils::AddScriptRunner(
|
2014-10-02 03:30:49 +00:00
|
|
|
NS_NewRunnableMethod(this, &HTMLImageElement::MaybeLoadImage));
|
2009-11-20 04:39:11 +00:00
|
|
|
}
|
2009-05-20 08:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-08-13 22:11:50 +00:00
|
|
|
void
|
2012-12-31 17:25:46 +00:00
|
|
|
HTMLImageElement::UnbindFromTree(bool aDeep, bool aNullParent)
|
2012-08-13 22:11:50 +00:00
|
|
|
{
|
2013-06-18 12:53:23 +00:00
|
|
|
if (mForm) {
|
|
|
|
if (aNullParent || !FindAncestorForm(mForm)) {
|
|
|
|
ClearForm(true);
|
|
|
|
} else {
|
|
|
|
UnsetFlags(MAYBE_ORPHAN_FORM_ELEMENT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 22:07:49 +00:00
|
|
|
if (GetParent() &&
|
2015-03-03 11:08:59 +00:00
|
|
|
GetParent()->IsHTMLElement(nsGkAtoms::picture) &&
|
2014-10-02 03:30:49 +00:00
|
|
|
HTMLPictureElement::IsPictureEnabled()) {
|
2015-05-11 22:07:49 +00:00
|
|
|
nsIDocument* doc = GetOurOwnerDoc();
|
|
|
|
if (doc) {
|
|
|
|
doc->RemoveResponsiveContent(this);
|
|
|
|
}
|
2014-10-02 03:30:49 +00:00
|
|
|
// Being removed from picture re-triggers selection, even if we
|
|
|
|
// weren't using a <source> peer
|
2015-05-11 22:07:49 +00:00
|
|
|
if (aNullParent) {
|
|
|
|
QueueImageLoadTask();
|
|
|
|
}
|
2014-10-02 03:30:49 +00:00
|
|
|
}
|
2014-05-02 23:26:22 +00:00
|
|
|
|
2012-08-13 22:11:50 +00:00
|
|
|
nsImageLoadingContent::UnbindFromTree(aDeep, aNullParent);
|
|
|
|
nsGenericHTMLElement::UnbindFromTree(aDeep, aNullParent);
|
|
|
|
}
|
|
|
|
|
2013-06-18 12:53:23 +00: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-20 08:00:04 +00:00
|
|
|
void
|
2012-12-31 17:25:46 +00:00
|
|
|
HTMLImageElement::MaybeLoadImage()
|
2009-05-20 08:00:04 +00:00
|
|
|
{
|
2014-05-02 23:26:22 +00: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 08:00:04 +00:00
|
|
|
// Note, check LoadingEnabled() after LoadImage call.
|
2014-05-02 23:26:22 +00:00
|
|
|
|
2014-10-02 03:30:49 +00:00
|
|
|
LoadSelectedImage(false, true);
|
2014-05-02 23:26:22 +00:00
|
|
|
|
2014-10-02 03:30:49 +00:00
|
|
|
if (!LoadingEnabled()) {
|
2011-10-17 14:59:28 +00:00
|
|
|
CancelImageRequests(true);
|
2003-07-12 22:32:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-03 04:18:36 +00:00
|
|
|
EventStates
|
2012-12-31 17:25:46 +00:00
|
|
|
HTMLImageElement::IntrinsicState() const
|
2005-09-18 18:05:40 +00:00
|
|
|
{
|
|
|
|
return nsGenericHTMLElement::IntrinsicState() |
|
|
|
|
nsImageLoadingContent::ImageState();
|
|
|
|
}
|
|
|
|
|
2013-01-22 10:53:13 +00:00
|
|
|
// static
|
|
|
|
already_AddRefed<HTMLImageElement>
|
|
|
|
HTMLImageElement::Image(const GlobalObject& aGlobal,
|
|
|
|
const Optional<uint32_t>& aWidth,
|
2013-08-23 05:17:08 +00:00
|
|
|
const Optional<uint32_t>& aHeight,
|
|
|
|
ErrorResult& aError)
|
1999-05-07 00:08:21 +00:00
|
|
|
{
|
2013-08-23 05:17:08 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(aGlobal.GetAsSupports());
|
2013-01-22 10:53:13 +00:00
|
|
|
nsIDocument* doc;
|
|
|
|
if (!win || !(doc = win->GetExtantDoc())) {
|
|
|
|
aError.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
1999-05-07 00:08:21 +00:00
|
|
|
}
|
|
|
|
|
2014-06-20 02:01:40 +00:00
|
|
|
already_AddRefed<mozilla::dom::NodeInfo> nodeInfo =
|
2013-01-22 10:53:13 +00:00
|
|
|
doc->NodeInfoManager()->GetNodeInfo(nsGkAtoms::img, nullptr,
|
|
|
|
kNameSpaceID_XHTML,
|
|
|
|
nsIDOMNode::ELEMENT_NODE);
|
2000-12-23 10:56:31 +00:00
|
|
|
|
2014-03-15 19:00:17 +00:00
|
|
|
nsRefPtr<HTMLImageElement> img = new HTMLImageElement(nodeInfo);
|
2001-09-07 04:00:37 +00:00
|
|
|
|
2013-01-22 10:53:13 +00:00
|
|
|
if (aWidth.WasPassed()) {
|
2013-04-04 07:03:50 +00:00
|
|
|
img->SetWidth(aWidth.Value(), aError);
|
2013-01-22 10:53:13 +00:00
|
|
|
if (aError.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2000-12-23 10:56:31 +00:00
|
|
|
|
2013-01-22 10:53:13 +00:00
|
|
|
if (aHeight.WasPassed()) {
|
2013-04-04 07:03:50 +00:00
|
|
|
img->SetHeight(aHeight.Value(), aError);
|
2013-01-22 10:53:13 +00:00
|
|
|
if (aError.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
1999-05-07 00:08:21 +00:00
|
|
|
}
|
|
|
|
|
2013-01-22 10:53:13 +00:00
|
|
|
return img.forget();
|
1999-05-07 00:08:21 +00:00
|
|
|
}
|
|
|
|
|
2012-12-31 18:48:55 +00:00
|
|
|
uint32_t
|
|
|
|
HTMLImageElement::NaturalHeight()
|
2000-09-08 02:06:56 +00:00
|
|
|
{
|
2014-05-03 00:05:19 +00:00
|
|
|
uint32_t height;
|
|
|
|
nsresult rv = nsImageLoadingContent::GetNaturalHeight(&height);
|
2012-12-31 18:48:55 +00:00
|
|
|
|
2014-05-03 00:05:19 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
MOZ_ASSERT(false, "GetNaturalHeight should not fail");
|
2012-12-31 18:48:55 +00:00
|
|
|
return 0;
|
2003-03-19 03:47:09 +00:00
|
|
|
}
|
2000-12-23 10:56:31 +00:00
|
|
|
|
2014-05-03 00:06:45 +00:00
|
|
|
if (mResponsiveSelector) {
|
|
|
|
double density = mResponsiveSelector->GetSelectedImageDensity();
|
2015-07-06 01:03:00 +00:00
|
|
|
MOZ_ASSERT(density >= 0.0);
|
2014-05-03 00:06:45 +00:00
|
|
|
height = NSToIntRound(double(height) / density);
|
|
|
|
height = std::max(height, 0u);
|
|
|
|
}
|
|
|
|
|
2014-05-03 00:05:19 +00:00
|
|
|
return height;
|
2000-09-08 02:06:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-12-31 18:48:55 +00:00
|
|
|
HTMLImageElement::GetNaturalHeight(uint32_t* aNaturalHeight)
|
2000-09-08 02:06:56 +00:00
|
|
|
{
|
2012-12-31 18:48:55 +00:00
|
|
|
*aNaturalHeight = NaturalHeight();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
HTMLImageElement::NaturalWidth()
|
|
|
|
{
|
2014-05-03 00:05:19 +00:00
|
|
|
uint32_t width;
|
|
|
|
nsresult rv = nsImageLoadingContent::GetNaturalWidth(&width);
|
2012-12-31 18:48:55 +00:00
|
|
|
|
2014-05-03 00:05:19 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
MOZ_ASSERT(false, "GetNaturalWidth should not fail");
|
2012-12-31 18:48:55 +00:00
|
|
|
return 0;
|
2003-03-19 03:47:09 +00:00
|
|
|
}
|
2000-12-23 10:56:31 +00:00
|
|
|
|
2014-05-03 00:06:45 +00:00
|
|
|
if (mResponsiveSelector) {
|
|
|
|
double density = mResponsiveSelector->GetSelectedImageDensity();
|
2015-07-06 01:03:00 +00:00
|
|
|
MOZ_ASSERT(density >= 0.0);
|
2014-05-03 00:06:45 +00:00
|
|
|
width = NSToIntRound(double(width) / density);
|
|
|
|
width = std::max(width, 0u);
|
|
|
|
}
|
|
|
|
|
2014-05-03 00:05:19 +00:00
|
|
|
return width;
|
2012-12-31 18:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
HTMLImageElement::GetNaturalWidth(uint32_t* aNaturalWidth)
|
|
|
|
{
|
|
|
|
*aNaturalWidth = NaturalWidth();
|
2001-09-07 04:00:37 +00:00
|
|
|
return NS_OK;
|
2000-09-08 02:06:56 +00:00
|
|
|
}
|
|
|
|
|
2009-12-11 04:02:13 +00:00
|
|
|
nsresult
|
2012-12-31 17:25:46 +00:00
|
|
|
HTMLImageElement::CopyInnerTo(Element* aDest)
|
2009-12-11 04:02:13 +00:00
|
|
|
{
|
2015-07-22 17:21:25 +00:00
|
|
|
bool destIsStatic = aDest->OwnerDoc()->IsStaticDocument();
|
|
|
|
auto dest = static_cast<HTMLImageElement*>(aDest);
|
|
|
|
if (destIsStatic) {
|
|
|
|
CreateStaticImageClone(dest);
|
2009-12-11 04:02:13 +00:00
|
|
|
}
|
2015-07-22 17:21:25 +00:00
|
|
|
|
|
|
|
nsresult rv = nsGenericHTMLElement::CopyInnerTo(aDest);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!destIsStatic) {
|
|
|
|
// In SetAttr (called from nsGenericHTMLElement::CopyInnerTo), dest skipped
|
|
|
|
// 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.
|
|
|
|
if (!dest->InResponsiveMode() &&
|
|
|
|
dest->HasAttr(kNameSpaceID_None, nsGkAtoms::src)) {
|
|
|
|
nsContentUtils::AddScriptRunner(
|
|
|
|
NS_NewRunnableMethod(dest, &HTMLImageElement::MaybeLoadImage));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2009-12-11 04:02:13 +00:00
|
|
|
}
|
2011-07-14 18:47:35 +00:00
|
|
|
|
2012-03-10 16:13:51 +00:00
|
|
|
CORSMode
|
2012-12-31 17:25:46 +00:00
|
|
|
HTMLImageElement::GetCORSMode()
|
2011-07-14 18:47:35 +00:00
|
|
|
{
|
2012-03-10 16:13:52 +00:00
|
|
|
return AttrValueToCORSMode(GetParsedAttr(nsGkAtoms::crossorigin));
|
2011-07-14 18:47:35 +00:00
|
|
|
}
|
2012-12-31 17:25:46 +00:00
|
|
|
|
2012-12-31 18:48:55 +00:00
|
|
|
JSObject*
|
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 14:13:33 +00:00
|
|
|
HTMLImageElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2012-12-31 18:48:55 +00: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 14:13:33 +00:00
|
|
|
return HTMLImageElementBinding::Wrap(aCx, this, aGivenProto);
|
2012-12-31 18:48:55 +00:00
|
|
|
}
|
|
|
|
|
2013-06-18 12:53:23 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
nsIDOMHTMLFormElement*
|
|
|
|
HTMLImageElement::GetForm() const
|
|
|
|
{
|
|
|
|
return mForm;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
HTMLImageElement::SetForm(nsIDOMHTMLFormElement* aForm)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aForm, "Don't pass null here");
|
|
|
|
NS_ASSERTION(!mForm,
|
|
|
|
"We don't support switching from one non-null form to another.");
|
|
|
|
|
2013-06-19 14:24:37 +00:00
|
|
|
mForm = static_cast<HTMLFormElement*>(aForm);
|
2013-06-18 12:53:23 +00: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()) {
|
2013-06-18 12:54:27 +00:00
|
|
|
mForm->RemoveImageElementFromTable(this, nameVal,
|
2013-06-19 14:24:37 +00:00
|
|
|
HTMLFormElement::ElementRemoved);
|
2013-06-18 12:53:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!idVal.IsEmpty()) {
|
2013-06-18 12:54:27 +00:00
|
|
|
mForm->RemoveImageElementFromTable(this, idVal,
|
2013-06-19 14:24:37 +00:00
|
|
|
HTMLFormElement::ElementRemoved);
|
2013-06-18 12:53:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UnsetFlags(ADDED_TO_FORM);
|
|
|
|
mForm = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-10-02 03:30:49 +00:00
|
|
|
void
|
|
|
|
HTMLImageElement::QueueImageLoadTask()
|
|
|
|
{
|
|
|
|
// If loading is temporarily disabled, we don't want to queue tasks
|
|
|
|
// that may then run when loading is re-enabled.
|
|
|
|
if (!LoadingEnabled() || !this->OwnerDoc()->IsCurrentActiveDocument()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-11 02:36:12 +00:00
|
|
|
nsCOMPtr<nsIRunnable> task = new ImageLoadTask(this);
|
|
|
|
// The task checks this to determine if it was the last
|
|
|
|
// queued event, and so earlier tasks are implicitly canceled.
|
|
|
|
mPendingImageLoadTask = task;
|
|
|
|
nsContentUtils::RunInStableState(task.forget());
|
2014-10-02 03:30:49 +00:00
|
|
|
}
|
|
|
|
|
2014-10-02 03:59:56 +00:00
|
|
|
bool
|
|
|
|
HTMLImageElement::HaveSrcsetOrInPicture()
|
|
|
|
{
|
|
|
|
if (IsSrcsetEnabled() && HasAttr(kNameSpaceID_None, nsGkAtoms::srcset)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HTMLPictureElement::IsPictureEnabled()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-19 11:43:42 +00:00
|
|
|
Element *parent = nsINode::GetParentElement();
|
2015-03-03 11:08:59 +00:00
|
|
|
return (parent && parent->IsHTMLElement(nsGkAtoms::picture));
|
2014-10-02 03:59:56 +00: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();
|
|
|
|
}
|
|
|
|
|
2014-05-02 23:26:22 +00:00
|
|
|
nsresult
|
|
|
|
HTMLImageElement::LoadSelectedImage(bool aForce, bool aNotify)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
|
2014-10-02 03:30:49 +00:00
|
|
|
if (aForce) {
|
|
|
|
// 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.
|
|
|
|
UpdateResponsiveSource();
|
|
|
|
}
|
|
|
|
|
2014-05-02 23:26:22 +00:00
|
|
|
if (mResponsiveSelector) {
|
|
|
|
nsCOMPtr<nsIURI> url = mResponsiveSelector->GetSelectedImageURL();
|
2014-12-10 23:53:00 +00:00
|
|
|
if (url) {
|
|
|
|
rv = LoadImage(url, aForce, aNotify, eImageLoadType_Imageset);
|
|
|
|
}
|
2014-05-02 23:26:22 +00:00
|
|
|
} else {
|
|
|
|
nsAutoString src;
|
|
|
|
if (!GetAttr(kNameSpaceID_None, nsGkAtoms::src, src)) {
|
|
|
|
CancelImageRequests(aNotify);
|
|
|
|
rv = NS_OK;
|
|
|
|
} else {
|
2014-08-19 21:49:38 +00: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
|
|
|
|
: eImageLoadType_Normal);
|
2014-05-02 23:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-02 03:30:49 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
CancelImageRequests(aNotify);
|
|
|
|
}
|
2014-05-02 23:26:22 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-02 21:32:20 +00:00
|
|
|
HTMLImageElement::PictureSourceSrcsetChanged(nsIContent *aSourceNode,
|
|
|
|
const nsAString& aNewValue,
|
|
|
|
bool aNotify)
|
2014-05-02 23:26:22 +00:00
|
|
|
{
|
2014-10-02 03:30:49 +00:00
|
|
|
bool isSelf = aSourceNode == this;
|
|
|
|
|
|
|
|
if (!IsSrcsetEnabled() ||
|
|
|
|
(!isSelf && !HTMLPictureElement::IsPictureEnabled())) {
|
2014-05-02 21:32:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-02 03:30:49 +00:00
|
|
|
MOZ_ASSERT(isSelf || IsPreviousSibling(aSourceNode, this),
|
|
|
|
"Should not be getting notifications for non-previous-siblings");
|
|
|
|
|
|
|
|
nsIContent *currentSrc =
|
|
|
|
mResponsiveSelector ? mResponsiveSelector->Content() : nullptr;
|
2014-05-02 21:32:20 +00:00
|
|
|
|
|
|
|
if (aSourceNode == currentSrc) {
|
2014-10-02 03:30:49 +00:00
|
|
|
// We're currently using this node as our responsive selector
|
|
|
|
// source.
|
2014-05-02 21:32:20 +00:00
|
|
|
mResponsiveSelector->SetCandidatesFromSourceSet(aNewValue);
|
|
|
|
}
|
2014-10-02 03:30:49 +00:00
|
|
|
|
|
|
|
// This always triggers the image update steps per the spec, even if
|
|
|
|
// we are not using this source.
|
|
|
|
QueueImageLoadTask();
|
2014-05-02 23:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-02 21:32:20 +00:00
|
|
|
HTMLImageElement::PictureSourceSizesChanged(nsIContent *aSourceNode,
|
|
|
|
const nsAString& aNewValue,
|
|
|
|
bool aNotify)
|
2014-05-02 23:26:22 +00:00
|
|
|
{
|
2014-05-02 21:32:20 +00:00
|
|
|
if (!HTMLPictureElement::IsPictureEnabled()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-05-02 23:26:22 +00:00
|
|
|
|
2014-10-02 03:30:49 +00: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-02 23:26:22 +00:00
|
|
|
|
2014-05-02 21:32:20 +00:00
|
|
|
if (aSourceNode == currentSrc) {
|
2014-10-02 03:30:49 +00:00
|
|
|
// We're currently using this node as our responsive selector
|
|
|
|
// source.
|
2014-05-02 21:32:20 +00:00
|
|
|
mResponsiveSelector->SetSizesFromDescriptor(aNewValue);
|
|
|
|
}
|
2014-10-02 03:30:49 +00:00
|
|
|
|
|
|
|
// This always triggers the image update steps per the spec, even if
|
|
|
|
// we are not using this source.
|
|
|
|
QueueImageLoadTask();
|
2014-05-02 21:32:20 +00:00
|
|
|
}
|
2014-05-02 23:26:22 +00:00
|
|
|
|
2014-05-02 21:32:20 +00:00
|
|
|
void
|
2014-10-08 21:01:19 +00:00
|
|
|
HTMLImageElement::PictureSourceMediaOrTypeChanged(nsIContent *aSourceNode,
|
|
|
|
bool aNotify)
|
2014-05-02 21:32:20 +00:00
|
|
|
{
|
2014-10-02 03:30:49 +00:00
|
|
|
if (!HTMLPictureElement::IsPictureEnabled()) {
|
|
|
|
return;
|
2014-05-02 21:32:20 +00:00
|
|
|
}
|
2014-10-02 03:30:49 +00: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
|
|
|
|
QueueImageLoadTask();
|
2014-05-02 21:32:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-10-02 03:30:49 +00:00
|
|
|
HTMLImageElement::PictureSourceAdded(nsIContent *aSourceNode)
|
2014-05-02 21:32:20 +00:00
|
|
|
{
|
2014-10-02 03:30:49 +00:00
|
|
|
if (!HTMLPictureElement::IsPictureEnabled()) {
|
|
|
|
return;
|
2014-05-02 21:32:20 +00:00
|
|
|
}
|
2014-10-02 03:30:49 +00:00
|
|
|
|
|
|
|
QueueImageLoadTask();
|
2014-05-02 21:32:20 +00:00
|
|
|
}
|
|
|
|
|
2014-10-02 03:30:49 +00:00
|
|
|
void
|
|
|
|
HTMLImageElement::PictureSourceRemoved(nsIContent *aSourceNode)
|
2014-05-02 21:32:20 +00:00
|
|
|
{
|
2014-10-02 03:30:49 +00:00
|
|
|
if (!HTMLPictureElement::IsPictureEnabled()) {
|
|
|
|
return;
|
2014-05-02 21:32:20 +00:00
|
|
|
}
|
|
|
|
|
2014-10-02 03:30:49 +00:00
|
|
|
QueueImageLoadTask();
|
|
|
|
}
|
2014-05-02 21:32:20 +00:00
|
|
|
|
2014-10-02 03:30:49 +00:00
|
|
|
void
|
|
|
|
HTMLImageElement::UpdateResponsiveSource()
|
|
|
|
{
|
2014-05-02 21:32:20 +00:00
|
|
|
if (!IsSrcsetEnabled()) {
|
2014-10-02 03:30:49 +00:00
|
|
|
mResponsiveSelector = nullptr;
|
|
|
|
return;
|
2014-05-02 21:32:20 +00:00
|
|
|
}
|
|
|
|
|
2014-10-02 03:30:49 +00:00
|
|
|
nsIContent *currentSource =
|
|
|
|
mResponsiveSelector ? mResponsiveSelector->Content() : nullptr;
|
2014-05-02 21:32:20 +00:00
|
|
|
bool pictureEnabled = HTMLPictureElement::IsPictureEnabled();
|
2015-02-19 11:43:42 +00:00
|
|
|
Element *parent = pictureEnabled ? nsINode::GetParentElement() : nullptr;
|
2014-10-02 03:30:49 +00:00
|
|
|
|
|
|
|
nsINode *candidateSource = nullptr;
|
2015-03-03 11:08:59 +00:00
|
|
|
if (parent && parent->IsHTMLElement(nsGkAtoms::picture)) {
|
2014-10-02 03:30:49 +00:00
|
|
|
// Walk source nodes previous to ourselves
|
|
|
|
candidateSource = parent->GetFirstChild();
|
|
|
|
} else {
|
|
|
|
candidateSource = this;
|
2014-05-02 21:32:20 +00:00
|
|
|
}
|
|
|
|
|
2014-10-02 03:30:49 +00: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.
|
|
|
|
mResponsiveSelector->SelectImage(true);
|
|
|
|
if (mResponsiveSelector->NumCandidates()) {
|
2015-05-11 22:07:49 +00: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) &&
|
|
|
|
!SourceElementMatches(candidateSource->AsContent())) {
|
|
|
|
isUsableCandidate = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isUsableCandidate) {
|
|
|
|
break;
|
|
|
|
}
|
2014-10-02 03:30:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// no longer valid
|
|
|
|
mResponsiveSelector = nullptr;
|
|
|
|
if (candidateSource == this) {
|
|
|
|
// No further possibilities
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (candidateSource == this) {
|
|
|
|
// We are the last possible source
|
|
|
|
if (!TryCreateResponsiveSelector(candidateSource->AsContent())) {
|
|
|
|
// Failed to find any source
|
|
|
|
mResponsiveSelector = nullptr;
|
|
|
|
}
|
2014-05-02 21:32:20 +00:00
|
|
|
break;
|
2015-03-03 11:08:59 +00:00
|
|
|
} else if (candidateSource->IsHTMLElement(nsGkAtoms::source) &&
|
2014-10-02 03:30:49 +00:00
|
|
|
TryCreateResponsiveSelector(candidateSource->AsContent())) {
|
|
|
|
// This led to a valid source, stop
|
2014-05-02 21:32:20 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-10-02 03:30:49 +00:00
|
|
|
candidateSource = candidateSource->GetNextSibling();
|
2014-05-02 21:32:20 +00:00
|
|
|
}
|
|
|
|
|
2014-10-02 03:30:49 +00:00
|
|
|
if (!candidateSource) {
|
|
|
|
// Ran out of siblings without finding ourself, e.g. XBL magic.
|
|
|
|
mResponsiveSelector = nullptr;
|
|
|
|
}
|
2014-05-02 21:32:20 +00:00
|
|
|
}
|
|
|
|
|
2014-12-10 23:53:00 +00:00
|
|
|
/*static */ bool
|
|
|
|
HTMLImageElement::SupportedPictureSourceType(const nsAString& aType)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
imgLoader::SupportImageWithMimeType(NS_ConvertUTF16toUTF8(aType).get(),
|
|
|
|
AcceptedMimeTypes::IMAGES_AND_DOCUMENTS);
|
|
|
|
}
|
|
|
|
|
2015-05-11 22:07:49 +00:00
|
|
|
bool
|
|
|
|
HTMLImageElement::SourceElementMatches(nsIContent* aSourceNode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aSourceNode->IsHTMLElement(nsGkAtoms::source));
|
|
|
|
|
|
|
|
DebugOnly<Element *> parent(nsINode::GetParentElement());
|
|
|
|
MOZ_ASSERT(parent && parent->IsHTMLElement(nsGkAtoms::picture));
|
|
|
|
MOZ_ASSERT(IsPreviousSibling(aSourceNode, this));
|
|
|
|
MOZ_ASSERT(HTMLPictureElement::IsPictureEnabled());
|
|
|
|
|
|
|
|
// Check media and type
|
|
|
|
HTMLSourceElement *src = static_cast<HTMLSourceElement*>(aSourceNode);
|
|
|
|
if (!src->MatchesCurrentMedia()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString type;
|
|
|
|
if (aSourceNode->GetAttr(kNameSpaceID_None, nsGkAtoms::type, type) &&
|
|
|
|
!SupportedPictureSourceType(type)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-02 21:32:20 +00:00
|
|
|
bool
|
|
|
|
HTMLImageElement::TryCreateResponsiveSelector(nsIContent *aSourceNode,
|
|
|
|
const nsAString *aSrcset,
|
|
|
|
const nsAString *aSizes)
|
|
|
|
{
|
|
|
|
if (!IsSrcsetEnabled()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool pictureEnabled = HTMLPictureElement::IsPictureEnabled();
|
|
|
|
// Skip if this is not a <source> with matching media query
|
2015-03-03 11:08:59 +00:00
|
|
|
bool isSourceTag = aSourceNode->IsHTMLElement(nsGkAtoms::source);
|
2014-05-02 21:32:20 +00:00
|
|
|
if (isSourceTag) {
|
2015-05-11 22:07:49 +00:00
|
|
|
if (!SourceElementMatches(aSourceNode)) {
|
2014-10-08 20:53:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-03-03 11:08:59 +00:00
|
|
|
} else if (aSourceNode->IsHTMLElement(nsGkAtoms::img)) {
|
2014-05-02 21:32:20 +00:00
|
|
|
// Otherwise this is the <img> tag itself
|
2014-10-02 03:30:49 +00:00
|
|
|
MOZ_ASSERT(aSourceNode == this);
|
2014-05-02 21:32:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Skip if has no srcset or an empty srcset
|
|
|
|
nsString srcset;
|
|
|
|
if (aSrcset) {
|
|
|
|
srcset = *aSrcset;
|
|
|
|
} else if (!aSourceNode->GetAttr(kNameSpaceID_None, nsGkAtoms::srcset,
|
|
|
|
srcset)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (srcset.IsEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Try to parse
|
2014-10-02 03:30:49 +00:00
|
|
|
nsRefPtr<ResponsiveImageSelector> sel = new ResponsiveImageSelector(aSourceNode);
|
2014-05-02 21:32:20 +00:00
|
|
|
if (!sel->SetCandidatesFromSourceSet(srcset)) {
|
|
|
|
// No possible candidates, don't need to bother parsing sizes
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-02 23:26:22 +00:00
|
|
|
|
2014-05-02 21:32:20 +00:00
|
|
|
if (pictureEnabled && aSizes) {
|
|
|
|
sel->SetSizesFromDescriptor(*aSizes);
|
|
|
|
} else if (pictureEnabled) {
|
|
|
|
nsAutoString sizes;
|
|
|
|
aSourceNode->GetAttr(kNameSpaceID_None, nsGkAtoms::sizes, sizes);
|
|
|
|
sel->SetSizesFromDescriptor(sizes);
|
2014-05-02 23:26:22 +00:00
|
|
|
}
|
2014-05-02 21:32:20 +00:00
|
|
|
|
|
|
|
// If this is the <img> tag, also pull in src as the default source
|
|
|
|
if (!isSourceTag) {
|
2014-10-02 03:30:49 +00:00
|
|
|
MOZ_ASSERT(aSourceNode == this);
|
2014-05-02 21:32:20 +00:00
|
|
|
nsAutoString src;
|
|
|
|
if (GetAttr(kNameSpaceID_None, nsGkAtoms::src, src) && !src.IsEmpty()) {
|
|
|
|
sel->SetDefaultSource(src);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mResponsiveSelector = sel;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-10 23:53:00 +00:00
|
|
|
/* static */ bool
|
|
|
|
HTMLImageElement::SelectSourceForTagWithAttrs(nsIDocument *aDocument,
|
|
|
|
bool aIsSourceTag,
|
|
|
|
const nsAString& aSrcAttr,
|
|
|
|
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");
|
|
|
|
|
|
|
|
bool pictureEnabled = HTMLPictureElement::IsPictureEnabled();
|
|
|
|
if (aIsSourceTag && !pictureEnabled) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsSrcsetEnabled() || aSrcsetAttr.IsEmpty()) {
|
|
|
|
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.
|
|
|
|
nsRefPtr<ResponsiveImageSelector> sel =
|
|
|
|
new ResponsiveImageSelector(aDocument);
|
|
|
|
|
|
|
|
sel->SetCandidatesFromSourceSet(aSrcsetAttr);
|
|
|
|
if (pictureEnabled && !aSizesAttr.IsEmpty()) {
|
|
|
|
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-02 21:32:20 +00:00
|
|
|
void
|
|
|
|
HTMLImageElement::DestroyContent()
|
|
|
|
{
|
|
|
|
mResponsiveSelector = nullptr;
|
2014-05-02 23:26:22 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 22:07:49 +00:00
|
|
|
void
|
|
|
|
HTMLImageElement::MediaFeatureValuesChanged()
|
|
|
|
{
|
|
|
|
QueueImageLoadTask();
|
|
|
|
}
|
|
|
|
|
2015-03-17 19:25:35 +00:00
|
|
|
void
|
|
|
|
HTMLImageElement::FlushUseCounters()
|
|
|
|
{
|
|
|
|
nsCOMPtr<imgIRequest> request;
|
|
|
|
GetRequest(CURRENT_REQUEST, getter_AddRefs(request));
|
|
|
|
|
|
|
|
nsCOMPtr<imgIContainer> container;
|
|
|
|
request->GetImage(getter_AddRefs(container));
|
|
|
|
|
|
|
|
static_cast<image::Image*>(container.get())->ReportUseCounters();
|
|
|
|
}
|
|
|
|
|
2012-12-31 17:25:46 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2014-05-02 23:26:22 +00:00
|
|
|
|