HTMLElement.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringBuilder.h>
  7. #include <LibWeb/ARIA/Roles.h>
  8. #include <LibWeb/Bindings/ExceptionOrUtils.h>
  9. #include <LibWeb/Bindings/HTMLElementPrototype.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/DOM/IDLEventListener.h>
  12. #include <LibWeb/DOM/LiveNodeList.h>
  13. #include <LibWeb/DOM/ShadowRoot.h>
  14. #include <LibWeb/HTML/BrowsingContext.h>
  15. #include <LibWeb/HTML/CustomElements/CustomElementDefinition.h>
  16. #include <LibWeb/HTML/DOMStringMap.h>
  17. #include <LibWeb/HTML/ElementInternals.h>
  18. #include <LibWeb/HTML/EventHandler.h>
  19. #include <LibWeb/HTML/Focus.h>
  20. #include <LibWeb/HTML/HTMLAnchorElement.h>
  21. #include <LibWeb/HTML/HTMLAreaElement.h>
  22. #include <LibWeb/HTML/HTMLBaseElement.h>
  23. #include <LibWeb/HTML/HTMLBodyElement.h>
  24. #include <LibWeb/HTML/HTMLElement.h>
  25. #include <LibWeb/HTML/HTMLLabelElement.h>
  26. #include <LibWeb/HTML/NavigableContainer.h>
  27. #include <LibWeb/HTML/VisibilityState.h>
  28. #include <LibWeb/HTML/Window.h>
  29. #include <LibWeb/Infra/CharacterTypes.h>
  30. #include <LibWeb/Infra/Strings.h>
  31. #include <LibWeb/Layout/Box.h>
  32. #include <LibWeb/Layout/BreakNode.h>
  33. #include <LibWeb/Layout/TextNode.h>
  34. #include <LibWeb/Painting/PaintableBox.h>
  35. #include <LibWeb/UIEvents/EventNames.h>
  36. #include <LibWeb/UIEvents/FocusEvent.h>
  37. #include <LibWeb/UIEvents/MouseEvent.h>
  38. #include <LibWeb/UIEvents/PointerEvent.h>
  39. #include <LibWeb/WebIDL/DOMException.h>
  40. #include <LibWeb/WebIDL/ExceptionOr.h>
  41. namespace Web::HTML {
  42. JS_DEFINE_ALLOCATOR(HTMLElement);
  43. HTMLElement::HTMLElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  44. : Element(document, move(qualified_name))
  45. {
  46. }
  47. HTMLElement::~HTMLElement() = default;
  48. void HTMLElement::initialize(JS::Realm& realm)
  49. {
  50. Base::initialize(realm);
  51. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLElement);
  52. }
  53. void HTMLElement::visit_edges(Cell::Visitor& visitor)
  54. {
  55. Base::visit_edges(visitor);
  56. visitor.visit(m_dataset);
  57. visitor.visit(m_labels);
  58. visitor.visit(m_attached_internals);
  59. }
  60. JS::NonnullGCPtr<DOMStringMap> HTMLElement::dataset()
  61. {
  62. if (!m_dataset)
  63. m_dataset = DOMStringMap::create(*this);
  64. return *m_dataset;
  65. }
  66. // https://html.spec.whatwg.org/multipage/dom.html#dom-dir
  67. StringView HTMLElement::dir() const
  68. {
  69. // FIXME: This should probably be `Reflect` in the IDL.
  70. // The dir IDL attribute on an element must reflect the dir content attribute of that element, limited to only known values.
  71. auto dir = get_attribute_value(HTML::AttributeNames::dir);
  72. #define __ENUMERATE_HTML_ELEMENT_DIR_ATTRIBUTE(keyword) \
  73. if (dir.equals_ignoring_ascii_case(#keyword##sv)) \
  74. return #keyword##sv;
  75. ENUMERATE_HTML_ELEMENT_DIR_ATTRIBUTES
  76. #undef __ENUMERATE_HTML_ELEMENT_DIR_ATTRIBUTE
  77. return {};
  78. }
  79. void HTMLElement::set_dir(String const& dir)
  80. {
  81. MUST(set_attribute(HTML::AttributeNames::dir, dir));
  82. }
  83. bool HTMLElement::is_editable() const
  84. {
  85. switch (m_content_editable_state) {
  86. case ContentEditableState::True:
  87. return true;
  88. case ContentEditableState::False:
  89. return false;
  90. case ContentEditableState::Inherit:
  91. return parent() && parent()->is_editable();
  92. default:
  93. VERIFY_NOT_REACHED();
  94. }
  95. }
  96. bool HTMLElement::is_focusable() const
  97. {
  98. return m_content_editable_state == ContentEditableState::True;
  99. }
  100. // https://html.spec.whatwg.org/multipage/interaction.html#dom-iscontenteditable
  101. bool HTMLElement::is_content_editable() const
  102. {
  103. // The isContentEditable IDL attribute, on getting, must return true if the element is either an editing host or
  104. // editable, and false otherwise.
  105. return is_editable();
  106. }
  107. StringView HTMLElement::content_editable() const
  108. {
  109. switch (m_content_editable_state) {
  110. case ContentEditableState::True:
  111. return "true"sv;
  112. case ContentEditableState::False:
  113. return "false"sv;
  114. case ContentEditableState::Inherit:
  115. return "inherit"sv;
  116. }
  117. VERIFY_NOT_REACHED();
  118. }
  119. // https://html.spec.whatwg.org/multipage/interaction.html#contenteditable
  120. WebIDL::ExceptionOr<void> HTMLElement::set_content_editable(StringView content_editable)
  121. {
  122. if (content_editable.equals_ignoring_ascii_case("inherit"sv)) {
  123. remove_attribute(HTML::AttributeNames::contenteditable);
  124. return {};
  125. }
  126. if (content_editable.equals_ignoring_ascii_case("true"sv)) {
  127. MUST(set_attribute(HTML::AttributeNames::contenteditable, "true"_string));
  128. return {};
  129. }
  130. if (content_editable.equals_ignoring_ascii_case("false"sv)) {
  131. MUST(set_attribute(HTML::AttributeNames::contenteditable, "false"_string));
  132. return {};
  133. }
  134. return WebIDL::SyntaxError::create(realm(), "Invalid contentEditable value, must be 'true', 'false', or 'inherit'"_fly_string);
  135. }
  136. void HTMLElement::set_inner_text(StringView text)
  137. {
  138. remove_all_children();
  139. MUST(append_child(document().create_text_node(MUST(String::from_utf8(text)))));
  140. set_needs_style_update(true);
  141. }
  142. // https://html.spec.whatwg.org/multipage/dom.html#the-innertext-idl-attribute:dom-outertext-2
  143. WebIDL::ExceptionOr<void> HTMLElement::set_outer_text(String)
  144. {
  145. dbgln("FIXME: Implement HTMLElement::set_outer_text()");
  146. return {};
  147. }
  148. // https://html.spec.whatwg.org/multipage/dom.html#get-the-text-steps
  149. String HTMLElement::get_the_text_steps()
  150. {
  151. // FIXME: Implement this according to spec.
  152. StringBuilder builder;
  153. // innerText for element being rendered takes visibility into account, so force a layout and then walk the layout tree.
  154. document().update_layout();
  155. if (!layout_node())
  156. return text_content().value_or(String {});
  157. Function<void(Layout::Node const&)> recurse = [&](auto& node) {
  158. for (auto* child = node.first_child(); child; child = child->next_sibling()) {
  159. if (is<Layout::TextNode>(child))
  160. builder.append(verify_cast<Layout::TextNode>(*child).text_for_rendering());
  161. if (is<Layout::BreakNode>(child))
  162. builder.append('\n');
  163. recurse(*child);
  164. }
  165. };
  166. recurse(*layout_node());
  167. return MUST(builder.to_string());
  168. }
  169. // https://html.spec.whatwg.org/multipage/dom.html#dom-innertext
  170. String HTMLElement::inner_text()
  171. {
  172. // The innerText and outerText getter steps are to return the result of running get the text steps with this.
  173. return get_the_text_steps();
  174. }
  175. // https://html.spec.whatwg.org/multipage/dom.html#dom-outertext
  176. String HTMLElement::outer_text()
  177. {
  178. // The innerText and outerText getter steps are to return the result of running get the text steps with this.
  179. return get_the_text_steps();
  180. }
  181. // https://www.w3.org/TR/cssom-view-1/#dom-htmlelement-offsetparent
  182. JS::GCPtr<DOM::Element> HTMLElement::offset_parent() const
  183. {
  184. const_cast<DOM::Document&>(document()).update_layout();
  185. // 1. If any of the following holds true return null and terminate this algorithm:
  186. // - The element does not have an associated CSS layout box.
  187. // - The element is the root element.
  188. // - The element is the HTML body element.
  189. // - The element’s computed value of the position property is fixed.
  190. if (!layout_node())
  191. return nullptr;
  192. if (is_document_element())
  193. return nullptr;
  194. if (is<HTML::HTMLBodyElement>(*this))
  195. return nullptr;
  196. if (layout_node()->is_fixed_position())
  197. return nullptr;
  198. // 2. Return the nearest ancestor element of the element for which at least one of the following is true
  199. // and terminate this algorithm if such an ancestor is found:
  200. // - The computed value of the position property is not static.
  201. // - It is the HTML body element.
  202. // - The computed value of the position property of the element is static
  203. // and the ancestor is one of the following HTML elements: td, th, or table.
  204. for (auto* ancestor = parent_element(); ancestor; ancestor = ancestor->parent_element()) {
  205. if (!ancestor->layout_node())
  206. continue;
  207. if (ancestor->layout_node()->is_positioned())
  208. return const_cast<Element*>(ancestor);
  209. if (is<HTML::HTMLBodyElement>(*ancestor))
  210. return const_cast<Element*>(ancestor);
  211. if (!ancestor->layout_node()->is_positioned() && ancestor->local_name().is_one_of(HTML::TagNames::td, HTML::TagNames::th, HTML::TagNames::table))
  212. return const_cast<Element*>(ancestor);
  213. }
  214. // 3. Return null.
  215. return nullptr;
  216. }
  217. // https://www.w3.org/TR/cssom-view-1/#dom-htmlelement-offsettop
  218. int HTMLElement::offset_top() const
  219. {
  220. // 1. If the element is the HTML body element or does not have any associated CSS layout box
  221. // return zero and terminate this algorithm.
  222. if (is<HTML::HTMLBodyElement>(*this))
  223. return 0;
  224. // NOTE: Ensure that layout is up-to-date before looking at metrics.
  225. const_cast<DOM::Document&>(document()).update_layout();
  226. if (!layout_node())
  227. return 0;
  228. // 2. If the offsetParent of the element is null
  229. // return the y-coordinate of the top border edge of the first CSS layout box associated with the element,
  230. // relative to the initial containing block origin,
  231. // ignoring any transforms that apply to the element and its ancestors, and terminate this algorithm.
  232. auto offset_parent = this->offset_parent();
  233. if (!offset_parent || !offset_parent->layout_node()) {
  234. auto position = paintable()->box_type_agnostic_position();
  235. return position.y().to_int();
  236. }
  237. // 3. Return the result of subtracting the y-coordinate of the top padding edge
  238. // of the first box associated with the offsetParent of the element
  239. // from the y-coordinate of the top border edge of the first box associated with the element,
  240. // relative to the initial containing block origin,
  241. // ignoring any transforms that apply to the element and its ancestors.
  242. auto offset_parent_position = offset_parent->paintable()->box_type_agnostic_position();
  243. auto position = paintable()->box_type_agnostic_position();
  244. return position.y().to_int() - offset_parent_position.y().to_int();
  245. }
  246. // https://www.w3.org/TR/cssom-view-1/#dom-htmlelement-offsetleft
  247. int HTMLElement::offset_left() const
  248. {
  249. // 1. If the element is the HTML body element or does not have any associated CSS layout box return zero and terminate this algorithm.
  250. if (is<HTML::HTMLBodyElement>(*this))
  251. return 0;
  252. // NOTE: Ensure that layout is up-to-date before looking at metrics.
  253. const_cast<DOM::Document&>(document()).update_layout();
  254. if (!layout_node())
  255. return 0;
  256. // 2. If the offsetParent of the element is null
  257. // return the x-coordinate of the left border edge of the first CSS layout box associated with the element,
  258. // relative to the initial containing block origin,
  259. // ignoring any transforms that apply to the element and its ancestors, and terminate this algorithm.
  260. auto offset_parent = this->offset_parent();
  261. if (!offset_parent || !offset_parent->layout_node()) {
  262. auto position = paintable()->box_type_agnostic_position();
  263. return position.x().to_int();
  264. }
  265. // 3. Return the result of subtracting the x-coordinate of the left padding edge
  266. // of the first CSS layout box associated with the offsetParent of the element
  267. // from the x-coordinate of the left border edge of the first CSS layout box associated with the element,
  268. // relative to the initial containing block origin,
  269. // ignoring any transforms that apply to the element and its ancestors.
  270. auto offset_parent_position = offset_parent->paintable()->box_type_agnostic_position();
  271. auto position = paintable()->box_type_agnostic_position();
  272. return position.x().to_int() - offset_parent_position.x().to_int();
  273. }
  274. // https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsetwidth
  275. int HTMLElement::offset_width() const
  276. {
  277. // NOTE: Ensure that layout is up-to-date before looking at metrics.
  278. const_cast<DOM::Document&>(document()).update_layout();
  279. // 1. If the element does not have any associated CSS layout box return zero and terminate this algorithm.
  280. if (!paintable_box())
  281. return 0;
  282. // 2. Return the width of the axis-aligned bounding box of the border boxes of all fragments generated by the element’s principal box,
  283. // ignoring any transforms that apply to the element and its ancestors.
  284. // FIXME: Account for inline boxes.
  285. return paintable_box()->border_box_width().to_int();
  286. }
  287. // https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsetheight
  288. int HTMLElement::offset_height() const
  289. {
  290. // NOTE: Ensure that layout is up-to-date before looking at metrics.
  291. const_cast<DOM::Document&>(document()).update_layout();
  292. // 1. If the element does not have any associated CSS layout box return zero and terminate this algorithm.
  293. if (!paintable_box())
  294. return 0;
  295. // 2. Return the height of the axis-aligned bounding box of the border boxes of all fragments generated by the element’s principal box,
  296. // ignoring any transforms that apply to the element and its ancestors.
  297. // FIXME: Account for inline boxes.
  298. return paintable_box()->border_box_height().to_int();
  299. }
  300. // https://html.spec.whatwg.org/multipage/links.html#cannot-navigate
  301. bool HTMLElement::cannot_navigate() const
  302. {
  303. // An element element cannot navigate if one of the following is true:
  304. // - element's node document is not fully active
  305. if (!document().is_fully_active())
  306. return true;
  307. // - element is not an a element and is not connected.
  308. return !is<HTML::HTMLAnchorElement>(this) && !is_connected();
  309. }
  310. void HTMLElement::attribute_changed(FlyString const& name, Optional<String> const& value)
  311. {
  312. Element::attribute_changed(name, value);
  313. if (name == HTML::AttributeNames::contenteditable) {
  314. if (!value.has_value()) {
  315. m_content_editable_state = ContentEditableState::Inherit;
  316. } else {
  317. if (value->is_empty() || value->equals_ignoring_ascii_case("true"sv)) {
  318. // "true", an empty string or a missing value map to the "true" state.
  319. m_content_editable_state = ContentEditableState::True;
  320. } else if (value->equals_ignoring_ascii_case("false"sv)) {
  321. // "false" maps to the "false" state.
  322. m_content_editable_state = ContentEditableState::False;
  323. } else {
  324. // Having no such attribute or an invalid value maps to the "inherit" state.
  325. m_content_editable_state = ContentEditableState::Inherit;
  326. }
  327. }
  328. }
  329. // 1. If namespace is not null, or localName is not the name of an event handler content attribute on element, then return.
  330. // FIXME: Add the namespace part once we support attribute namespaces.
  331. #undef __ENUMERATE
  332. #define __ENUMERATE(attribute_name, event_name) \
  333. if (name == HTML::AttributeNames::attribute_name) { \
  334. element_event_handler_attribute_changed(event_name, value); \
  335. }
  336. ENUMERATE_GLOBAL_EVENT_HANDLERS(__ENUMERATE)
  337. #undef __ENUMERATE
  338. }
  339. // https://html.spec.whatwg.org/multipage/interaction.html#dom-focus
  340. void HTMLElement::focus()
  341. {
  342. // 1. If the element is marked as locked for focus, then return.
  343. if (m_locked_for_focus)
  344. return;
  345. // 2. Mark the element as locked for focus.
  346. m_locked_for_focus = true;
  347. // 3. Run the focusing steps for the element.
  348. run_focusing_steps(this);
  349. // FIXME: 4. If the value of the preventScroll dictionary member of options is false,
  350. // then scroll the element into view with scroll behavior "auto",
  351. // block flow direction position set to an implementation-defined value,
  352. // and inline base direction position set to an implementation-defined value.
  353. // 5. Unmark the element as locked for focus.
  354. m_locked_for_focus = false;
  355. }
  356. // https://html.spec.whatwg.org/multipage/webappapis.html#fire-a-synthetic-pointer-event
  357. bool HTMLElement::fire_a_synthetic_pointer_event(FlyString const& type, DOM::Element& target, bool not_trusted)
  358. {
  359. // 1. Let event be the result of creating an event using PointerEvent.
  360. // 2. Initialize event's type attribute to e.
  361. auto event = UIEvents::PointerEvent::create(realm(), type);
  362. // 3. Initialize event's bubbles and cancelable attributes to true.
  363. event->set_bubbles(true);
  364. event->set_cancelable(true);
  365. // 4. Set event's composed flag.
  366. event->set_composed(true);
  367. // 5. If the not trusted flag is set, initialize event's isTrusted attribute to false.
  368. if (not_trusted) {
  369. event->set_is_trusted(false);
  370. }
  371. // FIXME: 6. Initialize event's ctrlKey, shiftKey, altKey, and metaKey attributes according to the current state
  372. // of the key input device, if any (false for any keys that are not available).
  373. // FIXME: 7. Initialize event's view attribute to target's node document's Window object, if any, and null otherwise.
  374. // FIXME: 8. event's getModifierState() method is to return values appropriately describing the current state of the key input device.
  375. // 9. Return the result of dispatching event at target.
  376. return target.dispatch_event(event);
  377. }
  378. // https://html.spec.whatwg.org/multipage/forms.html#dom-lfe-labels-dev
  379. JS::GCPtr<DOM::NodeList> HTMLElement::labels()
  380. {
  381. // Labelable elements and all input elements have a live NodeList object associated with them that represents the list of label elements, in tree order,
  382. // whose labeled control is the element in question. The labels IDL attribute of labelable elements that are not form-associated custom elements,
  383. // and the labels IDL attribute of input elements, on getting, must return that NodeList object, and that same value must always be returned,
  384. // unless this element is an input element whose type attribute is in the Hidden state, in which case it must instead return null.
  385. if (!is_labelable())
  386. return {};
  387. if (!m_labels) {
  388. m_labels = DOM::LiveNodeList::create(realm(), root(), DOM::LiveNodeList::Scope::Descendants, [&](auto& node) {
  389. return is<HTMLLabelElement>(node) && verify_cast<HTMLLabelElement>(node).control() == this;
  390. });
  391. }
  392. return m_labels;
  393. }
  394. // https://html.spec.whatwg.org/multipage/interaction.html#dom-click
  395. void HTMLElement::click()
  396. {
  397. // FIXME: 1. If this element is a form control that is disabled, then return.
  398. // 2. If this element's click in progress flag is set, then return.
  399. if (m_click_in_progress)
  400. return;
  401. // 3. Set this element's click in progress flag.
  402. m_click_in_progress = true;
  403. // 4. Fire a synthetic pointer event named click at this element, with the not trusted flag set.
  404. fire_a_synthetic_pointer_event(HTML::EventNames::click, *this, true);
  405. // 5. Unset this element's click in progress flag.
  406. m_click_in_progress = false;
  407. }
  408. // https://html.spec.whatwg.org/multipage/interaction.html#dom-blur
  409. void HTMLElement::blur()
  410. {
  411. // The blur() method, when invoked, should run the unfocusing steps for the element on which the method was called.
  412. run_unfocusing_steps(this);
  413. // User agents may selectively or uniformly ignore calls to this method for usability reasons.
  414. }
  415. Optional<ARIA::Role> HTMLElement::default_role() const
  416. {
  417. // https://www.w3.org/TR/html-aria/#el-article
  418. if (local_name() == TagNames::article)
  419. return ARIA::Role::article;
  420. // https://www.w3.org/TR/html-aria/#el-aside
  421. if (local_name() == TagNames::aside)
  422. return ARIA::Role::complementary;
  423. // https://www.w3.org/TR/html-aria/#el-b
  424. if (local_name() == TagNames::b)
  425. return ARIA::Role::generic;
  426. // https://www.w3.org/TR/html-aria/#el-bdi
  427. if (local_name() == TagNames::bdi)
  428. return ARIA::Role::generic;
  429. // https://www.w3.org/TR/html-aria/#el-bdo
  430. if (local_name() == TagNames::bdo)
  431. return ARIA::Role::generic;
  432. // https://www.w3.org/TR/html-aria/#el-code
  433. if (local_name() == TagNames::code)
  434. return ARIA::Role::code;
  435. // https://www.w3.org/TR/html-aria/#el-dfn
  436. if (local_name() == TagNames::dfn)
  437. return ARIA::Role::term;
  438. // https://www.w3.org/TR/html-aria/#el-em
  439. if (local_name() == TagNames::em)
  440. return ARIA::Role::emphasis;
  441. // https://www.w3.org/TR/html-aria/#el-figure
  442. if (local_name() == TagNames::figure)
  443. return ARIA::Role::figure;
  444. // https://www.w3.org/TR/html-aria/#el-footer
  445. if (local_name() == TagNames::footer) {
  446. // TODO: If not a descendant of an article, aside, main, nav or section element, or an element with role=article, complementary, main, navigation or region then role=contentinfo
  447. // Otherwise, role=generic
  448. return ARIA::Role::generic;
  449. }
  450. // https://www.w3.org/TR/html-aria/#el-header
  451. if (local_name() == TagNames::header) {
  452. // TODO: If not a descendant of an article, aside, main, nav or section element, or an element with role=article, complementary, main, navigation or region then role=banner
  453. // Otherwise, role=generic
  454. return ARIA::Role::generic;
  455. }
  456. // https://www.w3.org/TR/html-aria/#el-hgroup
  457. if (local_name() == TagNames::hgroup)
  458. return ARIA::Role::generic;
  459. // https://www.w3.org/TR/html-aria/#el-i
  460. if (local_name() == TagNames::i)
  461. return ARIA::Role::generic;
  462. // https://www.w3.org/TR/html-aria/#el-main
  463. if (local_name() == TagNames::main)
  464. return ARIA::Role::main;
  465. // https://www.w3.org/TR/html-aria/#el-nav
  466. if (local_name() == TagNames::nav)
  467. return ARIA::Role::navigation;
  468. // https://www.w3.org/TR/html-aria/#el-samp
  469. if (local_name() == TagNames::samp)
  470. return ARIA::Role::generic;
  471. // https://www.w3.org/TR/html-aria/#el-section
  472. if (local_name() == TagNames::section) {
  473. // TODO: role=region if the section element has an accessible name
  474. // Otherwise, no corresponding role
  475. return ARIA::Role::region;
  476. }
  477. // https://www.w3.org/TR/html-aria/#el-small
  478. if (local_name() == TagNames::small)
  479. return ARIA::Role::generic;
  480. // https://www.w3.org/TR/html-aria/#el-strong
  481. if (local_name() == TagNames::strong)
  482. return ARIA::Role::strong;
  483. // https://www.w3.org/TR/html-aria/#el-sub
  484. if (local_name() == TagNames::sub)
  485. return ARIA::Role::subscript;
  486. // https://www.w3.org/TR/html-aria/#el-summary
  487. if (local_name() == TagNames::summary)
  488. return ARIA::Role::button;
  489. // https://www.w3.org/TR/html-aria/#el-sup
  490. if (local_name() == TagNames::sup)
  491. return ARIA::Role::superscript;
  492. // https://www.w3.org/TR/html-aria/#el-u
  493. if (local_name() == TagNames::u)
  494. return ARIA::Role::generic;
  495. return {};
  496. }
  497. // https://html.spec.whatwg.org/multipage/semantics.html#get-an-element's-target
  498. String HTMLElement::get_an_elements_target() const
  499. {
  500. // To get an element's target, given an a, area, or form element element, run these steps:
  501. // 1. If element has a target attribute, then return that attribute's value.
  502. auto maybe_target = attribute(AttributeNames::target);
  503. if (maybe_target.has_value())
  504. return maybe_target.release_value();
  505. // FIXME: 2. If element's node document contains a base element with a
  506. // target attribute, then return the value of the target attribute of the
  507. // first such base element.
  508. // 3. Return the empty string.
  509. return String {};
  510. }
  511. // https://html.spec.whatwg.org/multipage/links.html#get-an-element's-noopener
  512. TokenizedFeature::NoOpener HTMLElement::get_an_elements_noopener(StringView target) const
  513. {
  514. // To get an element's noopener, given an a, area, or form element element and a string target:
  515. auto rel = MUST(get_attribute_value(HTML::AttributeNames::rel).to_lowercase());
  516. auto link_types = rel.bytes_as_string_view().split_view_if(Infra::is_ascii_whitespace);
  517. // 1. If element's link types include the noopener or noreferrer keyword, then return true.
  518. if (link_types.contains_slow("noopener"sv) || link_types.contains_slow("noreferrer"sv))
  519. return TokenizedFeature::NoOpener::Yes;
  520. // 2. If element's link types do not include the opener keyword and
  521. // target is an ASCII case-insensitive match for "_blank", then return true.
  522. if (!link_types.contains_slow("opener"sv) && Infra::is_ascii_case_insensitive_match(target, "_blank"sv))
  523. return TokenizedFeature::NoOpener::Yes;
  524. // 3. Return false.
  525. return TokenizedFeature::NoOpener::No;
  526. }
  527. WebIDL::ExceptionOr<JS::NonnullGCPtr<ElementInternals>> HTMLElement::attach_internals()
  528. {
  529. // 1. If this's is value is not null, then throw a "NotSupportedError" DOMException.
  530. if (is_value().has_value())
  531. return WebIDL::NotSupportedError::create(realm(), "ElementInternals cannot be attached to a customized build-in element"_fly_string);
  532. // 2. Let definition be the result of looking up a custom element definition given this's node document, its namespace, its local name, and null as the is value.
  533. auto definition = document().lookup_custom_element_definition(namespace_uri(), local_name(), is_value());
  534. // 3. If definition is null, then throw an "NotSupportedError" DOMException.
  535. if (!definition)
  536. return WebIDL::NotSupportedError::create(realm(), "ElementInternals cannot be attached to an element that is not a custom element"_fly_string);
  537. // 4. If definition's disable internals is true, then throw a "NotSupportedError" DOMException.
  538. if (definition->disable_internals())
  539. return WebIDL::NotSupportedError::create(realm(), "ElementInternals are disabled for this custom element"_fly_string);
  540. // 5. If this's attached internals is non-null, then throw an "NotSupportedError" DOMException.
  541. if (m_attached_internals)
  542. return WebIDL::NotSupportedError::create(realm(), "ElementInternals already attached"_fly_string);
  543. // 6. If this's custom element state is not "precustomized" or "custom", then throw a "NotSupportedError" DOMException.
  544. if (!first_is_one_of(custom_element_state(), DOM::CustomElementState::Precustomized, DOM::CustomElementState::Custom))
  545. return WebIDL::NotSupportedError::create(realm(), "Custom element is in an invalid state to attach ElementInternals"_fly_string);
  546. // 7. Set this's attached internals to a new ElementInternals instance whose target element is this.
  547. auto internals = ElementInternals::create(realm(), *this);
  548. m_attached_internals = internals;
  549. // 8. Return this's attached internals.
  550. return { internals };
  551. }
  552. void HTMLElement::did_receive_focus()
  553. {
  554. if (m_content_editable_state != ContentEditableState::True)
  555. return;
  556. auto navigable = document().navigable();
  557. if (!navigable)
  558. return;
  559. navigable->set_cursor_position(DOM::Position::create(realm(), *this, 0));
  560. }
  561. // https://html.spec.whatwg.org/multipage/interaction.html#dom-accesskeylabel
  562. String HTMLElement::access_key_label() const
  563. {
  564. dbgln("FIXME: Implement HTMLElement::access_key_label()");
  565. return String {};
  566. }
  567. }