HTMLTableElement.cpp 18 KB

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