HTMLTableElement.cpp 17 KB

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