nsresult HTMLImageElement::LoadSelectedImage(bool aForce, bool aNotify) { nsresult rv = NS_ERROR_FAILURE; if (aForce) { // In responsive mode we generally want to re-run the full // selection algorithm whenever starting a new load, per // spec. This also causes us to re-resolve the URI as appropriate. UpdateResponsiveSource(); } if (mResponsiveSelector) { nsCOMPtr<nsIURI> url = mResponsiveSelector->GetSelectedImageURL(); rv = LoadImage(url, aForce, aNotify, eImageLoadType_Imageset); } else { nsAutoString src; if (!GetAttr(kNameSpaceID_None, nsGkAtoms::src, src)) { CancelImageRequests(aNotify); rv = NS_OK; } else { // If we have a srcset attribute or are in a <picture> element, // we always use the Imageset load type, even if we parsed no // valid responsive sources from either, per spec. rv = LoadImage(src, aForce, aNotify, HaveSrcsetOrInPicture() ? eImageLoadType_Imageset : eImageLoadType_Normal); } } if (NS_FAILED(rv)) { CancelImageRequests(aNotify); } return rv; }
bool HTMLImageElement::InResponsiveMode() { // When we lose srcset or leave a <picture> element, the fallback to img.src // will happen from the microtask, and we should behave responsively in the // interim return mResponsiveSelector || mPendingImageLoadTask || HaveSrcsetOrInPicture(); }
nsresult HTMLImageElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent, nsIContent* aBindingParent, bool aCompileEventHandlers) { nsresult rv = nsGenericHTMLElement::BindToTree(aDocument, aParent, aBindingParent, aCompileEventHandlers); NS_ENSURE_SUCCESS(rv, rv); nsImageLoadingContent::BindToTree(aDocument, aParent, aBindingParent, aCompileEventHandlers); if (aParent) { UpdateFormOwner(); } if (HaveSrcsetOrInPicture()) { if (aDocument && !mInDocResponsiveContent) { aDocument->AddResponsiveContent(this); mInDocResponsiveContent = true; } bool forceLoadEvent = HTMLPictureElement::IsPictureEnabled() && aParent && aParent->IsHTMLElement(nsGkAtoms::picture); QueueImageLoadTask(forceLoadEvent); } else if (!InResponsiveMode() && HasAttr(kNameSpaceID_None, nsGkAtoms::src)) { // We skip loading when our attributes were set from parser land, // so trigger a aForce=false load now to check if things changed. // This isn't necessary for responsive mode, since creating the // image load task is asynchronous we don't need to take special // care to avoid doing so when being filled by the parser. // FIXME: Bug 660963 it would be nice if we could just have // ClearBrokenState update our state and do it fast... ClearBrokenState(); RemoveStatesSilently(NS_EVENT_STATE_BROKEN); // We still act synchronously for the non-responsive case (Bug // 1076583), but still need to delay if it is unsafe to run // script. // If loading is temporarily disabled, don't even launch MaybeLoadImage. // Otherwise MaybeLoadImage may run later when someone has reenabled // loading. if (LoadingEnabled()) { nsContentUtils::AddScriptRunner( NS_NewRunnableMethod(this, &HTMLImageElement::MaybeLoadImage)); } } return rv; }