From faf4f2c23f7f165fae58ee93217e2559d89c2317 Mon Sep 17 00:00:00 2001 From: Moonchild Date: Sun, 4 May 2025 14:39:03 +0200 Subject: [PATCH] Issue #2736 - Part 10: Re-work `srcset` attribute. Use subject principal as triggering principal in "srcset" attribute for . --- dom/html/HTMLImageElement.cpp | 4 ++++ dom/html/HTMLSourceElement.cpp | 5 +++++ dom/html/HTMLSourceElement.h | 14 +++++++++++--- dom/webidl/HTMLSourceElement.webidl | 2 +- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/dom/html/HTMLImageElement.cpp b/dom/html/HTMLImageElement.cpp index 6945174954..4dd46f5a0e 100644 --- a/dom/html/HTMLImageElement.cpp +++ b/dom/html/HTMLImageElement.cpp @@ -1060,6 +1060,8 @@ HTMLImageElement::PictureSourceSrcsetChanged(nsIContent *aSourceNode, nsCOMPtr principal; if (aSourceNode == this) { principal = mSrcsetTriggeringPrincipal; + } else if (auto* source = HTMLSourceElement::FromContent(aSourceNode)) { + principal = source->GetSrcsetTriggeringPrincipal(); } mResponsiveSelector->SetCandidatesFromSourceSet(aNewValue, principal); } @@ -1254,6 +1256,8 @@ HTMLImageElement::TryCreateResponsiveSelector(nsIContent *aSourceNode) if (!SourceElementMatches(aSourceNode)) { return false; } + auto* source = HTMLSourceElement::FromContent(aSourceNode); + principal = source->GetSrcsetTriggeringPrincipal(); } else if (aSourceNode->IsHTMLElement(nsGkAtoms::img)) { // Otherwise this is the tag itself MOZ_ASSERT(aSourceNode == this); diff --git a/dom/html/HTMLSourceElement.cpp b/dom/html/HTMLSourceElement.cpp index afe481dfd7..2a5ad4b908 100644 --- a/dom/html/HTMLSourceElement.cpp +++ b/dom/html/HTMLSourceElement.cpp @@ -102,6 +102,11 @@ HTMLSourceElement::AfterSetAttr(int32_t aNameSpaceID, nsIAtom* aName, nsIPrincipal* aMaybeScriptedPrincipal, bool aNotify) { + if (aNameSpaceID == kNameSpaceID_None && aName == nsGkAtoms::srcset) { + mSrcsetTriggeringPrincipal = nsContentUtils::GetAttrTriggeringPrincipal( + this, aValue ? aValue->GetStringValue() : EmptyString(), + aMaybeScriptedPrincipal); + } // If we are associated with a with a valid , notify it of // responsive parameter changes Element *parent = nsINode::GetParentElement(); diff --git a/dom/html/HTMLSourceElement.h b/dom/html/HTMLSourceElement.h index aeaf884c81..7d5fb4de31 100644 --- a/dom/html/HTMLSourceElement.h +++ b/dom/html/HTMLSourceElement.h @@ -69,6 +69,11 @@ public: return mSrcTriggeringPrincipal; } + nsIPrincipal* GetSrcsetTriggeringPrincipal() const + { + return mSrcsetTriggeringPrincipal; + } + void GetType(DOMString& aType) { GetHTMLAttr(nsGkAtoms::type, aType); @@ -78,13 +83,13 @@ public: SetHTMLAttr(nsGkAtoms::type, aType, rv); } - void GetSrcset(DOMString& aSrcset) + void GetSrcset(DOMString& aSrcset, nsIPrincipal&) { GetHTMLAttr(nsGkAtoms::srcset, aSrcset); } - void SetSrcset(const nsAString& aSrcset, mozilla::ErrorResult& rv) + void SetSrcset(const nsAString& aSrcset, nsIPrincipal& aTriggeringPrincipal, mozilla::ErrorResult& rv) { - SetHTMLAttr(nsGkAtoms::srcset, aSrcset, rv); + SetHTMLAttr(nsGkAtoms::srcset, aSrcset, aTriggeringPrincipal, rv); } void GetSizes(DOMString& aSizes) @@ -124,6 +129,9 @@ private: // The triggering principal for the src attribute. nsCOMPtr mSrcTriggeringPrincipal; + // The triggering principal for the srcset attribute. + nsCOMPtr mSrcsetTriggeringPrincipal; + // Generates a new nsMediaList using the given input void UpdateMediaList(const nsAttrValue* aValue); }; diff --git a/dom/webidl/HTMLSourceElement.webidl b/dom/webidl/HTMLSourceElement.webidl index 78570b8579..17f5ef7997 100644 --- a/dom/webidl/HTMLSourceElement.webidl +++ b/dom/webidl/HTMLSourceElement.webidl @@ -20,7 +20,7 @@ interface HTMLSourceElement : HTMLElement { }; partial interface HTMLSourceElement { - [CEReactions, SetterThrows] + [CEReactions, NeedsSubjectPrincipal, SetterThrows] attribute DOMString srcset; [CEReactions, SetterThrows] attribute DOMString sizes;