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