2 * Copyright (C) 2006 Rob Buis <buis@kde.org>
3 * (C) 2008 Nikolas Zimmermann <zimmermann@kde.org>
4 * Copyright (C) 2008 Apple Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #include "CSSCursorImageValue.h"
25 #include "CachedResourceLoader.h"
26 #include "TreeScope.h"
27 #include "PlatformString.h"
28 #include <wtf/MathExtras.h>
29 #include <wtf/UnusedParam.h>
32 #include "SVGCursorElement.h"
33 #include "SVGLengthContext.h"
35 #include "SVGURIReference.h"
41 static inline bool isSVGCursorIdentifier(const String& url)
43 KURL kurl(ParsedURLString, url);
44 return kurl.hasFragmentIdentifier();
47 static inline SVGCursorElement* resourceReferencedByCursorElement(const String& url, Document* document)
49 Element* element = SVGURIReference::targetElementFromIRIString(url, document);
50 if (element && element->hasTagName(SVGNames::cursorTag))
51 return static_cast<SVGCursorElement*>(element);
57 CSSCursorImageValue::CSSCursorImageValue(const String& url, const IntPoint& hotSpot)
58 : CSSImageValue(CursorImageClass, url)
63 CSSCursorImageValue::~CSSCursorImageValue()
66 const String& url = getStringValue();
67 if (!isSVGCursorIdentifier(url))
70 HashSet<SVGElement*>::const_iterator it = m_referencedElements.begin();
71 HashSet<SVGElement*>::const_iterator end = m_referencedElements.end();
73 for (; it != end; ++it) {
74 SVGElement* referencedElement = *it;
75 referencedElement->cursorImageValueRemoved();
76 if (SVGCursorElement* cursorElement = resourceReferencedByCursorElement(url, referencedElement->document()))
77 cursorElement->removeClient(referencedElement);
82 bool CSSCursorImageValue::updateIfSVGCursorIsUsed(Element* element)
85 UNUSED_PARAM(element);
87 if (!element || !element->isSVGElement())
90 const String& url = getStringValue();
91 if (!isSVGCursorIdentifier(url))
94 if (SVGCursorElement* cursorElement = resourceReferencedByCursorElement(url, element->document())) {
95 // FIXME: This will override hot spot specified in CSS, which is probably incorrect.
96 SVGLengthContext lengthContext(0);
97 float x = roundf(cursorElement->x().value(lengthContext));
98 m_hotSpot.setX(static_cast<int>(x));
100 float y = roundf(cursorElement->y().value(lengthContext));
101 m_hotSpot.setY(static_cast<int>(y));
103 if (cachedImageURL() != element->document()->completeURL(cursorElement->href()))
106 SVGElement* svgElement = static_cast<SVGElement*>(element);
107 m_referencedElements.add(svgElement);
108 svgElement->setCursorImageValue(this);
109 cursorElement->addClient(svgElement);
117 StyleCachedImage* CSSCursorImageValue::cachedImage(CachedResourceLoader* loader)
119 String url = getStringValue();
122 if (isSVGCursorIdentifier(url) && loader && loader->document()) {
123 // FIXME: This will fail if the <cursor> element is in a shadow DOM (bug 59827)
124 if (SVGCursorElement* cursorElement = resourceReferencedByCursorElement(url, loader->document()))
125 url = cursorElement->href();
129 return CSSImageValue::cachedImage(loader, url);
133 void CSSCursorImageValue::removeReferencedElement(SVGElement* element)
135 m_referencedElements.remove(element);
139 } // namespace WebCore