1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_HTMLSourceElement_h
8 #define mozilla_dom_HTMLSourceElement_h
10 #include "mozilla/Attributes.h"
11 #include "nsGenericHTMLElement.h"
12 #include "mozilla/dom/HTMLMediaElement.h"
16 namespace mozilla::dom
{
20 class HTMLSourceElement final
: public nsGenericHTMLElement
{
22 explicit HTMLSourceElement(
23 already_AddRefed
<mozilla::dom::NodeInfo
>&& aNodeInfo
);
26 NS_DECL_ISUPPORTS_INHERITED
27 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLSourceElement
,
30 NS_IMPL_FROMNODE_HTML_WITH_TAG(HTMLSourceElement
, source
)
32 nsresult
Clone(dom::NodeInfo
*, nsINode
** aResult
) const override
;
34 // Override BindToTree() so that we can trigger a load when we add a
35 // child source element.
36 nsresult
BindToTree(BindContext
&, nsINode
& aParent
) override
;
38 void UnbindFromTree(UnbindContext
&) override
;
40 // If this element's media attr matches for its owner document. Returns true
41 // if no media attr was set.
42 bool MatchesCurrentMedia();
44 // True if a source tag would match the given media attribute for the
45 // specified document. Used by the preloader to determine valid <source> tags
46 // prior to DOM creation.
47 static bool WouldMatchMediaForDocument(const nsAString
& aMediaStr
,
48 const Document
* aDocument
);
50 // Return the MediaSource object if any associated with the src attribute
52 MediaSource
* GetSrcMediaSource() { return mSrcMediaSource
; };
55 void GetSrc(nsString
& aSrc
) { GetURIAttr(nsGkAtoms::src
, nullptr, aSrc
); }
56 void SetSrc(const nsAString
& aSrc
, nsIPrincipal
* aTriggeringPrincipal
,
57 mozilla::ErrorResult
& rv
) {
58 SetHTMLAttr(nsGkAtoms::src
, aSrc
, aTriggeringPrincipal
, rv
);
61 nsIPrincipal
* GetSrcTriggeringPrincipal() const {
62 return mSrcTriggeringPrincipal
;
65 nsIPrincipal
* GetSrcsetTriggeringPrincipal() const {
66 return mSrcsetTriggeringPrincipal
;
69 void GetType(DOMString
& aType
) { GetHTMLAttr(nsGkAtoms::type
, aType
); }
70 void SetType(const nsAString
& aType
, ErrorResult
& rv
) {
71 SetHTMLAttr(nsGkAtoms::type
, aType
, rv
);
74 void GetSrcset(DOMString
& aSrcset
) {
75 GetHTMLAttr(nsGkAtoms::srcset
, aSrcset
);
77 void SetSrcset(const nsAString
& aSrcset
, nsIPrincipal
* aTriggeringPrincipal
,
78 mozilla::ErrorResult
& rv
) {
79 SetHTMLAttr(nsGkAtoms::srcset
, aSrcset
, aTriggeringPrincipal
, rv
);
82 void GetSizes(DOMString
& aSizes
) { GetHTMLAttr(nsGkAtoms::sizes
, aSizes
); }
83 void SetSizes(const nsAString
& aSizes
, mozilla::ErrorResult
& rv
) {
84 SetHTMLAttr(nsGkAtoms::sizes
, aSizes
, rv
);
87 void GetMedia(DOMString
& aMedia
) { GetHTMLAttr(nsGkAtoms::media
, aMedia
); }
88 void SetMedia(const nsAString
& aMedia
, mozilla::ErrorResult
& rv
) {
89 SetHTMLAttr(nsGkAtoms::media
, aMedia
, rv
);
92 uint32_t Width() const {
93 return GetDimensionAttrAsUnsignedInt(nsGkAtoms::width
, 0);
95 void SetWidth(uint32_t aWidth
, ErrorResult
& aRv
) {
96 SetUnsignedIntAttr(nsGkAtoms::width
, aWidth
, 0, aRv
);
99 uint32_t Height() const {
100 return GetDimensionAttrAsUnsignedInt(nsGkAtoms::height
, 0);
102 void SetHeight(uint32_t aHeight
, ErrorResult
& aRv
) {
103 SetUnsignedIntAttr(nsGkAtoms::height
, aHeight
, 0, aRv
);
106 const StyleLockedDeclarationBlock
* GetAttributesMappedForImage() const {
107 return mMappedAttributesForImage
;
110 static bool IsAttributeMappedToImages(const nsAtom
* aAttribute
) {
111 return aAttribute
== nsGkAtoms::width
|| aAttribute
== nsGkAtoms::height
;
115 virtual ~HTMLSourceElement();
117 JSObject
* WrapNode(JSContext
* aCx
,
118 JS::Handle
<JSObject
*> aGivenProto
) override
;
120 bool ParseAttribute(int32_t aNamespaceID
, nsAtom
* aAttribute
,
121 const nsAString
& aValue
,
122 nsIPrincipal
* aMaybeScriptedPrincipal
,
123 nsAttrValue
& aResult
) override
;
125 void AfterSetAttr(int32_t aNameSpaceID
, nsAtom
* aName
,
126 const nsAttrValue
* aValue
, const nsAttrValue
* aOldValue
,
127 nsIPrincipal
* aMaybeScriptedPrincipal
,
128 bool aNotify
) override
;
131 // Generates a new MediaList using the given input
132 void UpdateMediaList(const nsAttrValue
* aValue
);
134 void BuildMappedAttributesForImage();
136 bool IsInPicture() const {
137 return GetParentElement() &&
138 GetParentElement()->IsHTMLElement(nsGkAtoms::picture
);
141 RefPtr
<MediaList
> mMediaList
;
142 RefPtr
<MediaSource
> mSrcMediaSource
;
144 // The triggering principal for the src attribute.
145 nsCOMPtr
<nsIPrincipal
> mSrcTriggeringPrincipal
;
147 // The triggering principal for the srcset attribute.
148 nsCOMPtr
<nsIPrincipal
> mSrcsetTriggeringPrincipal
;
150 // The mapped attributes to HTMLImageElement if we are associated with a
151 // <picture> with a valid <img>.
152 RefPtr
<StyleLockedDeclarationBlock
> mMappedAttributesForImage
;
155 } // namespace mozilla::dom
157 #endif // mozilla_dom_HTMLSourceElement_h