HTMLAnchorElement.cpp 6.7 KB

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