Line data Source code
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 : #include "mozilla/dom/HTMLSourceElement.h"
8 : #include "mozilla/dom/HTMLSourceElementBinding.h"
9 :
10 : #include "mozilla/dom/HTMLImageElement.h"
11 : #include "mozilla/dom/ResponsiveImageSelector.h"
12 : #include "mozilla/dom/MediaList.h"
13 : #include "mozilla/dom/MediaSource.h"
14 :
15 : #include "nsGkAtoms.h"
16 :
17 : #include "nsCSSParser.h"
18 : #include "nsHostObjectProtocolHandler.h"
19 :
20 : #include "mozilla/Preferences.h"
21 :
22 0 : NS_IMPL_NS_NEW_HTML_ELEMENT(Source)
23 :
24 : namespace mozilla {
25 : namespace dom {
26 :
27 0 : HTMLSourceElement::HTMLSourceElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
28 0 : : nsGenericHTMLElement(aNodeInfo)
29 : {
30 0 : }
31 :
32 0 : HTMLSourceElement::~HTMLSourceElement()
33 : {
34 0 : }
35 :
36 0 : NS_IMPL_CYCLE_COLLECTION_INHERITED(HTMLSourceElement, nsGenericHTMLElement,
37 : mSrcMediaSource)
38 :
39 0 : NS_IMPL_ADDREF_INHERITED(HTMLSourceElement, nsGenericHTMLElement)
40 0 : NS_IMPL_RELEASE_INHERITED(HTMLSourceElement, nsGenericHTMLElement)
41 :
42 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(HTMLSourceElement)
43 0 : NS_INTERFACE_MAP_ENTRY(nsIDOMHTMLSourceElement)
44 0 : NS_INTERFACE_MAP_END_INHERITING(nsGenericHTMLElement)
45 :
46 0 : NS_IMPL_ELEMENT_CLONE(HTMLSourceElement)
47 :
48 0 : NS_IMPL_URI_ATTR(HTMLSourceElement, Src, src)
49 0 : NS_IMPL_STRING_ATTR(HTMLSourceElement, Type, type)
50 0 : NS_IMPL_STRING_ATTR(HTMLSourceElement, Srcset, srcset)
51 0 : NS_IMPL_STRING_ATTR(HTMLSourceElement, Sizes, sizes)
52 0 : NS_IMPL_STRING_ATTR(HTMLSourceElement, Media, media)
53 :
54 : bool
55 0 : HTMLSourceElement::MatchesCurrentMedia()
56 : {
57 0 : if (mMediaList) {
58 0 : nsIPresShell* presShell = OwnerDoc()->GetShell();
59 0 : nsPresContext* pctx = presShell ? presShell->GetPresContext() : nullptr;
60 0 : return pctx && mMediaList->Matches(pctx);
61 : }
62 :
63 : // No media specified
64 0 : return true;
65 : }
66 :
67 : /* static */ bool
68 0 : HTMLSourceElement::WouldMatchMediaForDocument(const nsAString& aMedia,
69 : const nsIDocument *aDocument)
70 : {
71 0 : if (aMedia.IsEmpty()) {
72 0 : return true;
73 : }
74 :
75 0 : nsIPresShell* presShell = aDocument->GetShell();
76 0 : nsPresContext* pctx = presShell ? presShell->GetPresContext() : nullptr;
77 :
78 : RefPtr<MediaList> mediaList =
79 0 : MediaList::Create(aDocument->GetStyleBackendType(), aMedia);
80 0 : return pctx && mediaList->Matches(pctx);
81 : }
82 :
83 : void
84 0 : HTMLSourceElement::UpdateMediaList(const nsAttrValue* aValue)
85 : {
86 0 : mMediaList = nullptr;
87 0 : nsString mediaStr;
88 0 : if (!aValue || (mediaStr = aValue->GetStringValue()).IsEmpty()) {
89 0 : return;
90 : }
91 :
92 0 : nsCSSParser cssParser;
93 0 : mMediaList = MediaList::Create(OwnerDoc()->GetStyleBackendType(), mediaStr);
94 : }
95 :
96 : nsresult
97 0 : HTMLSourceElement::AfterSetAttr(int32_t aNameSpaceID, nsIAtom* aName,
98 : const nsAttrValue* aValue,
99 : const nsAttrValue* aOldValue, bool aNotify)
100 : {
101 : // If we are associated with a <picture> with a valid <img>, notify it of
102 : // responsive parameter changes
103 0 : Element *parent = nsINode::GetParentElement();
104 0 : if (aNameSpaceID == kNameSpaceID_None &&
105 0 : (aName == nsGkAtoms::srcset ||
106 0 : aName == nsGkAtoms::sizes ||
107 0 : aName == nsGkAtoms::media ||
108 0 : aName == nsGkAtoms::type) &&
109 0 : parent && parent->IsHTMLElement(nsGkAtoms::picture)) {
110 0 : nsString strVal = aValue ? aValue->GetStringValue() : EmptyString();
111 : // Find all img siblings after this <source> and notify them of the change
112 0 : nsCOMPtr<nsIContent> sibling = AsContent();
113 0 : while ( (sibling = sibling->GetNextSibling()) ) {
114 0 : if (sibling->IsHTMLElement(nsGkAtoms::img)) {
115 0 : HTMLImageElement *img = static_cast<HTMLImageElement*>(sibling.get());
116 0 : if (aName == nsGkAtoms::srcset) {
117 0 : img->PictureSourceSrcsetChanged(AsContent(), strVal, aNotify);
118 0 : } else if (aName == nsGkAtoms::sizes) {
119 0 : img->PictureSourceSizesChanged(AsContent(), strVal, aNotify);
120 0 : } else if (aName == nsGkAtoms::media) {
121 0 : UpdateMediaList(aValue);
122 0 : img->PictureSourceMediaOrTypeChanged(AsContent(), aNotify);
123 0 : } else if (aName == nsGkAtoms::type) {
124 0 : img->PictureSourceMediaOrTypeChanged(AsContent(), aNotify);
125 : }
126 : }
127 : }
128 :
129 0 : } else if (aNameSpaceID == kNameSpaceID_None && aName == nsGkAtoms::media) {
130 0 : UpdateMediaList(aValue);
131 0 : } else if (aNameSpaceID == kNameSpaceID_None && aName == nsGkAtoms::src) {
132 0 : mSrcMediaSource = nullptr;
133 0 : if (aValue) {
134 0 : nsString srcStr = aValue->GetStringValue();
135 0 : nsCOMPtr<nsIURI> uri;
136 0 : NewURIFromString(srcStr, getter_AddRefs(uri));
137 0 : if (uri && IsMediaSourceURI(uri)) {
138 0 : NS_GetSourceForMediaSourceURI(uri, getter_AddRefs(mSrcMediaSource));
139 : }
140 : }
141 : }
142 :
143 0 : return nsGenericHTMLElement::AfterSetAttr(aNameSpaceID, aName,
144 0 : aValue, aOldValue, aNotify);
145 : }
146 :
147 : nsresult
148 0 : HTMLSourceElement::BindToTree(nsIDocument *aDocument,
149 : nsIContent *aParent,
150 : nsIContent *aBindingParent,
151 : bool aCompileEventHandlers)
152 : {
153 0 : nsresult rv = nsGenericHTMLElement::BindToTree(aDocument,
154 : aParent,
155 : aBindingParent,
156 0 : aCompileEventHandlers);
157 0 : NS_ENSURE_SUCCESS(rv, rv);
158 :
159 0 : if (aParent && aParent->IsNodeOfType(nsINode::eMEDIA)) {
160 0 : HTMLMediaElement* media = static_cast<HTMLMediaElement*>(aParent);
161 0 : media->NotifyAddedSource();
162 : }
163 :
164 0 : return NS_OK;
165 : }
166 :
167 : JSObject*
168 0 : HTMLSourceElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
169 : {
170 0 : return HTMLSourceElementBinding::Wrap(aCx, this, aGivenProto);
171 : }
172 :
173 : } // namespace dom
174 : } // namespace mozilla
|