HTMLAnchorElement.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. namespace Web::HTML {
  10. HTMLAnchorElement::HTMLAnchorElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  11. : HTMLElement(document, move(qualified_name))
  12. {
  13. activation_behavior = [this](auto const& event) {
  14. run_activation_behavior(event);
  15. };
  16. }
  17. HTMLAnchorElement::~HTMLAnchorElement() = default;
  18. JS::ThrowCompletionOr<void> HTMLAnchorElement::initialize(JS::Realm& realm)
  19. {
  20. MUST_OR_THROW_OOM(Base::initialize(realm));
  21. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLAnchorElementPrototype>(realm, "HTMLAnchorElement"));
  22. return {};
  23. }
  24. void HTMLAnchorElement::parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value)
  25. {
  26. HTMLElement::parse_attribute(name, value);
  27. if (name == HTML::AttributeNames::href) {
  28. set_the_url();
  29. }
  30. }
  31. DeprecatedString HTMLAnchorElement::hyperlink_element_utils_href() const
  32. {
  33. return attribute(HTML::AttributeNames::href);
  34. }
  35. void HTMLAnchorElement::set_hyperlink_element_utils_href(DeprecatedString href)
  36. {
  37. MUST(set_attribute(HTML::AttributeNames::href, move(href)));
  38. }
  39. void HTMLAnchorElement::run_activation_behavior(Web::DOM::Event const&)
  40. {
  41. // The activation behavior of an a element element given an event event is:
  42. // 1. If element has no href attribute, then return.
  43. if (href().is_empty())
  44. return;
  45. // 2. Let hyperlinkSuffix be null.
  46. Optional<DeprecatedString> hyperlink_suffix {};
  47. // FIXME: 3. If event's target is an img with an ismap attribute
  48. // specified, then:
  49. // 3.1. Let x and y be 0.
  50. //
  51. // 3.2. If event's isTrusted attribute is initialized to true, then
  52. // set x to the distance in CSS pixels from the left edge of the image
  53. // to the location of the click, and set y to the distance in CSS
  54. // pixels from the top edge of the image to the location of the click.
  55. //
  56. // 3.3. If x is negative, set x to 0.
  57. //
  58. // 3.4. If y is negative, set y to 0.
  59. //
  60. // 3.5. Set hyperlinkSuffix to the concatenation of U+003F (?), the
  61. // value of x expressed as a base-ten integer using ASCII digits,
  62. // U+002C (,), and the value of y expressed as a base-ten integer
  63. // using ASCII digits.
  64. // FIXME: 4. If element has a download attribute, or if the user has
  65. // expressed a preference to download the hyperlink, then download the
  66. // hyperlink created by element given hyperlinkSuffix.
  67. // 5. Otherwise, follow the hyperlink created by element given
  68. // hyperlinkSuffix.
  69. follow_the_hyperlink(hyperlink_suffix);
  70. }
  71. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  72. i32 HTMLAnchorElement::default_tab_index_value() const
  73. {
  74. // See the base function for the spec comments.
  75. return 0;
  76. }
  77. Optional<ARIA::Role> HTMLAnchorElement::default_role() const
  78. {
  79. // https://www.w3.org/TR/html-aria/#el-a-no-href
  80. if (!href().is_null())
  81. return ARIA::Role::link;
  82. // https://www.w3.org/TR/html-aria/#el-a
  83. return ARIA::Role::generic;
  84. }
  85. }