HTMLTableElement.cpp 18 KB

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