1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2000 Simon Hausmann <hausmann@kde.org>
5 * Copyright (C) 2007-2016 Apple Inc. All rights reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 */
23
24#pragma once
25
26#include "HTMLElement.h"
27#include "HTMLNames.h"
28#include "SharedStringHash.h"
29#include "URLUtils.h"
30#include <wtf/OptionSet.h>
31
32namespace WebCore {
33
34class AdClickAttribution;
35class DOMTokenList;
36
37// Link relation bitmask values.
38enum class Relation {
39 NoReferrer = 1 << 0,
40 NoOpener = 1 << 1,
41 Opener = 1 << 2,
42};
43
44class HTMLAnchorElement : public HTMLElement, public URLUtils<HTMLAnchorElement> {
45 WTF_MAKE_ISO_ALLOCATED(HTMLAnchorElement);
46public:
47 static Ref<HTMLAnchorElement> create(Document&);
48 static Ref<HTMLAnchorElement> create(const QualifiedName&, Document&);
49
50 virtual ~HTMLAnchorElement();
51
52 WEBCORE_EXPORT URL href() const;
53 void setHref(const AtomString&);
54
55 const AtomString& name() const;
56
57 WEBCORE_EXPORT String origin() const;
58
59 WEBCORE_EXPORT String text();
60 void setText(const String&);
61
62 bool isLiveLink() const;
63
64 bool willRespondToMouseClickEvents() final;
65
66 bool hasRel(Relation) const;
67
68 SharedStringHash visitedLinkHash() const;
69
70 WEBCORE_EXPORT DOMTokenList& relList() const;
71
72#if USE(SYSTEM_PREVIEW)
73 WEBCORE_EXPORT bool isSystemPreviewLink() const;
74#endif
75
76protected:
77 HTMLAnchorElement(const QualifiedName&, Document&);
78
79 void parseAttribute(const QualifiedName&, const AtomString&) override;
80
81private:
82 bool supportsFocus() const override;
83 bool isMouseFocusable() const override;
84 bool isKeyboardFocusable(KeyboardEvent*) const override;
85 void defaultEventHandler(Event&) final;
86 void setActive(bool active = true, bool pause = false) final;
87 void accessKeyAction(bool sendMouseEvents) final;
88 bool isURLAttribute(const Attribute&) const final;
89 bool canStartSelection() const final;
90 String target() const override;
91 int tabIndex() const final;
92 bool draggable() const final;
93
94 String effectiveTarget() const;
95
96 void sendPings(const URL& destinationURL);
97
98 Optional<AdClickAttribution> parseAdClickAttribution() const;
99
100 void handleClick(Event&);
101
102 enum EventType {
103 MouseEventWithoutShiftKey,
104 MouseEventWithShiftKey,
105 NonMouseEvent,
106 };
107 static EventType eventType(Event&);
108 bool treatLinkAsLiveForEventType(EventType) const;
109
110 Element* rootEditableElementForSelectionOnMouseDown() const;
111 void setRootEditableElementForSelectionOnMouseDown(Element*);
112 void clearRootEditableElementForSelectionOnMouseDown();
113
114 bool m_hasRootEditableElementForSelectionOnMouseDown;
115 bool m_wasShiftKeyDownOnMouseDown;
116 OptionSet<Relation> m_linkRelations;
117
118 // This is computed only once and must not be affected by subsequent URL changes.
119 mutable Optional<SharedStringHash> m_storedVisitedLinkHash;
120
121 mutable std::unique_ptr<DOMTokenList> m_relList;
122};
123
124inline SharedStringHash HTMLAnchorElement::visitedLinkHash() const
125{
126 ASSERT(isLink());
127 if (!m_storedVisitedLinkHash)
128 m_storedVisitedLinkHash = computeVisitedLinkHash(document().baseURL(), attributeWithoutSynchronization(HTMLNames::hrefAttr));
129 return *m_storedVisitedLinkHash;
130}
131
132// Functions shared with the other anchor elements (i.e., SVG).
133
134bool isEnterKeyKeydownEvent(Event&);
135bool shouldProhibitLinks(Element*);
136
137} // namespace WebCore
138