summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-04-15 20:48:55 +0300
committerAndreas Kling <kling@serenityos.org>2021-04-15 20:22:08 +0200
commit815934a95dc7f3bc540a6c66988073d0dddfb87e (patch)
tree58dc5ba490f975eb7f26f5b57d9372ded08f8436 /Userland/Libraries/LibWeb
parentc5769a7033b1acd5ff18f4fbb11f582809f77d47 (diff)
downloadserenity-815934a95dc7f3bc540a6c66988073d0dddfb87e.zip
LibWeb: Expose the HTMLElement::{offsetLeft, offsetTop} attributes
These describe the border box of an element relative to their parent.
Diffstat (limited to 'Userland/Libraries/LibWeb')
-rw-r--r--Userland/Libraries/LibWeb/HTML/HTMLElement.cpp20
-rw-r--r--Userland/Libraries/LibWeb/HTML/HTMLElement.h3
-rw-r--r--Userland/Libraries/LibWeb/HTML/HTMLElement.idl3
3 files changed, 25 insertions, 1 deletions
diff --git a/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp
index 73113dd242..d641396485 100644
--- a/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp
+++ b/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp
@@ -27,13 +27,13 @@
#include <AK/StringBuilder.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Parser.h>
-#include <LibJS/Runtime/ScriptFunction.h>
#include <LibWeb/DOM/DOMException.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/EventListener.h>
#include <LibWeb/DOM/ExceptionOr.h>
#include <LibWeb/HTML/EventHandler.h>
#include <LibWeb/HTML/HTMLAnchorElement.h>
+#include <LibWeb/HTML/HTMLBodyElement.h>
#include <LibWeb/HTML/HTMLElement.h>
#include <LibWeb/Layout/BreakNode.h>
#include <LibWeb/Layout/TextNode.h>
@@ -141,6 +141,24 @@ String HTMLElement::inner_text()
return builder.to_string();
}
+unsigned HTMLElement::offset_top() const
+{
+ if (is<HTML::HTMLBodyElement>(this) || !layout_node() || !parent_element() || !parent_element()->layout_node())
+ return 0;
+ auto position = layout_node()->box_type_agnostic_position();
+ auto parent_position = parent_element()->layout_node()->box_type_agnostic_position();
+ return position.y() - parent_position.y();
+}
+
+unsigned HTMLElement::offset_left() const
+{
+ if (is<HTML::HTMLBodyElement>(this) || !layout_node() || !parent_element() || !parent_element()->layout_node())
+ return 0;
+ auto position = layout_node()->box_type_agnostic_position();
+ auto parent_position = parent_element()->layout_node()->box_type_agnostic_position();
+ return position.x() - parent_position.x();
+}
+
bool HTMLElement::cannot_navigate() const
{
// FIXME: Return true if element's node document is not fully active
diff --git a/Userland/Libraries/LibWeb/HTML/HTMLElement.h b/Userland/Libraries/LibWeb/HTML/HTMLElement.h
index f498d6c781..b35e579217 100644
--- a/Userland/Libraries/LibWeb/HTML/HTMLElement.h
+++ b/Userland/Libraries/LibWeb/HTML/HTMLElement.h
@@ -50,6 +50,9 @@ public:
String inner_text();
void set_inner_text(StringView);
+ unsigned offset_top() const;
+ unsigned offset_left() const;
+
bool cannot_navigate() const;
protected:
diff --git a/Userland/Libraries/LibWeb/HTML/HTMLElement.idl b/Userland/Libraries/LibWeb/HTML/HTMLElement.idl
index 5369c9a3cd..5131df71c3 100644
--- a/Userland/Libraries/LibWeb/HTML/HTMLElement.idl
+++ b/Userland/Libraries/LibWeb/HTML/HTMLElement.idl
@@ -9,6 +9,9 @@ interface HTMLElement : Element {
[LegacyNullToEmptyString] attribute DOMString innerText;
+ readonly attribute long offsetTop;
+ readonly attribute long offsetLeft;
+
// FIXME: These should all come from a GlobalEventHandlers mixin
attribute EventHandler onabort;
attribute EventHandler onauxclick;