void ImageDocument::ShrinkToFit() { if (!mImageContent) { return; } if (GetZoomLevel() != mOriginalZoomLevel && mImageIsResized && !nsContentUtils::IsChildOfSameType(this)) { // If we're zoomed, so that we don't maintain the invariant that // mImageIsResized if and only if its displayed width/height fit in // mVisibleWidth/mVisibleHeight, then we may need to switch to/from the // overflowingVertical class here, because our viewport size may have // changed and we don't plan to adjust the image size to compensate. Since // mImageIsResized it has a "height" attribute set, and we can just get the // displayed image height by getting .height on the HTMLImageElement. HTMLImageElement* img = HTMLImageElement::FromContent(mImageContent); uint32_t imageHeight = img->Height(); nsDOMTokenList* classList = img->ClassList(); ErrorResult ignored; if (imageHeight > mVisibleHeight) { classList->Add(NS_LITERAL_STRING("overflowingVertical"), ignored); } else { classList->Remove(NS_LITERAL_STRING("overflowingVertical"), ignored); } ignored.SuppressException(); return; } // Keep image content alive while changing the attributes. nsCOMPtr<Element> imageContent = mImageContent; nsCOMPtr<nsIDOMHTMLImageElement> image = do_QueryInterface(imageContent); image->SetWidth(std::max(1, NSToCoordFloor(GetRatio() * mImageWidth))); image->SetHeight(std::max(1, NSToCoordFloor(GetRatio() * mImageHeight))); // The view might have been scrolled when zooming in, scroll back to the // origin now that we're showing a shrunk-to-window version. ScrollImageTo(0, 0, false); if (!mImageContent) { // ScrollImageTo flush destroyed our content. return; } SetModeClass(eShrinkToFit); mImageIsResized = true; UpdateTitleAndCharset(); }