Bladeren bron

HTML: Put HTMLTextAreaElement selection helpers in FormAssociatedElement

This will allow us to more closely follow the spec and share these
functions with HTMLInputElement.
Shannon Booth 11 maanden geleden
bovenliggende
commit
62bf428a7f

+ 57 - 0
Userland/Libraries/LibWeb/HTML/FormAssociatedElement.cpp

@@ -152,4 +152,61 @@ void FormAssociatedElement::reset_form_owner()
     }
     }
 }
 }
 
 
+// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectionstart
+WebIDL::UnsignedLong FormAssociatedElement::selection_start() const
+{
+    // FIXME: 1. If this element is an input element, and selectionStart does not apply to this element, return null.
+
+    // 2. If there is no selection, return the code unit offset within the relevant value to the character that
+    //    immediately follows the text entry cursor.
+    if (auto navigable = form_associated_element_to_html_element().document().navigable()) {
+        if (auto cursor = navigable->cursor_position())
+            return cursor->offset();
+    }
+
+    // FIXME: 3. Return the code unit offset within the relevant value to the character that immediately follows the start of
+    //           the selection.
+    return 0;
+}
+
+// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#textFieldSelection:dom-textarea/input-selectionstart-2
+WebIDL::ExceptionOr<void> FormAssociatedElement::set_selection_start(WebIDL::UnsignedLong)
+{
+    // FIXME: 1. If this element is an input element, and selectionStart does not apply to this element, throw an
+    //    "InvalidStateError" DOMException.
+
+    // FIXME: 2. Let end be the value of this element's selectionEnd attribute.
+    // FIXME: 3. If end is less than the given value, set end to the given value.
+    // FIXME: 4. Set the selection range with the given value, end, and the value of this element's selectionDirection attribute.
+    return {};
+}
+
+// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectionend
+WebIDL::UnsignedLong FormAssociatedElement::selection_end() const
+{
+    // FIXME: 1. If this element is an input element, and selectionEnd does not apply to this element, return null.
+
+    // 2. If there is no selection, return the code unit offset within the relevant value to the character that
+    //    immediately follows the text entry cursor.
+    if (auto navigable = form_associated_element_to_html_element().document().navigable()) {
+        if (auto cursor = navigable->cursor_position())
+            return cursor->offset();
+    }
+
+    // FIXME: 3. Return the code unit offset within the relevant value to the character that immediately follows the end of
+    //           the selection.
+    return 0;
+}
+
+// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#textFieldSelection:dom-textarea/input-selectionend-3
+WebIDL::ExceptionOr<void> FormAssociatedElement::set_selection_end(WebIDL::UnsignedLong)
+{
+    // FIXME: 1. If this element is an input element, and selectionEnd does not apply to this element, throw an
+    //    "InvalidStateError" DOMException.
+
+    // FIXME: 2. Set the selection range with the value of this element's selectionStart attribute, the given value, and the
+    //           value of this element's selectionDirection attribute.
+    return {};
+}
+
 }
 }

+ 7 - 0
Userland/Libraries/LibWeb/HTML/FormAssociatedElement.h

@@ -9,6 +9,7 @@
 #include <AK/String.h>
 #include <AK/String.h>
 #include <AK/WeakPtr.h>
 #include <AK/WeakPtr.h>
 #include <LibWeb/Forward.h>
 #include <LibWeb/Forward.h>
+#include <LibWeb/WebIDL/Types.h>
 
 
 namespace Web::HTML {
 namespace Web::HTML {
 
 
@@ -88,6 +89,12 @@ public:
     // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-form-reset-control
     // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-form-reset-control
     virtual void reset_algorithm() {};
     virtual void reset_algorithm() {};
 
 
+    WebIDL::UnsignedLong selection_start() const;
+    WebIDL::ExceptionOr<void> set_selection_start(WebIDL::UnsignedLong);
+
+    WebIDL::UnsignedLong selection_end() const;
+    WebIDL::ExceptionOr<void> set_selection_end(WebIDL::UnsignedLong);
+
 protected:
 protected:
     FormAssociatedElement() = default;
     FormAssociatedElement() = default;
     virtual ~FormAssociatedElement() = default;
     virtual ~FormAssociatedElement() = default;

+ 0 - 57
Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp

@@ -213,63 +213,6 @@ void HTMLTextAreaElement::set_custom_validity(String const& error)
     dbgln("(STUBBED) HTMLTextAreaElement::set_custom_validity(\"{}\"). Called on: {}", error, debug_description());
     dbgln("(STUBBED) HTMLTextAreaElement::set_custom_validity(\"{}\"). Called on: {}", error, debug_description());
 }
 }
 
 
-// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectionstart
-WebIDL::UnsignedLong HTMLTextAreaElement::selection_start() const
-{
-    // 1. If this element is an input element, and selectionStart does not apply to this element, return null.
-
-    // 2. If there is no selection, return the code unit offset within the relevant value to the character that
-    //    immediately follows the text entry cursor.
-    if (auto navigable = document().navigable()) {
-        if (auto cursor = navigable->cursor_position())
-            return cursor->offset();
-    }
-
-    // FIXME: 3. Return the code unit offset within the relevant value to the character that immediately follows the start of
-    //           the selection.
-    return 0;
-}
-
-// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#textFieldSelection:dom-textarea/input-selectionstart-2
-WebIDL::ExceptionOr<void> HTMLTextAreaElement::set_selection_start(WebIDL::UnsignedLong)
-{
-    // 1. If this element is an input element, and selectionStart does not apply to this element, throw an
-    //    "InvalidStateError" DOMException.
-
-    // FIXME: 2. Let end be the value of this element's selectionEnd attribute.
-    // FIXME: 3. If end is less than the given value, set end to the given value.
-    // FIXME: 4. Set the selection range with the given value, end, and the value of this element's selectionDirection attribute.
-    return {};
-}
-
-// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectionend
-WebIDL::UnsignedLong HTMLTextAreaElement::selection_end() const
-{
-    // 1. If this element is an input element, and selectionEnd does not apply to this element, return null.
-
-    // 2. If there is no selection, return the code unit offset within the relevant value to the character that
-    //    immediately follows the text entry cursor.
-    if (auto navigable = document().navigable()) {
-        if (auto cursor = navigable->cursor_position())
-            return cursor->offset();
-    }
-
-    // FIXME: 3. Return the code unit offset within the relevant value to the character that immediately follows the end of
-    //           the selection.
-    return 0;
-}
-
-// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#textFieldSelection:dom-textarea/input-selectionend-3
-WebIDL::ExceptionOr<void> HTMLTextAreaElement::set_selection_end(WebIDL::UnsignedLong)
-{
-    // 1. If this element is an input element, and selectionEnd does not apply to this element, throw an
-    //    "InvalidStateError" DOMException.
-
-    // FIXME: 2. Set the selection range with the value of this element's selectionStart attribute, the given value, and the
-    //           value of this element's selectionDirection attribute.
-    return {};
-}
-
 // https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-maxlength
 // https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-maxlength
 WebIDL::Long HTMLTextAreaElement::max_length() const
 WebIDL::Long HTMLTextAreaElement::max_length() const
 {
 {

+ 0 - 6
Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.h

@@ -84,12 +84,6 @@ public:
     bool report_validity();
     bool report_validity();
     void set_custom_validity(String const& error);
     void set_custom_validity(String const& error);
 
 
-    WebIDL::UnsignedLong selection_start() const;
-    WebIDL::ExceptionOr<void> set_selection_start(WebIDL::UnsignedLong);
-
-    WebIDL::UnsignedLong selection_end() const;
-    WebIDL::ExceptionOr<void> set_selection_end(WebIDL::UnsignedLong);
-
     WebIDL::Long max_length() const;
     WebIDL::Long max_length() const;
     WebIDL::ExceptionOr<void> set_max_length(WebIDL::Long);
     WebIDL::ExceptionOr<void> set_max_length(WebIDL::Long);