Serenity Operating System
at master 381 lines 15 kB view raw
1/* 2 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> 3 * Copyright (c) 2021, Adam Hodgen <ant1441@gmail.com> 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#include <LibWeb/Bindings/Intrinsics.h> 9#include <LibWeb/CSS/Parser/Parser.h> 10#include <LibWeb/DOM/ElementFactory.h> 11#include <LibWeb/DOM/HTMLCollection.h> 12#include <LibWeb/HTML/HTMLTableColElement.h> 13#include <LibWeb/HTML/HTMLTableElement.h> 14#include <LibWeb/HTML/HTMLTableRowElement.h> 15#include <LibWeb/HTML/Parser/HTMLParser.h> 16#include <LibWeb/Namespace.h> 17 18namespace Web::HTML { 19 20HTMLTableElement::HTMLTableElement(DOM::Document& document, DOM::QualifiedName qualified_name) 21 : HTMLElement(document, move(qualified_name)) 22{ 23} 24 25HTMLTableElement::~HTMLTableElement() = default; 26 27JS::ThrowCompletionOr<void> HTMLTableElement::initialize(JS::Realm& realm) 28{ 29 MUST_OR_THROW_OOM(Base::initialize(realm)); 30 set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLTableElementPrototype>(realm, "HTMLTableElement")); 31 32 return {}; 33} 34 35void HTMLTableElement::visit_edges(Cell::Visitor& visitor) 36{ 37 Base::visit_edges(visitor); 38 visitor.visit(m_rows); 39 visitor.visit(m_t_bodies); 40} 41 42void HTMLTableElement::apply_presentational_hints(CSS::StyleProperties& style) const 43{ 44 for_each_attribute([&](auto& name, auto& value) { 45 if (name == HTML::AttributeNames::width) { 46 if (auto parsed_value = parse_nonzero_dimension_value(value)) 47 style.set_property(CSS::PropertyID::Width, parsed_value.release_nonnull()); 48 return; 49 } 50 if (name == HTML::AttributeNames::height) { 51 if (auto parsed_value = parse_nonzero_dimension_value(value)) 52 style.set_property(CSS::PropertyID::Height, parsed_value.release_nonnull()); 53 return; 54 } 55 if (name == HTML::AttributeNames::bgcolor) { 56 auto color = Color::from_string(value); 57 if (color.has_value()) 58 style.set_property(CSS::PropertyID::BackgroundColor, CSS::ColorStyleValue::create(color.value())); 59 return; 60 } 61 }); 62} 63 64// https://html.spec.whatwg.org/multipage/tables.html#dom-table-caption 65JS::GCPtr<HTMLTableCaptionElement> HTMLTableElement::caption() 66{ 67 // The caption IDL attribute must return, on getting, the first caption element child of the table element, 68 // if any, or null otherwise. 69 return first_child_of_type<HTMLTableCaptionElement>(); 70} 71 72// https://html.spec.whatwg.org/multipage/tables.html#dom-table-caption 73void HTMLTableElement::set_caption(HTMLTableCaptionElement* caption) 74{ 75 // On setting, the first caption element child of the table element, if any, must be removed, 76 // and the new value, if not null, must be inserted as the first node of the table element. 77 delete_caption(); 78 79 if (caption) 80 MUST(pre_insert(*caption, first_child())); 81} 82 83// https://html.spec.whatwg.org/multipage/tables.html#dom-table-createcaption 84JS::NonnullGCPtr<HTMLTableCaptionElement> HTMLTableElement::create_caption() 85{ 86 auto maybe_caption = caption(); 87 if (maybe_caption) { 88 return *maybe_caption; 89 } 90 91 auto caption = DOM::create_element(document(), TagNames::caption, Namespace::HTML).release_value_but_fixme_should_propagate_errors(); 92 MUST(pre_insert(caption, first_child())); 93 return static_cast<HTMLTableCaptionElement&>(*caption); 94} 95 96// https://html.spec.whatwg.org/multipage/tables.html#dom-table-deletecaption 97void HTMLTableElement::delete_caption() 98{ 99 auto maybe_caption = caption(); 100 if (maybe_caption) { 101 maybe_caption->remove(false); 102 } 103} 104 105// https://html.spec.whatwg.org/multipage/tables.html#dom-table-thead 106JS::GCPtr<HTMLTableSectionElement> HTMLTableElement::t_head() 107{ 108 // The tHead IDL attribute must return, on getting, the first thead element child of the table element, 109 // if any, or null otherwise. 110 for (auto* child = first_child(); child; child = child->next_sibling()) { 111 if (is<HTMLTableSectionElement>(*child)) { 112 auto table_section_element = &verify_cast<HTMLTableSectionElement>(*child); 113 if (table_section_element->local_name() == TagNames::thead) 114 return table_section_element; 115 } 116 } 117 118 return nullptr; 119} 120 121// https://html.spec.whatwg.org/multipage/tables.html#dom-table-thead 122WebIDL::ExceptionOr<void> HTMLTableElement::set_t_head(HTMLTableSectionElement* thead) 123{ 124 // If the new value is neither null nor a thead element, then a "HierarchyRequestError" DOMException must be thrown instead. 125 if (thead && thead->local_name() != TagNames::thead) 126 return WebIDL::HierarchyRequestError::create(realm(), "Element is not thead"); 127 128 // On setting, if the new value is null or a thead element, the first thead element child of the table element, 129 // if any, must be removed, 130 delete_t_head(); 131 132 if (!thead) 133 return {}; 134 135 // and the new value, if not null, must be inserted immediately before the first element in the table element 136 // that is neither a caption element nor a colgroup element, if any, 137 // or at the end of the table if there are no such elements. 138 139 // We insert the new thead after any <caption> or <colgroup> elements 140 DOM::Node* child_to_insert_before = nullptr; 141 for (auto* child = first_child(); child; child = child->next_sibling()) { 142 if (!is<HTMLElement>(*child)) 143 continue; 144 if (is<HTMLTableCaptionElement>(*child)) 145 continue; 146 if (is<HTMLTableColElement>(*child)) { 147 auto table_col_element = &verify_cast<HTMLTableColElement>(*child); 148 if (table_col_element->local_name() == TagNames::colgroup) 149 continue; 150 } 151 152 // We have found an element which is not a <caption> or <colgroup>, we'll insert before this 153 child_to_insert_before = child; 154 break; 155 } 156 157 TRY(pre_insert(*thead, child_to_insert_before)); 158 159 return {}; 160} 161 162// https://html.spec.whatwg.org/multipage/tables.html#dom-table-createthead 163JS::NonnullGCPtr<HTMLTableSectionElement> HTMLTableElement::create_t_head() 164{ 165 auto maybe_thead = t_head(); 166 if (maybe_thead) 167 return *maybe_thead; 168 169 auto thead = DOM::create_element(document(), TagNames::thead, Namespace::HTML).release_value_but_fixme_should_propagate_errors(); 170 171 // We insert the new thead after any <caption> or <colgroup> elements 172 DOM::Node* child_to_insert_before = nullptr; 173 for (auto* child = first_child(); child; child = child->next_sibling()) { 174 if (!is<HTMLElement>(*child)) 175 continue; 176 if (is<HTMLTableCaptionElement>(*child)) 177 continue; 178 if (is<HTMLTableColElement>(*child)) { 179 auto table_col_element = &verify_cast<HTMLTableColElement>(*child); 180 if (table_col_element->local_name() == TagNames::colgroup) 181 continue; 182 } 183 184 // We have found an element which is not a <caption> or <colgroup>, we'll insert before this 185 child_to_insert_before = child; 186 break; 187 } 188 189 MUST(pre_insert(thead, child_to_insert_before)); 190 191 return static_cast<HTMLTableSectionElement&>(*thead); 192} 193 194// https://html.spec.whatwg.org/multipage/tables.html#dom-table-deletethead 195void HTMLTableElement::delete_t_head() 196{ 197 auto maybe_thead = t_head(); 198 if (maybe_thead) { 199 maybe_thead->remove(false); 200 } 201} 202 203// https://html.spec.whatwg.org/multipage/tables.html#dom-table-tfoot 204JS::GCPtr<HTMLTableSectionElement> HTMLTableElement::t_foot() 205{ 206 // The tFoot IDL attribute must return, on getting, the first tfoot element child of the table element, 207 // if any, or null otherwise. 208 for (auto* child = first_child(); child; child = child->next_sibling()) { 209 if (is<HTMLTableSectionElement>(*child)) { 210 auto table_section_element = &verify_cast<HTMLTableSectionElement>(*child); 211 if (table_section_element->local_name() == TagNames::tfoot) 212 return table_section_element; 213 } 214 } 215 216 return nullptr; 217} 218 219// https://html.spec.whatwg.org/multipage/tables.html#dom-table-tfoot 220WebIDL::ExceptionOr<void> HTMLTableElement::set_t_foot(HTMLTableSectionElement* tfoot) 221{ 222 // If the new value is neither null nor a tfoot element, then a "HierarchyRequestError" DOMException must be thrown instead. 223 if (tfoot && tfoot->local_name() != TagNames::tfoot) 224 return WebIDL::HierarchyRequestError::create(realm(), "Element is not tfoot"); 225 226 // On setting, if the new value is null or a tfoot element, the first tfoot element child of the table element, 227 // if any, must be removed, 228 delete_t_foot(); 229 230 // and the new value, if not null, must be inserted at the end of the table. 231 if (tfoot) { 232 TRY(append_child(*tfoot)); 233 } 234 235 return {}; 236} 237 238// https://html.spec.whatwg.org/multipage/tables.html#dom-table-createtfoot 239JS::NonnullGCPtr<HTMLTableSectionElement> HTMLTableElement::create_t_foot() 240{ 241 auto maybe_tfoot = t_foot(); 242 if (maybe_tfoot) 243 return *maybe_tfoot; 244 245 auto tfoot = DOM::create_element(document(), TagNames::tfoot, Namespace::HTML).release_value_but_fixme_should_propagate_errors(); 246 MUST(append_child(tfoot)); 247 return static_cast<HTMLTableSectionElement&>(*tfoot); 248} 249 250// https://html.spec.whatwg.org/multipage/tables.html#dom-table-deletetfoot 251void HTMLTableElement::delete_t_foot() 252{ 253 auto maybe_tfoot = t_foot(); 254 if (maybe_tfoot) { 255 maybe_tfoot->remove(false); 256 } 257} 258 259// https://html.spec.whatwg.org/multipage/tables.html#dom-table-tbodies 260JS::NonnullGCPtr<DOM::HTMLCollection> HTMLTableElement::t_bodies() 261{ 262 // The tBodies attribute must return an HTMLCollection rooted at the table node, 263 // whose filter matches only tbody elements that are children of the table element. 264 if (!m_t_bodies) { 265 m_t_bodies = DOM::HTMLCollection::create(*this, [](DOM::Element const& element) { 266 return element.local_name() == TagNames::tbody; 267 }).release_value_but_fixme_should_propagate_errors(); 268 } 269 return *m_t_bodies; 270} 271 272// https://html.spec.whatwg.org/multipage/tables.html#dom-table-createtbody 273JS::NonnullGCPtr<HTMLTableSectionElement> HTMLTableElement::create_t_body() 274{ 275 auto tbody = DOM::create_element(document(), TagNames::tbody, Namespace::HTML).release_value_but_fixme_should_propagate_errors(); 276 277 // We insert the new tbody after the last <tbody> element 278 DOM::Node* child_to_insert_before = nullptr; 279 for (auto* child = last_child(); child; child = child->previous_sibling()) { 280 if (!is<HTMLElement>(*child)) 281 continue; 282 if (is<HTMLTableSectionElement>(*child)) { 283 auto table_section_element = &verify_cast<HTMLTableSectionElement>(*child); 284 if (table_section_element->local_name() == TagNames::tbody) { 285 // We have found an element which is a <tbody> we'll insert after this 286 child_to_insert_before = child->next_sibling(); 287 break; 288 } 289 } 290 } 291 292 MUST(pre_insert(tbody, child_to_insert_before)); 293 294 return static_cast<HTMLTableSectionElement&>(*tbody); 295} 296 297// https://html.spec.whatwg.org/multipage/tables.html#dom-table-rows 298JS::NonnullGCPtr<DOM::HTMLCollection> HTMLTableElement::rows() 299{ 300 HTMLTableElement* table_node = this; 301 // FIXME: The elements in the collection must be ordered such that those elements whose parent is a thead are 302 // included first, in tree order, followed by those elements whose parent is either a table or tbody 303 // element, again in tree order, followed finally by those elements whose parent is a tfoot element, 304 // still in tree order. 305 // How do you sort HTMLCollection? 306 307 if (!m_rows) { 308 m_rows = DOM::HTMLCollection::create(*this, [table_node](DOM::Element const& element) { 309 // Only match TR elements which are: 310 // * children of the table element 311 // * children of the thead, tbody, or tfoot elements that are themselves children of the table element 312 if (!is<HTMLTableRowElement>(element)) { 313 return false; 314 } 315 if (element.parent_element() == table_node) 316 return true; 317 318 if (element.parent_element() && (element.parent_element()->local_name() == TagNames::thead || element.parent_element()->local_name() == TagNames::tbody || element.parent_element()->local_name() == TagNames::tfoot) 319 && element.parent()->parent() == table_node) { 320 return true; 321 } 322 323 return false; 324 }).release_value_but_fixme_should_propagate_errors(); 325 } 326 return *m_rows; 327} 328 329// https://html.spec.whatwg.org/multipage/tables.html#dom-table-insertrow 330WebIDL::ExceptionOr<JS::NonnullGCPtr<HTMLTableRowElement>> HTMLTableElement::insert_row(long index) 331{ 332 auto rows = this->rows(); 333 auto rows_length = rows->length(); 334 335 if (index < -1 || index > (long)rows_length) { 336 return WebIDL::IndexSizeError::create(realm(), "Index is negative or greater than the number of rows"); 337 } 338 auto& tr = static_cast<HTMLTableRowElement&>(*TRY(DOM::create_element(document(), TagNames::tr, Namespace::HTML))); 339 if (rows_length == 0 && !has_child_of_type<HTMLTableRowElement>()) { 340 auto tbody = TRY(DOM::create_element(document(), TagNames::tbody, Namespace::HTML)); 341 TRY(tbody->append_child(tr)); 342 TRY(append_child(tbody)); 343 } else if (rows_length == 0) { 344 auto tbody = last_child_of_type<HTMLTableRowElement>(); 345 TRY(tbody->append_child(tr)); 346 } else if (index == -1 || index == (long)rows_length) { 347 auto parent_of_last_tr = rows->item(rows_length - 1)->parent_element(); 348 TRY(parent_of_last_tr->append_child(tr)); 349 } else { 350 rows->item(index)->parent_element()->insert_before(tr, rows->item(index)); 351 } 352 return JS::NonnullGCPtr(tr); 353} 354 355// https://html.spec.whatwg.org/multipage/tables.html#dom-table-deleterow 356WebIDL::ExceptionOr<void> HTMLTableElement::delete_row(long index) 357{ 358 auto rows = this->rows(); 359 auto rows_length = rows->length(); 360 361 // 1. If index is less than −1 or greater than or equal to the number of elements in the rows collection, then throw an "IndexSizeError" DOMException. 362 if (index < -1 || index >= (long)rows_length) 363 return WebIDL::IndexSizeError::create(realm(), "Index is negative or greater than or equal to the number of rows"); 364 365 // 2. If index is −1, then remove the last element in the rows collection from its parent, or do nothing if the rows collection is empty. 366 if (index == -1) { 367 if (rows_length == 0) 368 return {}; 369 370 auto row_to_remove = rows->item(rows_length - 1); 371 row_to_remove->remove(false); 372 return {}; 373 } 374 375 // 3. Otherwise, remove the indexth element in the rows collection from its parent. 376 auto row_to_remove = rows->item(index); 377 row_to_remove->remove(false); 378 return {}; 379} 380 381}