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 "WebCoreMemoryInstrumentation.h"
28
#include <wtf/MathExtras.h>
29
#include <wtf/MemoryInstrumentationHashSet.h>
30
#include <wtf/UnusedParam.h>
31
#include <wtf/text/WTFString.h>
34
#include "SVGCursorElement.h"
35
#include "SVGLengthContext.h"
37
#include "SVGURIReference.h"
43
static inline bool isSVGCursorIdentifier(const String& url)
45
KURL kurl(ParsedURLString, url);
46
return kurl.hasFragmentIdentifier();
49
static inline SVGCursorElement* resourceReferencedByCursorElement(const String& url, Document* document)
51
Element* element = SVGURIReference::targetElementFromIRIString(url, document);
52
if (element && element->hasTagName(SVGNames::cursorTag))
53
return static_cast<SVGCursorElement*>(element);
59
CSSCursorImageValue::CSSCursorImageValue(const String& url, bool hasHotSpot, const IntPoint& hotSpot)
60
: CSSImageValue(CursorImageClass, url)
61
, m_hasHotSpot(hasHotSpot)
66
CSSCursorImageValue::~CSSCursorImageValue()
69
if (!isSVGCursorIdentifier(url()))
72
HashSet<SVGElement*>::const_iterator it = m_referencedElements.begin();
73
HashSet<SVGElement*>::const_iterator end = m_referencedElements.end();
75
for (; it != end; ++it) {
76
SVGElement* referencedElement = *it;
77
referencedElement->cursorImageValueRemoved();
78
if (SVGCursorElement* cursorElement = resourceReferencedByCursorElement(url(), referencedElement->document()))
79
cursorElement->removeClient(referencedElement);
84
String CSSCursorImageValue::customCssText() const
87
result.append(CSSImageValue::customCssText());
90
result.appendNumber(m_hotSpot.x());
92
result.appendNumber(m_hotSpot.y());
94
return result.toString();
97
bool CSSCursorImageValue::updateIfSVGCursorIsUsed(Element* element)
100
UNUSED_PARAM(element);
102
if (!element || !element->isSVGElement())
105
if (!isSVGCursorIdentifier(url()))
108
if (SVGCursorElement* cursorElement = resourceReferencedByCursorElement(url(), element->document())) {
109
// FIXME: This will override hot spot specified in CSS, which is probably incorrect.
110
SVGLengthContext lengthContext(0);
112
float x = roundf(cursorElement->x().value(lengthContext));
113
m_hotSpot.setX(static_cast<int>(x));
115
float y = roundf(cursorElement->y().value(lengthContext));
116
m_hotSpot.setY(static_cast<int>(y));
118
if (cachedImageURL() != element->document()->completeURL(cursorElement->href()))
121
SVGElement* svgElement = static_cast<SVGElement*>(element);
122
m_referencedElements.add(svgElement);
123
svgElement->setCursorImageValue(this);
124
cursorElement->addClient(svgElement);
132
StyleCachedImage* CSSCursorImageValue::cachedImage(CachedResourceLoader* loader)
135
if (isSVGCursorIdentifier(url()) && loader && loader->document()) {
136
// FIXME: This will fail if the <cursor> element is in a shadow DOM (bug 59827)
137
if (SVGCursorElement* cursorElement = resourceReferencedByCursorElement(url(), loader->document()))
138
return CSSImageValue::cachedImage(loader, cursorElement->href(), cursorElement);
142
return CSSImageValue::cachedImage(loader, url());
146
void CSSCursorImageValue::removeReferencedElement(SVGElement* element)
148
m_referencedElements.remove(element);
152
void CSSCursorImageValue::reportDescendantMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
154
MemoryClassInfo info(memoryObjectInfo, this, WebCoreMemoryTypes::CSS);
155
CSSImageValue::reportDescendantMemoryUsage(memoryObjectInfo);
157
info.addMember(m_referencedElements);
161
} // namespace WebCore