blob: f20c259b99d2d48b16961831f584d7144c90425d [file] [log] [blame]
dglazkov@chromium.orgb19b6a22012-12-27 22:57:00 +00001/*
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 "VisitedLinkState.h"
31
32#include "Frame.h"
33#include "HTMLAnchorElement.h"
34#include "HTMLNames.h"
35#include "NodeTraversal.h"
36#include "Page.h"
37#include "PageGroup.h"
38
39#if USE(PLATFORM_STRATEGIES)
40#include "PlatformStrategies.h"
41#include "VisitedLinkStrategy.h"
42#endif
43
44namespace WebCore {
45
46using namespace HTMLNames;
47
48inline static const AtomicString* linkAttribute(Element* element)
49{
50 if (!element->isLink())
51 return 0;
52 if (element->isHTMLElement())
53 return &element->fastGetAttribute(HTMLNames::hrefAttr);
54 if (element->isSVGElement())
55 return &element->getAttribute(XLinkNames::hrefAttr);
56 return 0;
57}
58
59PassOwnPtr<VisitedLinkState> VisitedLinkState::create(Document* document)
60{
61 return adoptPtr(new VisitedLinkState(document));
62}
63
64VisitedLinkState::VisitedLinkState(Document* document)
dglazkov@chromium.org19933ce2012-12-28 03:17:31 +000065 : m_document(document)
66{
67}
dglazkov@chromium.orgb19b6a22012-12-27 22:57:00 +000068
69void VisitedLinkState::invalidateStyleForAllLinks()
70{
71 if (m_linksCheckedForVisitedState.isEmpty())
72 return;
73 for (Element* element = ElementTraversal::firstWithin(m_document); element; element = ElementTraversal::next(element)) {
74 if (element->isLink())
75 element->setNeedsStyleRecalc();
76 }
77}
78
79inline static LinkHash linkHashForElement(Document* document, Element* element)
80{
81 if (element->hasTagName(aTag))
82 return static_cast<HTMLAnchorElement*>(element)->visitedLinkHash();
dglazkov@chromium.org19933ce2012-12-28 03:17:31 +000083 if (const AtomicString* attribute = linkAttribute(element))
84 return WebCore::visitedLinkHash(document->baseURL(), *attribute);
dglazkov@chromium.orgb19b6a22012-12-27 22:57:00 +000085 return 0;
86}
87
88void VisitedLinkState::invalidateStyleForLink(LinkHash linkHash)
89{
90 if (!m_linksCheckedForVisitedState.contains(linkHash))
91 return;
92 for (Element* element = ElementTraversal::firstWithin(m_document); element; element = ElementTraversal::next(element)) {
93 if (linkHashForElement(m_document, element) == linkHash)
94 element->setNeedsStyleRecalc();
95 }
96}
97
98EInsideLink VisitedLinkState::determineLinkStateSlowCase(Element* element)
99{
100 ASSERT(element->isLink());
101
102 const AtomicString* attribute = linkAttribute(element);
103 if (!attribute || attribute->isNull())
104 return NotInsideLink;
105
106 // An empty href refers to the document itself which is always visited. It is useful to check this explicitly so
107 // that visited links can be tested in platform independent manner, without explicit support in the test harness.
108 if (attribute->isEmpty())
109 return InsideVisitedLink;
110
111 LinkHash hash;
112 if (element->hasTagName(aTag))
113 hash = static_cast<HTMLAnchorElement*>(element)->visitedLinkHash();
114 else
115 hash = WebCore::visitedLinkHash(element->document()->baseURL(), *attribute);
116
117 if (!hash)
118 return InsideUnvisitedLink;
119
120 Frame* frame = element->document()->frame();
121 if (!frame)
122 return InsideUnvisitedLink;
123
124 Page* page = frame->page();
125 if (!page)
126 return InsideUnvisitedLink;
127
128 m_linksCheckedForVisitedState.add(hash);
129
130#if USE(PLATFORM_STRATEGIES)
131 return platformStrategies()->visitedLinkStrategy()->isLinkVisited(page, hash, element->document()->baseURL(), *attribute) ? InsideVisitedLink : InsideUnvisitedLink;
132#else
133 return page->group().isLinkVisited(hash) ? InsideVisitedLink : InsideUnvisitedLink;
134#endif
135}
136
137
138}