HTMLAnchorElement.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. HTMLAnchorElement::HTMLAnchorElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  12. : HTMLElement(document, move(qualified_name))
  13. {
  14. activation_behavior = [this](auto const& event) {
  15. run_activation_behavior(event);
  16. };
  17. }
  18. HTMLAnchorElement::~HTMLAnchorElement() = default;
  19. JS::ThrowCompletionOr<void> HTMLAnchorElement::initialize(JS::Realm& realm)
  20. {
  21. MUST_OR_THROW_OOM(Base::initialize(realm));
  22. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLAnchorElementPrototype>(realm, "HTMLAnchorElement"));
  23. return {};
  24. }
  25. void HTMLAnchorElement::parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value)
  26. {
  27. HTMLElement::parse_attribute(name, value);
  28. if (name == HTML::AttributeNames::href) {
  29. set_the_url();
  30. }
  31. }
  32. DeprecatedString HTMLAnchorElement::hyperlink_element_utils_href() const
  33. {
  34. return attribute(HTML::AttributeNames::href);
  35. }
  36. void HTMLAnchorElement::set_hyperlink_element_utils_href(DeprecatedString href)
  37. {
  38. MUST(set_attribute(HTML::AttributeNames::href, move(href)));
  39. }
  40. void HTMLAnchorElement::run_activation_behavior(Web::DOM::Event const&)
  41. {
  42. // The activation behavior of an a element element given an event event is:
  43. // 1. If element has no href attribute, then return.
  44. if (href().is_empty())
  45. return;
  46. // 2. Let hyperlinkSuffix be null.
  47. Optional<DeprecatedString> hyperlink_suffix {};
  48. // FIXME: 3. If event's target is an img with an ismap attribute
  49. // specified, then:
  50. // 3.1. Let x and y be 0.
  51. //
  52. // 3.2. If event's isTrusted attribute is initialized to true, then
  53. // set x to the distance in CSS pixels from the left edge of the image
  54. // to the location of the click, and set y to the distance in CSS
  55. // pixels from the top edge of the image to the location of the click.
  56. //
  57. // 3.3. If x is negative, set x to 0.
  58. //
  59. // 3.4. If y is negative, set y to 0.
  60. //
  61. // 3.5. Set hyperlinkSuffix to the concatenation of U+003F (?), the
  62. // value of x expressed as a base-ten integer using ASCII digits,
  63. // U+002C (,), and the value of y expressed as a base-ten integer
  64. // using ASCII digits.
  65. // FIXME: 4. If element has a download attribute, or if the user has
  66. // expressed a preference to download the hyperlink, then download the
  67. // hyperlink created by element given hyperlinkSuffix.
  68. // 5. Otherwise, follow the hyperlink created by element given
  69. // hyperlinkSuffix.
  70. follow_the_hyperlink(hyperlink_suffix);
  71. }
  72. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  73. i32 HTMLAnchorElement::default_tab_index_value() const
  74. {
  75. // See the base function for the spec comments.
  76. return 0;
  77. }
  78. Optional<ARIA::Role> HTMLAnchorElement::default_role() const
  79. {
  80. // https://www.w3.org/TR/html-aria/#el-a-no-href
  81. if (!href().is_null())
  82. return ARIA::Role::link;
  83. // https://www.w3.org/TR/html-aria/#el-a
  84. return ARIA::Role::generic;
  85. }
  86. // https://html.spec.whatwg.org/multipage/text-level-semantics.html#dom-a-text
  87. DeprecatedString HTMLAnchorElement::text() const
  88. {
  89. // The text attribute's getter must return this element's descendant text content.
  90. return descendant_text_content();
  91. }
  92. // https://html.spec.whatwg.org/multipage/text-level-semantics.html#dom-a-text
  93. void HTMLAnchorElement::set_text(DeprecatedString const& text)
  94. {
  95. // The text attribute's setter must string replace all with the given value within this element.
  96. string_replace_all(text);
  97. }
  98. // https://html.spec.whatwg.org/multipage/text-level-semantics.html#dom-a-referrerpolicy
  99. DeprecatedString HTMLAnchorElement::referrer_policy() const
  100. {
  101. // The IDL attribute referrerPolicy must reflect the referrerpolicy content attribute, limited to only known values.
  102. auto policy_string = attribute(HTML::AttributeNames::referrerpolicy);
  103. auto maybe_policy = ReferrerPolicy::from_string(policy_string);
  104. if (maybe_policy.has_value())
  105. return ReferrerPolicy::to_string(maybe_policy.value());
  106. return "";
  107. }
  108. // https://html.spec.whatwg.org/multipage/text-level-semantics.html#dom-a-referrerpolicy
  109. WebIDL::ExceptionOr<void> HTMLAnchorElement::set_referrer_policy(DeprecatedString const& referrer_policy)
  110. {
  111. // The IDL attribute referrerPolicy must reflect the referrerpolicy content attribute, limited to only known values.
  112. return set_attribute(HTML::AttributeNames::referrerpolicy, referrer_policy);
  113. }
  114. }