HTMLAnchorElement.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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& value)
  36. {
  37. HTMLElement::attribute_changed(name, 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. // 2. Let hyperlinkSuffix be null.
  69. Optional<String> hyperlink_suffix {};
  70. // 3. If element is an a element, and event's target is an img with an ismap attribute specified, then:
  71. if (event.target() && is<HTMLImageElement>(*event.target()) && static_cast<HTMLImageElement const&>(*event.target()).has_attribute(AttributeNames::ismap)) {
  72. // 1. Let x and y be 0.
  73. CSSPixels x { 0 };
  74. CSSPixels y { 0 };
  75. // 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
  76. // 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.
  77. if (event.is_trusted() && is<UIEvents::MouseEvent>(event)) {
  78. auto const& mouse_event = static_cast<UIEvents::MouseEvent const&>(event);
  79. x = CSSPixels { mouse_event.offset_x() };
  80. y = CSSPixels { mouse_event.offset_y() };
  81. }
  82. // 3. If x is negative, set x to 0.
  83. x = max(x, 0);
  84. // 4. If y is negative, set y to 0.
  85. y = max(y, 0);
  86. // 5. Set hyperlinkSuffix to the concatenation of U+003F (?), the value of x expressed as a base-ten integer using ASCII digits,
  87. // U+002C (,), and the value of y expressed as a base-ten integer using ASCII digits.
  88. hyperlink_suffix = MUST(String::formatted("?{},{}", x.to_int(), y.to_int()));
  89. }
  90. // 4. Let userInvolvement be event's user navigation involvement.
  91. auto user_involvement = user_navigation_involvement(event);
  92. // FIXME: 5. If the user has expressed a preference to download the hyperlink, then set userInvolvement to "browser UI".
  93. // NOTE: That is, if the user has expressed a specific preference for downloading, this no longer counts as merely "activation".
  94. // FIXME: 6. If element has a download attribute, or if the user has expressed a preference to download the
  95. // hyperlink, then download the hyperlink created by element with hyperlinkSuffix set to hyperlinkSuffix and
  96. // userInvolvement set to userInvolvement.
  97. // 7. Otherwise, follow the hyperlink created by element with hyperlinkSuffix set to hyperlinkSuffix and userInvolvement set to userInvolvement.
  98. follow_the_hyperlink(hyperlink_suffix, user_involvement);
  99. }
  100. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  101. i32 HTMLAnchorElement::default_tab_index_value() const
  102. {
  103. // See the base function for the spec comments.
  104. return 0;
  105. }
  106. Optional<ARIA::Role> HTMLAnchorElement::default_role() const
  107. {
  108. // https://www.w3.org/TR/html-aria/#el-a-no-href
  109. if (!href().is_empty())
  110. return ARIA::Role::link;
  111. // https://www.w3.org/TR/html-aria/#el-a
  112. return ARIA::Role::generic;
  113. }
  114. // https://html.spec.whatwg.org/multipage/text-level-semantics.html#dom-a-rellist
  115. JS::NonnullGCPtr<DOM::DOMTokenList> HTMLAnchorElement::rel_list()
  116. {
  117. // The IDL attribute relList must reflect the rel content attribute.
  118. if (!m_rel_list)
  119. m_rel_list = DOM::DOMTokenList::create(*this, HTML::AttributeNames::rel);
  120. return *m_rel_list;
  121. }
  122. // https://html.spec.whatwg.org/multipage/text-level-semantics.html#dom-a-text
  123. String HTMLAnchorElement::text() const
  124. {
  125. // The text attribute's getter must return this element's descendant text content.
  126. return descendant_text_content();
  127. }
  128. // https://html.spec.whatwg.org/multipage/text-level-semantics.html#dom-a-text
  129. void HTMLAnchorElement::set_text(String const& text)
  130. {
  131. // The text attribute's setter must string replace all with the given value within this element.
  132. string_replace_all(text);
  133. }
  134. }