HTMLElement.cpp 23 KB

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