Skip to content

Truly independent web browser

LibWeb: Let page zoom enlarge an image shown on its own

sideshowbarker0a4c6a58ea6f
diff --git a/Libraries/LibWeb/DOM/DocumentLoading.cpp b/Libraries/LibWeb/DOM/DocumentLoading.cpp
index 9878807fa63..756320dd6cc 100644
--- a/Libraries/LibWeb/DOM/DocumentLoading.cpp
+++ b/Libraries/LibWeb/DOM/DocumentLoading.cpp
@@ -389,7 +389,9 @@ static WebIDL::ExceptionOr<GC::Ref<DOM::Document>> load_media_document(HTML::Nav
if (type.is_image()) {
auto img_element = TRY(DOM::create_element(document, HTML::TagNames::img, Namespace::HTML));
image_element = as<HTML::HTMLImageElement>(*img_element);
- set_host_element_style(img_element, Utf16String::formatted("{} background-color: #fff;", host_element_style));
+ // Auto margins center an image that fits and keep overflow reachable from the top-left corner.
+ set_host_element_style(*document->body(), u"display: flex; min-height: 100vh; margin: 0;"sv);
+ set_host_element_style(img_element, u"flex: none; max-width: 100vw; max-height: 100vh; margin: auto; background-color: #fff;"sv);
img_element->set_attribute_value(HTML::AttributeNames::src, url_utf16);
TRY(document->body()->append_child(img_element));
TRY(insert_title(document, url_string));
diff --git a/Libraries/LibWeb/HTML/HTMLImageElement.cpp b/Libraries/LibWeb/HTML/HTMLImageElement.cpp
index 82b2911c7f0..e55008ff1bd 100644
--- a/Libraries/LibWeb/HTML/HTMLImageElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLImageElement.cpp
@@ -9,6 +9,7 @@
#include <LibGC/Weak.h>
#include <LibGfx/Bitmap.h>
#include <LibWeb/ARIA/Roles.h>
+#include <LibWeb/CSS/CSSStyleProperties.h>
#include <LibWeb/CSS/ElementBoxKind.h>
#include <LibWeb/CSS/Parser/Parser.h>
#include <LibWeb/CSS/PropertyID.h>
@@ -49,6 +50,7 @@
#include <LibWeb/Layout/RenderDocument.h>
#include <LibWeb/Loader/ResourceLoader.h>
#include <LibWeb/Namespace.h>
+#include <LibWeb/Page/Page.h>
#include <LibWeb/PaintTiming/PaintTiming.h>
#include <LibWeb/Painting/BoxViews.h>
#include <LibWeb/Painting/PaintFacts.h>
@@ -162,6 +164,7 @@ void HTMLImageElement::adopted_from(DOM::Document& old_document)
{
old_document.unregister_viewport_client(*this);
document().register_viewport_client(*this);
+ m_image_document_zoom_level.clear();
m_document_observer->retarget_for_adoption(document());
@@ -208,6 +211,20 @@ void HTMLImageElement::removed_from(IsSubtreeRoot is_subtree_root, DOM::Node* ol
void HTMLImageElement::set_resource_from_navigation_response(GC::Ref<SharedResourceRequest> resource)
{
m_resource_from_navigation_response = resource;
+ update_image_document_zoom();
+}
+
+void HTMLImageElement::update_image_document_zoom()
+{
+ auto zoom_level = document().page().client().zoom_level();
+ m_image_document_zoom_level = zoom_level;
+
+ // Fit against the viewport at 100% page zoom. Viewport units keep the fit responsive to window resizing.
+ auto style = this->style();
+ auto max_width = Utf16String::formatted("{}vw", 100 * zoom_level);
+ auto max_height = Utf16String::formatted("{}vh", 100 * zoom_level);
+ MUST(style->set_property(CSS::PropertyID::MaxWidth, max_width.utf16_view()));
+ MUST(style->set_property(CSS::PropertyID::MaxHeight, max_height.utf16_view()));
}
void HTMLImageElement::visit_edges(Cell::Visitor& visitor)
@@ -1436,6 +1453,9 @@ void HTMLImageElement::stop_delaying_the_load_event_for_aborted_requests()
void HTMLImageElement::did_set_viewport_rect(CSSPixelRect const& viewport_rect)
{
+ if (m_image_document_zoom_level.has_value() && m_image_document_zoom_level.value() != document().page().client().zoom_level())
+ update_image_document_zoom();
+
if (viewport_rect.size() == m_last_seen_viewport_size)
return;
m_last_seen_viewport_size = viewport_rect.size();
diff --git a/Libraries/LibWeb/HTML/HTMLImageElement.h b/Libraries/LibWeb/HTML/HTMLImageElement.h
index 3b2559dda6b..b2eb7b95794 100644
--- a/Libraries/LibWeb/HTML/HTMLImageElement.h
+++ b/Libraries/LibWeb/HTML/HTMLImageElement.h
@@ -167,6 +167,7 @@ private:
void handle_failed_fetch();
void add_callbacks_to_image_request(GC::Ref<ImageRequest>, bool maybe_omit_events, Utf16View url_string, Utf16View previous_url, SharedResourceRequest::DelaysLoadEvent);
void stop_delaying_the_load_event_for_aborted_requests();
+ void update_image_document_zoom();
void create_alt_text_shadow_tree();
void remove_alt_text_shadow_tree();
@@ -205,6 +206,7 @@ private:
SourceSet m_source_set;
CSSPixelSize m_last_seen_viewport_size;
+ Optional<double> m_image_document_zoom_level;
bool m_is_child_of_picture { false };
diff --git a/Tests/LibWeb/Text/expected/HTML/image-document-content-security-policy.txt b/Tests/LibWeb/Text/expected/HTML/image-document-content-security-policy.txt
index 2fd441da380..07e7bc460fb 100644
--- a/Tests/LibWeb/Text/expected/HTML/image-document-content-security-policy.txt
+++ b/Tests/LibWeb/Text/expected/HTML/image-document-content-security-policy.txt
@@ -1,5 +1,5 @@
Image size: 120x120
Root background: rgb(34, 34, 34)
-Image position: absolute
+Image position: static
Other image: blocked by img-src
Same image: blocked by img-src
diff --git a/Tests/LibWeb/Text/expected/HTML/image-document-page-zoom.txt b/Tests/LibWeb/Text/expected/HTML/image-document-page-zoom.txt
new file mode 100644
index 00000000000..7e26d8f2872
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/image-document-page-zoom.txt
@@ -0,0 +1,11 @@
+Large, 100%: 800x600 at 0,0
+Large, 200%: 1600x1200 at 0,0
+Scrollable in both axes: true
+Large, 50%: 400x300 at 200,150
+Large, resized at 200%: 800x600 at 0,0
+Small, 100%: 100x80 at 350,260
+Small, 200%: 200x160 at 300,220
+Small, 50%: 50x40 at 375,280
+Device pixel ratio: 2
+Large, DPR 2, 100%: 800x600 at 0,0
+Large, DPR 2, 200%: 1600x1200 at 0,0
diff --git a/Tests/LibWeb/Text/input/HTML/image-document-page-zoom.html b/Tests/LibWeb/Text/input/HTML/image-document-page-zoom.html
new file mode 100644
index 00000000000..efa2616c8ad
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/image-document-page-zoom.html
@@ -0,0 +1,104 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ const urls = [];
+ const popup = window.open();
+ const frame = () => new Promise(resolve => popup.requestAnimationFrame(resolve));
+ const settle = async () => {
+ await frame();
+ await frame();
+ };
+
+ async function imageURL(width, height) {
+ const canvas = document.createElement("canvas");
+ canvas.width = width;
+ canvas.height = height;
+ canvas.getContext("2d").fillRect(0, 0, width, height);
+ const blob = await new Promise(resolve => canvas.toBlob(resolve));
+ const url = URL.createObjectURL(blob);
+ urls.push(url);
+ return url;
+ }
+
+ async function resize(width, height) {
+ popup.internals.resizeWindow(width, height);
+ if (!await waitForCondition(() =>
+ popup.innerWidth === width / popup.devicePixelRatio &&
+ popup.innerHeight === height / popup.devicePixelRatio))
+ throw new Error("Viewport did not resize");
+ await settle();
+ }
+
+ async function navigate(url) {
+ const previousDocument = popup.document;
+ popup.location.href = url;
+ if (!await waitForCondition(() =>
+ popup.document !== previousDocument &&
+ popup.document.readyState === "complete" &&
+ popup.document.images[0]?.complete &&
+ popup.document.images[0].naturalWidth > 0))
+ throw new Error("Image document did not load");
+ await settle();
+ }
+
+ async function zoom(factor) {
+ popup.internals.setBrowserZoom(factor);
+ await settle();
+ }
+
+ function report(label) {
+ const scrollX = popup.scrollX;
+ const scrollY = popup.scrollY;
+ const rect = popup.document.images[0].getBoundingClientRect();
+ const pixels = value => Math.round(value * popup.devicePixelRatio);
+ println(`${label}: ${pixels(rect.width)}x${pixels(rect.height)} at ${pixels(rect.x + scrollX)},${pixels(rect.y + scrollY)}`);
+ }
+
+ try {
+ await zoom(1);
+ popup.internals.setDevicePixelRatio(1);
+ await resize(800, 600);
+
+ const large = await imageURL(2000, 1500);
+ const small = await imageURL(100, 80);
+ await navigate(large);
+ report("Large, 100%");
+ await zoom(2);
+ report("Large, 200%");
+ popup.scrollTo({ left: 10000, top: 10000, behavior: "instant" });
+ await settle();
+ println(`Scrollable in both axes: ${popup.scrollX > 0 && popup.scrollY > 0}`);
+ popup.scrollTo({ left: 0, top: 0, behavior: "instant" });
+ await settle();
+ await zoom(0.5);
+ report("Large, 50%");
+
+ await zoom(2);
+ await resize(400, 300);
+ report("Large, resized at 200%");
+ await resize(800, 600);
+ await zoom(1);
+ await navigate(small);
+ report("Small, 100%");
+ await zoom(2);
+ report("Small, 200%");
+ await zoom(0.5);
+ report("Small, 50%");
+
+ await zoom(1);
+ await navigate(large);
+ popup.internals.setDevicePixelRatio(2);
+ await resize(800, 600);
+ println(`Device pixel ratio: ${popup.devicePixelRatio}`);
+ report("Large, DPR 2, 100%");
+ await zoom(2);
+ report("Large, DPR 2, 200%");
+ } catch (error) {
+ println(`Error: ${error.message}`);
+ } finally {
+ popup.close();
+ urls.forEach(url => URL.revokeObjectURL(url));
+ }
+ });
+</script>