HTMLSourceElement.h (5323B)
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/. */ 6 7 #ifndef mozilla_dom_HTMLSourceElement_h 8 #define mozilla_dom_HTMLSourceElement_h 9 10 #include "mozilla/dom/HTMLMediaElement.h" 11 #include "nsGenericHTMLElement.h" 12 13 class nsAttrValue; 14 15 namespace mozilla::dom { 16 17 class MediaList; 18 19 class HTMLSourceElement final : public nsGenericHTMLElement { 20 public: 21 explicit HTMLSourceElement( 22 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo); 23 24 // nsISupports 25 NS_DECL_ISUPPORTS_INHERITED 26 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLSourceElement, 27 nsGenericHTMLElement) 28 29 NS_IMPL_FROMNODE_HTML_WITH_TAG(HTMLSourceElement, source) 30 31 nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override; 32 33 // Override BindToTree() so that we can trigger a load when we add a 34 // child source element. 35 nsresult BindToTree(BindContext&, nsINode& aParent) override; 36 37 void UnbindFromTree(UnbindContext&) override; 38 39 // If this element's media attr matches for its owner document. Returns true 40 // if no media attr was set. 41 bool MatchesCurrentMedia(); 42 43 // True if a source tag would match the given media attribute for the 44 // specified document. Used by the preloader to determine valid <source> tags 45 // prior to DOM creation. 46 static bool WouldMatchMediaForDocument(const nsAString& aMediaStr, 47 const Document* aDocument); 48 49 // Return the MediaSource object if any associated with the src attribute 50 // when it was set. 51 MediaSource* GetSrcMediaSource() { return mSrcMediaSource; }; 52 53 // WebIDL 54 void GetSrc(nsString& aSrc) { GetURIAttr(nsGkAtoms::src, nullptr, aSrc); } 55 void SetSrc(const nsAString& aSrc, nsIPrincipal* aTriggeringPrincipal, 56 mozilla::ErrorResult& rv) { 57 SetHTMLAttr(nsGkAtoms::src, aSrc, aTriggeringPrincipal, rv); 58 } 59 60 nsIPrincipal* GetSrcTriggeringPrincipal() const { 61 return mSrcTriggeringPrincipal; 62 } 63 64 nsIPrincipal* GetSrcsetTriggeringPrincipal() const { 65 return mSrcsetTriggeringPrincipal; 66 } 67 68 void GetType(DOMString& aType) { GetHTMLAttr(nsGkAtoms::type, aType); } 69 void SetType(const nsAString& aType, ErrorResult& rv) { 70 SetHTMLAttr(nsGkAtoms::type, aType, rv); 71 } 72 73 void GetSrcset(DOMString& aSrcset) { 74 GetHTMLAttr(nsGkAtoms::srcset, aSrcset); 75 } 76 void SetSrcset(const nsAString& aSrcset, nsIPrincipal* aTriggeringPrincipal, 77 mozilla::ErrorResult& rv) { 78 SetHTMLAttr(nsGkAtoms::srcset, aSrcset, aTriggeringPrincipal, rv); 79 } 80 81 void GetSizes(DOMString& aSizes) { GetHTMLAttr(nsGkAtoms::sizes, aSizes); } 82 void SetSizes(const nsAString& aSizes, mozilla::ErrorResult& rv) { 83 SetHTMLAttr(nsGkAtoms::sizes, aSizes, rv); 84 } 85 86 void GetMedia(DOMString& aMedia) { GetHTMLAttr(nsGkAtoms::media, aMedia); } 87 void SetMedia(const nsAString& aMedia, mozilla::ErrorResult& rv) { 88 SetHTMLAttr(nsGkAtoms::media, aMedia, rv); 89 } 90 91 uint32_t Width() const { 92 return GetDimensionAttrAsUnsignedInt(nsGkAtoms::width, 0); 93 } 94 void SetWidth(uint32_t aWidth, ErrorResult& aRv) { 95 SetUnsignedIntAttr(nsGkAtoms::width, aWidth, 0, aRv); 96 } 97 98 uint32_t Height() const { 99 return GetDimensionAttrAsUnsignedInt(nsGkAtoms::height, 0); 100 } 101 void SetHeight(uint32_t aHeight, ErrorResult& aRv) { 102 SetUnsignedIntAttr(nsGkAtoms::height, aHeight, 0, aRv); 103 } 104 105 const StyleLockedDeclarationBlock* GetAttributesMappedForImage() const { 106 return mMappedAttributesForImage; 107 } 108 109 static bool IsAttributeMappedToImages(const nsAtom* aAttribute) { 110 return aAttribute == nsGkAtoms::width || aAttribute == nsGkAtoms::height; 111 } 112 113 protected: 114 virtual ~HTMLSourceElement(); 115 116 JSObject* WrapNode(JSContext* aCx, 117 JS::Handle<JSObject*> aGivenProto) override; 118 119 bool ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute, 120 const nsAString& aValue, 121 nsIPrincipal* aMaybeScriptedPrincipal, 122 nsAttrValue& aResult) override; 123 124 void AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName, 125 const nsAttrValue* aValue, const nsAttrValue* aOldValue, 126 nsIPrincipal* aMaybeScriptedPrincipal, 127 bool aNotify) override; 128 129 private: 130 // Generates a new MediaList using the given input 131 void UpdateMediaList(const nsAttrValue* aValue); 132 133 void BuildMappedAttributesForImage(); 134 135 bool IsInPicture() const { 136 return GetParentElement() && 137 GetParentElement()->IsHTMLElement(nsGkAtoms::picture); 138 } 139 140 RefPtr<MediaList> mMediaList; 141 RefPtr<MediaSource> mSrcMediaSource; 142 143 // The triggering principal for the src attribute. 144 nsCOMPtr<nsIPrincipal> mSrcTriggeringPrincipal; 145 146 // The triggering principal for the srcset attribute. 147 nsCOMPtr<nsIPrincipal> mSrcsetTriggeringPrincipal; 148 149 // The mapped attributes to HTMLImageElement if we are associated with a 150 // <picture> with a valid <img>. 151 RefPtr<StyleLockedDeclarationBlock> mMappedAttributesForImage; 152 }; 153 154 } // namespace mozilla::dom 155 156 #endif // mozilla_dom_HTMLSourceElement_h