Truly independent web browser
LibWeb: Compute ResizeObserver sizes as the spec says
Andreas Kling180d0b975637
diff --git a/Libraries/LibWeb/ResizeObserver/ResizeObservation.cpp b/Libraries/LibWeb/ResizeObserver/ResizeObservation.cppindex 802cc644a46..d6e57fca211 100644--- a/Libraries/LibWeb/ResizeObserver/ResizeObservation.cpp+++ b/Libraries/LibWeb/ResizeObserver/ResizeObservation.cpp@@ -17,11 +17,13 @@ WebIDL::ExceptionOr<GC::Ref<ResizeObservation>> ResizeObservation::create(DOM::Ereturn GC::Heap::the().allocate<ResizeObservation>(target, observed_box);}+// https://drafts.csswg.org/resize-observer-1/#dom-resizeobservation-resizeobservationResizeObservation::ResizeObservation(DOM::Element& target, ObservedBox observed_box): m_target(target), m_observed_box(observed_box){- m_last_reported_sizes.append({});+ // 4. Set this internal lastReportedSizes slot to [(-1,-1)].+ m_last_reported_sizes.append({ .inline_size = -1, .block_size = -1 });}// https://drafts.csswg.org/resize-observer-1/#dom-resizeobservation-isactivediff --git a/Libraries/LibWeb/ResizeObserver/ResizeObserverEntry.cpp b/Libraries/LibWeb/ResizeObserver/ResizeObserverEntry.cppindex 53eb7c05d9b..1bbb1527dda 100644--- a/Libraries/LibWeb/ResizeObserver/ResizeObserverEntry.cpp+++ b/Libraries/LibWeb/ResizeObserver/ResizeObserverEntry.cpp@@ -34,22 +34,26 @@ WebIDL::ExceptionOr<GC::Ref<ResizeObserverEntry>> ResizeObserverEntry::create_anresize_observer_entry->m_device_pixel_content_box_size.append(device_pixel_content_box_size);// 6. Set this.contentRect to logical this.contentBoxSize given target and observedBox of "content-box".+ // NB: The contentRect is in physical coordinates, so the logical sizes are mapped back to a width and a height.double x = 0;double y = 0;double width = content_box_size->inline_size();double height = content_box_size->block_size();- // 7. If target is not an SVG element or target is an SVG element with an associated CSS layout box do these steps:// NB: Layout was up to date when observations were gathered, but a previous// observer's callback may have invalidated it before we get here.// This matches the behavior of all major browsers.auto const* layout_node = target.unsafe_layout_node(read);- if (!target.is_svg_element() && layout_node && Painting::has_committed_box(*layout_node)) {- auto absolute_padding_rect = Painting::absolute_padding_box_rect(*layout_node);+ if (layout_node && layout_node->writing_mode() != CSS::WritingMode::HorizontalTb)+ swap(width, height);++ // 7. If target is not an SVG element or target is an SVG element with an associated CSS layout box do these steps:+ if (!target.is_svg_element() && layout_node && !layout_node->is_inline_node() && Painting::has_committed_box(*layout_node)) {+ auto box_model = Painting::box_model(*layout_node);// Set this.contentRect.top to target.padding top.- y = absolute_padding_rect.y().to_double();+ y = box_model.padding.top.to_double();// Set this.contentRect.left to target.padding left.- x = absolute_padding_rect.x().to_double();+ x = box_model.padding.left.to_double();} else if (target.is_svg_element() && layout_node && Painting::has_committed_box(*layout_node)) {// 8. If target is an SVG element without an associated CSS layout box do these steps:// Set this.contentRect.top and this.contentRect.left to 0.diff --git a/Libraries/LibWeb/ResizeObserver/ResizeObserverSize.cpp b/Libraries/LibWeb/ResizeObserver/ResizeObserverSize.cppindex b537b3f039c..355eebc6721 100644--- a/Libraries/LibWeb/ResizeObserver/ResizeObserverSize.cpp+++ b/Libraries/LibWeb/ResizeObserver/ResizeObserverSize.cpp@@ -4,6 +4,7 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/Math.h>#include <LibGC/Heap.h>#include <LibWeb/DOM/Document.h>#include <LibWeb/HTML/Window.h>@@ -27,23 +28,46 @@ ResizeObserverSize::RawSize ResizeObserverSize::compute_box_size(DOM::Element& t// observer's callback may have invalidated it before we get here.// This matches the behavior of all major browsers.auto const* layout_node = target.unsafe_layout_node(read);- if (layout_node && Painting::has_committed_box(*layout_node)) {+ // INTEROP: Non-replaced inline boxes have no content area to observe, so they are reported with a size of 0x0.+ if (layout_node && !layout_node->is_inline_node() && Painting::has_committed_box(*layout_node)) {+ double width = 0;+ double height = 0;switch (observed_box) {+ // -> If observedBox is "border-box"case ObservedBox::BorderBox:- size.inline_size = Painting::border_box_width(*layout_node).to_double();- size.block_size = Painting::border_box_height(*layout_node).to_double();+ // 1. Set computedSize's inlineSize attribute to target's border area inline length.+ // 2. Set computedSize's blockSize attribute to target's border area block length.+ width = Painting::border_box_width(*layout_node).to_double();+ height = Painting::border_box_height(*layout_node).to_double();break;+ // -> If observedBox is "content-box"case ObservedBox::ContentBox:- size.inline_size = Painting::content_width(*layout_node).to_double();- size.block_size = Painting::content_height(*layout_node).to_double();+ // 1. Set computedSize's inlineSize attribute to target's content area inline length.+ // 2. Set computedSize's blockSize attribute to target's content area block length.+ width = Painting::content_width(*layout_node).to_double();+ height = Painting::content_height(*layout_node).to_double();break;+ // -> If observedBox is "device-pixel-content-box"case ObservedBox::DevicePixelContentBox: {+ // 1. Set computedSize's inlineSize attribute to target's content area inline length, in integral device+ // pixels.+ // 2. Set computedSize's blockSize attribute to target's content area block length, in integral device+ // pixels.auto device_pixel_ratio = target.document().window()->device_pixel_ratio();- size.inline_size = Painting::border_box_width(*layout_node).to_double() * device_pixel_ratio;- size.block_size = Painting::border_box_height(*layout_node).to_double() * device_pixel_ratio;+ width = AK::round(Painting::content_width(*layout_node).to_double() * device_pixel_ratio);+ height = AK::round(Painting::content_height(*layout_node).to_double() * device_pixel_ratio);break;}}++ // NB: The inline and block lengths follow the writing mode of the target.+ if (layout_node->writing_mode() == CSS::WritingMode::HorizontalTb) {+ size.inline_size = width;+ size.block_size = height;+ } else {+ size.inline_size = height;+ size.block_size = width;+ }}return size;diff --git a/Tests/LibWeb/Text/expected/ResizeObserver/observe-border-box.txt b/Tests/LibWeb/Text/expected/ResizeObserver/observe-border-box.txtindex 6eaddec1f63..62e60d09b44 100644--- a/Tests/LibWeb/Text/expected/ResizeObserver/observe-border-box.txt+++ b/Tests/LibWeb/Text/expected/ResizeObserver/observe-border-box.txt@@ -1,2 +1,2 @@-contentSize: 100px x 200px; borderBoxSize [inline=140px, block=240px]; contentBoxSize [inline=100px, block=200px]; deviceBoxSize [inline=140px, block=240px]-contentSize: 100px x 200px; borderBoxSize [inline=140px, block=280px]; contentBoxSize [inline=100px, block=200px]; deviceBoxSize [inline=140px, block=280px]+contentSize: 100px x 200px; borderBoxSize [inline=140px, block=240px]; contentBoxSize [inline=100px, block=200px]; deviceBoxSize [inline=100px, block=200px]+contentSize: 100px x 200px; borderBoxSize [inline=140px, block=280px]; contentBoxSize [inline=100px, block=200px]; deviceBoxSize [inline=100px, block=200px]diff --git a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-009.txt b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-009.txtindex df6a326f16a..cb0f20fbbbc 100644--- a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-009.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-009.txt@@ -2,5 +2,5 @@ Harness status: OKFound 1 tests-1 Fail-Fail simple content-box observation\ No newline at end of file+1 Pass+Pass simple content-box observation\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-010.txt b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-010.txtindex d619534db9b..ed182f85cdc 100644--- a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-010.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-010.txt@@ -2,5 +2,5 @@ Harness status: OKFound 1 tests-1 Fail-Fail simple content-box observation but keep border-box size unchanged\ No newline at end of file+1 Pass+Pass simple content-box observation but keep border-box size unchanged\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-011.txt b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-011.txtindex dd3db6aa1db..ac7c918cd82 100644--- a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-011.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-011.txt@@ -2,5 +2,5 @@ Harness status: OKFound 1 tests-1 Fail-Fail simple border-box observation\ No newline at end of file+1 Pass+Pass simple border-box observation\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-012.txt b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-012.txtindex 97d3ad128d5..86cc42b597b 100644--- a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-012.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-012.txt@@ -2,5 +2,5 @@ Harness status: OKFound 1 tests-1 Fail-Fail simple observation with vertical writing mode\ No newline at end of file+1 Pass+Pass simple observation with vertical writing mode\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-013.txt b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-013.txtindex b3bde897260..2c02c199b1d 100644--- a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-013.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-013.txt@@ -2,5 +2,5 @@ Harness status: OKFound 1 tests-1 Fail-Fail no observation is fired after the change of writing mode when box's specified size comes from logical size properties.\ No newline at end of file+1 Pass+Pass no observation is fired after the change of writing mode when box's specified size comes from logical size properties.\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-014.txt b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-014.txtindex 9ec852d0da6..54451899bc7 100644--- a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-014.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-014.txt@@ -2,5 +2,5 @@ Harness status: OKFound 1 tests-1 Fail-Fail an observation is fired after the change of writing mode when box's specified size comes from physical size properties.\ No newline at end of file+1 Pass+Pass an observation is fired after the change of writing mode when box's specified size comes from physical size properties.\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-015.txt b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-015.txtindex f3ae24fd801..8b9385620d5 100644--- a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-015.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-015.txt@@ -2,5 +2,5 @@ Harness status: OKFound 1 tests-1 Fail-Fail observe the same target but using a different box should override the previous one\ No newline at end of file+1 Pass+Pass observe the same target but using a different box should override the previous one\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-016.txt b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-016.txtindex 1b6c1cd1193..77a340f7c27 100644--- a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-016.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-016.txt@@ -2,5 +2,5 @@ Harness status: OKFound 1 tests-1 Fail-Fail an observation is fired with box dimensions 0 when element's display property is set to inline\ No newline at end of file+1 Pass+Pass an observation is fired with box dimensions 0 when element's display property is set to inline\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-017.txt b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-017.txtindex ee5ee91372c..511657086b7 100644--- a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-017.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-017.txt@@ -2,5 +2,5 @@ Harness status: OKFound 1 tests-1 Fail-Fail observations fire once with 0x0 size for non-replaced inline elements\ No newline at end of file+1 Pass+Pass observations fire once with 0x0 size for non-replaced inline elements\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-018.txt b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-018.txtindex f55df4641e0..d96374f7f61 100644--- a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-018.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe-018.txt@@ -2,5 +2,5 @@ Harness status: OKFound 1 tests-1 Fail-Fail Box sizing and Resize Observer notifications\ No newline at end of file+1 Pass+Pass Box sizing and Resize Observer notifications\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe.txt b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe.txtindex ea20142331f..b20d9bc11bb 100644--- a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/observe.txt@@ -2,8 +2,8 @@ Harness status: OKFound 21 tests-10 Pass-11 Fail+20 Pass+1 FailPass ResizeObserver implementedPass guardPass test0: simple observation@@ -14,14 +14,14 @@ Pass test4: unobserve target stops notifications, unobserve non-observed does noFail test5: observe imgPass test6: iframe notificationsPass test7: callback.this-Fail test8: simple content-box observation-Fail test9: simple content-box observation but keep border-box size unchanged-Fail test10: simple border-box observation-Fail test11: simple observation with vertical writing mode-Fail test12: no observation is fired after the change of writing mode when box's specified size comes from logical size properties.-Fail test13: an observation is fired after the change of writing mode when box's specified size comes from physical size properties.-Fail test14: observe the same target but using a different box should override the previous one-Fail test15: an observation is fired with box dimensions 0 when element's display property is set to inline-Fail test16: observations fire once with 0x0 size for non-replaced inline elements-Fail test17: Box sizing snd Resize Observer notifications+Pass test8: simple content-box observation+Pass test9: simple content-box observation but keep border-box size unchanged+Pass test10: simple border-box observation+Pass test11: simple observation with vertical writing mode+Pass test12: no observation is fired after the change of writing mode when box's specified size comes from logical size properties.+Pass test13: an observation is fired after the change of writing mode when box's specified size comes from physical size properties.+Pass test14: observe the same target but using a different box should override the previous one+Pass test15: an observation is fired with box dimensions 0 when element's display property is set to inline+Pass test16: observations fire once with 0x0 size for non-replaced inline elements+Pass test17: Box sizing snd Resize Observer notificationsPass test18: an observation is fired when device-pixel-content-box is being observed\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/zoom.txt b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/zoom.txtindex 3a5718b4fe0..764fd07c694 100644--- a/Tests/LibWeb/Text/expected/wpt-import/resize-observer/zoom.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/resize-observer/zoom.txt@@ -2,5 +2,5 @@ Harness status: OKFound 1 tests-1 Fail-Fail ResizeObserver sizes account for zoom\ No newline at end of file+1 Pass+Pass ResizeObserver sizes account for zoom\ No newline at end of file