HTMLAnchorElement.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/DOM/Event.h>
  9. #include <LibWeb/HTML/AttributeNames.h>
  10. #include <LibWeb/HTML/HTMLAnchorElement.h>
  11. #include <LibWeb/HTML/HTMLImageElement.h>
  12. #include <LibWeb/HTML/Window.h>
  13. #include <LibWeb/PixelUnits.h>
  14. #include <LibWeb/ReferrerPolicy/ReferrerPolicy.h>
  15. #include <LibWeb/UIEvents/MouseEvent.h>
  16. namespace Web::HTML {
  17. JS_DEFINE_ALLOCATOR(HTMLAnchorElement);
  18. HTMLAnchorElement::HTMLAnchorElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  19. : HTMLElement(document, move(qualified_name))
  20. {
  21. }
  22. HTMLAnchorElement::~HTMLAnchorElement() = default;
  23. void HTMLAnchorElement::initialize(JS::Realm& realm)
  24. {
  25. Base::initialize(realm);
  26. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLAnchorElement);
  27. }
  28. void HTMLAnchorElement::attribute_changed(FlyString const& name, Optional<String> const& value)
  29. {
  30. HTMLElement::attribute_changed(name, value);
  31. if (name == HTML::AttributeNames::href) {
  32. set_the_url();
  33. }
  34. }
  35. Optional<String> HTMLAnchorElement::hyperlink_element_utils_href() const
  36. {
  37. return attribute(HTML::AttributeNames::href);
  38. }
  39. WebIDL::ExceptionOr<void> HTMLAnchorElement::set_hyperlink_element_utils_href(String href)
  40. {
  41. return set_attribute(HTML::AttributeNames::href, move(href));
  42. }
  43. bool HTMLAnchorElement::has_activation_behavior() const
  44. {
  45. return true;
  46. }
  47. // https://html.spec.whatwg.org/multipage/links.html#links-created-by-a-and-area-elements
  48. void HTMLAnchorElement::activation_behavior(Web::DOM::Event const& event)
  49. {
  50. // The activation behavior of an a or area element element given an event event is:
  51. // 1. If element has no href attribute, then return.
  52. if (href().is_empty())
  53. return;
  54. // 2. Let hyperlinkSuffix be null.
  55. Optional<String> hyperlink_suffix {};
  56. // 3. If element is an a element, and event's target is an img with an ismap attribute specified, then:
  57. if (event.target() && is<HTMLImageElement>(*event.target()) && static_cast<HTMLImageElement const&>(*event.target()).has_attribute(AttributeNames::ismap)) {
  58. // 1. Let x and y be 0.
  59. CSSPixels x { 0 };
  60. CSSPixels y { 0 };
  61. // 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
  62. // 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.
  63. if (event.is_trusted() && is<UIEvents::MouseEvent>(event)) {
  64. auto const& mouse_event = static_cast<UIEvents::MouseEvent const&>(event);
  65. x = CSSPixels { mouse_event.offset_x() };
  66. y = CSSPixels { mouse_event.offset_y() };
  67. }
  68. // 3. If x is negative, set x to 0.
  69. x = max(x, 0);
  70. // 4. If y is negative, set y to 0.
  71. y = max(y, 0);
  72. // 5. Set hyperlinkSuffix to the concatenation of U+003F (?), the value of x expressed as a base-ten integer using ASCII digits,
  73. // U+002C (,), and the value of y expressed as a base-ten integer using ASCII digits.
  74. hyperlink_suffix = MUST(String::formatted("?{},{}", x.to_int(), y.to_int()));
  75. }
  76. // 4. Let userInvolvement be event's user navigation involvement.
  77. auto user_involvement = user_navigation_involvement(event);
  78. // FIXME: 5. If the user has expressed a preference to download the hyperlink, then set userInvolvement to "browser UI".
  79. // NOTE: That is, if the user has expressed a specific preference for downloading, this no longer counts as merely "activation".
  80. // FIXME: 6. If element has a download attribute, or if the user has expressed a preference to download the
  81. // hyperlink, then download the hyperlink created by element with hyperlinkSuffix set to hyperlinkSuffix and
  82. // userInvolvement set to userInvolvement.
  83. // 7. Otherwise, follow the hyperlink created by element with hyperlinkSuffix set to hyperlinkSuffix and userInvolvement set to userInvolvement.
  84. follow_the_hyperlink(hyperlink_suffix, user_involvement);
  85. }
  86. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  87. i32 HTMLAnchorElement::default_tab_index_value() const
  88. {
  89. // See the base function for the spec comments.
  90. return 0;
  91. }
  92. Optional<ARIA::Role> HTMLAnchorElement::default_role() const
  93. {
  94. // https://www.w3.org/TR/html-aria/#el-a-no-href
  95. if (!href().is_empty())
  96. return ARIA::Role::link;
  97. // https://www.w3.org/TR/html-aria/#el-a
  98. return ARIA::Role::generic;
  99. }
  100. // https://html.spec.whatwg.org/multipage/text-level-semantics.html#dom-a-text
  101. String HTMLAnchorElement::text() const
  102. {
  103. // The text attribute's getter must return this element's descendant text content.
  104. return descendant_text_content();
  105. }
  106. // https://html.spec.whatwg.org/multipage/text-level-semantics.html#dom-a-text
  107. void HTMLAnchorElement::set_text(String const& text)
  108. {
  109. // The text attribute's setter must string replace all with the given value within this element.
  110. string_replace_all(text);
  111. }
  112. // https://html.spec.whatwg.org/multipage/text-level-semantics.html#dom-a-referrerpolicy
  113. StringView HTMLAnchorElement::referrer_policy() const
  114. {
  115. // FIXME: This should probably be `Reflect` in the IDL.
  116. // The IDL attribute referrerPolicy must reflect the referrerpolicy content attribute, limited to only known values.
  117. auto maybe_policy_string = attribute(HTML::AttributeNames::referrerpolicy);
  118. if (!maybe_policy_string.has_value())
  119. return ""sv;
  120. auto maybe_policy = ReferrerPolicy::from_string(maybe_policy_string.value());
  121. if (!maybe_policy.has_value())
  122. return ""sv;
  123. return ReferrerPolicy::to_string(maybe_policy.value());
  124. }
  125. // https://html.spec.whatwg.org/multipage/text-level-semantics.html#dom-a-referrerpolicy
  126. WebIDL::ExceptionOr<void> HTMLAnchorElement::set_referrer_policy(String const& referrer_policy)
  127. {
  128. // The IDL attribute referrerPolicy must reflect the referrerpolicy content attribute, limited to only known values.
  129. return set_attribute(HTML::AttributeNames::referrerpolicy, referrer_policy);
  130. }
  131. }