瀏覽代碼

LibWeb: Add DOM::Element::queue_an_element_task(source, steps)

This roughly models the "queue an element task" algorithm from the spec.
For safety, this captures a strong reference to the element, and then
bundles that with a callback into a HTML::Task (that we then queue up.)
Andreas Kling 3 年之前
父節點
當前提交
aa3ba629ba
共有 2 個文件被更改,包括 13 次插入0 次删除
  1. 10 0
      Userland/Libraries/LibWeb/DOM/Element.cpp
  2. 3 0
      Userland/Libraries/LibWeb/DOM/Element.h

+ 10 - 0
Userland/Libraries/LibWeb/DOM/Element.cpp

@@ -16,6 +16,7 @@
 #include <LibWeb/DOM/HTMLCollection.h>
 #include <LibWeb/DOM/ShadowRoot.h>
 #include <LibWeb/DOM/Text.h>
+#include <LibWeb/HTML/EventLoop/EventLoop.h>
 #include <LibWeb/HTML/Parser/HTMLDocumentParser.h>
 #include <LibWeb/Layout/BlockBox.h>
 #include <LibWeb/Layout/InlineNode.h>
@@ -377,4 +378,13 @@ void Element::make_html_uppercased_qualified_name()
         m_html_uppercased_qualified_name = qualified_name();
 }
 
+// https://html.spec.whatwg.org/multipage/webappapis.html#queue-an-element-task
+void Element::queue_an_element_task(HTML::Task::Source source, Function<void()> steps)
+{
+    auto task = HTML::Task::create(source, &document(), [strong_this = NonnullRefPtr(*this), steps = move(steps)] {
+        steps();
+    });
+    HTML::main_thread_event_loop().task_queue().add(move(task));
+}
+
 }

+ 3 - 0
Userland/Libraries/LibWeb/DOM/Element.h

@@ -15,6 +15,7 @@
 #include <LibWeb/DOM/NonDocumentTypeChildNode.h>
 #include <LibWeb/DOM/ParentNode.h>
 #include <LibWeb/HTML/AttributeNames.h>
+#include <LibWeb/HTML/EventLoop/Task.h>
 #include <LibWeb/HTML/TagNames.h>
 #include <LibWeb/Layout/Node.h>
 #include <LibWeb/QualifiedName.h>
@@ -104,6 +105,8 @@ public:
         m_custom_properties.set(custom_property_name, style_property);
     }
 
+    void queue_an_element_task(HTML::Task::Source, Function<void()>);
+
 protected:
     RefPtr<Layout::Node> create_layout_node() override;