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.
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.
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.
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.
30 #include "core/dom/VisitedLinkState.h"
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"
40 static inline const AtomicString& linkAttribute(const Element& element)
42 ASSERT(element.isLink());
43 if (element.isHTMLElement())
44 return element.fastGetAttribute(HTMLNames::hrefAttr);
45 ASSERT(element.isSVGElement());
46 return element.getAttribute(XLinkNames::hrefAttr);
49 static inline LinkHash linkHashForElement(const Element& element, const AtomicString& attribute = AtomicString())
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);
57 VisitedLinkState::VisitedLinkState(const Document& document)
58 : m_document(document)
62 void VisitedLinkState::invalidateStyleForAllLinks()
64 if (m_linksCheckedForVisitedState.isEmpty())
66 for (Node& node : NodeTraversal::startsAt(document().firstChild())) {
68 node.setNeedsStyleRecalc(SubtreeStyleChange, StyleChangeReasonForTracing::create(StyleChangeReason::VisitedLink));
72 void VisitedLinkState::invalidateStyleForLink(LinkHash linkHash)
74 if (!m_linksCheckedForVisitedState.contains(linkHash))
76 for (Node& node : NodeTraversal::startsAt(document().firstChild())) {
77 if (node.isLink() && linkHashForElement(toElement(node)) == linkHash)
78 node.setNeedsStyleRecalc(SubtreeStyleChange, StyleChangeReasonForTracing::create(StyleChangeReason::VisitedLink));
82 EInsideLink VisitedLinkState::determineLinkStateSlowCase(const Element& element)
84 ASSERT(element.isLink());
85 ASSERT(document().isActive());
86 ASSERT(document() == element.document());
88 const AtomicString& attribute = linkAttribute(element);
90 if (attribute.isNull())
91 return NotInsideLink; // This can happen for <img usemap>
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;
100 if (LinkHash hash = linkHashForElement(element, attribute)) {
101 m_linksCheckedForVisitedState.add(hash);
102 if (Platform::current()->isLinkVisited(hash))
103 return InsideVisitedLink;
106 return InsideUnvisitedLink;
109 void VisitedLinkState::trace(Visitor* visitor)
111 visitor->trace(m_document);