OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2006 Rob Buis <buis@kde.org> | 2 * Copyright (C) 2006 Rob Buis <buis@kde.org> |
3 * (C) 2008 Nikolas Zimmermann <zimmermann@kde.org> | 3 * (C) 2008 Nikolas Zimmermann <zimmermann@kde.org> |
4 * Copyright (C) 2008 Apple Inc. All rights reserved. | 4 * Copyright (C) 2008 Apple Inc. All rights reserved. |
5 * | 5 * |
6 * This library is free software; you can redistribute it and/or | 6 * This library is free software; you can redistribute it and/or |
7 * modify it under the terms of the GNU Library General Public | 7 * modify it under the terms of the GNU Library General Public |
8 * License as published by the Free Software Foundation; either | 8 * License as published by the Free Software Foundation; either |
9 * version 2 of the License, or (at your option) any later version. | 9 * version 2 of the License, or (at your option) any later version. |
10 * | 10 * |
11 * This library is distributed in the hope that it will be useful, | 11 * This library is distributed in the hope that it will be useful, |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 * Library General Public License for more details. | 14 * Library General Public License for more details. |
15 * | 15 * |
16 * You should have received a copy of the GNU Library General Public License | 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 | 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, | 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
19 * Boston, MA 02110-1301, USA. | 19 * Boston, MA 02110-1301, USA. |
20 */ | 20 */ |
21 | 21 |
22 #include "core/css/CSSCursorImageValue.h" | 22 #include "core/css/CSSCursorImageValue.h" |
23 | 23 |
24 #include "core/SVGNames.h" | |
25 #include "core/css/CSSImageSetValue.h" | |
26 #include "core/fetch/ImageResourceContent.h" | |
27 #include "core/style/StyleFetchedImage.h" | |
28 #include "core/style/StyleFetchedImageSet.h" | |
29 #include "core/style/StyleImage.h" | |
30 #include "core/svg/SVGCursorElement.h" | |
31 #include "core/svg/SVGLengthContext.h" | |
32 #include "core/svg/SVGURIReference.h" | |
33 #include "wtf/MathExtras.h" | |
34 #include "wtf/text/StringBuilder.h" | 24 #include "wtf/text/StringBuilder.h" |
35 #include "wtf/text/WTFString.h" | 25 #include "wtf/text/WTFString.h" |
36 | 26 |
37 namespace blink { | 27 namespace blink { |
38 | 28 |
39 static inline SVGCursorElement* resourceReferencedByCursorElement( | 29 CSSCursorImageValue::CSSCursorImageValue(const CSSValue& imageValue, |
40 const String& url, | |
41 const TreeScope& treeScope) { | |
42 Element* element = | |
43 SVGURIReference::targetElementFromIRIString(url, treeScope); | |
44 return isSVGCursorElement(element) ? toSVGCursorElement(element) : nullptr; | |
45 } | |
46 | |
47 CSSCursorImageValue::CSSCursorImageValue(CSSValue* imageValue, | |
48 bool hotSpotSpecified, | 30 bool hotSpotSpecified, |
49 const IntPoint& hotSpot) | 31 const IntPoint& hotSpot) |
50 : CSSValue(CursorImageClass), | 32 : CSSValue(CursorImageClass), |
51 m_imageValue(imageValue), | 33 m_imageValue(&imageValue), |
52 m_hotSpotSpecified(hotSpotSpecified), | |
53 m_hotSpot(hotSpot), | 34 m_hotSpot(hotSpot), |
54 m_isCachePending(true) {} | 35 m_hotSpotSpecified(hotSpotSpecified) { |
| 36 DCHECK(imageValue.isImageValue() || imageValue.isImageSetValue()); |
| 37 } |
55 | 38 |
56 CSSCursorImageValue::~CSSCursorImageValue() {} | 39 CSSCursorImageValue::~CSSCursorImageValue() {} |
57 | 40 |
58 String CSSCursorImageValue::customCSSText() const { | 41 String CSSCursorImageValue::customCSSText() const { |
59 StringBuilder result; | 42 StringBuilder result; |
60 result.append(m_imageValue->cssText()); | 43 result.append(m_imageValue->cssText()); |
61 if (m_hotSpotSpecified) { | 44 if (m_hotSpotSpecified) { |
62 result.append(' '); | 45 result.append(' '); |
63 result.appendNumber(m_hotSpot.x()); | 46 result.appendNumber(m_hotSpot.x()); |
64 result.append(' '); | 47 result.append(' '); |
65 result.appendNumber(m_hotSpot.y()); | 48 result.appendNumber(m_hotSpot.y()); |
66 } | 49 } |
67 return result.toString(); | 50 return result.toString(); |
68 } | 51 } |
69 | 52 |
70 SVGCursorElement* CSSCursorImageValue::getSVGCursorElement( | |
71 Element* element) const { | |
72 if (!element || !element->isSVGElement()) | |
73 return nullptr; | |
74 | |
75 if (!hasFragmentInURL()) | |
76 return nullptr; | |
77 | |
78 String url = toCSSImageValue(m_imageValue.get())->url(); | |
79 return resourceReferencedByCursorElement(url, element->treeScope()); | |
80 } | |
81 | |
82 bool CSSCursorImageValue::isCachePending(float deviceScaleFactor) const { | |
83 // Need to delegate completely so that changes in device scale factor can be | |
84 // handled appropriately. | |
85 if (m_imageValue->isImageSetValue()) | |
86 return toCSSImageSetValue(*m_imageValue).isCachePending(deviceScaleFactor); | |
87 return m_isCachePending; | |
88 } | |
89 | |
90 StyleImage* CSSCursorImageValue::cachedImage(float deviceScaleFactor) const { | |
91 ASSERT(!isCachePending(deviceScaleFactor)); | |
92 | |
93 if (m_imageValue->isImageSetValue()) | |
94 return toCSSImageSetValue(*m_imageValue).cachedImage(deviceScaleFactor); | |
95 return m_cachedImage.get(); | |
96 } | |
97 | |
98 StyleImage* CSSCursorImageValue::cacheImage(const Document& document, | |
99 float deviceScaleFactor) { | |
100 if (m_imageValue->isImageSetValue()) | |
101 return toCSSImageSetValue(*m_imageValue) | |
102 .cacheImage(document, deviceScaleFactor); | |
103 | |
104 if (m_isCachePending) { | |
105 m_isCachePending = false; | |
106 | |
107 // For SVG images we need to lazily substitute in the correct URL. Rather | |
108 // than attempt to change the URL of the CSSImageValue (which would then | |
109 // change behavior like cssText), we create an alternate CSSImageValue to | |
110 // use. | |
111 if (hasFragmentInURL()) { | |
112 CSSImageValue* imageValue = toCSSImageValue(m_imageValue.get()); | |
113 // FIXME: This will fail if the <cursor> element is in a shadow DOM | |
114 // (http://crbug/59827) | |
115 if (SVGCursorElement* cursorElement = | |
116 resourceReferencedByCursorElement(imageValue->url(), document)) { | |
117 CSSImageValue* svgImageValue = | |
118 CSSImageValue::create(document.completeURL( | |
119 cursorElement->href()->currentValue()->value())); | |
120 svgImageValue->setReferrer(imageValue->referrer()); | |
121 m_cachedImage = svgImageValue->cacheImage(document); | |
122 return m_cachedImage.get(); | |
123 } | |
124 } | |
125 | |
126 if (m_imageValue->isImageValue()) | |
127 m_cachedImage = toCSSImageValue(*m_imageValue).cacheImage(document); | |
128 } | |
129 | |
130 if (m_cachedImage && m_cachedImage->isImageResource()) | |
131 return toStyleFetchedImage(m_cachedImage); | |
132 return nullptr; | |
133 } | |
134 | |
135 bool CSSCursorImageValue::hasFragmentInURL() const { | |
136 if (m_imageValue->isImageValue()) { | |
137 CSSImageValue* imageValue = toCSSImageValue(m_imageValue.get()); | |
138 KURL kurl(ParsedURLString, imageValue->url()); | |
139 return kurl.hasFragmentIdentifier(); | |
140 } | |
141 return false; | |
142 } | |
143 | |
144 String CSSCursorImageValue::cachedImageURL() const { | |
145 if (!m_cachedImage || !m_cachedImage->isImageResource()) | |
146 return String(); | |
147 return toStyleFetchedImage(m_cachedImage)->cachedImage()->url().getString(); | |
148 } | |
149 | |
150 void CSSCursorImageValue::clearImageResource() const { | |
151 m_cachedImage = nullptr; | |
152 m_isCachePending = true; | |
153 } | |
154 | |
155 bool CSSCursorImageValue::equals(const CSSCursorImageValue& other) const { | 53 bool CSSCursorImageValue::equals(const CSSCursorImageValue& other) const { |
156 return (m_hotSpotSpecified | 54 return (m_hotSpotSpecified |
157 ? other.m_hotSpotSpecified && m_hotSpot == other.m_hotSpot | 55 ? other.m_hotSpotSpecified && m_hotSpot == other.m_hotSpot |
158 : !other.m_hotSpotSpecified) && | 56 : !other.m_hotSpotSpecified) && |
159 compareCSSValuePtr(m_imageValue, other.m_imageValue); | 57 compareCSSValuePtr(m_imageValue, other.m_imageValue); |
160 } | 58 } |
161 | 59 |
162 DEFINE_TRACE_AFTER_DISPATCH(CSSCursorImageValue) { | 60 DEFINE_TRACE_AFTER_DISPATCH(CSSCursorImageValue) { |
163 visitor->trace(m_imageValue); | 61 visitor->trace(m_imageValue); |
164 visitor->trace(m_cachedImage); | |
165 CSSValue::traceAfterDispatch(visitor); | 62 CSSValue::traceAfterDispatch(visitor); |
166 } | 63 } |
167 | 64 |
168 } // namespace blink | 65 } // namespace blink |
OLD | NEW |