2012-05-31 11:16:48 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
2012-12-31 09:25:46 -08:00
|
|
|
#ifndef mozilla_dom_HTMLImageElement_h
|
|
|
|
#define mozilla_dom_HTMLImageElement_h
|
2012-05-31 11:16:48 -07:00
|
|
|
|
2013-05-29 13:43:41 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2012-05-31 11:16:48 -07:00
|
|
|
#include "nsGenericHTMLElement.h"
|
|
|
|
#include "nsImageLoadingContent.h"
|
|
|
|
#include "nsIDOMHTMLImageElement.h"
|
2012-12-31 10:48:55 -08:00
|
|
|
#include "imgRequestProxy.h"
|
2013-06-12 00:00:08 -07:00
|
|
|
#include "Units.h"
|
2014-10-13 13:36:01 -07:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2014-05-02 14:32:20 -07:00
|
|
|
|
|
|
|
// Only needed for IsPictureEnabled()
|
|
|
|
#include "mozilla/dom/HTMLPictureElement.h"
|
2012-05-31 11:16:48 -07:00
|
|
|
|
2012-12-31 09:25:46 -08:00
|
|
|
namespace mozilla {
|
2014-03-17 21:48:19 -07:00
|
|
|
class EventChainPreVisitor;
|
2012-12-31 09:25:46 -08:00
|
|
|
namespace dom {
|
|
|
|
|
2014-05-02 14:32:20 -07:00
|
|
|
class ResponsiveImageSelector;
|
2012-12-31 09:25:46 -08:00
|
|
|
class HTMLImageElement MOZ_FINAL : public nsGenericHTMLElement,
|
|
|
|
public nsImageLoadingContent,
|
2013-01-22 02:53:13 -08:00
|
|
|
public nsIDOMHTMLImageElement
|
2012-05-31 11:16:48 -07:00
|
|
|
{
|
2014-05-02 14:32:20 -07:00
|
|
|
friend class HTMLSourceElement;
|
2014-10-01 20:30:49 -07:00
|
|
|
friend class HTMLPictureElement;
|
|
|
|
friend class ImageLoadTask;
|
2012-05-31 11:16:48 -07:00
|
|
|
public:
|
2014-06-19 19:01:40 -07:00
|
|
|
explicit HTMLImageElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo);
|
2012-05-31 11:16:48 -07:00
|
|
|
|
2013-01-22 02:53:13 -08:00
|
|
|
static already_AddRefed<HTMLImageElement>
|
2013-08-22 22:17:08 -07:00
|
|
|
Image(const GlobalObject& aGlobal,
|
|
|
|
const Optional<uint32_t>& aWidth,
|
|
|
|
const Optional<uint32_t>& aHeight,
|
|
|
|
ErrorResult& aError);
|
2013-01-22 02:53:13 -08:00
|
|
|
|
2014-10-13 13:36:01 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLImageElement,
|
|
|
|
nsGenericHTMLElement)
|
|
|
|
|
2012-05-31 11:16:48 -07:00
|
|
|
// nsISupports
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
2012-10-06 00:19:52 -07:00
|
|
|
virtual bool Draggable() const MOZ_OVERRIDE;
|
2012-05-31 11:16:48 -07:00
|
|
|
|
|
|
|
// nsIDOMHTMLImageElement
|
|
|
|
NS_DECL_NSIDOMHTMLIMAGEELEMENT
|
|
|
|
|
2014-10-01 20:30:49 -07:00
|
|
|
NS_IMPL_FROMCONTENT_HTML_WITH_TAG(HTMLImageElement, img)
|
|
|
|
|
2012-05-31 11:16:48 -07:00
|
|
|
// override from nsImageLoadingContent
|
2012-12-31 09:25:46 -08:00
|
|
|
CORSMode GetCORSMode();
|
2012-05-31 11:16:48 -07:00
|
|
|
|
|
|
|
// nsIContent
|
2012-08-22 08:56:38 -07:00
|
|
|
virtual bool ParseAttribute(int32_t aNamespaceID,
|
2012-05-31 11:16:48 -07:00
|
|
|
nsIAtom* aAttribute,
|
|
|
|
const nsAString& aValue,
|
2013-05-29 13:43:41 -07:00
|
|
|
nsAttrValue& aResult) MOZ_OVERRIDE;
|
2012-05-31 11:16:48 -07:00
|
|
|
virtual nsChangeHint GetAttributeChangeHint(const nsIAtom* aAttribute,
|
2013-05-29 13:43:41 -07:00
|
|
|
int32_t aModType) const MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD_(bool) IsAttributeMapped(const nsIAtom* aAttribute) const MOZ_OVERRIDE;
|
|
|
|
virtual nsMapRuleToAttributesFunc GetAttributeMappingFunction() const MOZ_OVERRIDE;
|
2012-05-31 11:16:48 -07:00
|
|
|
|
2014-03-17 21:48:19 -07:00
|
|
|
virtual nsresult PreHandleEvent(EventChainPreVisitor& aVisitor) MOZ_OVERRIDE;
|
2012-05-31 11:16:48 -07:00
|
|
|
|
2013-05-29 13:43:41 -07:00
|
|
|
bool IsHTMLFocusable(bool aWithMouse, bool *aIsFocusable, int32_t *aTabIndex) MOZ_OVERRIDE;
|
2012-05-31 11:16:48 -07:00
|
|
|
|
|
|
|
// SetAttr override. C++ is stupid, so have to override both
|
|
|
|
// overloaded methods.
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult SetAttr(int32_t aNameSpaceID, nsIAtom* aName,
|
2012-05-31 11:16:48 -07:00
|
|
|
const nsAString& aValue, bool aNotify)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
return SetAttr(aNameSpaceID, aName, nullptr, aValue, aNotify);
|
2012-05-31 11:16:48 -07:00
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
virtual nsresult SetAttr(int32_t aNameSpaceID, nsIAtom* aName,
|
2012-05-31 11:16:48 -07:00
|
|
|
nsIAtom* aPrefix, const nsAString& aValue,
|
2013-05-29 13:43:41 -07:00
|
|
|
bool aNotify) MOZ_OVERRIDE;
|
2012-05-31 11:16:48 -07:00
|
|
|
|
|
|
|
virtual nsresult BindToTree(nsIDocument* aDocument, nsIContent* aParent,
|
|
|
|
nsIContent* aBindingParent,
|
2013-05-29 13:43:41 -07:00
|
|
|
bool aCompileEventHandlers) MOZ_OVERRIDE;
|
|
|
|
virtual void UnbindFromTree(bool aDeep, bool aNullParent) MOZ_OVERRIDE;
|
2012-05-31 11:16:48 -07:00
|
|
|
|
2014-04-02 21:18:36 -07:00
|
|
|
virtual EventStates IntrinsicState() const MOZ_OVERRIDE;
|
2014-06-19 19:01:40 -07:00
|
|
|
virtual nsresult Clone(mozilla::dom::NodeInfo *aNodeInfo, nsINode **aResult) const MOZ_OVERRIDE;
|
2012-05-31 11:16:48 -07:00
|
|
|
|
2012-12-31 09:25:46 -08:00
|
|
|
nsresult CopyInnerTo(Element* aDest);
|
2012-05-31 11:16:48 -07:00
|
|
|
|
|
|
|
void MaybeLoadImage();
|
2012-12-31 10:48:55 -08:00
|
|
|
|
2014-05-02 16:26:22 -07:00
|
|
|
static bool IsSrcsetEnabled();
|
|
|
|
|
2012-12-31 10:48:55 -08:00
|
|
|
bool IsMap()
|
|
|
|
{
|
|
|
|
return GetBoolAttr(nsGkAtoms::ismap);
|
|
|
|
}
|
|
|
|
void SetIsMap(bool aIsMap, ErrorResult& aError)
|
|
|
|
{
|
|
|
|
SetHTMLBoolAttr(nsGkAtoms::ismap, aIsMap, aError);
|
|
|
|
}
|
|
|
|
uint32_t Width()
|
|
|
|
{
|
|
|
|
return GetWidthHeightForImage(mCurrentRequest).width;
|
|
|
|
}
|
|
|
|
void SetWidth(uint32_t aWidth, ErrorResult& aError)
|
|
|
|
{
|
2013-04-04 00:03:50 -07:00
|
|
|
SetUnsignedIntAttr(nsGkAtoms::width, aWidth, aError);
|
2012-12-31 10:48:55 -08:00
|
|
|
}
|
|
|
|
uint32_t Height()
|
|
|
|
{
|
|
|
|
return GetWidthHeightForImage(mCurrentRequest).height;
|
|
|
|
}
|
|
|
|
void SetHeight(uint32_t aHeight, ErrorResult& aError)
|
|
|
|
{
|
2013-04-04 00:03:50 -07:00
|
|
|
SetUnsignedIntAttr(nsGkAtoms::height, aHeight, aError);
|
2012-12-31 10:48:55 -08:00
|
|
|
}
|
|
|
|
uint32_t NaturalWidth();
|
|
|
|
uint32_t NaturalHeight();
|
|
|
|
bool Complete();
|
2013-06-23 00:14:15 -07:00
|
|
|
uint32_t Hspace()
|
2012-12-31 10:48:55 -08:00
|
|
|
{
|
2013-06-23 00:14:15 -07:00
|
|
|
return GetUnsignedIntAttr(nsGkAtoms::hspace, 0);
|
2012-12-31 10:48:55 -08:00
|
|
|
}
|
2013-06-23 00:14:15 -07:00
|
|
|
void SetHspace(uint32_t aHspace, ErrorResult& aError)
|
2012-12-31 10:48:55 -08:00
|
|
|
{
|
2013-06-23 00:14:15 -07:00
|
|
|
SetUnsignedIntAttr(nsGkAtoms::hspace, aHspace, aError);
|
2012-12-31 10:48:55 -08:00
|
|
|
}
|
2013-06-23 00:14:15 -07:00
|
|
|
uint32_t Vspace()
|
2012-12-31 10:48:55 -08:00
|
|
|
{
|
2013-06-23 00:14:15 -07:00
|
|
|
return GetUnsignedIntAttr(nsGkAtoms::vspace, 0);
|
2012-12-31 10:48:55 -08:00
|
|
|
}
|
2013-06-23 00:14:15 -07:00
|
|
|
void SetVspace(uint32_t aVspace, ErrorResult& aError)
|
2012-12-31 10:48:55 -08:00
|
|
|
{
|
2013-06-23 00:14:15 -07:00
|
|
|
SetUnsignedIntAttr(nsGkAtoms::vspace, aVspace, aError);
|
2012-12-31 10:48:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// The XPCOM versions of the following getters work for Web IDL bindings as well
|
|
|
|
void SetAlt(const nsAString& aAlt, ErrorResult& aError)
|
|
|
|
{
|
|
|
|
SetHTMLAttr(nsGkAtoms::alt, aAlt, aError);
|
|
|
|
}
|
|
|
|
void SetSrc(const nsAString& aSrc, ErrorResult& aError)
|
|
|
|
{
|
|
|
|
SetHTMLAttr(nsGkAtoms::src, aSrc, aError);
|
|
|
|
}
|
2014-03-19 14:18:24 -07:00
|
|
|
void SetSrcset(const nsAString& aSrcset, ErrorResult& aError)
|
|
|
|
{
|
|
|
|
SetHTMLAttr(nsGkAtoms::srcset, aSrcset, aError);
|
|
|
|
}
|
2014-09-05 19:42:33 -07:00
|
|
|
void GetCrossOrigin(nsAString& aResult)
|
|
|
|
{
|
|
|
|
// Null for both missing and invalid defaults is ok, since we
|
|
|
|
// always parse to an enum value, so we don't need an invalid
|
|
|
|
// default, and we _want_ the missing default to be null.
|
|
|
|
GetEnumAttr(nsGkAtoms::crossorigin, nullptr, aResult);
|
|
|
|
}
|
2012-12-31 10:48:55 -08:00
|
|
|
void SetCrossOrigin(const nsAString& aCrossOrigin, ErrorResult& aError)
|
|
|
|
{
|
2014-09-05 19:42:33 -07:00
|
|
|
SetOrRemoveNullableStringAttr(nsGkAtoms::crossorigin, aCrossOrigin, aError);
|
2012-12-31 10:48:55 -08:00
|
|
|
}
|
|
|
|
void SetUseMap(const nsAString& aUseMap, ErrorResult& aError)
|
|
|
|
{
|
|
|
|
SetHTMLAttr(nsGkAtoms::usemap, aUseMap, aError);
|
|
|
|
}
|
|
|
|
void SetName(const nsAString& aName, ErrorResult& aError)
|
|
|
|
{
|
|
|
|
SetHTMLAttr(nsGkAtoms::name, aName, aError);
|
|
|
|
}
|
|
|
|
void SetAlign(const nsAString& aAlign, ErrorResult& aError)
|
|
|
|
{
|
|
|
|
SetHTMLAttr(nsGkAtoms::align, aAlign, aError);
|
|
|
|
}
|
|
|
|
void SetLongDesc(const nsAString& aLongDesc, ErrorResult& aError)
|
|
|
|
{
|
|
|
|
SetHTMLAttr(nsGkAtoms::longdesc, aLongDesc, aError);
|
|
|
|
}
|
2014-03-19 14:18:57 -07:00
|
|
|
void SetSizes(const nsAString& aSizes, ErrorResult& aError)
|
|
|
|
{
|
|
|
|
SetHTMLAttr(nsGkAtoms::sizes, aSizes, aError);
|
|
|
|
}
|
2012-12-31 10:48:55 -08:00
|
|
|
void SetBorder(const nsAString& aBorder, ErrorResult& aError)
|
|
|
|
{
|
|
|
|
SetHTMLAttr(nsGkAtoms::border, aBorder, aError);
|
|
|
|
}
|
|
|
|
|
2013-06-02 08:38:25 -07:00
|
|
|
int32_t X();
|
|
|
|
int32_t Y();
|
|
|
|
// Uses XPCOM GetLowsrc.
|
|
|
|
void SetLowsrc(const nsAString& aLowsrc, ErrorResult& aError)
|
|
|
|
{
|
|
|
|
SetHTMLAttr(nsGkAtoms::lowsrc, aLowsrc, aError);
|
|
|
|
}
|
|
|
|
|
2013-06-18 05:53:23 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
nsIDOMHTMLFormElement* GetForm() const;
|
|
|
|
#endif
|
|
|
|
void SetForm(nsIDOMHTMLFormElement* aForm);
|
|
|
|
void ClearForm(bool aRemoveFromForm);
|
|
|
|
|
2014-05-02 16:26:22 -07:00
|
|
|
virtual void DestroyContent() MOZ_OVERRIDE;
|
|
|
|
|
2012-05-31 11:16:48 -07:00
|
|
|
protected:
|
2014-07-08 14:23:16 -07:00
|
|
|
virtual ~HTMLImageElement();
|
|
|
|
|
2014-10-01 20:59:56 -07:00
|
|
|
// Queues a task to run LoadSelectedImage pending stable state.
|
|
|
|
//
|
|
|
|
// Pending Bug 1076583 this is only used by the responsive image
|
|
|
|
// algorithm (InResponsiveMode()) -- synchronous actions when just
|
|
|
|
// using img.src will bypass this, and update source and kick off
|
|
|
|
// image load synchronously.
|
2014-10-01 20:30:49 -07:00
|
|
|
void QueueImageLoadTask();
|
|
|
|
|
2014-10-01 20:59:56 -07:00
|
|
|
// True if we have a srcset attribute or a <picture> parent, regardless of if
|
|
|
|
// any valid responsive sources were parsed from either.
|
|
|
|
bool HaveSrcsetOrInPicture();
|
|
|
|
|
|
|
|
// True if we are using the newer image loading algorithm. This will be the
|
|
|
|
// only mode after Bug 1076583
|
|
|
|
bool InResponsiveMode();
|
|
|
|
|
2014-05-02 16:26:22 -07:00
|
|
|
// Resolve and load the current mResponsiveSelector (responsive mode) or src
|
|
|
|
// attr image.
|
|
|
|
nsresult LoadSelectedImage(bool aForce, bool aNotify);
|
|
|
|
|
|
|
|
// Update/create/destroy mResponsiveSelector
|
2014-05-02 14:32:20 -07:00
|
|
|
void PictureSourceSrcsetChanged(nsIContent *aSourceNode,
|
|
|
|
const nsAString& aNewValue, bool aNotify);
|
|
|
|
void PictureSourceSizesChanged(nsIContent *aSourceNode,
|
|
|
|
const nsAString& aNewValue, bool aNotify);
|
2014-10-08 14:01:19 -07:00
|
|
|
// As we re-run the source selection on these mutations regardless,
|
|
|
|
// we don't actually care which changed or to what
|
|
|
|
void PictureSourceMediaOrTypeChanged(nsIContent *aSourceNode, bool aNotify);
|
2014-05-02 14:32:20 -07:00
|
|
|
|
|
|
|
void PictureSourceAdded(nsIContent *aSourceNode);
|
|
|
|
// This should be called prior to the unbind, such that nextsibling works
|
|
|
|
void PictureSourceRemoved(nsIContent *aSourceNode);
|
|
|
|
|
2014-10-01 20:30:49 -07:00
|
|
|
// Re-evaluates all source nodes (picture <source>,<img>) and finds
|
|
|
|
// the best source set for mResponsiveSelector. If a better source
|
|
|
|
// is found, creates a new selector and feeds the source to it. If
|
|
|
|
// the current ResponsiveSelector is not changed, runs
|
|
|
|
// SelectImage(true) to re-evaluate its candidates.
|
|
|
|
//
|
|
|
|
// Because keeping the existing selector is the common case (and we
|
|
|
|
// often do no-op reselections), this does not re-parse values for
|
|
|
|
// the existing mResponsiveSelector, meaning you need to update its
|
|
|
|
// parameters as appropriate before calling (or null it out to force
|
|
|
|
// recreation)
|
|
|
|
void UpdateResponsiveSource();
|
2014-05-02 14:32:20 -07:00
|
|
|
|
|
|
|
// Given a <source> node that is a previous sibling *or* ourselves, try to
|
|
|
|
// create a ResponsiveSelector.
|
|
|
|
|
|
|
|
// If the node's srcset/sizes make for an invalid selector, returns
|
|
|
|
// false. This does not guarantee the resulting selector matches an image,
|
|
|
|
// only that it is valid.
|
|
|
|
bool TryCreateResponsiveSelector(nsIContent *aSourceNode,
|
|
|
|
const nsAString *aSrcset = nullptr,
|
|
|
|
const nsAString *aSizes = nullptr);
|
2014-05-02 16:26:22 -07:00
|
|
|
|
2013-06-12 00:00:08 -07:00
|
|
|
CSSIntPoint GetXY();
|
2013-05-29 13:43:41 -07:00
|
|
|
virtual void GetItemValueText(nsAString& text) MOZ_OVERRIDE;
|
|
|
|
virtual void SetItemValueText(const nsAString& text) MOZ_OVERRIDE;
|
2014-04-08 15:27:17 -07:00
|
|
|
virtual JSObject* WrapNode(JSContext *aCx) MOZ_OVERRIDE;
|
2013-06-18 05:53:23 -07:00
|
|
|
void UpdateFormOwner();
|
|
|
|
|
|
|
|
virtual nsresult BeforeSetAttr(int32_t aNameSpaceID, nsIAtom* aName,
|
|
|
|
const nsAttrValueOrString* aValue,
|
|
|
|
bool aNotify) MOZ_OVERRIDE;
|
|
|
|
|
|
|
|
virtual nsresult AfterSetAttr(int32_t aNameSpaceID, nsIAtom* aName,
|
|
|
|
const nsAttrValue* aValue, bool aNotify) MOZ_OVERRIDE;
|
|
|
|
|
|
|
|
// This is a weak reference that this element and the HTMLFormElement
|
|
|
|
// cooperate in maintaining.
|
2013-06-19 07:24:37 -07:00
|
|
|
HTMLFormElement* mForm;
|
2013-11-19 11:21:29 -08:00
|
|
|
|
2014-05-02 16:26:22 -07:00
|
|
|
// Created when we're tracking responsive image state
|
|
|
|
nsRefPtr<ResponsiveImageSelector> mResponsiveSelector;
|
|
|
|
|
2013-11-19 11:21:29 -08:00
|
|
|
private:
|
|
|
|
static void MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
|
|
|
|
nsRuleData* aData);
|
2014-10-01 20:30:49 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> mPendingImageLoadTask;
|
2012-05-31 11:16:48 -07:00
|
|
|
};
|
|
|
|
|
2012-12-31 09:25:46 -08:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* mozilla_dom_HTMLImageElement_h */
|