HTMLTableElement.cpp 17 KB

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