HTMLAnchorElement.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/ARIA/Roles.h>
  8. #include <LibWeb/Bindings/HTMLAnchorElementPrototype.h>
  9. #include <LibWeb/DOM/DOMTokenList.h>
  10. #include <LibWeb/DOM/Event.h>
  11. #include <LibWeb/HTML/AttributeNames.h>
  12. #include <LibWeb/HTML/HTMLAnchorElement.h>
  13. #include <LibWeb/HTML/HTMLImageElement.h>
  14. #include <LibWeb/HTML/Window.h>
  15. #include <LibWeb/PixelUnits.h>
  16. #include <LibWeb/ReferrerPolicy/ReferrerPolicy.h>
  17. #include <LibWeb/UIEvents/MouseEvent.h>
  18. namespace Web::HTML {
  19. JS_DEFINE_ALLOCATOR(HTMLAnchorElement);
  20. HTMLAnchorElement::HTMLAnchorElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  21. : HTMLElement(document, move(qualified_name))
  22. {
  23. }
  24. HTMLAnchorElement::~HTMLAnchorElement() = default;
  25. void HTMLAnchorElement::initialize(JS::Realm& realm)
  26. {
  27. Base::initialize(realm);
  28. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLAnchorElement);
  29. }
  30. void HTMLAnchorElement::visit_edges(Cell::Visitor& visitor)
  31. {
  32. Base::visit_edges(visitor);
  33. visitor.visit(m_rel_list);
  34. }
  35. void HTMLAnchorElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value)
  36. {
  37. HTMLElement::attribute_changed(name, old_value, value);
  38. if (name == HTML::AttributeNames::href) {
  39. set_the_url();
  40. } else if (name == HTML::AttributeNames::rel) {
  41. if (m_rel_list)
  42. m_rel_list->associated_attribute_changed(value.value_or(String {}));
  43. }
  44. }
  45. Optional<String> HTMLAnchorElement::hyperlink_element_utils_href() const
  46. {
  47. return attribute(HTML::AttributeNames::href);
  48. }
  49. WebIDL::ExceptionOr<void> HTMLAnchorElement::set_hyperlink_element_utils_href(String href)
  50. {
  51. return set_attribute(HTML::AttributeNames::href, move(href));
  52. }
  53. Optional<String> HTMLAnchorElement::hyperlink_element_utils_referrerpolicy() const
  54. {
  55. return attribute(HTML::AttributeNames::referrerpolicy);
  56. }
  57. bool HTMLAnchorElement::has_activation_behavior() const
  58. {
  59. return true;
  60. }
  61. // https://html.spec.whatwg.org/multipage/links.html#links-created-by-a-and-area-elements
  62. void HTMLAnchorElement::activation_behavior(Web::DOM::Event const& event)
  63. {
  64. // The activation behavior of an a or area element element given an event event is:
  65. // 1. If element has no href attribute, then return.
  66. if (href().is_empty())
  67. return;
  68. // AD-HOC: Do not activate the element for clicks with the ctrl/cmd modifier present. This lets
  69. // the chrome open the link in a new tab.
  70. if (is<UIEvents::MouseEvent>(event)) {
  71. auto const& mouse_event = static_cast<UIEvents::MouseEvent const&>(event);
  72. if (mouse_event.platform_ctrl_key())
  73. return;
  74. }
  75. // 2. Let hyperlinkSuffix be null.
  76. Optional<String> hyperlink_suffix {};
  77. // 3. If element is an a element, and event's target is an img with an ismap attribute specified, then:
  78. if (event.target() && is<HTMLImageElement>(*event.target()) && static_cast<HTMLImageElement const&>(*event.target()).has_attribute(AttributeNames::ismap)) {
  79. // 1. Let x and y be 0.
  80. CSSPixels x { 0 };
  81. CSSPixels y { 0 };
  82. // 2. If event's isTrusted attribute is initialized to true, then set x to the distance in CSS pixels from the left edge of the image
  83. // to the location of the click, and set y to the distance in CSS pixels from the top edge of the image to the location of the click.
  84. if (event.is_trusted() && is<UIEvents::MouseEvent>(event)) {
  85. auto const& mouse_event = static_cast<UIEvents::MouseEvent const&>(event);
  86. x = CSSPixels { mouse_event.offset_x() };
  87. y = CSSPixels { mouse_event.offset_y() };
  88. }
  89. // 3. If x is negative, set x to 0.
  90. x = max(x, 0);
  91. // 4. If y is negative, set y to 0.
  92. y = max(y, 0);
  93. // 5. Set hyperlinkSuffix to the concatenation of U+003F (?), the value of x expressed as a base-ten integer using ASCII digits,
  94. // U+002C (,), and the value of y expressed as a base-ten integer using ASCII digits.
  95. hyperlink_suffix = MUST(String::formatted("?{},{}", x.to_int(), y.to_int()));
  96. }
  97. // 4. Let userInvolvement be event's user navigation involvement.
  98. auto user_involvement = user_navigation_involvement(event);
  99. // FIXME: 5. If the user has expressed a preference to download the hyperlink, then set userInvolvement to "browser UI".
  100. // NOTE: That is, if the user has expressed a specific preference for downloading, this no longer counts as merely "activation".
  101. // FIXME: 6. If element has a download attribute, or if the user has expressed a preference to download the
  102. // hyperlink, then download the hyperlink created by element with hyperlinkSuffix set to hyperlinkSuffix and
  103. // userInvolvement set to userInvolvement.
  104. // 7. Otherwise, follow the hyperlink created by element with hyperlinkSuffix set to hyperlinkSuffix and userInvolvement set to userInvolvement.
  105. follow_the_hyperlink(hyperlink_suffix, user_involvement);
  106. }
  107. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  108. i32 HTMLAnchorElement::default_tab_index_value() const
  109. {
  110. // See the base function for the spec comments.
  111. return 0;
  112. }
  113. Optional<ARIA::Role> HTMLAnchorElement::default_role() const
  114. {
  115. // https://www.w3.org/TR/html-aria/#el-a-no-href
  116. if (!href().is_empty())
  117. return ARIA::Role::link;
  118. // https://www.w3.org/TR/html-aria/#el-a
  119. return ARIA::Role::generic;
  120. }
  121. // https://html.spec.whatwg.org/multipage/text-level-semantics.html#dom-a-rellist
  122. JS::NonnullGCPtr<DOM::DOMTokenList> HTMLAnchorElement::rel_list()
  123. {
  124. // The IDL attribute relList must reflect the rel content attribute.
  125. if (!m_rel_list)
  126. m_rel_list = DOM::DOMTokenList::create(*this, HTML::AttributeNames::rel);
  127. return *m_rel_list;
  128. }
  129. // https://html.spec.whatwg.org/multipage/text-level-semantics.html#dom-a-text
  130. String HTMLAnchorElement::text() const
  131. {
  132. // The text attribute's getter must return this element's descendant text content.
  133. return descendant_text_content();
  134. }
  135. // https://html.spec.whatwg.org/multipage/text-level-semantics.html#dom-a-text
  136. void HTMLAnchorElement::set_text(String const& text)
  137. {
  138. // The text attribute's setter must string replace all with the given value within this element.
  139. string_replace_all(text);
  140. }
  141. }