From 60ec00626572b256ca91a92bfbf29f84adc2aca0 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 10 Apr 2022 20:38:16 +0200 Subject: [PATCH] LibWeb: Update layout in HTMLElement.offset{Width,Height} If we don't do this, we may be returning stale values. --- .../Libraries/LibWeb/HTML/HTMLElement.cpp | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp index 73de1cd369..ed1efe27c2 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp @@ -149,17 +149,33 @@ int HTMLElement::offset_left() const // https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsetwidth int HTMLElement::offset_width() const { - if (auto* paint_box = this->paint_box()) - return paint_box->border_box_width(); - return 0; + // NOTE: Ensure that layout is up-to-date before looking at metrics. + const_cast(document()).update_layout(); + + // 1. If the element does not have any associated CSS layout box return zero and terminate this algorithm. + if (!paint_box()) + return 0; + + // 2. Return the width of the axis-aligned bounding box of the border boxes of all fragments generated by the element’s principal box, + // ignoring any transforms that apply to the element and its ancestors. + // FIXME: Account for inline boxes. + return paint_box()->border_box_width(); } // https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsetheight int HTMLElement::offset_height() const { - if (auto* paint_box = this->paint_box()) - return paint_box->border_box_height(); - return 0; + // NOTE: Ensure that layout is up-to-date before looking at metrics. + const_cast(document()).update_layout(); + + // 1. If the element does not have any associated CSS layout box return zero and terminate this algorithm. + if (!paint_box()) + return 0; + + // 2. Return the height of the axis-aligned bounding box of the border boxes of all fragments generated by the element’s principal box, + // ignoring any transforms that apply to the element and its ancestors. + // FIXME: Account for inline boxes. + return paint_box()->border_box_height(); } bool HTMLElement::cannot_navigate() const