SVGStyledElement.cpp [plain text]
#include "config.h"
#if ENABLE(SVG)
#include "SVGStyledElement.h"
#include "Attr.h"
#include "CSSParser.h"
#include "Document.h"
#include "HTMLNames.h"
#include "PlatformString.h"
#include "RenderObject.h"
#include "RenderSVGResource.h"
#include "RenderSVGResourceClipper.h"
#include "RenderSVGResourceFilter.h"
#include "RenderSVGResourceMasker.h"
#include "SVGElement.h"
#include "SVGElementInstance.h"
#include "SVGElementRareData.h"
#include "SVGNames.h"
#include "SVGRenderStyle.h"
#include "SVGRenderSupport.h"
#include "SVGSVGElement.h"
#include "SVGUseElement.h"
#include <wtf/Assertions.h>
#include <wtf/HashMap.h>
namespace WebCore {
DEFINE_ANIMATED_STRING(SVGStyledElement, HTMLNames::classAttr, ClassName, className)
using namespace SVGNames;
void mapAttributeToCSSProperty(HashMap<AtomicStringImpl*, int>* propertyNameToIdMap, const QualifiedName& attrName)
{
int propertyId = cssPropertyID(attrName.localName());
ASSERT(propertyId > 0);
propertyNameToIdMap->set(attrName.localName().impl(), propertyId);
}
SVGStyledElement::SVGStyledElement(const QualifiedName& tagName, Document* document)
: SVGElement(tagName, document)
{
}
SVGStyledElement::~SVGStyledElement()
{
if (needsPendingResourceHandling() && hasPendingResources() && document())
document()->accessSVGExtensions()->removeElementFromPendingResources(this);
ASSERT(!hasPendingResources());
}
String SVGStyledElement::title() const
{
if (hasTagName(SVGNames::svgTag)) {
const SVGSVGElement* svg = static_cast<const SVGSVGElement*>(this);
if (svg->isOutermostSVG())
return String();
}
Node* parent = const_cast<SVGStyledElement*>(this);
while (parent) {
if (!parent->isSVGShadowRoot()) {
parent = parent->parentNodeGuaranteedHostFree();
continue;
}
Element* shadowParent = parent->svgShadowHost();
if (shadowParent && shadowParent->isSVGElement() && shadowParent->hasTagName(SVGNames::useTag)) {
SVGUseElement* useElement = static_cast<SVGUseElement*>(shadowParent);
String useTitle(useElement->title());
if (useTitle.isEmpty())
break;
return useTitle;
}
parent = parent->parentNode();
}
Element* titleElement = firstElementChild();
for (; titleElement; titleElement = titleElement->nextElementSibling()) {
if (titleElement->hasTagName(SVGNames::titleTag) && titleElement->isSVGElement())
break;
}
if (titleElement)
return titleElement->innerText();
return String();
}
bool SVGStyledElement::rendererIsNeeded(RenderStyle* style)
{
if (!parentNode() || parentNode()->isSVGElement())
return StyledElement::rendererIsNeeded(style);
return false;
}
int SVGStyledElement::cssPropertyIdForSVGAttributeName(const QualifiedName& attrName)
{
if (!attrName.namespaceURI().isNull())
return 0;
static HashMap<AtomicStringImpl*, int>* propertyNameToIdMap = 0;
if (!propertyNameToIdMap) {
propertyNameToIdMap = new HashMap<AtomicStringImpl*, int>;
mapAttributeToCSSProperty(propertyNameToIdMap, alignment_baselineAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, baseline_shiftAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, clipAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, clip_pathAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, clip_ruleAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, SVGNames::colorAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, color_interpolationAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, color_interpolation_filtersAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, color_profileAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, color_renderingAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, cursorAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, SVGNames::directionAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, displayAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, dominant_baselineAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, enable_backgroundAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, fillAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, fill_opacityAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, fill_ruleAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, filterAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, flood_colorAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, flood_opacityAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, font_familyAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, font_sizeAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, font_stretchAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, font_styleAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, font_variantAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, font_weightAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, glyph_orientation_horizontalAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, glyph_orientation_verticalAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, image_renderingAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, kerningAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, letter_spacingAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, lighting_colorAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, marker_endAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, marker_midAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, marker_startAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, maskAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, opacityAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, overflowAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, pointer_eventsAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, shape_renderingAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, stop_colorAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, stop_opacityAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, strokeAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, stroke_dasharrayAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, stroke_dashoffsetAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, stroke_linecapAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, stroke_linejoinAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, stroke_miterlimitAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, stroke_opacityAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, stroke_widthAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, text_anchorAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, text_decorationAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, text_renderingAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, unicode_bidiAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, vector_effectAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, visibilityAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, word_spacingAttr);
mapAttributeToCSSProperty(propertyNameToIdMap, writing_modeAttr);
}
return propertyNameToIdMap->get(attrName.localName().impl());
}
static inline AttributeToPropertyTypeMap& cssPropertyToTypeMap()
{
DEFINE_STATIC_LOCAL(AttributeToPropertyTypeMap, s_cssPropertyMap, ());
if (!s_cssPropertyMap.isEmpty())
return s_cssPropertyMap;
s_cssPropertyMap.set(alignment_baselineAttr, AnimatedString);
s_cssPropertyMap.set(baseline_shiftAttr, AnimatedString);
s_cssPropertyMap.set(clipAttr, AnimatedRect);
s_cssPropertyMap.set(clip_pathAttr, AnimatedString);
s_cssPropertyMap.set(clip_ruleAttr, AnimatedString);
s_cssPropertyMap.set(SVGNames::colorAttr, AnimatedColor);
s_cssPropertyMap.set(color_interpolationAttr, AnimatedString);
s_cssPropertyMap.set(color_interpolation_filtersAttr, AnimatedString);
s_cssPropertyMap.set(color_profileAttr, AnimatedString);
s_cssPropertyMap.set(color_renderingAttr, AnimatedString);
s_cssPropertyMap.set(cursorAttr, AnimatedString);
s_cssPropertyMap.set(displayAttr, AnimatedString);
s_cssPropertyMap.set(dominant_baselineAttr, AnimatedString);
s_cssPropertyMap.set(fillAttr, AnimatedColor);
s_cssPropertyMap.set(fill_opacityAttr, AnimatedNumber);
s_cssPropertyMap.set(fill_ruleAttr, AnimatedString);
s_cssPropertyMap.set(filterAttr, AnimatedString);
s_cssPropertyMap.set(flood_colorAttr, AnimatedColor);
s_cssPropertyMap.set(flood_opacityAttr, AnimatedNumber);
s_cssPropertyMap.set(font_familyAttr, AnimatedString);
s_cssPropertyMap.set(font_sizeAttr, AnimatedLength);
s_cssPropertyMap.set(font_stretchAttr, AnimatedString);
s_cssPropertyMap.set(font_styleAttr, AnimatedString);
s_cssPropertyMap.set(font_variantAttr, AnimatedString);
s_cssPropertyMap.set(font_weightAttr, AnimatedString);
s_cssPropertyMap.set(image_renderingAttr, AnimatedString);
s_cssPropertyMap.set(kerningAttr, AnimatedLength);
s_cssPropertyMap.set(letter_spacingAttr, AnimatedLength);
s_cssPropertyMap.set(lighting_colorAttr, AnimatedColor);
s_cssPropertyMap.set(marker_endAttr, AnimatedString);
s_cssPropertyMap.set(marker_midAttr, AnimatedString);
s_cssPropertyMap.set(marker_startAttr, AnimatedString);
s_cssPropertyMap.set(maskAttr, AnimatedString);
s_cssPropertyMap.set(opacityAttr, AnimatedNumber);
s_cssPropertyMap.set(overflowAttr, AnimatedString);
s_cssPropertyMap.set(pointer_eventsAttr, AnimatedString);
s_cssPropertyMap.set(shape_renderingAttr, AnimatedString);
s_cssPropertyMap.set(stop_colorAttr, AnimatedColor);
s_cssPropertyMap.set(stop_opacityAttr, AnimatedNumber);
s_cssPropertyMap.set(strokeAttr, AnimatedColor);
s_cssPropertyMap.set(stroke_dasharrayAttr, AnimatedLengthList);
s_cssPropertyMap.set(stroke_dashoffsetAttr, AnimatedLength);
s_cssPropertyMap.set(stroke_linecapAttr, AnimatedString);
s_cssPropertyMap.set(stroke_linejoinAttr, AnimatedString);
s_cssPropertyMap.set(stroke_miterlimitAttr, AnimatedNumber);
s_cssPropertyMap.set(stroke_opacityAttr, AnimatedNumber);
s_cssPropertyMap.set(stroke_widthAttr, AnimatedLength);
s_cssPropertyMap.set(text_anchorAttr, AnimatedString);
s_cssPropertyMap.set(text_decorationAttr, AnimatedString);
s_cssPropertyMap.set(text_renderingAttr, AnimatedString);
s_cssPropertyMap.set(vector_effectAttr, AnimatedString);
s_cssPropertyMap.set(visibilityAttr, AnimatedString);
s_cssPropertyMap.set(word_spacingAttr, AnimatedLength);
return s_cssPropertyMap;
}
AnimatedAttributeType SVGStyledElement::animatedPropertyTypeForCSSProperty(const QualifiedName& attrName)
{
AttributeToPropertyTypeMap& cssPropertyTypeMap = cssPropertyToTypeMap();
if (cssPropertyTypeMap.contains(attrName))
return cssPropertyTypeMap.get(attrName);
return AnimatedUnknown;
}
bool SVGStyledElement::isAnimatableCSSProperty(const QualifiedName& attrName)
{
return cssPropertyToTypeMap().contains(attrName);
}
void SVGStyledElement::fillPassedAttributeToPropertyTypeMap(AttributeToPropertyTypeMap& attributeToPropertyTypeMap)
{
attributeToPropertyTypeMap.set(HTMLNames::classAttr, AnimatedString);
}
bool SVGStyledElement::mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const
{
if (SVGStyledElement::cssPropertyIdForSVGAttributeName(attrName) > 0) {
result = eSVG;
return false;
}
return SVGElement::mapToEntry(attrName, result);
}
void SVGStyledElement::parseMappedAttribute(Attribute* attr)
{
const QualifiedName& attrName = attr->name();
int propId = SVGStyledElement::cssPropertyIdForSVGAttributeName(attrName);
if (propId > 0) {
addCSSProperty(attr, propId, attr->value());
setNeedsStyleRecalc();
return;
}
if (attrName.matches(HTMLNames::classAttr))
setClassNameBaseValue(attr->value());
else
SVGElement::parseMappedAttribute(attr);
}
bool SVGStyledElement::isKnownAttribute(const QualifiedName& attrName)
{
return isIdAttributeName(attrName);
}
void SVGStyledElement::svgAttributeChanged(const QualifiedName& attrName)
{
SVGElement::svgAttributeChanged(attrName);
if (attrName.matches(HTMLNames::classAttr))
classAttributeChanged(className());
RenderObject* object = renderer();
if (isIdAttributeName(attrName)) {
if (object && object->isSVGResourceContainer())
object->toRenderSVGResourceContainer()->idChanged();
}
SVGElementInstance::invalidateAllInstancesOfElement(this);
}
void SVGStyledElement::synchronizeProperty(const QualifiedName& attrName)
{
SVGElement::synchronizeProperty(attrName);
if (attrName == anyQName() || attrName.matches(HTMLNames::classAttr))
synchronizeClassName();
}
void SVGStyledElement::attach()
{
SVGElement::attach();
if (RenderObject* object = renderer())
object->updateFromElement();
}
void SVGStyledElement::insertedIntoDocument()
{
SVGElement::insertedIntoDocument();
updateRelativeLengthsInformation();
Document* document = this->document();
if (!needsPendingResourceHandling() || !document)
return;
SVGDocumentExtensions* extensions = document->accessSVGExtensions();
String resourceId = getIdAttribute();
if (!extensions->hasPendingResources(resourceId))
return;
OwnPtr<SVGDocumentExtensions::SVGPendingElements> clients(extensions->removePendingResource(resourceId));
ASSERT(!clients->isEmpty());
const SVGDocumentExtensions::SVGPendingElements::const_iterator end = clients->end();
for (SVGDocumentExtensions::SVGPendingElements::const_iterator it = clients->begin(); it != end; ++it) {
ASSERT((*it)->hasPendingResources());
(*it)->buildPendingResource();
(*it)->clearHasPendingResourcesIfPossible();
}
}
void SVGStyledElement::removedFromDocument()
{
updateRelativeLengthsInformation(false, this);
SVGElement::removedFromDocument();
Document* document = this->document();
if (!needsPendingResourceHandling() || !document)
return;
document->accessSVGExtensions()->removeElementFromPendingResources(this);
}
void SVGStyledElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
{
SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
if (!changedByParser)
SVGElementInstance::invalidateAllInstancesOfElement(this);
}
PassRefPtr<CSSValue> SVGStyledElement::getPresentationAttribute(const String& name)
{
if (!attributeMap())
return 0;
QualifiedName attributeName(nullAtom, name, nullAtom);
Attribute* attr = attributeMap()->getAttributeItem(attributeName);
if (!attr || !attr->isMappedAttribute() || !attr->style())
return 0;
Attribute* cssSVGAttr = attr;
MappedAttributeEntry entry;
mapToEntry(attributeName, entry);
if (getMappedAttributeDecl(entry, cssSVGAttr) == cssSVGAttr->decl()) {
cssSVGAttr->setDecl(0);
int propId = SVGStyledElement::cssPropertyIdForSVGAttributeName(cssSVGAttr->name());
addCSSProperty(cssSVGAttr, propId, cssSVGAttr->value());
}
return cssSVGAttr->style()->getPropertyCSSValue(name);
}
bool SVGStyledElement::instanceUpdatesBlocked() const
{
return hasRareSVGData() && rareSVGData()->instanceUpdatesBlocked();
}
void SVGStyledElement::setInstanceUpdatesBlocked(bool value)
{
if (hasRareSVGData())
rareSVGData()->setInstanceUpdatesBlocked(value);
}
bool SVGStyledElement::hasPendingResources() const
{
return hasRareSVGData() && rareSVGData()->hasPendingResources();
}
void SVGStyledElement::setHasPendingResources()
{
ensureRareSVGData()->setHasPendingResources(true);
}
void SVGStyledElement::clearHasPendingResourcesIfPossible()
{
if (!document()->accessSVGExtensions()->isElementInPendingResources(this))
ensureRareSVGData()->setHasPendingResources(false);
}
AffineTransform SVGStyledElement::localCoordinateSpaceTransform(SVGLocatable::CTMScope) const
{
ASSERT_NOT_REACHED();
return AffineTransform();
}
void SVGStyledElement::updateRelativeLengthsInformation(bool hasRelativeLengths, SVGStyledElement* element)
{
if (!inDocument())
return;
if (hasRelativeLengths)
m_elementsWithRelativeLengths.add(element);
else {
if (!m_elementsWithRelativeLengths.contains(element)) {
return;
}
m_elementsWithRelativeLengths.remove(element);
}
ContainerNode* node = parentNode();
while (node) {
if (!node->isSVGElement())
break;
SVGElement* element = static_cast<SVGElement*>(node);
if (!element->isStyled()) {
node = node->parentNode();
continue;
}
static_cast<SVGStyledElement*>(element)->updateRelativeLengthsInformation(hasRelativeLengths, this);
break;
}
}
}
#endif // ENABLE(SVG)