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, 2008, 2009, 2010 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#ifndef HTMLAnchorElement_h
25#define HTMLAnchorElement_h
26
27#include "HTMLElement.h"
28#include "HTMLNames.h"
29#include "LinkHash.h"
30
31namespace WebCore {
32
33// Link relation bitmask values.
34// FIXME: Uncomment as the various link relations are implemented.
35enum {
36//     RelationAlternate   = 0x00000001,
37//     RelationArchives    = 0x00000002,
38//     RelationAuthor      = 0x00000004,
39//     RelationBoomark     = 0x00000008,
40//     RelationExternal    = 0x00000010,
41//     RelationFirst       = 0x00000020,
42//     RelationHelp        = 0x00000040,
43//     RelationIndex       = 0x00000080,
44//     RelationLast        = 0x00000100,
45//     RelationLicense     = 0x00000200,
46//     RelationNext        = 0x00000400,
47//     RelationNoFolow    = 0x00000800,
48    RelationNoReferrer     = 0x00001000,
49//     RelationPrev        = 0x00002000,
50//     RelationSearch      = 0x00004000,
51//     RelationSidebar     = 0x00008000,
52//     RelationTag         = 0x00010000,
53//     RelationUp          = 0x00020000,
54};
55
56class HTMLAnchorElement : public HTMLElement {
57public:
58    static PassRefPtr<HTMLAnchorElement> create(Document&);
59    static PassRefPtr<HTMLAnchorElement> create(const QualifiedName&, Document&);
60
61    virtual ~HTMLAnchorElement();
62
63    URL href() const;
64    void setHref(const AtomicString&);
65
66    const AtomicString& name() const;
67
68    String hash() const;
69    void setHash(const String&);
70
71    String host() const;
72    void setHost(const String&);
73
74    String hostname() const;
75    void setHostname(const String&);
76
77    String pathname() const;
78    void setPathname(const String&);
79
80    String port() const;
81    void setPort(const String&);
82
83    String protocol() const;
84    void setProtocol(const String&);
85
86    String search() const;
87    void setSearch(const String&);
88
89    String origin() const;
90
91    String text();
92
93    String toString() const;
94
95    bool isLiveLink() const;
96
97    virtual bool willRespondToMouseClickEvents() override;
98
99    bool hasRel(uint32_t relation) const;
100    void setRel(const String&);
101
102    LinkHash visitedLinkHash() const;
103    void invalidateCachedVisitedLinkHash() { m_cachedVisitedLinkHash = 0; }
104
105protected:
106    HTMLAnchorElement(const QualifiedName&, Document&);
107
108    virtual void parseAttribute(const QualifiedName&, const AtomicString&) override;
109
110private:
111    virtual bool supportsFocus() const override;
112    virtual bool isMouseFocusable() const override;
113    virtual bool isKeyboardFocusable(KeyboardEvent*) const override;
114    virtual void defaultEventHandler(Event*) override;
115    virtual void setActive(bool active = true, bool pause = false) override final;
116    virtual void accessKeyAction(bool sendMouseEvents) override;
117    virtual bool isURLAttribute(const Attribute&) const override;
118    virtual bool canStartSelection() const override;
119    virtual String target() const override;
120    virtual short tabIndex() const override final;
121    virtual bool draggable() const override;
122
123    void sendPings(const URL& destinationURL);
124
125    void handleClick(Event*);
126
127    enum EventType {
128        MouseEventWithoutShiftKey,
129        MouseEventWithShiftKey,
130        NonMouseEvent,
131    };
132    static EventType eventType(Event*);
133    bool treatLinkAsLiveForEventType(EventType) const;
134
135    Element* rootEditableElementForSelectionOnMouseDown() const;
136    void setRootEditableElementForSelectionOnMouseDown(Element*);
137    void clearRootEditableElementForSelectionOnMouseDown();
138
139    bool m_hasRootEditableElementForSelectionOnMouseDown : 1;
140    bool m_wasShiftKeyDownOnMouseDown : 1;
141    uint32_t m_linkRelations : 30;
142    mutable LinkHash m_cachedVisitedLinkHash;
143};
144
145inline LinkHash HTMLAnchorElement::visitedLinkHash() const
146{
147    if (!m_cachedVisitedLinkHash)
148        m_cachedVisitedLinkHash = WebCore::visitedLinkHash(document().baseURL(), fastGetAttribute(HTMLNames::hrefAttr));
149    return m_cachedVisitedLinkHash;
150}
151
152// Functions shared with the other anchor elements (i.e., SVG).
153
154bool isEnterKeyKeydownEvent(Event*);
155bool isLinkClick(Event*);
156bool shouldProhibitLinks(Element*);
157
158NODE_TYPE_CASTS(HTMLAnchorElement)
159
160} // namespace WebCore
161
162#endif // HTMLAnchorElement_h
163