Browse Source

LibWeb: Implement HTMLElement.click()

This doesn't send the correct type of click event, but it does send
something, so it's already somewhat useful. :^)
Andreas Kling 3 years ago
parent
commit
e842e955e5

+ 20 - 0
Userland/Libraries/LibWeb/HTML/HTMLElement.cpp

@@ -389,4 +389,24 @@ void HTMLElement::focus()
     // 5. Unmark the element as locked for focus.
     // 5. Unmark the element as locked for focus.
     m_locked_for_focus = false;
     m_locked_for_focus = false;
 }
 }
+
+// https://html.spec.whatwg.org/multipage/interaction.html#dom-click
+void HTMLElement::click()
+{
+    // FIXME: 1. If this element is a form control that is disabled, then return.
+
+    // 2. If this element's click in progress flag is set, then return.
+    if (m_click_in_progress)
+        return;
+
+    // 3. Set this element's click in progress flag.
+    m_click_in_progress = true;
+
+    // FIXME: 4. Fire a synthetic pointer event named click at this element, with the not trusted flag set.
+    dispatch_event(DOM::Event::create("click"));
+
+    // 5. Unset this element's click in progress flag.
+    m_click_in_progress = false;
+}
+
 }
 }

+ 6 - 1
Userland/Libraries/LibWeb/HTML/HTMLElement.h

@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  *
  *
  * SPDX-License-Identifier: BSD-2-Clause
  * SPDX-License-Identifier: BSD-2-Clause
  */
  */
@@ -42,6 +42,8 @@ public:
 
 
     void focus();
     void focus();
 
 
+    void click();
+
 protected:
 protected:
     virtual void parse_attribute(const FlyString& name, const String& value) override;
     virtual void parse_attribute(const FlyString& name, const String& value) override;
 
 
@@ -60,6 +62,9 @@ private:
 
 
     // https://html.spec.whatwg.org/multipage/interaction.html#locked-for-focus
     // https://html.spec.whatwg.org/multipage/interaction.html#locked-for-focus
     bool m_locked_for_focus { false };
     bool m_locked_for_focus { false };
+
+    // https://html.spec.whatwg.org/multipage/interaction.html#click-in-progress-flag
+    bool m_click_in_progress { false };
 };
 };
 
 
 }
 }

+ 2 - 0
Userland/Libraries/LibWeb/HTML/HTMLElement.idl

@@ -7,6 +7,8 @@ interface HTMLElement : Element {
 
 
     attribute DOMString contentEditable;
     attribute DOMString contentEditable;
 
 
+    undefined click();
+
     // FIXME: Support the optional FocusOptions parameter.
     // FIXME: Support the optional FocusOptions parameter.
     undefined focus();
     undefined focus();