1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#include "mozilla/dom/HTMLSourceElement.h"
#include "mozilla/AttributeStyles.h"
#include "mozilla/MappedDeclarationsBuilder.h"
#include "mozilla/Preferences.h"
#include "mozilla/dom/BlobURLProtocolHandler.h"
#include "mozilla/dom/DocumentInlines.h"
#include "mozilla/dom/HTMLImageElement.h"
#include "mozilla/dom/HTMLMediaElement.h"
#include "mozilla/dom/HTMLSourceElementBinding.h"
#include "mozilla/dom/MediaList.h"
#include "mozilla/dom/MediaSource.h"
#include "mozilla/dom/ResponsiveImageSelector.h"
#include "nsGkAtoms.h"
NS_IMPL_NS_NEW_HTML_ELEMENT(Source)
namespace mozilla::dom {
HTMLSourceElement::HTMLSourceElement(
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
: nsGenericHTMLElement(std::move(aNodeInfo)) {}
HTMLSourceElement::~HTMLSourceElement() = default;
NS_IMPL_CYCLE_COLLECTION_INHERITED(HTMLSourceElement, nsGenericHTMLElement,
mSrcMediaSource)
NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(HTMLSourceElement,
nsGenericHTMLElement)
NS_IMPL_ELEMENT_CLONE(HTMLSourceElement)
bool HTMLSourceElement::MatchesCurrentMedia() {
if (mMediaList) {
return mMediaList->Matches(*OwnerDoc());
}
// No media specified
return true;
}
/* static */
bool HTMLSourceElement::WouldMatchMediaForDocument(const nsAString& aMedia,
const Document* aDocument) {
if (aMedia.IsEmpty()) {
return true;
}
RefPtr<MediaList> mediaList =
MediaList::Create(NS_ConvertUTF16toUTF8(aMedia));
return mediaList->Matches(*aDocument);
}
void HTMLSourceElement::UpdateMediaList(const nsAttrValue* aValue) {
mMediaList = nullptr;
if (!aValue) {
return;
}
NS_ConvertUTF16toUTF8 mediaStr(aValue->GetStringValue());
mMediaList = MediaList::Create(mediaStr);
}
bool HTMLSourceElement::ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
const nsAString& aValue,
nsIPrincipal* aMaybeScriptedPrincipal,
nsAttrValue& aResult) {
if (aNamespaceID == kNameSpaceID_None &&
(aAttribute == nsGkAtoms::width || aAttribute == nsGkAtoms::height)) {
return aResult.ParseHTMLDimension(aValue);
}
return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
aMaybeScriptedPrincipal, aResult);
}
void HTMLSourceElement::AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
const nsAttrValue* aValue,
const nsAttrValue* aOldValue,
nsIPrincipal* aMaybeScriptedPrincipal,
bool aNotify) {
if (aNameSpaceID == kNameSpaceID_None && aName == nsGkAtoms::srcset) {
mSrcsetTriggeringPrincipal = nsContentUtils::GetAttrTriggeringPrincipal(
this, aValue ? aValue->GetStringValue() : EmptyString(),
aMaybeScriptedPrincipal);
}
// If we are associated with a <picture> with a valid <img>, notify it of
// responsive parameter changes
if (aNameSpaceID == kNameSpaceID_None &&
(aName == nsGkAtoms::srcset || aName == nsGkAtoms::sizes ||
aName == nsGkAtoms::media || aName == nsGkAtoms::type) &&
IsInPicture()) {
if (aName == nsGkAtoms::media) {
UpdateMediaList(aValue);
}
nsString strVal = aValue ? aValue->GetStringValue() : EmptyString();
// Find all img siblings after this <source> and notify them of the change
nsCOMPtr<nsIContent> sibling = AsContent();
while ((sibling = sibling->GetNextSibling())) {
if (auto* img = HTMLImageElement::FromNode(sibling)) {
if (aName == nsGkAtoms::srcset) {
img->PictureSourceSrcsetChanged(this, strVal, aNotify);
} else if (aName == nsGkAtoms::sizes) {
img->PictureSourceSizesChanged(this, strVal, aNotify);
} else if (aName == nsGkAtoms::media || aName == nsGkAtoms::type) {
img->PictureSourceMediaOrTypeChanged(this, aNotify);
}
}
}
} else if (aNameSpaceID == kNameSpaceID_None && aName == nsGkAtoms::media) {
UpdateMediaList(aValue);
} else if (aNameSpaceID == kNameSpaceID_None && aName == nsGkAtoms::src) {
mSrcTriggeringPrincipal = nsContentUtils::GetAttrTriggeringPrincipal(
this, aValue ? aValue->GetStringValue() : EmptyString(),
aMaybeScriptedPrincipal);
mSrcMediaSource = nullptr;
if (aValue) {
nsString srcStr = aValue->GetStringValue();
nsCOMPtr<nsIURI> uri;
NewURIFromString(srcStr, getter_AddRefs(uri));
if (uri && IsMediaSourceURI(uri)) {
NS_GetSourceForMediaSourceURI(uri, getter_AddRefs(mSrcMediaSource));
}
}
} else if (aNameSpaceID == kNameSpaceID_None &&
IsAttributeMappedToImages(aName) && IsInPicture()) {
BuildMappedAttributesForImage();
nsCOMPtr<nsIContent> sibling = AsContent();
while ((sibling = sibling->GetNextSibling())) {
if (auto* img = HTMLImageElement::FromNode(sibling)) {
img->PictureSourceDimensionChanged(this, aNotify);
}
}
}
return nsGenericHTMLElement::AfterSetAttr(
aNameSpaceID, aName, aValue, aOldValue, aMaybeScriptedPrincipal, aNotify);
}
nsresult HTMLSourceElement::BindToTree(BindContext& aContext,
nsINode& aParent) {
nsresult rv = nsGenericHTMLElement::BindToTree(aContext, aParent);
NS_ENSURE_SUCCESS(rv, rv);
if (auto* media = HTMLMediaElement::FromNode(aParent)) {
media->NotifyAddedSource();
}
if (aParent.IsHTMLElement(nsGkAtoms::picture)) {
BuildMappedAttributesForImage();
} else {
mMappedAttributesForImage = nullptr;
}
return NS_OK;
}
void HTMLSourceElement::UnbindFromTree(UnbindContext& aContext) {
mMappedAttributesForImage = nullptr;
nsGenericHTMLElement::UnbindFromTree(aContext);
}
JSObject* HTMLSourceElement::WrapNode(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return HTMLSourceElement_Binding::Wrap(aCx, this, aGivenProto);
}
/**
* Helper to map the image source attributes.
* Note: This will override the declaration created by the presentation
* attributes of HTMLImageElement (i.e. mapped by MapImageSizeAttributeInto).
* https://html.spec.whatwg.org/multipage/embedded-content.html#the-source-element
*/
void HTMLSourceElement::BuildMappedAttributesForImage() {
MOZ_ASSERT(NS_IsMainThread());
mMappedAttributesForImage = nullptr;
Document* document = GetComposedDoc();
if (!document) {
return;
}
const nsAttrValue* width = mAttrs.GetAttr(nsGkAtoms::width);
const nsAttrValue* height = mAttrs.GetAttr(nsGkAtoms::height);
if (!width && !height) {
return;
}
MappedDeclarationsBuilder builder(*this, *document);
// We should set the missing property values with auto value to make sure it
// overrides the declaration created by the presentation attributes of
// HTMLImageElement. This can make sure we compute the ratio-dependent axis
// size properly by the natural aspect-ratio of the image.
//
// Note: The spec doesn't specify this, so we follow the implementation in
// other browsers.
// Spec issue: https://github.com/whatwg/html/issues/8178.
if (width) {
MapDimensionAttributeInto(builder, eCSSProperty_width, *width);
} else {
builder.SetAutoValue(eCSSProperty_width);
}
if (height) {
MapDimensionAttributeInto(builder, eCSSProperty_height, *height);
} else {
builder.SetAutoValue(eCSSProperty_height);
}
if (width && height) {
DoMapAspectRatio(*width, *height, builder);
} else {
builder.SetAutoValue(eCSSProperty_aspect_ratio);
}
mMappedAttributesForImage = builder.TakeDeclarationBlock();
}
} // namespace mozilla::dom
|