Backed out changeset 935617a393ae (bug 1917548) for causing a top crash as in bug...
[gecko.git] / dom / html / HTMLImageElement.cpp
blob626c6bdafc3602acad381650db0c21477e9205f9
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 #include "mozilla/dom/HTMLImageElement.h"
8 #include "mozilla/PresShell.h"
9 #include "mozilla/FocusModel.h"
10 #include "mozilla/dom/BindContext.h"
11 #include "mozilla/dom/BindingUtils.h"
12 #include "mozilla/dom/HTMLImageElementBinding.h"
13 #include "mozilla/dom/NameSpaceConstants.h"
14 #include "mozilla/dom/UnbindContext.h"
15 #include "nsGenericHTMLElement.h"
16 #include "nsGkAtoms.h"
17 #include "nsPresContext.h"
18 #include "nsSize.h"
19 #include "mozilla/dom/Document.h"
20 #include "nsImageFrame.h"
21 #include "nsContentUtils.h"
22 #include "nsContainerFrame.h"
23 #include "nsNodeInfoManager.h"
24 #include "mozilla/MouseEvents.h"
25 #include "nsFocusManager.h"
26 #include "mozilla/dom/DOMIntersectionObserver.h"
27 #include "mozilla/dom/HTMLFormElement.h"
28 #include "mozilla/dom/MutationEventBinding.h"
29 #include "mozilla/dom/UserActivation.h"
30 #include "nsAttrValueOrString.h"
31 #include "imgLoader.h"
33 // Responsive images!
34 #include "mozilla/dom/HTMLSourceElement.h"
35 #include "mozilla/dom/ResponsiveImageSelector.h"
37 #include "imgINotificationObserver.h"
38 #include "imgRequestProxy.h"
40 #include "mozilla/CycleCollectedJSContext.h"
42 #include "mozilla/EventDispatcher.h"
43 #include "mozilla/MappedDeclarationsBuilder.h"
44 #include "mozilla/Maybe.h"
45 #include "mozilla/RestyleManager.h"
47 #include "nsLayoutUtils.h"
49 using namespace mozilla::net;
50 using mozilla::Maybe;
52 NS_IMPL_NS_NEW_HTML_ELEMENT(Image)
54 #ifdef DEBUG
55 // Is aSubject a previous sibling of aNode.
56 static bool IsPreviousSibling(const nsINode* aSubject, const nsINode* aNode) {
57 if (aSubject == aNode) {
58 return false;
61 nsINode* parent = aSubject->GetParentNode();
62 if (parent && parent == aNode->GetParentNode()) {
63 const Maybe<uint32_t> indexOfSubject = parent->ComputeIndexOf(aSubject);
64 const Maybe<uint32_t> indexOfNode = parent->ComputeIndexOf(aNode);
65 if (MOZ_LIKELY(indexOfSubject.isSome() && indexOfNode.isSome())) {
66 return *indexOfSubject < *indexOfNode;
68 // XXX Keep the odd traditional behavior for now.
69 return indexOfSubject.isNothing() && indexOfNode.isSome();
72 return false;
74 #endif
76 namespace mozilla::dom {
78 // Calls LoadSelectedImage on host element unless it has been superseded or
79 // canceled -- this is the synchronous section of "update the image data".
80 // https://html.spec.whatwg.org/#update-the-image-data
81 class ImageLoadTask final : public MicroTaskRunnable {
82 public:
83 ImageLoadTask(HTMLImageElement* aElement, bool aAlwaysLoad,
84 bool aUseUrgentStartForChannel)
85 : mElement(aElement),
86 mDocument(aElement->OwnerDoc()),
87 mAlwaysLoad(aAlwaysLoad),
88 mUseUrgentStartForChannel(aUseUrgentStartForChannel) {
89 mDocument->BlockOnload();
92 void Run(AutoSlowOperation& aAso) override {
93 if (mElement->mPendingImageLoadTask == this) {
94 JSCallingLocation::AutoFallback fallback(&mCallingLocation);
95 mElement->ClearImageLoadTask();
96 mElement->mUseUrgentStartForChannel = mUseUrgentStartForChannel;
97 mElement->LoadSelectedImage(mAlwaysLoad);
99 mDocument->UnblockOnload(false);
102 bool Suppressed() override {
103 nsIGlobalObject* global = mElement->GetOwnerGlobal();
104 return global && global->IsInSyncOperation();
107 bool AlwaysLoad() const { return mAlwaysLoad; }
109 private:
110 ~ImageLoadTask() = default;
111 const RefPtr<HTMLImageElement> mElement;
112 const RefPtr<Document> mDocument;
113 const JSCallingLocation mCallingLocation{JSCallingLocation::Get()};
114 const bool mAlwaysLoad;
115 // True if we want to set nsIClassOfService::UrgentStart to the channel to get
116 // the response ASAP for better user responsiveness.
117 const bool mUseUrgentStartForChannel;
120 HTMLImageElement::HTMLImageElement(
121 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
122 : nsGenericHTMLElement(std::move(aNodeInfo)) {
123 // We start out broken
124 AddStatesSilently(ElementState::BROKEN);
127 HTMLImageElement::~HTMLImageElement() {
128 nsImageLoadingContent::Destroy();
129 if (mInDocResponsiveContent) {
130 OwnerDoc()->RemoveResponsiveContent(this);
131 mInDocResponsiveContent = false;
135 NS_IMPL_CYCLE_COLLECTION_INHERITED(HTMLImageElement, nsGenericHTMLElement,
136 mResponsiveSelector)
138 NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED(HTMLImageElement,
139 nsGenericHTMLElement,
140 nsIImageLoadingContent,
141 imgINotificationObserver)
143 NS_IMPL_ELEMENT_CLONE(HTMLImageElement)
145 bool HTMLImageElement::IsInteractiveHTMLContent() const {
146 return HasAttr(nsGkAtoms::usemap) ||
147 nsGenericHTMLElement::IsInteractiveHTMLContent();
150 void HTMLImageElement::AsyncEventRunning(AsyncEventDispatcher* aEvent) {
151 nsImageLoadingContent::AsyncEventRunning(aEvent);
154 void HTMLImageElement::GetCurrentSrc(nsAString& aValue) {
155 nsCOMPtr<nsIURI> currentURI;
156 GetCurrentURI(getter_AddRefs(currentURI));
157 if (currentURI) {
158 nsAutoCString spec;
159 currentURI->GetSpec(spec);
160 CopyUTF8toUTF16(spec, aValue);
161 } else {
162 SetDOMStringToNull(aValue);
166 bool HTMLImageElement::Draggable() const {
167 // images may be dragged unless the draggable attribute is false
168 return !AttrValueIs(kNameSpaceID_None, nsGkAtoms::draggable,
169 nsGkAtoms::_false, eIgnoreCase);
172 bool HTMLImageElement::Complete() {
173 // It is still not clear what value should img.complete return in various
174 // cases, see https://github.com/whatwg/html/issues/4884
175 if (!HasAttr(nsGkAtoms::srcset) && !HasNonEmptyAttr(nsGkAtoms::src)) {
176 return true;
179 if (!mCurrentRequest || mPendingRequest || mPendingImageLoadTask) {
180 return false;
183 uint32_t status;
184 mCurrentRequest->GetImageStatus(&status);
185 return (status &
186 (imgIRequest::STATUS_LOAD_COMPLETE | imgIRequest::STATUS_ERROR)) != 0;
189 CSSIntPoint HTMLImageElement::GetXY() {
190 nsIFrame* frame = GetPrimaryFrame(FlushType::Layout);
191 if (!frame) {
192 return CSSIntPoint(0, 0);
194 return CSSIntPoint::FromAppUnitsRounded(
195 frame->GetOffsetTo(frame->PresShell()->GetRootFrame()));
198 int32_t HTMLImageElement::X() { return GetXY().x; }
200 int32_t HTMLImageElement::Y() { return GetXY().y; }
202 void HTMLImageElement::GetDecoding(nsAString& aValue) {
203 GetEnumAttr(nsGkAtoms::decoding, kDecodingTableDefault->tag, aValue);
206 already_AddRefed<Promise> HTMLImageElement::Decode(ErrorResult& aRv) {
207 return nsImageLoadingContent::QueueDecodeAsync(aRv);
210 bool HTMLImageElement::ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
211 const nsAString& aValue,
212 nsIPrincipal* aMaybeScriptedPrincipal,
213 nsAttrValue& aResult) {
214 if (aNamespaceID == kNameSpaceID_None) {
215 if (aAttribute == nsGkAtoms::align) {
216 return ParseAlignValue(aValue, aResult);
218 if (aAttribute == nsGkAtoms::crossorigin) {
219 ParseCORSValue(aValue, aResult);
220 return true;
222 if (aAttribute == nsGkAtoms::decoding) {
223 return aResult.ParseEnumValue(aValue, kDecodingTable,
224 /* aCaseSensitive = */ false,
225 kDecodingTableDefault);
227 if (aAttribute == nsGkAtoms::loading) {
228 return ParseLoadingAttribute(aValue, aResult);
230 if (aAttribute == nsGkAtoms::fetchpriority) {
231 ParseFetchPriority(aValue, aResult);
232 return true;
234 if (ParseImageAttribute(aAttribute, aValue, aResult)) {
235 return true;
239 return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
240 aMaybeScriptedPrincipal, aResult);
243 void HTMLImageElement::MapAttributesIntoRule(
244 MappedDeclarationsBuilder& aBuilder) {
245 MapImageAlignAttributeInto(aBuilder);
246 MapImageBorderAttributeInto(aBuilder);
247 MapImageMarginAttributeInto(aBuilder);
248 MapImageSizeAttributesInto(aBuilder, MapAspectRatio::Yes);
249 MapCommonAttributesInto(aBuilder);
252 nsChangeHint HTMLImageElement::GetAttributeChangeHint(const nsAtom* aAttribute,
253 int32_t aModType) const {
254 nsChangeHint retval =
255 nsGenericHTMLElement::GetAttributeChangeHint(aAttribute, aModType);
256 if (aAttribute == nsGkAtoms::usemap || aAttribute == nsGkAtoms::ismap) {
257 retval |= nsChangeHint_ReconstructFrame;
258 } else if (aAttribute == nsGkAtoms::alt) {
259 if (aModType == MutationEvent_Binding::ADDITION ||
260 aModType == MutationEvent_Binding::REMOVAL) {
261 retval |= nsChangeHint_ReconstructFrame;
264 return retval;
267 NS_IMETHODIMP_(bool)
268 HTMLImageElement::IsAttributeMapped(const nsAtom* aAttribute) const {
269 static const MappedAttributeEntry* const map[] = {
270 sCommonAttributeMap, sImageMarginSizeAttributeMap,
271 sImageBorderAttributeMap, sImageAlignAttributeMap};
273 return FindAttributeDependence(aAttribute, map);
276 nsMapRuleToAttributesFunc HTMLImageElement::GetAttributeMappingFunction()
277 const {
278 return &MapAttributesIntoRule;
281 void HTMLImageElement::BeforeSetAttr(int32_t aNameSpaceID, nsAtom* aName,
282 const nsAttrValue* aValue, bool aNotify) {
283 if (aNameSpaceID == kNameSpaceID_None && mForm &&
284 (aName == nsGkAtoms::name || aName == nsGkAtoms::id)) {
285 // remove the image from the hashtable as needed
286 if (const auto* old = GetParsedAttr(aName); old && !old->IsEmptyString()) {
287 mForm->RemoveImageElementFromTable(
288 this, nsDependentAtomString(old->GetAtomValue()));
292 return nsGenericHTMLElement::BeforeSetAttr(aNameSpaceID, aName, aValue,
293 aNotify);
296 void HTMLImageElement::AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
297 const nsAttrValue* aValue,
298 const nsAttrValue* aOldValue,
299 nsIPrincipal* aMaybeScriptedPrincipal,
300 bool aNotify) {
301 if (aNameSpaceID != kNameSpaceID_None) {
302 return nsGenericHTMLElement::AfterSetAttr(aNameSpaceID, aName, aValue,
303 aOldValue,
304 aMaybeScriptedPrincipal, aNotify);
307 nsAttrValueOrString attrVal(aValue);
308 if (aName == nsGkAtoms::src) {
309 mSrcURI = nullptr;
310 if (aValue && !aValue->IsEmptyString()) {
311 StringToURI(attrVal.String(), OwnerDoc(), getter_AddRefs(mSrcURI));
315 if (aValue) {
316 AfterMaybeChangeAttr(aNameSpaceID, aName, attrVal, aOldValue,
317 aMaybeScriptedPrincipal, aNotify);
320 if (mForm && (aName == nsGkAtoms::name || aName == nsGkAtoms::id) && aValue &&
321 !aValue->IsEmptyString()) {
322 // add the image to the hashtable as needed
323 MOZ_ASSERT(aValue->Type() == nsAttrValue::eAtom,
324 "Expected atom value for name/id");
325 mForm->AddImageElementToTable(
326 this, nsDependentAtomString(aValue->GetAtomValue()));
329 bool forceReload = false;
330 if (aName == nsGkAtoms::loading) {
331 if (aValue && Loading(aValue->GetEnumValue()) == Loading::Lazy) {
332 SetLazyLoading();
333 } else if (aOldValue &&
334 Loading(aOldValue->GetEnumValue()) == Loading::Lazy) {
335 StopLazyLoading(StartLoad(aNotify));
337 } else if (aName == nsGkAtoms::src && !aValue) {
338 // AfterMaybeChangeAttr handles setting src since it needs to catch
339 // img.src = img.src, so we only need to handle the unset case
340 // NOTE: regular src value changes are handled in AfterMaybeChangeAttr, so
341 // this only needs to handle unsetting the src attribute.
342 // Mark channel as urgent-start before load image if the image load is
343 // initiated by a user interaction.
344 if (mResponsiveSelector && mResponsiveSelector->Content() == this) {
345 mResponsiveSelector->SetDefaultSource(VoidString());
347 forceReload = true;
348 } else if (aName == nsGkAtoms::srcset) {
349 // Mark channel as urgent-start before load image if the image load is
350 // initaiated by a user interaction.
351 mUseUrgentStartForChannel = UserActivation::IsHandlingUserInput();
353 mSrcsetTriggeringPrincipal = aMaybeScriptedPrincipal;
355 if (aValue) {
356 if (!mInDocResponsiveContent) {
357 OwnerDoc()->AddResponsiveContent(this);
358 mInDocResponsiveContent = true;
360 } else if (mInDocResponsiveContent && !IsInPicture()) {
361 OwnerDoc()->RemoveResponsiveContent(this);
362 mInDocResponsiveContent = false;
365 PictureSourceSrcsetChanged(this, attrVal.String(), aNotify);
366 } else if (aName == nsGkAtoms::sizes) {
367 // Mark channel as urgent-start before load image if the image load is
368 // initiated by a user interaction.
369 mUseUrgentStartForChannel = UserActivation::IsHandlingUserInput();
371 PictureSourceSizesChanged(this, attrVal.String(), aNotify);
372 } else if (aName == nsGkAtoms::decoding) {
373 // Request sync or async image decoding.
374 SetSyncDecodingHint(
375 aValue && static_cast<ImageDecodingType>(aValue->GetEnumValue()) ==
376 ImageDecodingType::Sync);
377 } else if (aName == nsGkAtoms::referrerpolicy) {
378 ReferrerPolicy referrerPolicy = GetReferrerPolicyAsEnum();
379 forceReload = referrerPolicy != ReferrerPolicy::_empty &&
380 referrerPolicy != ReferrerPolicyFromAttr(aOldValue);
381 } else if (aName == nsGkAtoms::crossorigin) {
382 forceReload = GetCORSMode() != AttrValueToCORSMode(aOldValue);
385 // NOTE(emilio): When not notifying, we come from the parser or some other
386 // internal caller, in which cases we can skip the load since we are about to
387 // get bound to a tree.
388 if (forceReload) {
389 mUseUrgentStartForChannel = UserActivation::IsHandlingUserInput();
390 UpdateSourceSyncAndQueueImageTask(true, aNotify);
393 return nsGenericHTMLElement::AfterSetAttr(
394 aNameSpaceID, aName, aValue, aOldValue, aMaybeScriptedPrincipal, aNotify);
397 void HTMLImageElement::OnAttrSetButNotChanged(int32_t aNamespaceID,
398 nsAtom* aName,
399 const nsAttrValueOrString& aValue,
400 bool aNotify) {
401 AfterMaybeChangeAttr(aNamespaceID, aName, aValue, nullptr, nullptr, aNotify);
402 return nsGenericHTMLElement::OnAttrSetButNotChanged(aNamespaceID, aName,
403 aValue, aNotify);
406 void HTMLImageElement::AfterMaybeChangeAttr(
407 int32_t aNamespaceID, nsAtom* aName, const nsAttrValueOrString& aValue,
408 const nsAttrValue* aOldValue, nsIPrincipal* aMaybeScriptedPrincipal,
409 bool aNotify) {
410 if (aNamespaceID != kNameSpaceID_None || aName != nsGkAtoms::src) {
411 return;
414 // We need to force our image to reload. This must be done here, not in
415 // AfterSetAttr or BeforeSetAttr, because we want to do it even if the attr is
416 // being set to its existing value, which is normally optimized away as a
417 // no-op.
419 // If we are in responsive mode, we drop the forced reload behavior, but still
420 // trigger a image load task for img.src = img.src per spec.
422 // Both cases handle unsetting src in AfterSetAttr
423 mSrcTriggeringPrincipal = nsContentUtils::GetAttrTriggeringPrincipal(
424 this, aValue.String(), aMaybeScriptedPrincipal);
426 if (mResponsiveSelector && mResponsiveSelector->Content() == this) {
427 mResponsiveSelector->SetDefaultSource(mSrcURI, mSrcTriggeringPrincipal);
429 mUseUrgentStartForChannel = UserActivation::IsHandlingUserInput();
430 UpdateSourceSyncAndQueueImageTask(true, aNotify);
433 void HTMLImageElement::GetEventTargetParent(EventChainPreVisitor& aVisitor) {
434 // We handle image element with attribute ismap in its corresponding frame
435 // element. Set mMultipleActionsPrevented here to prevent the click event
436 // trigger the behaviors in Element::PostHandleEventForLinks
437 WidgetMouseEvent* mouseEvent = aVisitor.mEvent->AsMouseEvent();
438 if (mouseEvent && mouseEvent->IsLeftClickEvent() && IsMap()) {
439 mouseEvent->mFlags.mMultipleActionsPrevented = true;
441 nsGenericHTMLElement::GetEventTargetParent(aVisitor);
444 nsINode* HTMLImageElement::GetScopeChainParent() const {
445 if (mForm) {
446 return mForm;
448 return nsGenericHTMLElement::GetScopeChainParent();
451 bool HTMLImageElement::IsHTMLFocusable(IsFocusableFlags aFlags,
452 bool* aIsFocusable, int32_t* aTabIndex) {
453 int32_t tabIndex = TabIndex();
455 if (IsInComposedDoc() && FindImageMap()) {
456 // Use tab index on individual map areas.
457 *aTabIndex = FocusModel::IsTabFocusable(TabFocusableType::Links) ? 0 : -1;
458 // Image map is not focusable itself, but flag as tabbable
459 // so that image map areas get walked into.
460 *aIsFocusable = false;
461 return false;
464 // Can be in tab order if tabindex >=0 and form controls are tabbable.
465 *aTabIndex = FocusModel::IsTabFocusable(TabFocusableType::FormElements)
466 ? tabIndex
467 : -1;
468 *aIsFocusable = IsFormControlDefaultFocusable(aFlags) &&
469 (tabIndex >= 0 || GetTabIndexAttrValue().isSome());
471 return false;
474 nsresult HTMLImageElement::BindToTree(BindContext& aContext, nsINode& aParent) {
475 MOZ_TRY(nsGenericHTMLElement::BindToTree(aContext, aParent));
477 nsImageLoadingContent::BindToTree(aContext, aParent);
479 UpdateFormOwner();
481 // Mark channel as urgent-start before load image if the image load is
482 // initiated by a user interaction.
483 if (IsInPicture()) {
484 if (!mInDocResponsiveContent) {
485 OwnerDoc()->AddResponsiveContent(this);
486 mInDocResponsiveContent = true;
488 mUseUrgentStartForChannel = UserActivation::IsHandlingUserInput();
489 UpdateSourceSyncAndQueueImageTask(false, /* aNotify = */ false);
491 return NS_OK;
494 void HTMLImageElement::UnbindFromTree(UnbindContext& aContext) {
495 if (mForm) {
496 if (aContext.IsUnbindRoot(this) || !FindAncestorForm(mForm)) {
497 ClearForm(true);
498 } else {
499 UnsetFlags(MAYBE_ORPHAN_FORM_ELEMENT);
502 // Our in-pictureness can only change if we're the unbind root.
503 const bool wasInPicture = IsInPicture();
505 nsImageLoadingContent::UnbindFromTree();
506 nsGenericHTMLElement::UnbindFromTree(aContext);
508 if (wasInPicture != IsInPicture()) {
509 MOZ_ASSERT(wasInPicture);
510 MOZ_ASSERT(aContext.IsUnbindRoot(this));
511 MOZ_ASSERT(mInDocResponsiveContent);
512 if (!HasAttr(nsGkAtoms::srcset)) {
513 OwnerDoc()->RemoveResponsiveContent(this);
514 mInDocResponsiveContent = false;
516 UpdateSourceSyncAndQueueImageTask(false, /* aNotify = */ false);
520 void HTMLImageElement::UpdateFormOwner() {
521 if (!mForm) {
522 mForm = FindAncestorForm();
525 if (mForm && !HasFlag(ADDED_TO_FORM)) {
526 // Now we need to add ourselves to the form
527 nsAutoString nameVal, idVal;
528 GetAttr(nsGkAtoms::name, nameVal);
529 GetAttr(nsGkAtoms::id, idVal);
531 SetFlags(ADDED_TO_FORM);
533 mForm->AddImageElement(this);
535 if (!nameVal.IsEmpty()) {
536 mForm->AddImageElementToTable(this, nameVal);
539 if (!idVal.IsEmpty()) {
540 mForm->AddImageElementToTable(this, idVal);
545 void HTMLImageElement::NodeInfoChanged(Document* aOldDoc) {
546 nsGenericHTMLElement::NodeInfoChanged(aOldDoc);
548 if (mInDocResponsiveContent) {
549 aOldDoc->RemoveResponsiveContent(this);
550 OwnerDoc()->AddResponsiveContent(this);
553 // Reparse the URI if needed. Note that we can't check whether we already have
554 // a parsed URI, because it might be null even if we have a valid src
555 // attribute, if we tried to parse with a different base.
556 mSrcURI = nullptr;
557 nsAutoString src;
558 if (GetAttr(nsGkAtoms::src, src) && !src.IsEmpty()) {
559 StringToURI(src, OwnerDoc(), getter_AddRefs(mSrcURI));
562 if (mLazyLoading) {
563 aOldDoc->GetLazyLoadObserver()->Unobserve(*this);
564 mLazyLoading = false;
565 SetLazyLoading();
568 // Run selection algorithm synchronously and reload when an img element's
569 // adopting steps are run, in order to react to changes in the environment,
570 // per spec,
571 // https://html.spec.whatwg.org/#reacting-to-dom-mutations, and
572 // https://html.spec.whatwg.org/#reacting-to-environment-changes.
573 UpdateSourceSyncAndQueueImageTask(true, /* aNotify = */ false);
576 // static
577 already_AddRefed<HTMLImageElement> HTMLImageElement::Image(
578 const GlobalObject& aGlobal, const Optional<uint32_t>& aWidth,
579 const Optional<uint32_t>& aHeight, ErrorResult& aError) {
580 nsCOMPtr<nsPIDOMWindowInner> win = do_QueryInterface(aGlobal.GetAsSupports());
581 Document* doc;
582 if (!win || !(doc = win->GetExtantDoc())) {
583 aError.Throw(NS_ERROR_FAILURE);
584 return nullptr;
587 RefPtr<mozilla::dom::NodeInfo> nodeInfo = doc->NodeInfoManager()->GetNodeInfo(
588 nsGkAtoms::img, nullptr, kNameSpaceID_XHTML, ELEMENT_NODE);
590 auto* nim = nodeInfo->NodeInfoManager();
591 RefPtr<HTMLImageElement> img = new (nim) HTMLImageElement(nodeInfo.forget());
593 if (aWidth.WasPassed()) {
594 img->SetWidth(aWidth.Value(), aError);
595 if (aError.Failed()) {
596 return nullptr;
599 if (aHeight.WasPassed()) {
600 img->SetHeight(aHeight.Value(), aError);
601 if (aError.Failed()) {
602 return nullptr;
607 return img.forget();
610 uint32_t HTMLImageElement::Height() { return GetWidthHeightForImage().height; }
612 uint32_t HTMLImageElement::Width() { return GetWidthHeightForImage().width; }
614 nsIntSize HTMLImageElement::NaturalSize() {
615 if (!mCurrentRequest) {
616 return {};
619 nsCOMPtr<imgIContainer> image;
620 mCurrentRequest->GetImage(getter_AddRefs(image));
621 if (!image) {
622 return {};
625 nsIntSize size;
626 Unused << image->GetHeight(&size.height);
627 Unused << image->GetWidth(&size.width);
629 ImageResolution resolution = image->GetResolution();
630 // NOTE(emilio): What we implement here matches the image-set() spec, but it's
631 // unclear whether this is the right thing to do, see
632 // https://github.com/whatwg/html/pull/5574#issuecomment-826335244.
633 if (mResponsiveSelector) {
634 float density = mResponsiveSelector->GetSelectedImageDensity();
635 MOZ_ASSERT(density >= 0.0);
636 resolution.ScaleBy(density);
639 resolution.ApplyTo(size.width, size.height);
640 return size;
643 nsresult HTMLImageElement::CopyInnerTo(HTMLImageElement* aDest) {
644 MOZ_TRY(nsGenericHTMLElement::CopyInnerTo(aDest));
646 // In SetAttr (called from nsGenericHTMLElement::CopyInnerTo), aDest skipped
647 // doing the image load because we passed in false for aNotify. But we
648 // really do want it to do the load, so set it up to happen once the cloning
649 // reaches a stable state.
650 aDest->UpdateSourceSyncAndQueueImageTask(false, /* aNotify = */ false);
651 return NS_OK;
654 CORSMode HTMLImageElement::GetCORSMode() {
655 return AttrValueToCORSMode(GetParsedAttr(nsGkAtoms::crossorigin));
658 JSObject* HTMLImageElement::WrapNode(JSContext* aCx,
659 JS::Handle<JSObject*> aGivenProto) {
660 return HTMLImageElement_Binding::Wrap(aCx, this, aGivenProto);
663 #ifdef DEBUG
664 HTMLFormElement* HTMLImageElement::GetForm() const { return mForm; }
665 #endif
667 void HTMLImageElement::SetForm(HTMLFormElement* aForm) {
668 MOZ_ASSERT(aForm, "Don't pass null here");
669 NS_ASSERTION(!mForm,
670 "We don't support switching from one non-null form to another.");
672 mForm = aForm;
675 void HTMLImageElement::ClearForm(bool aRemoveFromForm) {
676 NS_ASSERTION((mForm != nullptr) == HasFlag(ADDED_TO_FORM),
677 "Form control should have had flag set correctly");
679 if (!mForm) {
680 return;
683 if (aRemoveFromForm) {
684 nsAutoString nameVal, idVal;
685 GetAttr(nsGkAtoms::name, nameVal);
686 GetAttr(nsGkAtoms::id, idVal);
688 mForm->RemoveImageElement(this);
690 if (!nameVal.IsEmpty()) {
691 mForm->RemoveImageElementFromTable(this, nameVal);
694 if (!idVal.IsEmpty()) {
695 mForm->RemoveImageElementFromTable(this, idVal);
699 UnsetFlags(ADDED_TO_FORM);
700 mForm = nullptr;
703 void HTMLImageElement::ClearImageLoadTask() {
704 mPendingImageLoadTask = nullptr;
705 mHasPendingLoadTask = false;
708 // Roughly corresponds to https://html.spec.whatwg.org/#update-the-image-data
709 void HTMLImageElement::UpdateSourceSyncAndQueueImageTask(
710 bool aAlwaysLoad, bool aNotify, const HTMLSourceElement* aSkippedSource) {
711 // Per spec, when updating the image data or reacting to environment
712 // changes, we always run the full selection (including selecting the source
713 // element and the best fit image from srcset) even if it doesn't directly
714 // affect the source selection.
716 // However, in the spec of updating the image data, the selection of image
717 // source URL is in the asynchronous part (i.e. in a microtask), and so this
718 // doesn't guarantee that the image style is correct after we flush the style
719 // synchronously. So here we update the responsive source synchronously always
720 // to make sure the image source is always up-to-date after each DOM mutation.
721 // Spec issue: https://github.com/whatwg/html/issues/8207.
722 UpdateResponsiveSource(aSkippedSource);
724 // If loading is temporarily disabled, we don't want to queue tasks that may
725 // then run when loading is re-enabled.
726 // Roughly step 1 and 2.
727 // FIXME(emilio): Would be great to do this more per-spec. We don't cancel
728 // existing loads etc.
729 if (!LoadingEnabled() || !ShouldLoadImage()) {
730 return;
733 // Ensure that we don't overwrite a previous load request that requires
734 // a complete load to occur.
735 const bool alwaysLoad = aAlwaysLoad || (mPendingImageLoadTask &&
736 mPendingImageLoadTask->AlwaysLoad());
738 // Steps 5 and 7 (sync cache check for src).
739 const bool shouldLoadSync = [&] {
740 if (HaveSrcsetOrInPicture()) {
741 return false;
743 if (!mSrcURI) {
744 // NOTE(emilio): we need to also do a sync check for empty / invalid src,
745 // see https://github.com/whatwg/html/issues/2429
746 // But do it sync only when there's a current request.
747 return !!mCurrentRequest;
749 return nsContentUtils::IsImageAvailable(
750 this, mSrcURI, mSrcTriggeringPrincipal, GetCORSMode());
751 }();
753 if (shouldLoadSync) {
754 if (!nsContentUtils::IsSafeToRunScript()) {
755 // If not safe to run script, we should do the sync load task as soon as
756 // possible instead. This prevents unsound state changes from frame
757 // construction and such.
758 nsContentUtils::AddScriptRunner(
759 NewRunnableMethod<bool, bool, HTMLSourceElement*>(
760 "HTMLImageElement::UpdateSourceSyncAndQueueImageTask", this,
761 &HTMLImageElement::UpdateSourceSyncAndQueueImageTask, aAlwaysLoad,
762 /* aNotify = */ true, nullptr));
763 return;
766 if (mLazyLoading && mSrcURI) {
767 StopLazyLoading(StartLoad::No);
769 ClearImageLoadTask();
770 LoadSelectedImage(alwaysLoad);
771 return;
774 if (mLazyLoading) {
775 // This check is not in the spec, but it is just a performance optimization.
776 // The reasoning for why it is sound is that we early-return from the image
777 // task when lazy loading, and that StopLazyLoading makes us queue a new
778 // task (which will implicitly cancel all the pre-existing tasks).
779 return;
782 RefPtr task = new ImageLoadTask(this, alwaysLoad, mUseUrgentStartForChannel);
783 mPendingImageLoadTask = task;
784 mHasPendingLoadTask = true;
785 // We might have just become non-broken.
786 UpdateImageState(aNotify);
787 // The task checks this to determine if it was the last queued event, and so
788 // earlier tasks are implicitly canceled.
789 CycleCollectedJSContext::Get()->DispatchToMicroTask(task.forget());
792 bool HTMLImageElement::HaveSrcsetOrInPicture() const {
793 return HasAttr(nsGkAtoms::srcset) || IsInPicture();
796 bool HTMLImageElement::SelectedSourceMatchesLast(nsIURI* aSelectedSource) {
797 // If there was no selected source previously, we don't want to short-circuit
798 // the load. Similarly for if there is no newly selected source.
799 if (!mLastSelectedSource || !aSelectedSource) {
800 return false;
802 bool equal = false;
803 return NS_SUCCEEDED(mLastSelectedSource->Equals(aSelectedSource, &equal)) &&
804 equal;
807 void HTMLImageElement::LoadSelectedImage(bool aAlwaysLoad) {
808 // In responsive mode, we have to make sure we ran the full selection
809 // algorithm before loading the selected image.
810 // Use this assertion to catch any cases we missed.
811 MOZ_ASSERT(!UpdateResponsiveSource(),
812 "The image source should be the same because we update the "
813 "responsive source synchronously");
815 // The density is default to 1.0 for the src attribute case.
816 double currentDensity = mResponsiveSelector
817 ? mResponsiveSelector->GetSelectedImageDensity()
818 : 1.0;
820 nsCOMPtr<nsIURI> selectedSource;
821 nsCOMPtr<nsIPrincipal> triggeringPrincipal;
822 ImageLoadType type = eImageLoadType_Normal;
823 bool hasSrc = false;
824 if (mResponsiveSelector) {
825 selectedSource = mResponsiveSelector->GetSelectedImageURL();
826 triggeringPrincipal =
827 mResponsiveSelector->GetSelectedImageTriggeringPrincipal();
828 type = eImageLoadType_Imageset;
829 } else if (mSrcURI || HasAttr(nsGkAtoms::src)) {
830 hasSrc = true;
831 if (mSrcURI) {
832 selectedSource = mSrcURI;
833 if (HaveSrcsetOrInPicture()) {
834 // If we have a srcset attribute or are in a <picture> element, we
835 // always use the Imageset load type, even if we parsed no valid
836 // responsive sources from either, per spec.
837 type = eImageLoadType_Imageset;
839 triggeringPrincipal = mSrcTriggeringPrincipal;
843 if (!aAlwaysLoad && SelectedSourceMatchesLast(selectedSource)) {
844 // Update state when only density may have changed (i.e., the source to load
845 // hasn't changed, and we don't do any request at all). We need (apart from
846 // updating our internal state) to tell the image frame because its
847 // intrinsic size may have changed.
849 // In the case we actually trigger a new load, that load will trigger a call
850 // to nsImageFrame::NotifyNewCurrentRequest, which takes care of that for
851 // us.
852 SetDensity(currentDensity);
853 // If we're (re-)loading a broken image, we might have just become broken
854 // again.
855 UpdateImageState(true);
856 return;
859 if (mLazyLoading) {
860 return;
863 nsresult rv = NS_ERROR_FAILURE;
865 const bool kNotify = true;
866 // src triggers an error event on invalid URI, unlike other loads.
867 if (selectedSource || hasSrc) {
868 // We can pass true for aForce because we already do a manual check for
869 // SelectedSourceMatchesLast.
870 rv = LoadImage(selectedSource, /* aForce = */ true, kNotify, type,
871 triggeringPrincipal);
874 mLastSelectedSource = selectedSource;
875 mCurrentDensity = currentDensity;
877 if (NS_FAILED(rv)) {
878 CancelImageRequests(kNotify);
882 void HTMLImageElement::PictureSourceSrcsetChanged(nsIContent* aSourceNode,
883 const nsAString& aNewValue,
884 bool aNotify) {
885 MOZ_ASSERT(aSourceNode == this || IsPreviousSibling(aSourceNode, this),
886 "Should not be getting notifications for non-previous-siblings");
888 nsIContent* currentSrc =
889 mResponsiveSelector ? mResponsiveSelector->Content() : nullptr;
891 if (aSourceNode == currentSrc) {
892 // We're currently using this node as our responsive selector
893 // source.
894 nsCOMPtr<nsIPrincipal> principal;
895 if (aSourceNode == this) {
896 principal = mSrcsetTriggeringPrincipal;
897 } else if (auto* source = HTMLSourceElement::FromNode(aSourceNode)) {
898 principal = source->GetSrcsetTriggeringPrincipal();
900 mResponsiveSelector->SetCandidatesFromSourceSet(aNewValue, principal);
903 // This always triggers the image update steps per the spec, even if we are
904 // not using this source.
905 UpdateSourceSyncAndQueueImageTask(true, aNotify);
908 void HTMLImageElement::PictureSourceSizesChanged(nsIContent* aSourceNode,
909 const nsAString& aNewValue,
910 bool aNotify) {
911 MOZ_ASSERT(aSourceNode == this || IsPreviousSibling(aSourceNode, this),
912 "Should not be getting notifications for non-previous-siblings");
914 nsIContent* currentSrc =
915 mResponsiveSelector ? mResponsiveSelector->Content() : nullptr;
917 if (aSourceNode == currentSrc) {
918 // We're currently using this node as our responsive selector
919 // source.
920 mResponsiveSelector->SetSizesFromDescriptor(aNewValue);
923 // This always triggers the image update steps per the spec, even if
924 // we are not using this source.
925 UpdateSourceSyncAndQueueImageTask(true, aNotify);
928 void HTMLImageElement::PictureSourceMediaOrTypeChanged(nsIContent* aSourceNode,
929 bool aNotify) {
930 MOZ_ASSERT(IsPreviousSibling(aSourceNode, this),
931 "Should not be getting notifications for non-previous-siblings");
933 // This always triggers the image update steps per the spec, even if
934 // we are not switching to/from this source
935 UpdateSourceSyncAndQueueImageTask(true, aNotify);
938 void HTMLImageElement::PictureSourceDimensionChanged(
939 HTMLSourceElement* aSourceNode, bool aNotify) {
940 MOZ_ASSERT(IsPreviousSibling(aSourceNode, this),
941 "Should not be getting notifications for non-previous-siblings");
943 // "width" and "height" affect the dimension of images, but they don't have
944 // impact on the selection of <source> elements. In other words,
945 // UpdateResponsiveSource doesn't change the source, so all we need to do is
946 // just request restyle.
947 if (mResponsiveSelector && mResponsiveSelector->Content() == aSourceNode) {
948 InvalidateAttributeMapping();
952 void HTMLImageElement::PictureSourceAdded(bool aNotify,
953 HTMLSourceElement* aSourceNode) {
954 MOZ_ASSERT(!aSourceNode || IsPreviousSibling(aSourceNode, this),
955 "Should not be getting notifications for non-previous-siblings");
957 UpdateSourceSyncAndQueueImageTask(true, aNotify);
960 void HTMLImageElement::PictureSourceRemoved(bool aNotify,
961 HTMLSourceElement* aSourceNode) {
962 MOZ_ASSERT(!aSourceNode || IsPreviousSibling(aSourceNode, this),
963 "Should not be getting notifications for non-previous-siblings");
964 UpdateSourceSyncAndQueueImageTask(true, aNotify, aSourceNode);
967 bool HTMLImageElement::UpdateResponsiveSource(
968 const HTMLSourceElement* aSkippedSource) {
969 bool hadSelector = !!mResponsiveSelector;
971 nsIContent* currentSource =
972 mResponsiveSelector ? mResponsiveSelector->Content() : nullptr;
974 // Walk source nodes previous to ourselves if IsInPicture().
975 nsINode* candidateSource =
976 IsInPicture() ? GetParentElement()->GetFirstChild() : this;
978 // Initialize this as nullptr so we don't have to nullify it when runing out
979 // of siblings without finding ourself, e.g. XBL magic.
980 RefPtr<ResponsiveImageSelector> newResponsiveSelector = nullptr;
982 for (; candidateSource; candidateSource = candidateSource->GetNextSibling()) {
983 if (aSkippedSource == candidateSource) {
984 continue;
987 if (candidateSource == currentSource) {
988 // found no better source before current, re-run selection on
989 // that and keep it if it's still usable.
990 bool changed = mResponsiveSelector->SelectImage(true);
991 if (mResponsiveSelector->NumCandidates()) {
992 bool isUsableCandidate = true;
994 // an otherwise-usable source element may still have a media query that
995 // may not match any more.
996 if (candidateSource->IsHTMLElement(nsGkAtoms::source) &&
997 !SourceElementMatches(candidateSource->AsElement())) {
998 isUsableCandidate = false;
1001 if (isUsableCandidate) {
1002 // We are still using the current source, but the selected image may
1003 // be changed, so always set the density from the selected image.
1004 SetDensity(mResponsiveSelector->GetSelectedImageDensity());
1005 return changed;
1009 // no longer valid
1010 newResponsiveSelector = nullptr;
1011 if (candidateSource == this) {
1012 // No further possibilities
1013 break;
1015 } else if (candidateSource == this) {
1016 // We are the last possible source
1017 newResponsiveSelector =
1018 TryCreateResponsiveSelector(candidateSource->AsElement());
1019 break;
1020 } else if (auto* source = HTMLSourceElement::FromNode(candidateSource)) {
1021 if (RefPtr<ResponsiveImageSelector> selector =
1022 TryCreateResponsiveSelector(source)) {
1023 newResponsiveSelector = selector.forget();
1024 // This led to a valid source, stop
1025 break;
1030 // If we reach this point, either:
1031 // - there was no selector originally, and there is not one now
1032 // - there was no selector originally, and there is one now
1033 // - there was a selector, and there is a different one now
1034 // - there was a selector, and there is not one now
1035 SetResponsiveSelector(std::move(newResponsiveSelector));
1036 return hadSelector || mResponsiveSelector;
1039 /*static */
1040 bool HTMLImageElement::SupportedPictureSourceType(const nsAString& aType) {
1041 nsAutoString type;
1042 nsAutoString params;
1044 nsContentUtils::SplitMimeType(aType, type, params);
1045 if (type.IsEmpty()) {
1046 return true;
1049 return imgLoader::SupportImageWithMimeType(
1050 NS_ConvertUTF16toUTF8(type), AcceptedMimeTypes::IMAGES_AND_DOCUMENTS);
1053 bool HTMLImageElement::SourceElementMatches(Element* aSourceElement) {
1054 MOZ_ASSERT(aSourceElement->IsHTMLElement(nsGkAtoms::source));
1056 MOZ_ASSERT(IsInPicture());
1057 MOZ_ASSERT(IsPreviousSibling(aSourceElement, this));
1059 // Check media and type
1060 auto* src = static_cast<HTMLSourceElement*>(aSourceElement);
1061 if (!src->MatchesCurrentMedia()) {
1062 return false;
1065 nsAutoString type;
1066 return !src->GetAttr(nsGkAtoms::type, type) ||
1067 SupportedPictureSourceType(type);
1070 already_AddRefed<ResponsiveImageSelector>
1071 HTMLImageElement::TryCreateResponsiveSelector(Element* aSourceElement) {
1072 nsCOMPtr<nsIPrincipal> principal;
1074 // Skip if this is not a <source> with matching media query
1075 bool isSourceTag = aSourceElement->IsHTMLElement(nsGkAtoms::source);
1076 if (isSourceTag) {
1077 if (!SourceElementMatches(aSourceElement)) {
1078 return nullptr;
1080 auto* source = HTMLSourceElement::FromNode(aSourceElement);
1081 principal = source->GetSrcsetTriggeringPrincipal();
1082 } else if (aSourceElement->IsHTMLElement(nsGkAtoms::img)) {
1083 // Otherwise this is the <img> tag itself
1084 MOZ_ASSERT(aSourceElement == this);
1085 principal = mSrcsetTriggeringPrincipal;
1088 // Skip if has no srcset or an empty srcset
1089 nsString srcset;
1090 if (!aSourceElement->GetAttr(nsGkAtoms::srcset, srcset)) {
1091 return nullptr;
1094 if (srcset.IsEmpty()) {
1095 return nullptr;
1098 // Try to parse
1099 RefPtr<ResponsiveImageSelector> sel =
1100 new ResponsiveImageSelector(aSourceElement);
1101 if (!sel->SetCandidatesFromSourceSet(srcset, principal)) {
1102 // No possible candidates, don't need to bother parsing sizes
1103 return nullptr;
1106 nsAutoString sizes;
1107 aSourceElement->GetAttr(nsGkAtoms::sizes, sizes);
1108 sel->SetSizesFromDescriptor(sizes);
1110 // If this is the <img> tag, also pull in src as the default source
1111 if (!isSourceTag) {
1112 MOZ_ASSERT(aSourceElement == this);
1113 if (mSrcURI) {
1114 sel->SetDefaultSource(mSrcURI, mSrcTriggeringPrincipal);
1118 return sel.forget();
1121 /* static */
1122 bool HTMLImageElement::SelectSourceForTagWithAttrs(
1123 Document* aDocument, bool aIsSourceTag, const nsAString& aSrcAttr,
1124 const nsAString& aSrcsetAttr, const nsAString& aSizesAttr,
1125 const nsAString& aTypeAttr, const nsAString& aMediaAttr,
1126 nsAString& aResult) {
1127 MOZ_ASSERT(aIsSourceTag || (aTypeAttr.IsEmpty() && aMediaAttr.IsEmpty()),
1128 "Passing type or media attrs makes no sense without aIsSourceTag");
1129 MOZ_ASSERT(!aIsSourceTag || aSrcAttr.IsEmpty(),
1130 "Passing aSrcAttr makes no sense with aIsSourceTag set");
1132 if (aSrcsetAttr.IsEmpty()) {
1133 if (!aIsSourceTag) {
1134 // For an <img> with no srcset, we would always select the src attr.
1135 aResult.Assign(aSrcAttr);
1136 return true;
1138 // Otherwise, a <source> without srcset is never selected
1139 return false;
1142 // Would not consider source tags with unsupported media or type
1143 if (aIsSourceTag &&
1144 ((!aMediaAttr.IsVoid() && !HTMLSourceElement::WouldMatchMediaForDocument(
1145 aMediaAttr, aDocument)) ||
1146 (!aTypeAttr.IsVoid() && !SupportedPictureSourceType(aTypeAttr)))) {
1147 return false;
1150 // Using srcset or picture <source>, build a responsive selector for this
1151 // tag.
1152 RefPtr<ResponsiveImageSelector> sel = new ResponsiveImageSelector(aDocument);
1154 sel->SetCandidatesFromSourceSet(aSrcsetAttr);
1155 if (!aSizesAttr.IsEmpty()) {
1156 sel->SetSizesFromDescriptor(aSizesAttr);
1158 if (!aIsSourceTag) {
1159 sel->SetDefaultSource(aSrcAttr);
1162 if (sel->GetSelectedImageURLSpec(aResult)) {
1163 return true;
1166 if (!aIsSourceTag) {
1167 // <img> tag with no match would definitively load nothing.
1168 aResult.Truncate();
1169 return true;
1172 // <source> tags with no match would leave source yet-undetermined.
1173 return false;
1176 void HTMLImageElement::DestroyContent() {
1177 // Clear the load task to avoid running LoadSelectedImage() after getting
1178 // destroyed.
1179 ClearImageLoadTask();
1181 mResponsiveSelector = nullptr;
1183 nsImageLoadingContent::Destroy();
1184 nsGenericHTMLElement::DestroyContent();
1187 void HTMLImageElement::MediaFeatureValuesChanged() {
1188 UpdateSourceSyncAndQueueImageTask(false, /* aNotify = */ true);
1191 bool HTMLImageElement::ShouldLoadImage() const {
1192 return OwnerDoc()->ShouldLoadImages();
1195 void HTMLImageElement::SetLazyLoading() {
1196 if (mLazyLoading) {
1197 return;
1200 // If scripting is disabled don't do lazy load.
1201 // https://whatpr.org/html/3752/images.html#updating-the-image-data
1203 // Same for printing.
1204 Document* doc = OwnerDoc();
1205 if (!doc->IsScriptEnabled() || doc->IsStaticDocument()) {
1206 return;
1209 doc->EnsureLazyLoadObserver().Observe(*this);
1210 mLazyLoading = true;
1211 UpdateImageState(true);
1214 void HTMLImageElement::StopLazyLoading(StartLoad aStartLoad) {
1215 if (!mLazyLoading) {
1216 return;
1218 mLazyLoading = false;
1219 Document* doc = OwnerDoc();
1220 if (auto* obs = doc->GetLazyLoadObserver()) {
1221 obs->Unobserve(*this);
1224 if (aStartLoad == StartLoad::Yes) {
1225 UpdateSourceSyncAndQueueImageTask(true, /* aNotify = */ true);
1229 const StyleLockedDeclarationBlock*
1230 HTMLImageElement::GetMappedAttributesFromSource() const {
1231 if (!IsInPicture() || !mResponsiveSelector) {
1232 return nullptr;
1235 const auto* source =
1236 HTMLSourceElement::FromNodeOrNull(mResponsiveSelector->Content());
1237 if (!source) {
1238 return nullptr;
1241 MOZ_ASSERT(IsPreviousSibling(source, this),
1242 "Incorrect or out-of-date source");
1243 return source->GetAttributesMappedForImage();
1246 void HTMLImageElement::InvalidateAttributeMapping() {
1247 if (!IsInPicture()) {
1248 return;
1251 nsPresContext* presContext = nsContentUtils::GetContextForContent(this);
1252 if (!presContext) {
1253 return;
1256 // Note: Unfortunately, we have to use RESTYLE_SELF, instead of using
1257 // RESTYLE_STYLE_ATTRIBUTE or other ways, to avoid re-selector-match because
1258 // we are using Gecko_GetExtraContentStyleDeclarations() to retrieve the
1259 // extra declaration block from |this|'s width and height attributes, and
1260 // other restyle hints seems not enough.
1261 // FIXME: We may refine this together with the restyle for presentation
1262 // attributes in RestyleManger::AttributeChagned()
1263 presContext->RestyleManager()->PostRestyleEvent(
1264 this, RestyleHint::RESTYLE_SELF, nsChangeHint(0));
1267 void HTMLImageElement::SetResponsiveSelector(
1268 RefPtr<ResponsiveImageSelector>&& aSource) {
1269 if (mResponsiveSelector == aSource) {
1270 return;
1273 mResponsiveSelector = std::move(aSource);
1275 // Invalidate the style if needed.
1276 InvalidateAttributeMapping();
1278 // Update density.
1279 SetDensity(mResponsiveSelector
1280 ? mResponsiveSelector->GetSelectedImageDensity()
1281 : 1.0);
1284 void HTMLImageElement::SetDensity(double aDensity) {
1285 if (mCurrentDensity == aDensity) {
1286 return;
1289 mCurrentDensity = aDensity;
1291 // Invalidate the reflow.
1292 if (nsImageFrame* f = do_QueryFrame(GetPrimaryFrame())) {
1293 f->ResponsiveContentDensityChanged();
1297 FetchPriority HTMLImageElement::GetFetchPriorityForImage() const {
1298 return nsGenericHTMLElement::GetFetchPriority();
1301 } // namespace mozilla::dom