HTMLAnchorElement.cpp 6.2 KB

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