1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 *           (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com)
4 * Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com)
5 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
6 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
7 * Copyright (C) 2007, 2008 Eric Seidel <eric@webkit.org>
8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
9 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
10 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
11 * Copyright (C) 2012 Google Inc. All rights reserved.
12 *
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Library General Public
15 * License as published by the Free Software Foundation; either
16 * version 2 of the License, or (at your option) any later version.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 * Library General Public License for more details.
22 *
23 * You should have received a copy of the GNU Library General Public License
24 * along with this library; see the file COPYING.LIB.  If not, write to
25 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 * Boston, MA 02110-1301, USA.
27 */
28
29#include "config.h"
30#include "core/dom/VisitedLinkState.h"
31
32#include "core/HTMLNames.h"
33#include "core/XLinkNames.h"
34#include "core/dom/ElementTraversal.h"
35#include "core/html/HTMLAnchorElement.h"
36#include "public/platform/Platform.h"
37
38namespace blink {
39
40static inline const AtomicString& linkAttribute(const Element& element)
41{
42    ASSERT(element.isLink());
43    if (element.isHTMLElement())
44        return element.fastGetAttribute(HTMLNames::hrefAttr);
45    ASSERT(element.isSVGElement());
46    return element.getAttribute(XLinkNames::hrefAttr);
47}
48
49static inline LinkHash linkHashForElement(const Element& element, const AtomicString& attribute = AtomicString())
50{
51    ASSERT(attribute.isNull() || linkAttribute(element) == attribute);
52    if (isHTMLAnchorElement(element))
53        return toHTMLAnchorElement(element).visitedLinkHash();
54    return visitedLinkHash(element.document().baseURL(), attribute.isNull() ? linkAttribute(element) : attribute);
55}
56
57VisitedLinkState::VisitedLinkState(const Document& document)
58    : m_document(document)
59{
60}
61
62void VisitedLinkState::invalidateStyleForAllLinks()
63{
64    if (m_linksCheckedForVisitedState.isEmpty())
65        return;
66    for (Node* node = document().firstChild(); node; node = NodeTraversal::next(*node)) {
67        if (node->isLink())
68            node->setNeedsStyleRecalc(SubtreeStyleChange);
69    }
70}
71
72void VisitedLinkState::invalidateStyleForLink(LinkHash linkHash)
73{
74    if (!m_linksCheckedForVisitedState.contains(linkHash))
75        return;
76    for (Node* node = document().firstChild(); node; node = NodeTraversal::next(*node)) {
77        if (node->isLink() && linkHashForElement(toElement(*node)) == linkHash)
78            node->setNeedsStyleRecalc(SubtreeStyleChange);
79    }
80}
81
82EInsideLink VisitedLinkState::determineLinkStateSlowCase(const Element& element)
83{
84    ASSERT(element.isLink());
85    ASSERT(document().isActive());
86    ASSERT(document() == element.document());
87
88    const AtomicString& attribute = linkAttribute(element);
89
90    if (attribute.isNull())
91        return NotInsideLink; // This can happen for <img usemap>
92
93    // An empty attribute refers to the document itself which is always
94    // visited. It is useful to check this explicitly so that visited
95    // links can be tested in platform independent manner, without
96    // explicit support in the test harness.
97    if (attribute.isEmpty())
98        return InsideVisitedLink;
99
100    if (LinkHash hash = linkHashForElement(element, attribute)) {
101        m_linksCheckedForVisitedState.add(hash);
102        if (Platform::current()->isLinkVisited(hash))
103            return InsideVisitedLink;
104    }
105
106    return InsideUnvisitedLink;
107}
108
109void VisitedLinkState::trace(Visitor* visitor)
110{
111    visitor->trace(m_document);
112}
113
114} // namespace blink
115