HTMLTableElement.cpp 17 KB

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