HTMLTableSectionElement.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Bindings/HTMLTableSectionElementPrototype.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/CSS/StyleProperties.h>
  10. #include <LibWeb/CSS/StyleValues/CSSColorValue.h>
  11. #include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
  12. #include <LibWeb/DOM/Document.h>
  13. #include <LibWeb/DOM/ElementFactory.h>
  14. #include <LibWeb/DOM/HTMLCollection.h>
  15. #include <LibWeb/HTML/HTMLTableRowElement.h>
  16. #include <LibWeb/HTML/HTMLTableSectionElement.h>
  17. #include <LibWeb/HTML/Parser/HTMLParser.h>
  18. #include <LibWeb/Namespace.h>
  19. namespace Web::HTML {
  20. GC_DEFINE_ALLOCATOR(HTMLTableSectionElement);
  21. HTMLTableSectionElement::HTMLTableSectionElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  22. : HTMLElement(document, move(qualified_name))
  23. {
  24. }
  25. HTMLTableSectionElement::~HTMLTableSectionElement() = default;
  26. void HTMLTableSectionElement::initialize(JS::Realm& realm)
  27. {
  28. Base::initialize(realm);
  29. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLTableSectionElement);
  30. }
  31. void HTMLTableSectionElement::visit_edges(Cell::Visitor& visitor)
  32. {
  33. Base::visit_edges(visitor);
  34. visitor.visit(m_rows);
  35. }
  36. // https://html.spec.whatwg.org/multipage/tables.html#dom-tbody-rows
  37. GC::Ref<DOM::HTMLCollection> HTMLTableSectionElement::rows() const
  38. {
  39. // The rows attribute must return an HTMLCollection rooted at this element,
  40. // whose filter matches only tr elements that are children of this element.
  41. if (!m_rows) {
  42. m_rows = DOM::HTMLCollection::create(const_cast<HTMLTableSectionElement&>(*this), DOM::HTMLCollection::Scope::Children, [](Element const& element) {
  43. return is<HTMLTableRowElement>(element);
  44. });
  45. }
  46. return *m_rows;
  47. }
  48. // https://html.spec.whatwg.org/multipage/tables.html#dom-tbody-insertrow
  49. WebIDL::ExceptionOr<GC::Ref<HTMLTableRowElement>> HTMLTableSectionElement::insert_row(WebIDL::Long index)
  50. {
  51. auto rows_collection = rows();
  52. auto rows_collection_size = static_cast<long>(rows_collection->length());
  53. // 1. If index is less than −1 or greater than the number of elements in the rows collection, throw an "IndexSizeError" DOMException.
  54. if (index < -1 || index > rows_collection_size)
  55. return WebIDL::IndexSizeError::create(realm(), "Index is negative or greater than the number of rows"_string);
  56. // 2. Let table row be the result of creating an element given this element's node document, tr, and the HTML namespace.
  57. auto& table_row = static_cast<HTMLTableRowElement&>(*TRY(DOM::create_element(document(), TagNames::tr, Namespace::HTML)));
  58. // 3. If index is −1 or equal to the number of items in the rows collection, then append table row to this element.
  59. if (index == -1 || index == rows_collection_size)
  60. TRY(append_child(table_row));
  61. // 4. Otherwise, insert table row as a child of this element, immediately before the index-th tr element in the rows collection.
  62. else
  63. insert_before(table_row, rows_collection->item(index));
  64. // 5. Return table row.
  65. return GC::Ref(table_row);
  66. }
  67. // https://html.spec.whatwg.org/multipage/tables.html#dom-tbody-deleterow
  68. WebIDL::ExceptionOr<void> HTMLTableSectionElement::delete_row(WebIDL::Long index)
  69. {
  70. auto rows_collection = rows();
  71. auto rows_collection_size = static_cast<long>(rows_collection->length());
  72. // 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.
  73. if (index < -1 || index >= rows_collection_size)
  74. return WebIDL::IndexSizeError::create(realm(), "Index is negative or greater than or equal to the number of rows"_string);
  75. // 2. If index is −1, then remove the last element in the rows collection from this element, or do nothing if the rows collection is empty.
  76. if (index == -1) {
  77. if (rows_collection_size > 0)
  78. rows_collection->item(rows_collection_size - 1)->remove();
  79. }
  80. // 3. Otherwise, remove the indexth element in the rows collection from this element.
  81. else {
  82. rows_collection->item(index)->remove();
  83. }
  84. return {};
  85. }
  86. void HTMLTableSectionElement::apply_presentational_hints(CSS::StyleProperties& style) const
  87. {
  88. for_each_attribute([&](auto& name, auto& value) {
  89. // https://html.spec.whatwg.org/multipage/rendering.html#tables-2:encoding-parsing-and-serializing-a-url
  90. if (name == HTML::AttributeNames::background) {
  91. if (auto parsed_value = document().encoding_parse_url(value); parsed_value.is_valid())
  92. style.set_property(CSS::PropertyID::BackgroundImage, CSS::ImageStyleValue::create(parsed_value));
  93. }
  94. // https://html.spec.whatwg.org/multipage/rendering.html#tables-2:rules-for-parsing-a-legacy-colour-value
  95. else if (name == HTML::AttributeNames::bgcolor) {
  96. if (auto color = parse_legacy_color_value(value); color.has_value())
  97. style.set_property(CSS::PropertyID::BackgroundColor, CSS::CSSColorValue::create_from_color(color.value()));
  98. }
  99. });
  100. }
  101. }