Files
tubestation/dom/html/HTMLPictureElement.cpp
Emilio Cobos Álvarez bb2400428b Bug 1076583 - Make src attribute not load sync. r=smaug,extension-reviewers,devtools-reviewers,robwu,nchevobbe
This fixes src loads to be consistent with srcset/picture loads, modulo
the special synchronous case in the spec
(https://html.spec.whatwg.org/#update-the-image-data step 7), which
requires src loads to be sync if the image is available.

We now avoid triggering the load from the parser consistently for src /
srcset / picture, and unify the codepath with BindToTree. That should
avoid some useless task allocations.

Only the sync load code-path needs a script runner (mostly to deal with
anonymous content like the video poster <img> and such, but it also
helps not trigger sync loads at unexpected times like on adoption).

About the HTMLImageElement::Complete() getter change, we need to also
return false if there's an existing load task. That is the proposal in
https://github.com/whatwg/html/issues/4884, and prevents some failures
in the-img-element/{update-src-complete,img.complete}.html WPTs. It
technically changes our behavior on .srcset changes, but it makes it
consistent with .src changes and other browsers, so seems fine.

There are a couple regressions in CSP tests and the networkEvent stubs,
but these are really a pre-existing issue. What happens is that, since
the loads are now async, CSP can't figure out the script that triggered
the load anymore. I need to look if there's an easy way to propagate
that information in the image load tasks, but this is trivially
reproducible by changing these tests to use srcset rather than src.

The rest of the test changes are as expected: either new passes, or
expected test changes from this.

Differential Revision: https://phabricator.services.mozilla.com/D215519
2024-08-05 12:23:44 +00:00

80 lines
3.0 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/HTMLPictureElement.h"
#include "mozilla/dom/HTMLPictureElementBinding.h"
#include "mozilla/dom/HTMLImageElement.h"
#include "mozilla/dom/HTMLSourceElement.h"
// Expand NS_IMPL_NS_NEW_HTML_ELEMENT(Picture) to add pref check.
nsGenericHTMLElement* NS_NewHTMLPictureElement(
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
mozilla::dom::FromParser aFromParser) {
RefPtr<mozilla::dom::NodeInfo> nodeInfo(aNodeInfo);
auto* nim = nodeInfo->NodeInfoManager();
return new (nim) mozilla::dom::HTMLPictureElement(nodeInfo.forget());
}
namespace mozilla::dom {
HTMLPictureElement::HTMLPictureElement(
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
: nsGenericHTMLElement(std::move(aNodeInfo)) {}
HTMLPictureElement::~HTMLPictureElement() = default;
NS_IMPL_ELEMENT_CLONE(HTMLPictureElement)
void HTMLPictureElement::RemoveChildNode(nsIContent* aKid, bool aNotify) {
MOZ_ASSERT(aKid);
if (auto* img = HTMLImageElement::FromNode(aKid)) {
img->PictureSourceRemoved(aNotify);
} else if (auto* source = HTMLSourceElement::FromNode(aKid)) {
// Find all img siblings after this <source> to notify them of its demise
nsCOMPtr<nsIContent> nextSibling = source->GetNextSibling();
if (nextSibling && nextSibling->GetParentNode() == this) {
do {
if (auto* img = HTMLImageElement::FromNode(nextSibling)) {
img->PictureSourceRemoved(aNotify, source);
}
} while ((nextSibling = nextSibling->GetNextSibling()));
}
}
nsGenericHTMLElement::RemoveChildNode(aKid, aNotify);
}
void HTMLPictureElement::InsertChildBefore(nsIContent* aKid,
nsIContent* aBeforeThis,
bool aNotify, ErrorResult& aRv) {
nsGenericHTMLElement::InsertChildBefore(aKid, aBeforeThis, aNotify, aRv);
if (aRv.Failed() || !aKid) {
return;
}
if (auto* img = HTMLImageElement::FromNode(aKid)) {
img->PictureSourceAdded(aNotify);
} else if (auto* source = HTMLSourceElement::FromNode(aKid)) {
// Find all img siblings after this <source> to notify them of its insertion
nsCOMPtr<nsIContent> nextSibling = source->GetNextSibling();
if (nextSibling && nextSibling->GetParentNode() == this) {
do {
if (auto* img = HTMLImageElement::FromNode(nextSibling)) {
img->PictureSourceAdded(aNotify, source);
}
} while ((nextSibling = nextSibling->GetNextSibling()));
}
}
}
JSObject* HTMLPictureElement::WrapNode(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return HTMLPictureElement_Binding::Wrap(aCx, this, aGivenProto);
}
} // namespace mozilla::dom