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/HTMLImageElement.h"
8 #include "mozilla/dom/BindingUtils.h"
9 #include "mozilla/dom/HTMLImageElementBinding.h"
10 #include "mozilla/dom/BindContext.h"
11 #include "mozilla/dom/NameSpaceConstants.h"
12 #include "nsGenericHTMLElement.h"
13 #include "nsGkAtoms.h"
14 #include "nsStyleConsts.h"
15 #include "nsPresContext.h"
16 #include "nsMappedAttributes.h"
17 #include "nsSize.h"
18 #include "mozilla/dom/Document.h"
19 #include "nsImageFrame.h"
20 #include "nsIScriptContext.h"
21 #include "nsContentUtils.h"
22 #include "nsContainerFrame.h"
23 #include "nsNodeInfoManager.h"
24 #include "mozilla/MouseEvents.h"
25 #include "nsContentPolicyUtils.h"
26 #include "nsFocusManager.h"
27 #include "mozilla/dom/DOMIntersectionObserver.h"
28 #include "mozilla/dom/HTMLFormElement.h"
29 #include "mozilla/dom/MutationEventBinding.h"
30 #include "mozilla/dom/UserActivation.h"
31 #include "nsAttrValueOrString.h"
32 #include "imgLoader.h"
33 #include "Image.h"
34
35 // Responsive images!
36 #include "mozilla/dom/HTMLSourceElement.h"
37 #include "mozilla/dom/ResponsiveImageSelector.h"
38
39 #include "imgINotificationObserver.h"
40 #include "imgRequestProxy.h"
41
42 #include "mozilla/CycleCollectedJSContext.h"
43
44 #include "mozilla/EventDispatcher.h"
45 #include "mozilla/EventStates.h"
46 #include "mozilla/MappedDeclarations.h"
47 #include "mozilla/Maybe.h"
48
49 #include "nsLayoutUtils.h"
50
51 using namespace mozilla::net;
52
NS_IMPL_NS_NEW_HTML_ELEMENT(Image)53 NS_IMPL_NS_NEW_HTML_ELEMENT(Image)
54
55 #ifdef DEBUG
56 // Is aSubject a previous sibling of aNode.
57 static bool IsPreviousSibling(nsINode* aSubject, nsINode* aNode) {
58 if (aSubject == aNode) {
59 return false;
60 }
61
62 nsINode* parent = aSubject->GetParentNode();
63 if (parent && parent == aNode->GetParentNode()) {
64 const Maybe<uint32_t> indexOfSubject = parent->ComputeIndexOf(aSubject);
65 const Maybe<uint32_t> indexOfNode = parent->ComputeIndexOf(aNode);
66 if (MOZ_LIKELY(indexOfSubject.isSome() && indexOfNode.isSome())) {
67 return *indexOfSubject < *indexOfNode;
68 }
69 // XXX Keep the odd traditional behavior for now.
70 return indexOfSubject.isNothing() && indexOfNode.isSome();
71 }
72
73 return false;
74 }
75 #endif
76
77 namespace mozilla::dom {
78
79 // Calls LoadSelectedImage on host element unless it has been superseded or
80 // canceled -- this is the synchronous section of "update the image data".
81 // https://html.spec.whatwg.org/multipage/embedded-content.html#update-the-image-data
82 class ImageLoadTask final : public MicroTaskRunnable {
83 public:
ImageLoadTask(HTMLImageElement * aElement,bool aAlwaysLoad,bool aUseUrgentStartForChannel)84 ImageLoadTask(HTMLImageElement* aElement, bool aAlwaysLoad,
85 bool aUseUrgentStartForChannel)
86 : MicroTaskRunnable(),
87 mElement(aElement),
88 mAlwaysLoad(aAlwaysLoad),
89 mUseUrgentStartForChannel(aUseUrgentStartForChannel) {
90 mDocument = aElement->OwnerDoc();
91 mDocument->BlockOnload();
92 }
93
Run(AutoSlowOperation & aAso)94 void Run(AutoSlowOperation& aAso) override {
95 if (mElement->mPendingImageLoadTask == this) {
96 mElement->mPendingImageLoadTask = nullptr;
97 mElement->mUseUrgentStartForChannel = mUseUrgentStartForChannel;
98 mElement->LoadSelectedImage(true, true, mAlwaysLoad);
99 }
100 mDocument->UnblockOnload(false);
101 }
102
Suppressed()103 bool Suppressed() override {
104 nsIGlobalObject* global = mElement->GetOwnerGlobal();
105 return global && global->IsInSyncOperation();
106 }
107
AlwaysLoad()108 bool AlwaysLoad() { return mAlwaysLoad; }
109
110 private:
111 ~ImageLoadTask() = default;
112 RefPtr<HTMLImageElement> mElement;
113 nsCOMPtr<Document> mDocument;
114 bool mAlwaysLoad;
115
116 // True if we want to set nsIClassOfService::UrgentStart to the channel to
117 // get the response ASAP for better user responsiveness.
118 bool mUseUrgentStartForChannel;
119 };
120
HTMLImageElement(already_AddRefed<mozilla::dom::NodeInfo> && aNodeInfo)121 HTMLImageElement::HTMLImageElement(
122 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
123 : nsGenericHTMLElement(std::move(aNodeInfo)),
124 mForm(nullptr),
125 mInDocResponsiveContent(false),
126 mCurrentDensity(1.0) {
127 // We start out broken
128 AddStatesSilently(NS_EVENT_STATE_BROKEN);
129 }
130
~HTMLImageElement()131 HTMLImageElement::~HTMLImageElement() { nsImageLoadingContent::Destroy(); }
132
NS_IMPL_CYCLE_COLLECTION_INHERITED(HTMLImageElement,nsGenericHTMLElement,mResponsiveSelector)133 NS_IMPL_CYCLE_COLLECTION_INHERITED(HTMLImageElement, nsGenericHTMLElement,
134 mResponsiveSelector)
135
136 NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED(HTMLImageElement,
137 nsGenericHTMLElement,
138 nsIImageLoadingContent,
139 imgINotificationObserver)
140
141 NS_IMPL_ELEMENT_CLONE(HTMLImageElement)
142
143 bool HTMLImageElement::IsInteractiveHTMLContent() const {
144 return HasAttr(kNameSpaceID_None, nsGkAtoms::usemap) ||
145 nsGenericHTMLElement::IsInteractiveHTMLContent();
146 }
147
AsyncEventRunning(AsyncEventDispatcher * aEvent)148 void HTMLImageElement::AsyncEventRunning(AsyncEventDispatcher* aEvent) {
149 nsImageLoadingContent::AsyncEventRunning(aEvent);
150 }
151
GetCurrentSrc(nsAString & aValue)152 void HTMLImageElement::GetCurrentSrc(nsAString& aValue) {
153 nsCOMPtr<nsIURI> currentURI;
154 GetCurrentURI(getter_AddRefs(currentURI));
155 if (currentURI) {
156 nsAutoCString spec;
157 currentURI->GetSpec(spec);
158 CopyUTF8toUTF16(spec, aValue);
159 } else {
160 SetDOMStringToNull(aValue);
161 }
162 }
163
Draggable() const164 bool HTMLImageElement::Draggable() const {
165 // images may be dragged unless the draggable attribute is false
166 return !AttrValueIs(kNameSpaceID_None, nsGkAtoms::draggable,
167 nsGkAtoms::_false, eIgnoreCase);
168 }
169
Complete()170 bool HTMLImageElement::Complete() {
171 // It is still not clear what value should img.complete return in various
172 // cases, see https://github.com/whatwg/html/issues/4884
173
174 if (!HasAttr(nsGkAtoms::srcset) && !HasNonEmptyAttr(nsGkAtoms::src)) {
175 return true;
176 }
177
178 if (!mCurrentRequest || mPendingRequest) {
179 return false;
180 }
181
182 uint32_t status;
183 mCurrentRequest->GetImageStatus(&status);
184 return (status &
185 (imgIRequest::STATUS_LOAD_COMPLETE | imgIRequest::STATUS_ERROR)) != 0;
186 }
187
GetXY()188 CSSIntPoint HTMLImageElement::GetXY() {
189 nsIFrame* frame = GetPrimaryFrame(FlushType::Layout);
190 if (!frame) {
191 return CSSIntPoint(0, 0);
192 }
193
194 nsIFrame* layer = nsLayoutUtils::GetClosestLayer(frame->GetParent());
195 return CSSIntPoint::FromAppUnitsRounded(frame->GetOffsetTo(layer));
196 }
197
X()198 int32_t HTMLImageElement::X() { return GetXY().x; }
199
Y()200 int32_t HTMLImageElement::Y() { return GetXY().y; }
201
GetDecoding(nsAString & aValue)202 void HTMLImageElement::GetDecoding(nsAString& aValue) {
203 GetEnumAttr(nsGkAtoms::decoding, kDecodingTableDefault->tag, aValue);
204 }
205
206 // https://whatpr.org/html/3752/urls-and-fetching.html#lazy-loading-attributes
207 static const nsAttrValue::EnumTable kLoadingTable[] = {
208 {"eager", HTMLImageElement::Loading::Eager},
209 {"lazy", HTMLImageElement::Loading::Lazy},
210 {nullptr, 0}};
211
GetLoading(nsAString & aValue) const212 void HTMLImageElement::GetLoading(nsAString& aValue) const {
213 GetEnumAttr(nsGkAtoms::loading, kLoadingTable[0].tag, aValue);
214 }
215
LoadingState() const216 HTMLImageElement::Loading HTMLImageElement::LoadingState() const {
217 const nsAttrValue* val = mAttrs.GetAttr(nsGkAtoms::loading);
218 if (!val) {
219 return HTMLImageElement::Loading::Eager;
220 }
221 return static_cast<HTMLImageElement::Loading>(val->GetEnumValue());
222 }
223
Decode(ErrorResult & aRv)224 already_AddRefed<Promise> HTMLImageElement::Decode(ErrorResult& aRv) {
225 return nsImageLoadingContent::QueueDecodeAsync(aRv);
226 }
227
ParseAttribute(int32_t aNamespaceID,nsAtom * aAttribute,const nsAString & aValue,nsIPrincipal * aMaybeScriptedPrincipal,nsAttrValue & aResult)228 bool HTMLImageElement::ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
229 const nsAString& aValue,
230 nsIPrincipal* aMaybeScriptedPrincipal,
231 nsAttrValue& aResult) {
232 if (aNamespaceID == kNameSpaceID_None) {
233 if (aAttribute == nsGkAtoms::align) {
234 return ParseAlignValue(aValue, aResult);
235 }
236 if (aAttribute == nsGkAtoms::crossorigin) {
237 ParseCORSValue(aValue, aResult);
238 return true;
239 }
240 if (aAttribute == nsGkAtoms::decoding) {
241 return aResult.ParseEnumValue(aValue, kDecodingTable,
242 /* aCaseSensitive = */ false,
243 kDecodingTableDefault);
244 }
245 if (aAttribute == nsGkAtoms::loading) {
246 return aResult.ParseEnumValue(aValue, kLoadingTable,
247 /* aCaseSensitive = */ false,
248 kLoadingTable);
249 }
250 if (ParseImageAttribute(aAttribute, aValue, aResult)) {
251 return true;
252 }
253 }
254
255 return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
256 aMaybeScriptedPrincipal, aResult);
257 }
258
MapAttributesIntoRule(const nsMappedAttributes * aAttributes,MappedDeclarations & aDecls)259 void HTMLImageElement::MapAttributesIntoRule(
260 const nsMappedAttributes* aAttributes, MappedDeclarations& aDecls) {
261 MapImageAlignAttributeInto(aAttributes, aDecls);
262 MapImageBorderAttributeInto(aAttributes, aDecls);
263 MapImageMarginAttributeInto(aAttributes, aDecls);
264 MapImageSizeAttributesInto(aAttributes, aDecls, MapAspectRatio::Yes);
265 MapCommonAttributesInto(aAttributes, aDecls);
266 }
267
GetAttributeChangeHint(const nsAtom * aAttribute,int32_t aModType) const268 nsChangeHint HTMLImageElement::GetAttributeChangeHint(const nsAtom* aAttribute,
269 int32_t aModType) const {
270 nsChangeHint retval =
271 nsGenericHTMLElement::GetAttributeChangeHint(aAttribute, aModType);
272 if (aAttribute == nsGkAtoms::usemap || aAttribute == nsGkAtoms::ismap) {
273 retval |= nsChangeHint_ReconstructFrame;
274 } else if (aAttribute == nsGkAtoms::alt) {
275 if (aModType == MutationEvent_Binding::ADDITION ||
276 aModType == MutationEvent_Binding::REMOVAL) {
277 retval |= nsChangeHint_ReconstructFrame;
278 }
279 }
280 return retval;
281 }
282
NS_IMETHODIMP_(bool)283 NS_IMETHODIMP_(bool)
284 HTMLImageElement::IsAttributeMapped(const nsAtom* aAttribute) const {
285 static const MappedAttributeEntry* const map[] = {
286 sCommonAttributeMap, sImageMarginSizeAttributeMap,
287 sImageBorderAttributeMap, sImageAlignAttributeMap};
288
289 return FindAttributeDependence(aAttribute, map);
290 }
291
GetAttributeMappingFunction() const292 nsMapRuleToAttributesFunc HTMLImageElement::GetAttributeMappingFunction()
293 const {
294 return &MapAttributesIntoRule;
295 }
296
BeforeSetAttr(int32_t aNameSpaceID,nsAtom * aName,const nsAttrValueOrString * aValue,bool aNotify)297 nsresult HTMLImageElement::BeforeSetAttr(int32_t aNameSpaceID, nsAtom* aName,
298 const nsAttrValueOrString* aValue,
299 bool aNotify) {
300 if (aNameSpaceID == kNameSpaceID_None && mForm &&
301 (aName == nsGkAtoms::name || aName == nsGkAtoms::id)) {
302 // remove the image from the hashtable as needed
303 nsAutoString tmp;
304 GetAttr(kNameSpaceID_None, aName, tmp);
305
306 if (!tmp.IsEmpty()) {
307 mForm->RemoveImageElementFromTable(this, tmp);
308 }
309 }
310
311 return nsGenericHTMLElement::BeforeSetAttr(aNameSpaceID, aName, aValue,
312 aNotify);
313 }
314
AfterSetAttr(int32_t aNameSpaceID,nsAtom * aName,const nsAttrValue * aValue,const nsAttrValue * aOldValue,nsIPrincipal * aMaybeScriptedPrincipal,bool aNotify)315 nsresult HTMLImageElement::AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
316 const nsAttrValue* aValue,
317 const nsAttrValue* aOldValue,
318 nsIPrincipal* aMaybeScriptedPrincipal,
319 bool aNotify) {
320 if (aNameSpaceID != kNameSpaceID_None) {
321 return nsGenericHTMLElement::AfterSetAttr(aNameSpaceID, aName, aValue,
322 aOldValue,
323 aMaybeScriptedPrincipal, aNotify);
324 }
325
326 nsAttrValueOrString attrVal(aValue);
327
328 if (aValue) {
329 AfterMaybeChangeAttr(aNameSpaceID, aName, attrVal, aOldValue,
330 aMaybeScriptedPrincipal, aNotify);
331 }
332
333 if (mForm && (aName == nsGkAtoms::name || aName == nsGkAtoms::id) && aValue &&
334 !aValue->IsEmptyString()) {
335 // add the image to the hashtable as needed
336 MOZ_ASSERT(aValue->Type() == nsAttrValue::eAtom,
337 "Expected atom value for name/id");
338 mForm->AddImageElementToTable(
339 this, nsDependentAtomString(aValue->GetAtomValue()));
340 }
341
342 bool forceReload = false;
343
344 if (aName == nsGkAtoms::loading &&
345 !ImageState().HasState(NS_EVENT_STATE_LOADING)) {
346 if (aValue && Loading(aValue->GetEnumValue()) == Loading::Lazy) {
347 SetLazyLoading();
348 } else if (aOldValue &&
349 Loading(aOldValue->GetEnumValue()) == Loading::Lazy) {
350 StopLazyLoading(FromIntersectionObserver::No, StartLoading::Yes);
351 }
352 } else if (aName == nsGkAtoms::src && !aValue) {
353 // NOTE: regular src value changes are handled in AfterMaybeChangeAttr, so
354 // this only needs to handle unsetting the src attribute.
355 // Mark channel as urgent-start before load image if the image load is
356 // initaiated by a user interaction.
357 mUseUrgentStartForChannel = UserActivation::IsHandlingUserInput();
358
359 // AfterMaybeChangeAttr handles setting src since it needs to catch
360 // img.src = img.src, so we only need to handle the unset case
361 if (InResponsiveMode()) {
362 if (mResponsiveSelector && mResponsiveSelector->Content() == this) {
363 mResponsiveSelector->SetDefaultSource(VoidString());
364 }
365 QueueImageLoadTask(true);
366 } else {
367 // Bug 1076583 - We still behave synchronously in the non-responsive case
368 CancelImageRequests(aNotify);
369 }
370 } else if (aName == nsGkAtoms::srcset) {
371 // Mark channel as urgent-start before load image if the image load is
372 // initaiated by a user interaction.
373 mUseUrgentStartForChannel = UserActivation::IsHandlingUserInput();
374
375 mSrcsetTriggeringPrincipal = aMaybeScriptedPrincipal;
376
377 PictureSourceSrcsetChanged(this, attrVal.String(), aNotify);
378 } else if (aName == nsGkAtoms::sizes) {
379 // Mark channel as urgent-start before load image if the image load is
380 // initiated by a user interaction.
381 mUseUrgentStartForChannel = UserActivation::IsHandlingUserInput();
382
383 PictureSourceSizesChanged(this, attrVal.String(), aNotify);
384 } else if (aName == nsGkAtoms::decoding) {
385 // Request sync or async image decoding.
386 SetSyncDecodingHint(
387 aValue && static_cast<ImageDecodingType>(aValue->GetEnumValue()) ==
388 ImageDecodingType::Sync);
389 } else if (aName == nsGkAtoms::referrerpolicy) {
390 ReferrerPolicy referrerPolicy = GetReferrerPolicyAsEnum();
391 // FIXME(emilio): Why only when not in responsive mode? Also see below for
392 // aNotify.
393 forceReload = aNotify && !InResponsiveMode() &&
394 referrerPolicy != ReferrerPolicy::_empty &&
395 referrerPolicy != ReferrerPolicyFromAttr(aOldValue);
396 } else if (aName == nsGkAtoms::crossorigin) {
397 // FIXME(emilio): The aNotify bit seems a bit suspicious, but it is useful
398 // to avoid extra sync loads, specially in non-responsive mode. Ideally we
399 // can unify the responsive and non-responsive code paths (bug 1076583), and
400 // simplify this a bit.
401 forceReload = aNotify && GetCORSMode() != AttrValueToCORSMode(aOldValue);
402 }
403
404 if (forceReload) {
405 // Because we load image synchronously in non-responsive-mode, we need to do
406 // reload after the attribute has been set if the reload is triggered by
407 // cross origin / referrer policy changing.
408 //
409 // Mark channel as urgent-start before load image if the image load is
410 // initiated by a user interaction.
411 mUseUrgentStartForChannel = UserActivation::IsHandlingUserInput();
412 if (InResponsiveMode()) {
413 // Per spec, full selection runs when this changes, even though
414 // it doesn't directly affect the source selection
415 QueueImageLoadTask(true);
416 } else if (ShouldLoadImage()) {
417 // Bug 1076583 - We still use the older synchronous algorithm in
418 // non-responsive mode. Force a new load of the image with the
419 // new cross origin policy
420 ForceReload(aNotify, IgnoreErrors());
421 }
422 }
423
424 return nsGenericHTMLElement::AfterSetAttr(
425 aNameSpaceID, aName, aValue, aOldValue, aMaybeScriptedPrincipal, aNotify);
426 }
427
OnAttrSetButNotChanged(int32_t aNamespaceID,nsAtom * aName,const nsAttrValueOrString & aValue,bool aNotify)428 nsresult HTMLImageElement::OnAttrSetButNotChanged(
429 int32_t aNamespaceID, nsAtom* aName, const nsAttrValueOrString& aValue,
430 bool aNotify) {
431 AfterMaybeChangeAttr(aNamespaceID, aName, aValue, nullptr, nullptr, aNotify);
432 return nsGenericHTMLElement::OnAttrSetButNotChanged(aNamespaceID, aName,
433 aValue, aNotify);
434 }
435
AfterMaybeChangeAttr(int32_t aNamespaceID,nsAtom * aName,const nsAttrValueOrString & aValue,const nsAttrValue * aOldValue,nsIPrincipal * aMaybeScriptedPrincipal,bool aNotify)436 void HTMLImageElement::AfterMaybeChangeAttr(
437 int32_t aNamespaceID, nsAtom* aName, const nsAttrValueOrString& aValue,
438 const nsAttrValue* aOldValue, nsIPrincipal* aMaybeScriptedPrincipal,
439 bool aNotify) {
440 if (aNamespaceID != kNameSpaceID_None || aName != nsGkAtoms::src) {
441 return;
442 }
443
444 // We need to force our image to reload. This must be done here, not in
445 // AfterSetAttr or BeforeSetAttr, because we want to do it even if the attr is
446 // being set to its existing value, which is normally optimized away as a
447 // no-op.
448 //
449 // If we are in responsive mode, we drop the forced reload behavior,
450 // but still trigger a image load task for img.src = img.src per
451 // spec.
452 //
453 // Both cases handle unsetting src in AfterSetAttr
454 // Mark channel as urgent-start before load image if the image load is
455 // initaiated by a user interaction.
456 mUseUrgentStartForChannel = UserActivation::IsHandlingUserInput();
457
458 mSrcTriggeringPrincipal = nsContentUtils::GetAttrTriggeringPrincipal(
459 this, aValue.String(), aMaybeScriptedPrincipal);
460
461 if (InResponsiveMode()) {
462 if (mResponsiveSelector && mResponsiveSelector->Content() == this) {
463 mResponsiveSelector->SetDefaultSource(aValue.String(),
464 mSrcTriggeringPrincipal);
465 }
466 QueueImageLoadTask(true);
467 } else if (aNotify && ShouldLoadImage()) {
468 // If aNotify is false, we are coming from the parser or some such place;
469 // we'll get bound after all the attributes have been set, so we'll do the
470 // sync image load from BindToTree. Skip the LoadImage call in that case.
471
472 // Note that this sync behavior is partially removed from the spec, bug
473 // 1076583
474
475 // A hack to get animations to reset. See bug 594771.
476 mNewRequestsWillNeedAnimationReset = true;
477
478 // Force image loading here, so that we'll try to load the image from
479 // network if it's set to be not cacheable.
480 // Potentially, false could be passed here rather than aNotify since
481 // UpdateState will be called by SetAttrAndNotify, but there are two
482 // obstacles to this: 1) LoadImage will end up calling
483 // UpdateState(aNotify), and we do not want it to call UpdateState(false)
484 // when aNotify is true, and 2) When this function is called by
485 // OnAttrSetButNotChanged, SetAttrAndNotify will not subsequently call
486 // UpdateState.
487 LoadSelectedImage(/* aForce = */ true, aNotify,
488 /* aAlwaysLoad = */ true);
489
490 mNewRequestsWillNeedAnimationReset = false;
491 }
492 }
493
GetEventTargetParent(EventChainPreVisitor & aVisitor)494 void HTMLImageElement::GetEventTargetParent(EventChainPreVisitor& aVisitor) {
495 // We handle image element with attribute ismap in its corresponding frame
496 // element. Set mMultipleActionsPrevented here to prevent the click event
497 // trigger the behaviors in Element::PostHandleEventForLinks
498 WidgetMouseEvent* mouseEvent = aVisitor.mEvent->AsMouseEvent();
499 if (mouseEvent && mouseEvent->IsLeftClickEvent() && IsMap()) {
500 mouseEvent->mFlags.mMultipleActionsPrevented = true;
501 }
502 nsGenericHTMLElement::GetEventTargetParent(aVisitor);
503 }
504
IsHTMLFocusable(bool aWithMouse,bool * aIsFocusable,int32_t * aTabIndex)505 bool HTMLImageElement::IsHTMLFocusable(bool aWithMouse, bool* aIsFocusable,
506 int32_t* aTabIndex) {
507 int32_t tabIndex = TabIndex();
508
509 if (IsInComposedDoc() && FindImageMap()) {
510 if (aTabIndex) {
511 // Use tab index on individual map areas
512 *aTabIndex = (sTabFocusModel & eTabFocus_linksMask) ? 0 : -1;
513 }
514 // Image map is not focusable itself, but flag as tabbable
515 // so that image map areas get walked into.
516 *aIsFocusable = false;
517
518 return false;
519 }
520
521 if (aTabIndex) {
522 // Can be in tab order if tabindex >=0 and form controls are tabbable.
523 *aTabIndex = (sTabFocusModel & eTabFocus_formElementsMask) ? tabIndex : -1;
524 }
525
526 *aIsFocusable = IsFormControlDefaultFocusable(aWithMouse) &&
527 (tabIndex >= 0 || GetTabIndexAttrValue().isSome());
528
529 return false;
530 }
531
BindToTree(BindContext & aContext,nsINode & aParent)532 nsresult HTMLImageElement::BindToTree(BindContext& aContext, nsINode& aParent) {
533 nsresult rv = nsGenericHTMLElement::BindToTree(aContext, aParent);
534 NS_ENSURE_SUCCESS(rv, rv);
535
536 nsImageLoadingContent::BindToTree(aContext, aParent);
537
538 UpdateFormOwner();
539
540 if (HaveSrcsetOrInPicture()) {
541 if (IsInComposedDoc() && !mInDocResponsiveContent) {
542 aContext.OwnerDoc().AddResponsiveContent(this);
543 mInDocResponsiveContent = true;
544 }
545
546 // Mark channel as urgent-start before load image if the image load is
547 // initaiated by a user interaction.
548 mUseUrgentStartForChannel = UserActivation::IsHandlingUserInput();
549
550 // Run selection algorithm when an img element is inserted into a document
551 // in order to react to changes in the environment. See note of
552 // https://html.spec.whatwg.org/multipage/embedded-content.html#img-environment-changes
553 QueueImageLoadTask(false);
554 } else if (!InResponsiveMode() && HasAttr(nsGkAtoms::src)) {
555 // We skip loading when our attributes were set from parser land,
556 // so trigger a aForce=false load now to check if things changed.
557 // This isn't necessary for responsive mode, since creating the
558 // image load task is asynchronous we don't need to take special
559 // care to avoid doing so when being filled by the parser.
560
561 // Mark channel as urgent-start before load image if the image load is
562 // initaiated by a user interaction.
563 mUseUrgentStartForChannel = UserActivation::IsHandlingUserInput();
564
565 // We still act synchronously for the non-responsive case (Bug
566 // 1076583), but still need to delay if it is unsafe to run
567 // script.
568
569 // If loading is temporarily disabled, don't even launch MaybeLoadImage.
570 // Otherwise MaybeLoadImage may run later when someone has reenabled
571 // loading.
572 if (LoadingEnabled() && ShouldLoadImage()) {
573 nsContentUtils::AddScriptRunner(
574 NewRunnableMethod<bool>("dom::HTMLImageElement::MaybeLoadImage", this,
575 &HTMLImageElement::MaybeLoadImage, false));
576 }
577 }
578
579 return rv;
580 }
581
UnbindFromTree(bool aNullParent)582 void HTMLImageElement::UnbindFromTree(bool aNullParent) {
583 if (mForm) {
584 if (aNullParent || !FindAncestorForm(mForm)) {
585 ClearForm(true);
586 } else {
587 UnsetFlags(MAYBE_ORPHAN_FORM_ELEMENT);
588 }
589 }
590
591 if (mInDocResponsiveContent) {
592 OwnerDoc()->RemoveResponsiveContent(this);
593 mInDocResponsiveContent = false;
594 }
595
596 nsImageLoadingContent::UnbindFromTree(aNullParent);
597 nsGenericHTMLElement::UnbindFromTree(aNullParent);
598 }
599
UpdateFormOwner()600 void HTMLImageElement::UpdateFormOwner() {
601 if (!mForm) {
602 mForm = FindAncestorForm();
603 }
604
605 if (mForm && !HasFlag(ADDED_TO_FORM)) {
606 // Now we need to add ourselves to the form
607 nsAutoString nameVal, idVal;
608 GetAttr(kNameSpaceID_None, nsGkAtoms::name, nameVal);
609 GetAttr(kNameSpaceID_None, nsGkAtoms::id, idVal);
610
611 SetFlags(ADDED_TO_FORM);
612
613 mForm->AddImageElement(this);
614
615 if (!nameVal.IsEmpty()) {
616 mForm->AddImageElementToTable(this, nameVal);
617 }
618
619 if (!idVal.IsEmpty()) {
620 mForm->AddImageElementToTable(this, idVal);
621 }
622 }
623 }
624
MaybeLoadImage(bool aAlwaysForceLoad)625 void HTMLImageElement::MaybeLoadImage(bool aAlwaysForceLoad) {
626 // Our base URI may have changed, or we may have had responsive parameters
627 // change while not bound to the tree. Re-parse src/srcset and call LoadImage,
628 // which is a no-op if it resolves to the same effective URI without aForce.
629
630 // Note, check LoadingEnabled() after LoadImage call.
631
632 LoadSelectedImage(aAlwaysForceLoad, /* aNotify */ true, aAlwaysForceLoad);
633
634 if (!LoadingEnabled()) {
635 CancelImageRequests(true);
636 }
637 }
638
IntrinsicState() const639 EventStates HTMLImageElement::IntrinsicState() const {
640 return nsGenericHTMLElement::IntrinsicState() |
641 nsImageLoadingContent::ImageState();
642 }
643
NodeInfoChanged(Document * aOldDoc)644 void HTMLImageElement::NodeInfoChanged(Document* aOldDoc) {
645 nsGenericHTMLElement::NodeInfoChanged(aOldDoc);
646
647 // Unlike the LazyLoadImageObserver, the intersection observer
648 // for the viewport could contain the element even if
649 // it's not lazy-loading. For instance, the element has
650 // started to load, but haven't reached to the viewport.
651 // So here we always try to unobserve it.
652 if (auto* observer = aOldDoc->GetLazyLoadImageObserverViewport()) {
653 observer->Unobserve(*this);
654 }
655
656 if (mLazyLoading) {
657 aOldDoc->GetLazyLoadImageObserver()->Unobserve(*this);
658 aOldDoc->DecLazyLoadImageCount();
659 mLazyLoading = false;
660 SetLazyLoading();
661 }
662
663 // Force reload image if adoption steps are run.
664 // If loading is temporarily disabled, don't even launch script runner.
665 // Otherwise script runner may run later when someone has reenabled loading.
666 StartLoadingIfNeeded();
667 }
668
669 // static
Image(const GlobalObject & aGlobal,const Optional<uint32_t> & aWidth,const Optional<uint32_t> & aHeight,ErrorResult & aError)670 already_AddRefed<HTMLImageElement> HTMLImageElement::Image(
671 const GlobalObject& aGlobal, const Optional<uint32_t>& aWidth,
672 const Optional<uint32_t>& aHeight, ErrorResult& aError) {
673 nsCOMPtr<nsPIDOMWindowInner> win = do_QueryInterface(aGlobal.GetAsSupports());
674 Document* doc;
675 if (!win || !(doc = win->GetExtantDoc())) {
676 aError.Throw(NS_ERROR_FAILURE);
677 return nullptr;
678 }
679
680 RefPtr<mozilla::dom::NodeInfo> nodeInfo = doc->NodeInfoManager()->GetNodeInfo(
681 nsGkAtoms::img, nullptr, kNameSpaceID_XHTML, ELEMENT_NODE);
682
683 auto* nim = nodeInfo->NodeInfoManager();
684 RefPtr<HTMLImageElement> img = new (nim) HTMLImageElement(nodeInfo.forget());
685
686 if (aWidth.WasPassed()) {
687 img->SetWidth(aWidth.Value(), aError);
688 if (aError.Failed()) {
689 return nullptr;
690 }
691
692 if (aHeight.WasPassed()) {
693 img->SetHeight(aHeight.Value(), aError);
694 if (aError.Failed()) {
695 return nullptr;
696 }
697 }
698 }
699
700 return img.forget();
701 }
702
Height()703 uint32_t HTMLImageElement::Height() { return GetWidthHeightForImage().height; }
704
Width()705 uint32_t HTMLImageElement::Width() { return GetWidthHeightForImage().width; }
706
NaturalSize()707 nsIntSize HTMLImageElement::NaturalSize() {
708 if (!mCurrentRequest) {
709 return {};
710 }
711
712 nsCOMPtr<imgIContainer> image;
713 mCurrentRequest->GetImage(getter_AddRefs(image));
714 if (!image) {
715 return {};
716 }
717
718 nsIntSize size;
719 Unused << image->GetHeight(&size.height);
720 Unused << image->GetWidth(&size.width);
721
722 ImageResolution resolution = image->GetResolution();
723 // NOTE(emilio): What we implement here matches the image-set() spec, but it's
724 // unclear whether this is the right thing to do, see
725 // https://github.com/whatwg/html/pull/5574#issuecomment-826335244.
726 if (mResponsiveSelector) {
727 float density = mResponsiveSelector->GetSelectedImageDensity();
728 MOZ_ASSERT(density >= 0.0);
729 resolution.ScaleBy(density);
730 }
731
732 resolution.ApplyTo(size.width, size.height);
733 return size;
734 }
735
CopyInnerTo(HTMLImageElement * aDest)736 nsresult HTMLImageElement::CopyInnerTo(HTMLImageElement* aDest) {
737 nsresult rv = nsGenericHTMLElement::CopyInnerTo(aDest);
738 if (NS_FAILED(rv)) {
739 return rv;
740 }
741
742 // In SetAttr (called from nsGenericHTMLElement::CopyInnerTo), aDest skipped
743 // doing the image load because we passed in false for aNotify. But we
744 // really do want it to do the load, so set it up to happen once the cloning
745 // reaches a stable state.
746 if (!aDest->InResponsiveMode() && aDest->HasAttr(nsGkAtoms::src) &&
747 aDest->ShouldLoadImage()) {
748 // Mark channel as urgent-start before load image if the image load is
749 // initaiated by a user interaction.
750 mUseUrgentStartForChannel = UserActivation::IsHandlingUserInput();
751
752 nsContentUtils::AddScriptRunner(
753 NewRunnableMethod<bool>("dom::HTMLImageElement::MaybeLoadImage", aDest,
754 &HTMLImageElement::MaybeLoadImage, false));
755 }
756
757 return NS_OK;
758 }
759
GetCORSMode()760 CORSMode HTMLImageElement::GetCORSMode() {
761 return AttrValueToCORSMode(GetParsedAttr(nsGkAtoms::crossorigin));
762 }
763
WrapNode(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)764 JSObject* HTMLImageElement::WrapNode(JSContext* aCx,
765 JS::Handle<JSObject*> aGivenProto) {
766 return HTMLImageElement_Binding::Wrap(aCx, this, aGivenProto);
767 }
768
769 #ifdef DEBUG
GetForm() const770 HTMLFormElement* HTMLImageElement::GetForm() const { return mForm; }
771 #endif
772
SetForm(HTMLFormElement * aForm)773 void HTMLImageElement::SetForm(HTMLFormElement* aForm) {
774 MOZ_ASSERT(aForm, "Don't pass null here");
775 NS_ASSERTION(!mForm,
776 "We don't support switching from one non-null form to another.");
777
778 mForm = aForm;
779 }
780
ClearForm(bool aRemoveFromForm)781 void HTMLImageElement::ClearForm(bool aRemoveFromForm) {
782 NS_ASSERTION((mForm != nullptr) == HasFlag(ADDED_TO_FORM),
783 "Form control should have had flag set correctly");
784
785 if (!mForm) {
786 return;
787 }
788
789 if (aRemoveFromForm) {
790 nsAutoString nameVal, idVal;
791 GetAttr(kNameSpaceID_None, nsGkAtoms::name, nameVal);
792 GetAttr(kNameSpaceID_None, nsGkAtoms::id, idVal);
793
794 mForm->RemoveImageElement(this);
795
796 if (!nameVal.IsEmpty()) {
797 mForm->RemoveImageElementFromTable(this, nameVal);
798 }
799
800 if (!idVal.IsEmpty()) {
801 mForm->RemoveImageElementFromTable(this, idVal);
802 }
803 }
804
805 UnsetFlags(ADDED_TO_FORM);
806 mForm = nullptr;
807 }
808
QueueImageLoadTask(bool aAlwaysLoad)809 void HTMLImageElement::QueueImageLoadTask(bool aAlwaysLoad) {
810 // If loading is temporarily disabled, we don't want to queue tasks
811 // that may then run when loading is re-enabled.
812 if (!LoadingEnabled() || !ShouldLoadImage()) {
813 return;
814 }
815
816 // Ensure that we don't overwrite a previous load request that requires
817 // a complete load to occur.
818 bool alwaysLoad = aAlwaysLoad;
819 if (mPendingImageLoadTask) {
820 alwaysLoad = alwaysLoad || mPendingImageLoadTask->AlwaysLoad();
821 }
822 RefPtr<ImageLoadTask> task =
823 new ImageLoadTask(this, alwaysLoad, mUseUrgentStartForChannel);
824 // The task checks this to determine if it was the last
825 // queued event, and so earlier tasks are implicitly canceled.
826 mPendingImageLoadTask = task;
827 CycleCollectedJSContext::Get()->DispatchToMicroTask(task.forget());
828 }
829
HaveSrcsetOrInPicture()830 bool HTMLImageElement::HaveSrcsetOrInPicture() {
831 if (HasAttr(nsGkAtoms::srcset)) {
832 return true;
833 }
834
835 Element* parent = GetParentElement();
836 return parent && parent->IsHTMLElement(nsGkAtoms::picture);
837 }
838
InResponsiveMode()839 bool HTMLImageElement::InResponsiveMode() {
840 // When we lose srcset or leave a <picture> element, the fallback to img.src
841 // will happen from the microtask, and we should behave responsively in the
842 // interim
843 return mResponsiveSelector || mPendingImageLoadTask ||
844 HaveSrcsetOrInPicture();
845 }
846
SelectedSourceMatchesLast(nsIURI * aSelectedSource)847 bool HTMLImageElement::SelectedSourceMatchesLast(nsIURI* aSelectedSource) {
848 // If there was no selected source previously, we don't want to short-circuit
849 // the load. Similarly for if there is no newly selected source.
850 if (!mLastSelectedSource || !aSelectedSource) {
851 return false;
852 }
853 bool equal = false;
854 return NS_SUCCEEDED(mLastSelectedSource->Equals(aSelectedSource, &equal)) &&
855 equal;
856 }
857
LoadSelectedImage(bool aForce,bool aNotify,bool aAlwaysLoad)858 nsresult HTMLImageElement::LoadSelectedImage(bool aForce, bool aNotify,
859 bool aAlwaysLoad) {
860 double currentDensity = 1.0; // default to 1.0 for the src attribute case
861
862 // Helper to update state when only density may have changed (i.e., the source
863 // to load hasn't changed, and we don't do any request at all). We need (apart
864 // from updating our internal state) to tell the image frame because its
865 // intrinsic size may have changed.
866 //
867 // In the case we actually trigger a new load, that load will trigger a call
868 // to nsImageFrame::NotifyNewCurrentRequest, which takes care of that for us.
869 auto UpdateDensityOnly = [&]() -> void {
870 if (mCurrentDensity == currentDensity) {
871 return;
872 }
873 mCurrentDensity = currentDensity;
874 if (nsImageFrame* f = do_QueryFrame(GetPrimaryFrame())) {
875 f->ResponsiveContentDensityChanged();
876 }
877 };
878
879 if (aForce) {
880 // In responsive mode we generally want to re-run the full selection
881 // algorithm whenever starting a new load, per spec.
882 //
883 // This also causes us to re-resolve the URI as appropriate.
884 const bool sourceChanged = UpdateResponsiveSource();
885
886 if (mResponsiveSelector) {
887 currentDensity = mResponsiveSelector->GetSelectedImageDensity();
888 }
889
890 if (!sourceChanged && !aAlwaysLoad) {
891 UpdateDensityOnly();
892 return NS_OK;
893 }
894 } else if (mResponsiveSelector) {
895 currentDensity = mResponsiveSelector->GetSelectedImageDensity();
896 }
897
898 nsCOMPtr<nsIURI> selectedSource;
899 nsCOMPtr<nsIPrincipal> triggeringPrincipal;
900 ImageLoadType type = eImageLoadType_Normal;
901 bool hasSrc = false;
902 if (mResponsiveSelector) {
903 selectedSource = mResponsiveSelector->GetSelectedImageURL();
904 triggeringPrincipal =
905 mResponsiveSelector->GetSelectedImageTriggeringPrincipal();
906 type = eImageLoadType_Imageset;
907 } else {
908 nsAutoString src;
909 hasSrc = GetAttr(nsGkAtoms::src, src);
910 if (hasSrc && !src.IsEmpty()) {
911 Document* doc = OwnerDoc();
912 StringToURI(src, doc, getter_AddRefs(selectedSource));
913 if (HaveSrcsetOrInPicture()) {
914 // If we have a srcset attribute or are in a <picture> element, we
915 // always use the Imageset load type, even if we parsed no valid
916 // responsive sources from either, per spec.
917 type = eImageLoadType_Imageset;
918 }
919 triggeringPrincipal = mSrcTriggeringPrincipal;
920 }
921 }
922
923 if (!aAlwaysLoad && SelectedSourceMatchesLast(selectedSource)) {
924 UpdateDensityOnly();
925 return NS_OK;
926 }
927
928 // Before we actually defer the lazy-loading
929 if (mLazyLoading) {
930 if (!selectedSource ||
931 !nsContentUtils::IsImageAvailable(this, selectedSource,
932 triggeringPrincipal, GetCORSMode())) {
933 return NS_OK;
934 }
935 StopLazyLoading(FromIntersectionObserver::No, StartLoading::No);
936 }
937
938 nsresult rv = NS_ERROR_FAILURE;
939
940 // src triggers an error event on invalid URI, unlike other loads.
941 if (selectedSource || hasSrc) {
942 rv = LoadImage(selectedSource, aForce, aNotify, type, triggeringPrincipal);
943 }
944
945 mLastSelectedSource = selectedSource;
946 mCurrentDensity = currentDensity;
947
948 if (NS_FAILED(rv)) {
949 CancelImageRequests(aNotify);
950 }
951 return rv;
952 }
953
PictureSourceSrcsetChanged(nsIContent * aSourceNode,const nsAString & aNewValue,bool aNotify)954 void HTMLImageElement::PictureSourceSrcsetChanged(nsIContent* aSourceNode,
955 const nsAString& aNewValue,
956 bool aNotify) {
957 MOZ_ASSERT(aSourceNode == this || IsPreviousSibling(aSourceNode, this),
958 "Should not be getting notifications for non-previous-siblings");
959
960 nsIContent* currentSrc =
961 mResponsiveSelector ? mResponsiveSelector->Content() : nullptr;
962
963 if (aSourceNode == currentSrc) {
964 // We're currently using this node as our responsive selector
965 // source.
966 nsCOMPtr<nsIPrincipal> principal;
967 if (aSourceNode == this) {
968 principal = mSrcsetTriggeringPrincipal;
969 } else if (auto* source = HTMLSourceElement::FromNode(aSourceNode)) {
970 principal = source->GetSrcsetTriggeringPrincipal();
971 }
972 mResponsiveSelector->SetCandidatesFromSourceSet(aNewValue, principal);
973 }
974
975 if (!mInDocResponsiveContent && IsInComposedDoc()) {
976 OwnerDoc()->AddResponsiveContent(this);
977 mInDocResponsiveContent = true;
978 }
979
980 // This always triggers the image update steps per the spec, even if
981 // we are not using this source.
982 QueueImageLoadTask(true);
983 }
984
PictureSourceSizesChanged(nsIContent * aSourceNode,const nsAString & aNewValue,bool aNotify)985 void HTMLImageElement::PictureSourceSizesChanged(nsIContent* aSourceNode,
986 const nsAString& aNewValue,
987 bool aNotify) {
988 MOZ_ASSERT(aSourceNode == this || IsPreviousSibling(aSourceNode, this),
989 "Should not be getting notifications for non-previous-siblings");
990
991 nsIContent* currentSrc =
992 mResponsiveSelector ? mResponsiveSelector->Content() : nullptr;
993
994 if (aSourceNode == currentSrc) {
995 // We're currently using this node as our responsive selector
996 // source.
997 mResponsiveSelector->SetSizesFromDescriptor(aNewValue);
998 }
999
1000 // This always triggers the image update steps per the spec, even if
1001 // we are not using this source.
1002 QueueImageLoadTask(true);
1003 }
1004
PictureSourceMediaOrTypeChanged(nsIContent * aSourceNode,bool aNotify)1005 void HTMLImageElement::PictureSourceMediaOrTypeChanged(nsIContent* aSourceNode,
1006 bool aNotify) {
1007 MOZ_ASSERT(IsPreviousSibling(aSourceNode, this),
1008 "Should not be getting notifications for non-previous-siblings");
1009
1010 // This always triggers the image update steps per the spec, even if
1011 // we are not switching to/from this source
1012 QueueImageLoadTask(true);
1013 }
1014
PictureSourceAdded(nsIContent * aSourceNode)1015 void HTMLImageElement::PictureSourceAdded(nsIContent* aSourceNode) {
1016 MOZ_ASSERT(aSourceNode == this || IsPreviousSibling(aSourceNode, this),
1017 "Should not be getting notifications for non-previous-siblings");
1018
1019 QueueImageLoadTask(true);
1020 }
1021
PictureSourceRemoved(nsIContent * aSourceNode)1022 void HTMLImageElement::PictureSourceRemoved(nsIContent* aSourceNode) {
1023 MOZ_ASSERT(aSourceNode == this || IsPreviousSibling(aSourceNode, this),
1024 "Should not be getting notifications for non-previous-siblings");
1025
1026 QueueImageLoadTask(true);
1027 }
1028
UpdateResponsiveSource()1029 bool HTMLImageElement::UpdateResponsiveSource() {
1030 bool hadSelector = !!mResponsiveSelector;
1031
1032 nsIContent* currentSource =
1033 mResponsiveSelector ? mResponsiveSelector->Content() : nullptr;
1034 Element* parent = nsINode::GetParentElement();
1035
1036 nsINode* candidateSource = nullptr;
1037 if (parent && parent->IsHTMLElement(nsGkAtoms::picture)) {
1038 // Walk source nodes previous to ourselves
1039 candidateSource = parent->GetFirstChild();
1040 } else {
1041 candidateSource = this;
1042 }
1043
1044 while (candidateSource) {
1045 if (candidateSource == currentSource) {
1046 // found no better source before current, re-run selection on
1047 // that and keep it if it's still usable.
1048 bool changed = mResponsiveSelector->SelectImage(true);
1049 if (mResponsiveSelector->NumCandidates()) {
1050 bool isUsableCandidate = true;
1051
1052 // an otherwise-usable source element may still have a media query that
1053 // may not match any more.
1054 if (candidateSource->IsHTMLElement(nsGkAtoms::source) &&
1055 !SourceElementMatches(candidateSource->AsElement())) {
1056 isUsableCandidate = false;
1057 }
1058
1059 if (isUsableCandidate) {
1060 return changed;
1061 }
1062 }
1063
1064 // no longer valid
1065 mResponsiveSelector = nullptr;
1066 if (candidateSource == this) {
1067 // No further possibilities
1068 break;
1069 }
1070 } else if (candidateSource == this) {
1071 // We are the last possible source
1072 if (!TryCreateResponsiveSelector(candidateSource->AsElement())) {
1073 // Failed to find any source
1074 mResponsiveSelector = nullptr;
1075 }
1076 break;
1077 } else if (candidateSource->IsHTMLElement(nsGkAtoms::source) &&
1078 TryCreateResponsiveSelector(candidateSource->AsElement())) {
1079 // This led to a valid source, stop
1080 break;
1081 }
1082 candidateSource = candidateSource->GetNextSibling();
1083 }
1084
1085 if (!candidateSource) {
1086 // Ran out of siblings without finding ourself, e.g. XBL magic.
1087 mResponsiveSelector = nullptr;
1088 }
1089
1090 // If we reach this point, either:
1091 // - there was no selector originally, and there is not one now
1092 // - there was no selector originally, and there is one now
1093 // - there was a selector, and there is a different one now
1094 // - there was a selector, and there is not one now
1095 return hadSelector || mResponsiveSelector;
1096 }
1097
1098 /*static */
SupportedPictureSourceType(const nsAString & aType)1099 bool HTMLImageElement::SupportedPictureSourceType(const nsAString& aType) {
1100 nsAutoString type;
1101 nsAutoString params;
1102
1103 nsContentUtils::SplitMimeType(aType, type, params);
1104 if (type.IsEmpty()) {
1105 return true;
1106 }
1107
1108 return imgLoader::SupportImageWithMimeType(
1109 NS_ConvertUTF16toUTF8(type), AcceptedMimeTypes::IMAGES_AND_DOCUMENTS);
1110 }
1111
SourceElementMatches(Element * aSourceElement)1112 bool HTMLImageElement::SourceElementMatches(Element* aSourceElement) {
1113 MOZ_ASSERT(aSourceElement->IsHTMLElement(nsGkAtoms::source));
1114
1115 DebugOnly<Element*> parent(nsINode::GetParentElement());
1116 MOZ_ASSERT(parent && parent->IsHTMLElement(nsGkAtoms::picture));
1117 MOZ_ASSERT(IsPreviousSibling(aSourceElement, this));
1118
1119 // Check media and type
1120 auto* src = static_cast<HTMLSourceElement*>(aSourceElement);
1121 if (!src->MatchesCurrentMedia()) {
1122 return false;
1123 }
1124
1125 nsAutoString type;
1126 if (src->GetAttr(kNameSpaceID_None, nsGkAtoms::type, type) &&
1127 !SupportedPictureSourceType(type)) {
1128 return false;
1129 }
1130
1131 return true;
1132 }
1133
TryCreateResponsiveSelector(Element * aSourceElement)1134 bool HTMLImageElement::TryCreateResponsiveSelector(Element* aSourceElement) {
1135 nsCOMPtr<nsIPrincipal> principal;
1136
1137 // Skip if this is not a <source> with matching media query
1138 bool isSourceTag = aSourceElement->IsHTMLElement(nsGkAtoms::source);
1139 if (isSourceTag) {
1140 if (!SourceElementMatches(aSourceElement)) {
1141 return false;
1142 }
1143 auto* source = HTMLSourceElement::FromNode(aSourceElement);
1144 principal = source->GetSrcsetTriggeringPrincipal();
1145 } else if (aSourceElement->IsHTMLElement(nsGkAtoms::img)) {
1146 // Otherwise this is the <img> tag itself
1147 MOZ_ASSERT(aSourceElement == this);
1148 principal = mSrcsetTriggeringPrincipal;
1149 }
1150
1151 // Skip if has no srcset or an empty srcset
1152 nsString srcset;
1153 if (!aSourceElement->GetAttr(nsGkAtoms::srcset, srcset)) {
1154 return false;
1155 }
1156
1157 if (srcset.IsEmpty()) {
1158 return false;
1159 }
1160
1161 // Try to parse
1162 RefPtr<ResponsiveImageSelector> sel =
1163 new ResponsiveImageSelector(aSourceElement);
1164 if (!sel->SetCandidatesFromSourceSet(srcset, principal)) {
1165 // No possible candidates, don't need to bother parsing sizes
1166 return false;
1167 }
1168
1169 nsAutoString sizes;
1170 aSourceElement->GetAttr(nsGkAtoms::sizes, sizes);
1171 sel->SetSizesFromDescriptor(sizes);
1172
1173 // If this is the <img> tag, also pull in src as the default source
1174 if (!isSourceTag) {
1175 MOZ_ASSERT(aSourceElement == this);
1176 nsAutoString src;
1177 if (GetAttr(nsGkAtoms::src, src) && !src.IsEmpty()) {
1178 sel->SetDefaultSource(src, mSrcTriggeringPrincipal);
1179 }
1180 }
1181
1182 mResponsiveSelector = sel;
1183 return true;
1184 }
1185
1186 /* static */
SelectSourceForTagWithAttrs(Document * aDocument,bool aIsSourceTag,const nsAString & aSrcAttr,const nsAString & aSrcsetAttr,const nsAString & aSizesAttr,const nsAString & aTypeAttr,const nsAString & aMediaAttr,nsAString & aResult)1187 bool HTMLImageElement::SelectSourceForTagWithAttrs(
1188 Document* aDocument, bool aIsSourceTag, const nsAString& aSrcAttr,
1189 const nsAString& aSrcsetAttr, const nsAString& aSizesAttr,
1190 const nsAString& aTypeAttr, const nsAString& aMediaAttr,
1191 nsAString& aResult) {
1192 MOZ_ASSERT(aIsSourceTag || (aTypeAttr.IsEmpty() && aMediaAttr.IsEmpty()),
1193 "Passing type or media attrs makes no sense without aIsSourceTag");
1194 MOZ_ASSERT(!aIsSourceTag || aSrcAttr.IsEmpty(),
1195 "Passing aSrcAttr makes no sense with aIsSourceTag set");
1196
1197 if (aSrcsetAttr.IsEmpty()) {
1198 if (!aIsSourceTag) {
1199 // For an <img> with no srcset, we would always select the src attr.
1200 aResult.Assign(aSrcAttr);
1201 return true;
1202 }
1203 // Otherwise, a <source> without srcset is never selected
1204 return false;
1205 }
1206
1207 // Would not consider source tags with unsupported media or type
1208 if (aIsSourceTag &&
1209 ((!aMediaAttr.IsVoid() && !HTMLSourceElement::WouldMatchMediaForDocument(
1210 aMediaAttr, aDocument)) ||
1211 (!aTypeAttr.IsVoid() && !SupportedPictureSourceType(aTypeAttr)))) {
1212 return false;
1213 }
1214
1215 // Using srcset or picture <source>, build a responsive selector for this tag.
1216 RefPtr<ResponsiveImageSelector> sel = new ResponsiveImageSelector(aDocument);
1217
1218 sel->SetCandidatesFromSourceSet(aSrcsetAttr);
1219 if (!aSizesAttr.IsEmpty()) {
1220 sel->SetSizesFromDescriptor(aSizesAttr);
1221 }
1222 if (!aIsSourceTag) {
1223 sel->SetDefaultSource(aSrcAttr);
1224 }
1225
1226 if (sel->GetSelectedImageURLSpec(aResult)) {
1227 return true;
1228 }
1229
1230 if (!aIsSourceTag) {
1231 // <img> tag with no match would definitively load nothing.
1232 aResult.Truncate();
1233 return true;
1234 }
1235
1236 // <source> tags with no match would leave source yet-undetermined.
1237 return false;
1238 }
1239
DestroyContent()1240 void HTMLImageElement::DestroyContent() {
1241 mResponsiveSelector = nullptr;
1242
1243 nsImageLoadingContent::Destroy();
1244 nsGenericHTMLElement::DestroyContent();
1245 }
1246
MediaFeatureValuesChanged()1247 void HTMLImageElement::MediaFeatureValuesChanged() {
1248 QueueImageLoadTask(false);
1249 }
1250
ShouldLoadImage() const1251 bool HTMLImageElement::ShouldLoadImage() const {
1252 return OwnerDoc()->ShouldLoadImages();
1253 }
1254
SetLazyLoading()1255 void HTMLImageElement::SetLazyLoading() {
1256 if (mLazyLoading) {
1257 return;
1258 }
1259
1260 if (!StaticPrefs::dom_image_lazy_loading_enabled()) {
1261 return;
1262 }
1263
1264 // If scripting is disabled don't do lazy load.
1265 // https://whatpr.org/html/3752/images.html#updating-the-image-data
1266 //
1267 // Same for printing.
1268 Document* doc = OwnerDoc();
1269 if (!doc->IsScriptEnabled() || doc->IsStaticDocument()) {
1270 return;
1271 }
1272
1273 doc->EnsureLazyLoadImageObserver().Observe(*this);
1274 doc->EnsureLazyLoadImageObserverViewport().Observe(*this);
1275 doc->IncLazyLoadImageCount();
1276 mLazyLoading = true;
1277 UpdateImageState(true);
1278 }
1279
StartLoadingIfNeeded()1280 void HTMLImageElement::StartLoadingIfNeeded() {
1281 if (LoadingEnabled() && ShouldLoadImage()) {
1282 // Use script runner for the case the adopt is from appendChild.
1283 // Bug 1076583 - We still behave synchronously in the non-responsive case
1284 nsContentUtils::AddScriptRunner(
1285 InResponsiveMode()
1286 ? NewRunnableMethod<bool>(
1287 "dom::HTMLImageElement::QueueImageLoadTask", this,
1288 &HTMLImageElement::QueueImageLoadTask, true)
1289 : NewRunnableMethod<bool>("dom::HTMLImageElement::MaybeLoadImage",
1290 this, &HTMLImageElement::MaybeLoadImage,
1291 true));
1292 }
1293 }
1294
StopLazyLoading(FromIntersectionObserver aFromIntersectionObserver,StartLoading aStartLoading)1295 void HTMLImageElement::StopLazyLoading(
1296 FromIntersectionObserver aFromIntersectionObserver,
1297 StartLoading aStartLoading) {
1298 if (!mLazyLoading) {
1299 return;
1300 }
1301 mLazyLoading = false;
1302 Document* doc = OwnerDoc();
1303 if (auto* obs = doc->GetLazyLoadImageObserver()) {
1304 obs->Unobserve(*this);
1305 }
1306
1307 if (bool(aFromIntersectionObserver)) {
1308 doc->IncLazyLoadImageStarted();
1309 } else {
1310 doc->DecLazyLoadImageCount();
1311 if (auto* obs = doc->GetLazyLoadImageObserverViewport()) {
1312 obs->Unobserve(*this);
1313 }
1314 }
1315
1316 if (bool(aStartLoading)) {
1317 StartLoadingIfNeeded();
1318 }
1319 }
1320
LazyLoadImageReachedViewport()1321 void HTMLImageElement::LazyLoadImageReachedViewport() {
1322 Document* doc = OwnerDoc();
1323 if (auto* obs = doc->GetLazyLoadImageObserverViewport()) {
1324 obs->Unobserve(*this);
1325 }
1326 doc->IncLazyLoadImageReachViewport(!Complete());
1327 }
1328
1329 } // namespace mozilla::dom
1330