Issue #2736 - Part 10: Re-work <source> srcset attribute.

Use subject principal as triggering principal in <source> "srcset"
attribute for <picture>.
This commit is contained in:
Moonchild 2025-05-04 14:39:03 +02:00 committed by roytam1
commit faf4f2c23f
4 changed files with 21 additions and 4 deletions

View file

@ -1060,6 +1060,8 @@ HTMLImageElement::PictureSourceSrcsetChanged(nsIContent *aSourceNode,
nsCOMPtr<nsIPrincipal> 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 <img> tag itself
MOZ_ASSERT(aSourceNode == this);

View file

@ -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 <picture> with a valid <img>, notify it of
// responsive parameter changes
Element *parent = nsINode::GetParentElement();

View file

@ -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<nsIPrincipal> mSrcTriggeringPrincipal;
// The triggering principal for the srcset attribute.
nsCOMPtr<nsIPrincipal> mSrcsetTriggeringPrincipal;
// Generates a new nsMediaList using the given input
void UpdateMediaList(const nsAttrValue* aValue);
};

View file

@ -20,7 +20,7 @@ interface HTMLSourceElement : HTMLElement {
};
partial interface HTMLSourceElement {
[CEReactions, SetterThrows]
[CEReactions, NeedsSubjectPrincipal, SetterThrows]
attribute DOMString srcset;
[CEReactions, SetterThrows]
attribute DOMString sizes;