HTMLElement.cpp 23 KB

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