HTMLAnchorElement.cpp 3.2 KB

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