2 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2007, 2008 Rob Buis <buis@kde.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
24 #include "SVGStyledElement.h"
27 #include "CSSParser.h"
29 #include "EventNames.h"
30 #include "HTMLNames.h"
31 #include "RenderObject.h"
32 #include "RenderSVGResource.h"
33 #include "RenderSVGResourceClipper.h"
34 #include "RenderSVGResourceFilter.h"
35 #include "RenderSVGResourceMasker.h"
36 #include "SVGElement.h"
37 #include "SVGElementInstance.h"
38 #include "SVGElementRareData.h"
40 #include "SVGRenderStyle.h"
41 #include "SVGRenderSupport.h"
42 #include "SVGSVGElement.h"
43 #include "SVGUseElement.h"
44 #include "ShadowRoot.h"
45 #include <wtf/Assertions.h>
46 #include <wtf/HashMap.h>
47 #include <wtf/StdLibExtras.h>
48 #include <wtf/text/WTFString.h>
52 // Animated property definitions
53 DEFINE_ANIMATED_STRING(SVGStyledElement, HTMLNames::classAttr, ClassName, className)
55 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGStyledElement)
56 REGISTER_LOCAL_ANIMATED_PROPERTY(className)
57 END_REGISTER_ANIMATED_PROPERTIES
59 using namespace SVGNames;
61 void mapAttributeToCSSProperty(HashMap<AtomicStringImpl*, CSSPropertyID>* propertyNameToIdMap, const QualifiedName& attrName)
63 // FIXME: when CSS supports "transform-origin" the special case for transform_originAttr can be removed.
64 CSSPropertyID propertyId = cssPropertyID(attrName.localName());
65 if (!propertyId && attrName == transform_originAttr)
66 propertyId = CSSPropertyWebkitTransformOrigin; // cssPropertyID("-webkit-transform-origin")
67 ASSERT(propertyId > 0);
68 propertyNameToIdMap->set(attrName.localName().impl(), propertyId);
71 SVGStyledElement::SVGStyledElement(const QualifiedName& tagName, Document* document, ConstructionType constructionType)
72 : SVGElement(tagName, document, constructionType)
74 registerAnimatedPropertiesForSVGStyledElement();
77 String SVGStyledElement::title() const
79 // According to spec, we should not return titles when hovering over root <svg> elements (those
80 // <title> elements are the title of the document, not a tooltip) so we instantly return.
81 if (isOutermostSVGSVGElement())
84 // Walk up the tree, to find out whether we're inside a <use> shadow tree, to find the right title.
85 if (isInShadowTree()) {
86 Element* shadowHostElement = toShadowRoot(treeScope()->rootNode())->host();
87 // At this time, SVG nodes are not allowed in non-<use> shadow trees, so any shadow root we do
88 // have should be a use. The assert and following test is here to catch future shadow DOM changes
89 // that do enable SVG in a shadow tree.
90 ASSERT(!shadowHostElement || shadowHostElement->hasTagName(SVGNames::useTag));
91 if (shadowHostElement && shadowHostElement->hasTagName(SVGNames::useTag)) {
92 SVGUseElement* useElement = static_cast<SVGUseElement*>(shadowHostElement);
94 // If the <use> title is not empty we found the title to use.
95 String useTitle(useElement->title());
96 if (!useTitle.isEmpty())
101 // If we aren't an instance in a <use> or the <use> title was not found, then find the first
102 // <title> child of this element.
103 Element* titleElement = firstElementChild();
104 for (; titleElement; titleElement = titleElement->nextElementSibling()) {
105 if (titleElement->hasTagName(SVGNames::titleTag) && titleElement->isSVGElement())
109 // If a title child was found, return the text contents.
111 return titleElement->innerText();
113 // Otherwise return a null/empty string.
117 bool SVGStyledElement::rendererIsNeeded(const NodeRenderingContext& context)
119 // http://www.w3.org/TR/SVG/extend.html#PrivateData
120 // Prevent anything other than SVG renderers from appearing in our render tree
121 // Spec: SVG allows inclusion of elements from foreign namespaces anywhere
122 // with the SVG content. In general, the SVG user agent will include the unknown
123 // elements in the DOM but will otherwise ignore unknown elements.
124 if (!parentOrHostElement() || parentOrHostElement()->isSVGElement())
125 return StyledElement::rendererIsNeeded(context);
130 CSSPropertyID SVGStyledElement::cssPropertyIdForSVGAttributeName(const QualifiedName& attrName)
132 if (!attrName.namespaceURI().isNull())
133 return CSSPropertyInvalid;
135 static HashMap<AtomicStringImpl*, CSSPropertyID>* propertyNameToIdMap = 0;
136 if (!propertyNameToIdMap) {
137 propertyNameToIdMap = new HashMap<AtomicStringImpl*, CSSPropertyID>;
138 // This is a list of all base CSS and SVG CSS properties which are exposed as SVG XML attributes
139 mapAttributeToCSSProperty(propertyNameToIdMap, alignment_baselineAttr);
140 mapAttributeToCSSProperty(propertyNameToIdMap, baseline_shiftAttr);
141 mapAttributeToCSSProperty(propertyNameToIdMap, clipAttr);
142 mapAttributeToCSSProperty(propertyNameToIdMap, clip_pathAttr);
143 mapAttributeToCSSProperty(propertyNameToIdMap, clip_ruleAttr);
144 mapAttributeToCSSProperty(propertyNameToIdMap, SVGNames::colorAttr);
145 mapAttributeToCSSProperty(propertyNameToIdMap, color_interpolationAttr);
146 mapAttributeToCSSProperty(propertyNameToIdMap, color_interpolation_filtersAttr);
147 mapAttributeToCSSProperty(propertyNameToIdMap, color_profileAttr);
148 mapAttributeToCSSProperty(propertyNameToIdMap, color_renderingAttr);
149 mapAttributeToCSSProperty(propertyNameToIdMap, cursorAttr);
150 mapAttributeToCSSProperty(propertyNameToIdMap, SVGNames::directionAttr);
151 mapAttributeToCSSProperty(propertyNameToIdMap, displayAttr);
152 mapAttributeToCSSProperty(propertyNameToIdMap, dominant_baselineAttr);
153 mapAttributeToCSSProperty(propertyNameToIdMap, enable_backgroundAttr);
154 mapAttributeToCSSProperty(propertyNameToIdMap, fillAttr);
155 mapAttributeToCSSProperty(propertyNameToIdMap, fill_opacityAttr);
156 mapAttributeToCSSProperty(propertyNameToIdMap, fill_ruleAttr);
157 mapAttributeToCSSProperty(propertyNameToIdMap, filterAttr);
158 mapAttributeToCSSProperty(propertyNameToIdMap, flood_colorAttr);
159 mapAttributeToCSSProperty(propertyNameToIdMap, flood_opacityAttr);
160 mapAttributeToCSSProperty(propertyNameToIdMap, font_familyAttr);
161 mapAttributeToCSSProperty(propertyNameToIdMap, font_sizeAttr);
162 mapAttributeToCSSProperty(propertyNameToIdMap, font_stretchAttr);
163 mapAttributeToCSSProperty(propertyNameToIdMap, font_styleAttr);
164 mapAttributeToCSSProperty(propertyNameToIdMap, font_variantAttr);
165 mapAttributeToCSSProperty(propertyNameToIdMap, font_weightAttr);
166 mapAttributeToCSSProperty(propertyNameToIdMap, glyph_orientation_horizontalAttr);
167 mapAttributeToCSSProperty(propertyNameToIdMap, glyph_orientation_verticalAttr);
168 mapAttributeToCSSProperty(propertyNameToIdMap, image_renderingAttr);
169 mapAttributeToCSSProperty(propertyNameToIdMap, kerningAttr);
170 mapAttributeToCSSProperty(propertyNameToIdMap, letter_spacingAttr);
171 mapAttributeToCSSProperty(propertyNameToIdMap, lighting_colorAttr);
172 mapAttributeToCSSProperty(propertyNameToIdMap, marker_endAttr);
173 mapAttributeToCSSProperty(propertyNameToIdMap, marker_midAttr);
174 mapAttributeToCSSProperty(propertyNameToIdMap, marker_startAttr);
175 mapAttributeToCSSProperty(propertyNameToIdMap, maskAttr);
176 mapAttributeToCSSProperty(propertyNameToIdMap, mask_typeAttr);
177 mapAttributeToCSSProperty(propertyNameToIdMap, opacityAttr);
178 mapAttributeToCSSProperty(propertyNameToIdMap, overflowAttr);
179 mapAttributeToCSSProperty(propertyNameToIdMap, pointer_eventsAttr);
180 mapAttributeToCSSProperty(propertyNameToIdMap, shape_renderingAttr);
181 mapAttributeToCSSProperty(propertyNameToIdMap, stop_colorAttr);
182 mapAttributeToCSSProperty(propertyNameToIdMap, stop_opacityAttr);
183 mapAttributeToCSSProperty(propertyNameToIdMap, strokeAttr);
184 mapAttributeToCSSProperty(propertyNameToIdMap, stroke_dasharrayAttr);
185 mapAttributeToCSSProperty(propertyNameToIdMap, stroke_dashoffsetAttr);
186 mapAttributeToCSSProperty(propertyNameToIdMap, stroke_linecapAttr);
187 mapAttributeToCSSProperty(propertyNameToIdMap, stroke_linejoinAttr);
188 mapAttributeToCSSProperty(propertyNameToIdMap, stroke_miterlimitAttr);
189 mapAttributeToCSSProperty(propertyNameToIdMap, stroke_opacityAttr);
190 mapAttributeToCSSProperty(propertyNameToIdMap, stroke_widthAttr);
191 mapAttributeToCSSProperty(propertyNameToIdMap, text_anchorAttr);
192 mapAttributeToCSSProperty(propertyNameToIdMap, text_decorationAttr);
193 mapAttributeToCSSProperty(propertyNameToIdMap, text_renderingAttr);
194 mapAttributeToCSSProperty(propertyNameToIdMap, transform_originAttr);
195 mapAttributeToCSSProperty(propertyNameToIdMap, unicode_bidiAttr);
196 mapAttributeToCSSProperty(propertyNameToIdMap, vector_effectAttr);
197 mapAttributeToCSSProperty(propertyNameToIdMap, visibilityAttr);
198 mapAttributeToCSSProperty(propertyNameToIdMap, word_spacingAttr);
199 mapAttributeToCSSProperty(propertyNameToIdMap, writing_modeAttr);
202 return propertyNameToIdMap->get(attrName.localName().impl());
205 typedef HashMap<QualifiedName, AnimatedPropertyType> AttributeToPropertyTypeMap;
206 static inline AttributeToPropertyTypeMap& cssPropertyToTypeMap()
208 DEFINE_STATIC_LOCAL(AttributeToPropertyTypeMap, s_cssPropertyMap, ());
210 if (!s_cssPropertyMap.isEmpty())
211 return s_cssPropertyMap;
213 // Fill the map for the first use.
214 s_cssPropertyMap.set(alignment_baselineAttr, AnimatedString);
215 s_cssPropertyMap.set(baseline_shiftAttr, AnimatedString);
216 s_cssPropertyMap.set(clipAttr, AnimatedRect);
217 s_cssPropertyMap.set(clip_pathAttr, AnimatedString);
218 s_cssPropertyMap.set(clip_ruleAttr, AnimatedString);
219 s_cssPropertyMap.set(SVGNames::colorAttr, AnimatedColor);
220 s_cssPropertyMap.set(color_interpolationAttr, AnimatedString);
221 s_cssPropertyMap.set(color_interpolation_filtersAttr, AnimatedString);
222 s_cssPropertyMap.set(color_profileAttr, AnimatedString);
223 s_cssPropertyMap.set(color_renderingAttr, AnimatedString);
224 s_cssPropertyMap.set(cursorAttr, AnimatedString);
225 s_cssPropertyMap.set(displayAttr, AnimatedString);
226 s_cssPropertyMap.set(dominant_baselineAttr, AnimatedString);
227 s_cssPropertyMap.set(fillAttr, AnimatedColor);
228 s_cssPropertyMap.set(fill_opacityAttr, AnimatedNumber);
229 s_cssPropertyMap.set(fill_ruleAttr, AnimatedString);
230 s_cssPropertyMap.set(filterAttr, AnimatedString);
231 s_cssPropertyMap.set(flood_colorAttr, AnimatedColor);
232 s_cssPropertyMap.set(flood_opacityAttr, AnimatedNumber);
233 s_cssPropertyMap.set(font_familyAttr, AnimatedString);
234 s_cssPropertyMap.set(font_sizeAttr, AnimatedLength);
235 s_cssPropertyMap.set(font_stretchAttr, AnimatedString);
236 s_cssPropertyMap.set(font_styleAttr, AnimatedString);
237 s_cssPropertyMap.set(font_variantAttr, AnimatedString);
238 s_cssPropertyMap.set(font_weightAttr, AnimatedString);
239 s_cssPropertyMap.set(image_renderingAttr, AnimatedString);
240 s_cssPropertyMap.set(kerningAttr, AnimatedLength);
241 s_cssPropertyMap.set(letter_spacingAttr, AnimatedLength);
242 s_cssPropertyMap.set(lighting_colorAttr, AnimatedColor);
243 s_cssPropertyMap.set(marker_endAttr, AnimatedString);
244 s_cssPropertyMap.set(marker_midAttr, AnimatedString);
245 s_cssPropertyMap.set(marker_startAttr, AnimatedString);
246 s_cssPropertyMap.set(maskAttr, AnimatedString);
247 s_cssPropertyMap.set(mask_typeAttr, AnimatedString);
248 s_cssPropertyMap.set(opacityAttr, AnimatedNumber);
249 s_cssPropertyMap.set(overflowAttr, AnimatedString);
250 s_cssPropertyMap.set(pointer_eventsAttr, AnimatedString);
251 s_cssPropertyMap.set(shape_renderingAttr, AnimatedString);
252 s_cssPropertyMap.set(stop_colorAttr, AnimatedColor);
253 s_cssPropertyMap.set(stop_opacityAttr, AnimatedNumber);
254 s_cssPropertyMap.set(strokeAttr, AnimatedColor);
255 s_cssPropertyMap.set(stroke_dasharrayAttr, AnimatedLengthList);
256 s_cssPropertyMap.set(stroke_dashoffsetAttr, AnimatedLength);
257 s_cssPropertyMap.set(stroke_linecapAttr, AnimatedString);
258 s_cssPropertyMap.set(stroke_linejoinAttr, AnimatedString);
259 s_cssPropertyMap.set(stroke_miterlimitAttr, AnimatedNumber);
260 s_cssPropertyMap.set(stroke_opacityAttr, AnimatedNumber);
261 s_cssPropertyMap.set(stroke_widthAttr, AnimatedLength);
262 s_cssPropertyMap.set(text_anchorAttr, AnimatedString);
263 s_cssPropertyMap.set(text_decorationAttr, AnimatedString);
264 s_cssPropertyMap.set(text_renderingAttr, AnimatedString);
265 s_cssPropertyMap.set(vector_effectAttr, AnimatedString);
266 s_cssPropertyMap.set(visibilityAttr, AnimatedString);
267 s_cssPropertyMap.set(word_spacingAttr, AnimatedLength);
268 return s_cssPropertyMap;
271 void SVGStyledElement::animatedPropertyTypeForAttribute(const QualifiedName& attrName, Vector<AnimatedPropertyType>& propertyTypes)
273 SVGElement::animatedPropertyTypeForAttribute(attrName, propertyTypes);
274 if (!propertyTypes.isEmpty())
277 AttributeToPropertyTypeMap& cssPropertyTypeMap = cssPropertyToTypeMap();
278 if (cssPropertyTypeMap.contains(attrName))
279 propertyTypes.append(cssPropertyTypeMap.get(attrName));
282 bool SVGStyledElement::isAnimatableCSSProperty(const QualifiedName& attrName)
284 return cssPropertyToTypeMap().contains(attrName);
287 bool SVGStyledElement::isPresentationAttribute(const QualifiedName& name) const
289 if (SVGStyledElement::cssPropertyIdForSVGAttributeName(name) > 0)
291 return SVGElement::isPresentationAttribute(name);
294 void SVGStyledElement::collectStyleForAttribute(const Attribute& attribute, StylePropertySet* style)
296 CSSPropertyID propertyID = SVGStyledElement::cssPropertyIdForSVGAttributeName(attribute.name());
298 addPropertyToAttributeStyle(style, propertyID, attribute.value());
301 void SVGStyledElement::parseAttribute(const Attribute& attribute)
303 // SVG animation has currently requires special storage of values so we set
304 // the className here. svgAttributeChanged actually causes the resulting
305 // style updates (instead of StyledElement::parseAttribute). We don't
306 // tell StyledElement about the change to avoid parsing the class list twice
307 if (attribute.name() == HTMLNames::classAttr) {
308 setClassNameBaseValue(attribute.value());
312 // id is handled by StyledElement which SVGElement inherits from
313 SVGElement::parseAttribute(attribute);
316 bool SVGStyledElement::isKnownAttribute(const QualifiedName& attrName)
318 return isIdAttributeName(attrName);
321 void SVGStyledElement::svgAttributeChanged(const QualifiedName& attrName)
323 CSSPropertyID propId = SVGStyledElement::cssPropertyIdForSVGAttributeName(attrName);
325 SVGElementInstance::invalidateAllInstancesOfElement(this);
329 if (attrName == HTMLNames::classAttr) {
330 classAttributeChanged(className());
331 SVGElementInstance::invalidateAllInstancesOfElement(this);
335 if (isIdAttributeName(attrName)) {
336 RenderObject* object = renderer();
337 // Notify resources about id changes, this is important as we cache resources by id in SVGDocumentExtensions
338 if (object && object->isSVGResourceContainer())
339 object->toRenderSVGResourceContainer()->idChanged();
341 buildPendingResourcesIfNeeded();
342 SVGElementInstance::invalidateAllInstancesOfElement(this);
347 Node::InsertionNotificationRequest SVGStyledElement::insertedInto(ContainerNode* rootParent)
349 SVGElement::insertedInto(rootParent);
350 updateRelativeLengthsInformation();
351 buildPendingResourcesIfNeeded();
352 return InsertionDone;
355 void SVGStyledElement::buildPendingResourcesIfNeeded()
357 Document* document = this->document();
358 if (!needsPendingResourceHandling() || !document)
361 SVGDocumentExtensions* extensions = document->accessSVGExtensions();
362 String resourceId = getIdAttribute();
363 if (!extensions->hasPendingResource(resourceId))
366 // Mark pending resources as pending for removal.
367 extensions->markPendingResourcesForRemoval(resourceId);
369 // Rebuild pending resources for each client of a pending resource that is being removed.
370 while (SVGElement* clientElement = extensions->removeElementFromPendingResourcesForRemoval(resourceId)) {
371 ASSERT(clientElement->hasPendingResources());
372 if (clientElement->hasPendingResources()) {
373 clientElement->buildPendingResource();
374 clientElement->clearHasPendingResourcesIfPossible();
379 void SVGStyledElement::removedFrom(ContainerNode* rootParent)
381 if (rootParent->inDocument())
382 updateRelativeLengthsInformation(false, this);
383 SVGElement::removedFrom(rootParent);
384 SVGElementInstance::invalidateAllInstancesOfElement(this);
387 void SVGStyledElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
389 SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
391 // Invalidate all SVGElementInstances associated with us
392 if (!changedByParser)
393 SVGElementInstance::invalidateAllInstancesOfElement(this);
396 PassRefPtr<CSSValue> SVGStyledElement::getPresentationAttribute(const String& name)
398 if (!hasAttributesWithoutUpdate())
401 QualifiedName attributeName(nullAtom, name, nullAtom);
402 Attribute* attr = getAttributeItem(attributeName);
406 RefPtr<StylePropertySet> style = StylePropertySet::create(SVGAttributeMode);
407 CSSPropertyID propertyID = SVGStyledElement::cssPropertyIdForSVGAttributeName(attr->name());
408 style->setProperty(propertyID, attr->value());
409 RefPtr<CSSValue> cssValue = style->getPropertyCSSValue(propertyID);
410 return cssValue ? cssValue->cloneForCSSOM() : 0;
413 bool SVGStyledElement::instanceUpdatesBlocked() const
415 return hasSVGRareData() && svgRareData()->instanceUpdatesBlocked();
418 void SVGStyledElement::setInstanceUpdatesBlocked(bool value)
420 if (hasSVGRareData())
421 svgRareData()->setInstanceUpdatesBlocked(value);
424 AffineTransform SVGStyledElement::localCoordinateSpaceTransform(SVGLocatable::CTMScope) const
426 // To be overriden by SVGStyledLocatableElement/SVGStyledTransformableElement (or as special case SVGTextElement and SVGPatternElement)
427 return AffineTransform();
430 void SVGStyledElement::updateRelativeLengthsInformation(bool hasRelativeLengths, SVGStyledElement* element)
432 // If we're not yet in a document, this function will be called again from insertedInto(). Do nothing now.
436 // An element wants to notify us that its own relative lengths state changed.
437 // Register it in the relative length map, and register us in the parent relative length map.
438 // Register the parent in the grandparents map, etc. Repeat procedure until the root of the SVG tree.
440 if (hasRelativeLengths)
441 m_elementsWithRelativeLengths.add(element);
443 if (!m_elementsWithRelativeLengths.contains(element)) {
444 // We were never registered. Do nothing.
448 m_elementsWithRelativeLengths.remove(element);
451 // Find first styled parent node, and notify it that we've changed our relative length state.
452 ContainerNode* node = parentNode();
454 if (!node->isSVGElement())
457 SVGElement* element = static_cast<SVGElement*>(node);
458 if (!element->isStyled()) {
459 node = node->parentNode();
463 // Register us in the parent element map.
464 static_cast<SVGStyledElement*>(element)->updateRelativeLengthsInformation(hasRelativeLengths, this);
469 bool SVGStyledElement::isMouseFocusable() const
473 Element* eventTarget = const_cast<SVGStyledElement *>(this);
474 return eventTarget->hasEventListeners(eventNames().focusinEvent) || eventTarget->hasEventListeners(eventNames().focusoutEvent);
477 bool SVGStyledElement::isKeyboardFocusable(KeyboardEvent*) const
479 return isMouseFocusable();
484 #endif // ENABLE(SVG)