HTMLTableElement.cpp 19 KB

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