HTMLAnchorElement.cpp 5.3 KB

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