Bug 1648064 - Switch DOM images to work like CSS images for the purposes of printing. r=tnikkel,smaug

Make them perform the image load (if needed), instead of copying the
image requests from the original document.

This is needed for CSS for stuff like:

@media print {
  #foo::before {
    content: url(bar.png);
  }
}

And so on. For images, we should do this as well. Nothing prevents you
from doing:

  <picture>
    <source srcset="print.png" media="print">
    <source srcset="screen.png" media="not print">
    <img>
  </picture>

And that should in theory work. It works after this patch, and I added a
test for that.

This patch is a bit bigger than I'd like, but I didn't find a more
reasonable way to split it up.

Making static docs able to do image loads is most of the patch and is
mostly straight-forward. This allows to remove the hacky "change the
loading document" thing that CSS images do, which is just working around
the CSP of the print document.

I need to enable background colors in printpreview_helper so as to be
able to have a reference page for all the different image types.

Differential Revision: https://phabricator.services.mozilla.com/D81779
This commit is contained in:
Emilio Cobos Álvarez
2020-07-22 20:29:00 +00:00
parent bee55a2da6
commit f7bd619fb5
19 changed files with 173 additions and 142 deletions

View File

@@ -735,32 +735,25 @@ uint32_t HTMLImageElement::NaturalWidth() {
}
nsresult HTMLImageElement::CopyInnerTo(HTMLImageElement* aDest) {
bool destIsStatic = aDest->OwnerDoc()->IsStaticDocument();
if (destIsStatic) {
CreateStaticImageClone(aDest);
}
nsresult rv = nsGenericHTMLElement::CopyInnerTo(aDest);
if (NS_FAILED(rv)) {
return rv;
}
if (!destIsStatic) {
// In SetAttr (called from nsGenericHTMLElement::CopyInnerTo), aDest skipped
// doing the image load because we passed in false for aNotify. But we
// really do want it to do the load, so set it up to happen once the cloning
// reaches a stable state.
if (!aDest->InResponsiveMode() &&
aDest->HasAttr(kNameSpaceID_None, nsGkAtoms::src) &&
aDest->ShouldLoadImage()) {
// Mark channel as urgent-start before load image if the image load is
// initaiated by a user interaction.
mUseUrgentStartForChannel = UserActivation::IsHandlingUserInput();
// In SetAttr (called from nsGenericHTMLElement::CopyInnerTo), aDest skipped
// doing the image load because we passed in false for aNotify. But we
// really do want it to do the load, so set it up to happen once the cloning
// reaches a stable state.
if (!aDest->InResponsiveMode() &&
aDest->HasAttr(kNameSpaceID_None, nsGkAtoms::src) &&
aDest->ShouldLoadImage()) {
// Mark channel as urgent-start before load image if the image load is
// initaiated by a user interaction.
mUseUrgentStartForChannel = UserActivation::IsHandlingUserInput();
nsContentUtils::AddScriptRunner(NewRunnableMethod<bool>(
"dom::HTMLImageElement::MaybeLoadImage", aDest,
&HTMLImageElement::MaybeLoadImage, false));
}
nsContentUtils::AddScriptRunner(NewRunnableMethod<bool>(
"dom::HTMLImageElement::MaybeLoadImage", aDest,
&HTMLImageElement::MaybeLoadImage, false));
}
return NS_OK;