HTMLAnchorElement.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLAnchorElementPrototype>(realm, "HTMLAnchorElement"_fly_string));
  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. void HTMLAnchorElement::activation_behavior(Web::DOM::Event const&)
  42. {
  43. // The activation behavior of an a element element given an event event is:
  44. // 1. If element has no href attribute, then return.
  45. if (href().is_empty())
  46. return;
  47. // 2. Let hyperlinkSuffix be null.
  48. Optional<String> hyperlink_suffix {};
  49. // FIXME: 3. If event's target is an img with an ismap attribute
  50. // specified, then:
  51. // 3.1. Let x and y be 0.
  52. //
  53. // 3.2. If event's isTrusted attribute is initialized to true, then
  54. // set x to the distance in CSS pixels from the left edge of the image
  55. // to the location of the click, and set y to the distance in CSS
  56. // pixels from the top edge of the image to the location of the click.
  57. //
  58. // 3.3. If x is negative, set x to 0.
  59. //
  60. // 3.4. If y is negative, set y to 0.
  61. //
  62. // 3.5. Set hyperlinkSuffix to the concatenation of U+003F (?), the
  63. // value of x expressed as a base-ten integer using ASCII digits,
  64. // U+002C (,), and the value of y expressed as a base-ten integer
  65. // using ASCII digits.
  66. // FIXME: 4. If element has a download attribute, or if the user has
  67. // expressed a preference to download the hyperlink, then download the
  68. // hyperlink created by element given hyperlinkSuffix.
  69. // 5. Otherwise, follow the hyperlink created by element given
  70. // hyperlinkSuffix.
  71. follow_the_hyperlink(hyperlink_suffix);
  72. }
  73. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  74. i32 HTMLAnchorElement::default_tab_index_value() const
  75. {
  76. // See the base function for the spec comments.
  77. return 0;
  78. }
  79. Optional<ARIA::Role> HTMLAnchorElement::default_role() const
  80. {
  81. // https://www.w3.org/TR/html-aria/#el-a-no-href
  82. if (!href().is_empty())
  83. return ARIA::Role::link;
  84. // https://www.w3.org/TR/html-aria/#el-a
  85. return ARIA::Role::generic;
  86. }
  87. // https://html.spec.whatwg.org/multipage/text-level-semantics.html#dom-a-text
  88. String HTMLAnchorElement::text() const
  89. {
  90. // The text attribute's getter must return this element's descendant text content.
  91. return descendant_text_content();
  92. }
  93. // https://html.spec.whatwg.org/multipage/text-level-semantics.html#dom-a-text
  94. void HTMLAnchorElement::set_text(String const& text)
  95. {
  96. // The text attribute's setter must string replace all with the given value within this element.
  97. string_replace_all(text);
  98. }
  99. // https://html.spec.whatwg.org/multipage/text-level-semantics.html#dom-a-referrerpolicy
  100. StringView HTMLAnchorElement::referrer_policy() const
  101. {
  102. // FIXME: This should probably be `Reflect` in the IDL.
  103. // The IDL attribute referrerPolicy must reflect the referrerpolicy content attribute, limited to only known values.
  104. auto maybe_policy_string = attribute(HTML::AttributeNames::referrerpolicy);
  105. if (!maybe_policy_string.has_value())
  106. return ""sv;
  107. auto maybe_policy = ReferrerPolicy::from_string(maybe_policy_string.value());
  108. if (!maybe_policy.has_value())
  109. return ""sv;
  110. return ReferrerPolicy::to_string(maybe_policy.value());
  111. }
  112. // https://html.spec.whatwg.org/multipage/text-level-semantics.html#dom-a-referrerpolicy
  113. WebIDL::ExceptionOr<void> HTMLAnchorElement::set_referrer_policy(String const& referrer_policy)
  114. {
  115. // The IDL attribute referrerPolicy must reflect the referrerpolicy content attribute, limited to only known values.
  116. return set_attribute(HTML::AttributeNames::referrerpolicy, referrer_policy);
  117. }
  118. }