Browse Source

LibWeb: Implement HTMLTableElement caption attributes

* caption - Getter and setter for the caption element
* createCaption - If necessary, creates a new caption element
  and add it to the table
* deleteCaption - If a caption element exists in the table, delete it
Adam Hodgen 4 năm trước cách đây
mục cha
commit
0fa0367a39

+ 2 - 0
Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp

@@ -872,6 +872,7 @@ void generate_implementation(const IDL::Interface& interface)
 #include <LibWeb/Bindings/HTMLFormElementWrapper.h>
 #include <LibWeb/Bindings/HTMLHeadElementWrapper.h>
 #include <LibWeb/Bindings/HTMLImageElementWrapper.h>
+#include <LibWeb/Bindings/HTMLTableCaptionElementWrapper.h>
 #include <LibWeb/Bindings/ImageDataWrapper.h>
 #include <LibWeb/Bindings/NodeWrapperFactory.h>
 #include <LibWeb/Bindings/TextWrapper.h>
@@ -1217,6 +1218,7 @@ void generate_prototype_implementation(const IDL::Interface& interface)
 #include <LibWeb/Bindings/HTMLFormElementWrapper.h>
 #include <LibWeb/Bindings/HTMLHeadElementWrapper.h>
 #include <LibWeb/Bindings/HTMLImageElementWrapper.h>
+#include <LibWeb/Bindings/HTMLTableCaptionElementWrapper.h>
 #include <LibWeb/Bindings/ImageDataWrapper.h>
 #include <LibWeb/Bindings/NodeWrapperFactory.h>
 #include <LibWeb/Bindings/PerformanceTimingWrapper.h>

+ 34 - 0
Userland/Libraries/LibWeb/HTML/HTMLTableElement.cpp

@@ -44,6 +44,40 @@ void HTMLTableElement::apply_presentational_hints(CSS::StyleProperties& style) c
     });
 }
 
+RefPtr<HTMLTableCaptionElement> HTMLTableElement::caption()
+{
+    return first_child_of_type<HTMLTableCaptionElement>();
+}
+
+void HTMLTableElement::set_caption(HTMLTableCaptionElement& caption)
+{
+    // FIXME: The spec requires deleting the current caption if caption is null
+    //        Currently the wrapper generator doesn't send us a nullable value
+    delete_caption();
+
+    pre_insert(caption, first_child());
+}
+
+NonnullRefPtr<HTMLTableCaptionElement> HTMLTableElement::create_caption()
+{
+    auto maybe_caption = caption();
+    if (maybe_caption) {
+        return *maybe_caption;
+    }
+
+    auto caption = DOM::create_element(document(), TagNames::caption, Namespace::HTML);
+    pre_insert(caption, first_child());
+    return caption;
+}
+
+void HTMLTableElement::delete_caption()
+{
+    auto maybe_caption = caption();
+    if (maybe_caption) {
+        maybe_caption->remove(false);
+    }
+}
+
 NonnullRefPtr<DOM::HTMLCollection> HTMLTableElement::rows()
 {
     HTMLTableElement* table_node = this;

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

@@ -8,6 +8,7 @@
 
 #include <LibWeb/DOM/ExceptionOr.h>
 #include <LibWeb/HTML/HTMLElement.h>
+#include <LibWeb/HTML/HTMLTableCaptionElement.h>
 #include <LibWeb/HTML/HTMLTableRowElement.h>
 
 namespace Web::HTML {
@@ -19,6 +20,11 @@ public:
     HTMLTableElement(DOM::Document&, QualifiedName);
     virtual ~HTMLTableElement() override;
 
+    RefPtr<HTMLTableCaptionElement> caption();
+    void set_caption(HTMLTableCaptionElement&);
+    NonnullRefPtr<HTMLTableCaptionElement> create_caption();
+    void delete_caption();
+
     NonnullRefPtr<DOM::HTMLCollection> rows();
     DOM::ExceptionOr<NonnullRefPtr<HTMLTableRowElement>> insert_row(long index);
     DOM::ExceptionOr<void> delete_row(long index);

+ 4 - 0
Userland/Libraries/LibWeb/HTML/HTMLTableElement.idl

@@ -1,5 +1,9 @@
 interface HTMLTableElement : HTMLElement {
 
+    attribute HTMLTableCaptionElement? caption;
+    HTMLTableCaptionElement createCaption();
+    undefined deleteCaption();
+
     readonly attribute HTMLCollection rows;
     HTMLTableRowElement insertRow(optional long index = -1);
     undefined deleteRow(long index);