LibWeb: Support HTMLElement.offset{Width,Height}

This commit is contained in:
Andreas Kling 2021-09-30 01:35:19 +02:00
parent 840822b8f1
commit 439be913cf
Notes: sideshowbarker 2024-07-18 03:18:50 +09:00
3 changed files with 27 additions and 4 deletions

View File

@ -15,6 +15,7 @@
#include <LibWeb/HTML/HTMLAnchorElement.h>
#include <LibWeb/HTML/HTMLBodyElement.h>
#include <LibWeb/HTML/HTMLElement.h>
#include <LibWeb/Layout/Box.h>
#include <LibWeb/Layout/BreakNode.h>
#include <LibWeb/Layout/TextNode.h>
#include <LibWeb/UIEvents/EventNames.h>
@ -122,7 +123,8 @@ String HTMLElement::inner_text()
return builder.to_string();
}
unsigned HTMLElement::offset_top() const
// // https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsettop
int HTMLElement::offset_top() const
{
if (is<HTML::HTMLBodyElement>(this) || !layout_node() || !parent_element() || !parent_element()->layout_node())
return 0;
@ -131,7 +133,8 @@ unsigned HTMLElement::offset_top() const
return position.y() - parent_position.y();
}
unsigned HTMLElement::offset_left() const
// https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsetleft
int HTMLElement::offset_left() const
{
if (is<HTML::HTMLBodyElement>(this) || !layout_node() || !parent_element() || !parent_element()->layout_node())
return 0;
@ -140,6 +143,22 @@ unsigned HTMLElement::offset_left() const
return position.x() - parent_position.x();
}
// https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsetwidth
int HTMLElement::offset_width() const
{
if (!layout_node() || !layout_node()->is_box())
return 0;
return static_cast<Layout::Box const&>(*layout_node()).border_box_width();
}
// https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsetheight
int HTMLElement::offset_height() const
{
if (!layout_node() || !layout_node()->is_box())
return 0;
return static_cast<Layout::Box const&>(*layout_node()).border_box_height();
}
bool HTMLElement::cannot_navigate() const
{
// FIXME: Return true if element's node document is not fully active

View File

@ -31,8 +31,10 @@ public:
String inner_text();
void set_inner_text(StringView);
unsigned offset_top() const;
unsigned offset_left() const;
int offset_top() const;
int offset_left() const;
int offset_width() const;
int offset_height() const;
bool cannot_navigate() const;

View File

@ -11,6 +11,8 @@ interface HTMLElement : Element {
readonly attribute long offsetTop;
readonly attribute long offsetLeft;
readonly attribute long offsetWidth;
readonly attribute long offsetHeight;
// FIXME: This should come from a HTMLOrSVGElement mixin
[SameObject] readonly attribute DOMStringMap dataset;