HTMLTableElement.cpp 18 KB

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