mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-05-02 01:08:48 +00:00
LibWeb: Implement HTMLTableRowElement.insertCell
This commit is contained in:
parent
f984c70b20
commit
6c9b3fb62e
Notes:
sideshowbarker
2024-07-17 04:42:00 +09:00
Author: https://github.com/Lubrsi
Commit: 6c9b3fb62e
Pull-request: https://github.com/SerenityOS/serenity/pull/15964
Reviewed-by: https://github.com/awesomekling
2 changed files with 29 additions and 1 deletions
|
@ -5,11 +5,13 @@
|
|||
*/
|
||||
|
||||
#include <LibWeb/Bindings/Intrinsics.h>
|
||||
#include <LibWeb/DOM/ElementFactory.h>
|
||||
#include <LibWeb/DOM/HTMLCollection.h>
|
||||
#include <LibWeb/HTML/HTMLTableCellElement.h>
|
||||
#include <LibWeb/HTML/HTMLTableElement.h>
|
||||
#include <LibWeb/HTML/HTMLTableRowElement.h>
|
||||
#include <LibWeb/HTML/HTMLTableSectionElement.h>
|
||||
#include <LibWeb/Namespace.h>
|
||||
|
||||
namespace Web::HTML {
|
||||
|
||||
|
@ -85,4 +87,29 @@ int HTMLTableRowElement::section_row_index() const
|
|||
return -1;
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/tables.html#dom-tr-insertcell
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<HTMLTableCellElement>> HTMLTableRowElement::insert_cell(i32 index)
|
||||
{
|
||||
auto cells_collection = cells();
|
||||
auto cells_collection_size = static_cast<i32>(cells_collection->length());
|
||||
|
||||
// 1. If index is less than −1 or greater than the number of elements in the cells collection, then throw an "IndexSizeError" DOMException.
|
||||
if (index < -1 || index > cells_collection_size)
|
||||
return WebIDL::IndexSizeError::create(realm(), "Index is negative or greater than the number of cells");
|
||||
|
||||
// 2. Let table cell be the result of creating an element given this tr element's node document, td, and the HTML namespace.
|
||||
auto& table_cell = static_cast<HTMLTableCellElement&>(*DOM::create_element(document(), HTML::TagNames::td, Namespace::HTML));
|
||||
|
||||
// 3. If index is equal to −1 or equal to the number of items in cells collection, then append table cell to this tr element.
|
||||
if (index == -1 || index == cells_collection_size)
|
||||
TRY(append_child(table_cell));
|
||||
|
||||
// 4. Otherwise, insert table cell as a child of this tr element, immediately before the indexth td or th element in the cells collection.
|
||||
else
|
||||
insert_before(table_cell, cells_collection->item(index));
|
||||
|
||||
// 5. Return table cell.
|
||||
return JS::NonnullGCPtr(table_cell);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue