Improve use of NeverDestroyed
[WebKit-https.git] / Source / WebCore / svg / SVGMaskElement.cpp
index 5cc19e1..d44988e 100644 (file)
@@ -4,6 +4,7 @@
  * Copyright (C) 2005 Alexander Kellett <lypanov@kde.org>
  * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
  * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
+ * Copyright (C) 2014 Adobe Systems Incorporated. All rights reserved.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
  */
 
 #include "config.h"
-
-#if ENABLE(SVG)
 #include "SVGMaskElement.h"
 
-#include "Attribute.h"
-#include "CSSStyleSelector.h"
 #include "RenderSVGResourceMasker.h"
-#include "SVGElementInstance.h"
 #include "SVGNames.h"
 #include "SVGRenderSupport.h"
+#include "SVGStringList.h"
 #include "SVGUnitTypes.h"
+#include "StyleResolver.h"
+#include <wtf/NeverDestroyed.h>
 
 namespace WebCore {
 
@@ -53,12 +52,12 @@ BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGMaskElement)
     REGISTER_LOCAL_ANIMATED_PROPERTY(width)
     REGISTER_LOCAL_ANIMATED_PROPERTY(height)
     REGISTER_LOCAL_ANIMATED_PROPERTY(externalResourcesRequired)
-    REGISTER_PARENT_ANIMATED_PROPERTIES(SVGStyledLocatableElement)
+    REGISTER_PARENT_ANIMATED_PROPERTIES(SVGElement)
     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGTests)
 END_REGISTER_ANIMATED_PROPERTIES
 
-inline SVGMaskElement::SVGMaskElement(const QualifiedName& tagName, Document* document)
-    : SVGStyledLocatableElement(tagName, document)
+inline SVGMaskElement::SVGMaskElement(const QualifiedName& tagName, Document& document)
+    : SVGElement(tagName, document)
     , m_maskUnits(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
     , m_maskContentUnits(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE)
     , m_x(LengthModeWidth, "-10%")
@@ -72,139 +71,115 @@ inline SVGMaskElement::SVGMaskElement(const QualifiedName& tagName, Document* do
     registerAnimatedPropertiesForSVGMaskElement();
 }
 
-PassRefPtr<SVGMaskElement> SVGMaskElement::create(const QualifiedName& tagName, Document* document)
+Ref<SVGMaskElement> SVGMaskElement::create(const QualifiedName& tagName, Document& document)
 {
-    return adoptRef(new SVGMaskElement(tagName, document));
+    return adoptRef(*new SVGMaskElement(tagName, document));
 }
 
 bool SVGMaskElement::isSupportedAttribute(const QualifiedName& attrName)
 {
-    DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
-    if (supportedAttributes.isEmpty()) {
-        SVGTests::addSupportedAttributes(supportedAttributes);
-        SVGLangSpace::addSupportedAttributes(supportedAttributes);
-        SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
-        supportedAttributes.add(SVGNames::maskUnitsAttr);
-        supportedAttributes.add(SVGNames::maskContentUnitsAttr);
-        supportedAttributes.add(SVGNames::xAttr);
-        supportedAttributes.add(SVGNames::yAttr);
-        supportedAttributes.add(SVGNames::widthAttr);
-        supportedAttributes.add(SVGNames::heightAttr);
-    }
-    return supportedAttributes.contains(attrName);
+    static const auto supportedAttributes = makeNeverDestroyed([] {
+        HashSet<QualifiedName> set;
+        SVGTests::addSupportedAttributes(set);
+        SVGLangSpace::addSupportedAttributes(set);
+        SVGExternalResourcesRequired::addSupportedAttributes(set);
+        set.add({
+            SVGNames::maskUnitsAttr,
+            SVGNames::maskContentUnitsAttr,
+            SVGNames::refYAttr,
+            SVGNames::xAttr,
+            SVGNames::yAttr,
+            SVGNames::widthAttr,
+            SVGNames::heightAttr,
+        });
+        return set;
+    }());
+    return supportedAttributes.get().contains<SVGAttributeHashTranslator>(attrName);
 }
 
-void SVGMaskElement::parseMappedAttribute(Attribute* attr)
+void SVGMaskElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
 {
-    if (!isSupportedAttribute(attr->name())) {
-        SVGStyledElement::parseMappedAttribute(attr);
-        return;
-    }
-
-    if (attr->name() == SVGNames::maskUnitsAttr) {
-        SVGUnitTypes::SVGUnitType propertyValue = SVGPropertyTraits<SVGUnitTypes::SVGUnitType>::fromString(attr->value());
+    if (name == SVGNames::maskUnitsAttr) {
+        auto propertyValue = SVGPropertyTraits<SVGUnitTypes::SVGUnitType>::fromString(value);
         if (propertyValue > 0)
             setMaskUnitsBaseValue(propertyValue);
         return;
     }
-
-    if (attr->name() == SVGNames::maskContentUnitsAttr) {
-        SVGUnitTypes::SVGUnitType propertyValue = SVGPropertyTraits<SVGUnitTypes::SVGUnitType>::fromString(attr->value());
+    if (name == SVGNames::maskContentUnitsAttr) {
+        auto propertyValue = SVGPropertyTraits<SVGUnitTypes::SVGUnitType>::fromString(value);
         if (propertyValue > 0)
             setMaskContentUnitsBaseValue(propertyValue);
         return;
     }
 
-    if (attr->name() == SVGNames::xAttr) {
-        setXBaseValue(SVGLength(LengthModeWidth, attr->value()));
-        return;
-    }
+    SVGParsingError parseError = NoError;
 
-    if (attr->name() == SVGNames::yAttr) {
-        setYBaseValue(SVGLength(LengthModeHeight, attr->value()));
-        return;
-    }
+    if (name == SVGNames::xAttr)
+        setXBaseValue(SVGLengthValue::construct(LengthModeWidth, value, parseError));
+    else if (name == SVGNames::yAttr)
+        setYBaseValue(SVGLengthValue::construct(LengthModeHeight, value, parseError));
+    else if (name == SVGNames::widthAttr)
+        setWidthBaseValue(SVGLengthValue::construct(LengthModeWidth, value, parseError));
+    else if (name == SVGNames::heightAttr)
+        setHeightBaseValue(SVGLengthValue::construct(LengthModeHeight, value, parseError));
 
-    if (attr->name() == SVGNames::widthAttr) {
-        setWidthBaseValue(SVGLength(LengthModeWidth, attr->value()));
-        return;
-    }
+    reportAttributeParsingError(parseError, name, value);
 
-    if (attr->name() == SVGNames::heightAttr) {
-        setHeightBaseValue(SVGLength(LengthModeHeight, attr->value()));
-        return;
-    }
-
-    if (SVGTests::parseMappedAttribute(attr))
-        return;
-    if (SVGLangSpace::parseMappedAttribute(attr))
-        return;
-    if (SVGExternalResourcesRequired::parseMappedAttribute(attr))
-        return;
-
-    ASSERT_NOT_REACHED();
+    SVGElement::parseAttribute(name, value);
+    SVGTests::parseAttribute(name, value);
+    SVGExternalResourcesRequired::parseAttribute(name, value);
 }
 
 void SVGMaskElement::svgAttributeChanged(const QualifiedName& attrName)
 {
     if (!isSupportedAttribute(attrName)) {
-        SVGStyledElement::svgAttributeChanged(attrName);
+        SVGElement::svgAttributeChanged(attrName);
         return;
     }
 
-    SVGElementInstance::InvalidationGuard invalidationGuard(this);
-    
+    InstanceInvalidationGuard guard(*this);
+
     if (attrName == SVGNames::xAttr
         || attrName == SVGNames::yAttr
         || attrName == SVGNames::widthAttr
-        || attrName == SVGNames::heightAttr)
-        updateRelativeLengthsInformation();
+        || attrName == SVGNames::heightAttr) {
+        invalidateSVGPresentationAttributeStyle();
+        return;
+    }
 
     if (RenderObject* object = renderer())
-        object->setNeedsLayout(true);
+        object->setNeedsLayout();
 }
 
-void SVGMaskElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
+void SVGMaskElement::childrenChanged(const ChildChange& change)
 {
-    SVGStyledElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
+    SVGElement::childrenChanged(change);
 
-    if (changedByParser)
+    if (change.source == ChildChangeSourceParser)
         return;
 
     if (RenderObject* object = renderer())
-        object->setNeedsLayout(true);
+        object->setNeedsLayout();
 }
 
-FloatRect SVGMaskElement::maskBoundingBox(const FloatRect& objectBoundingBox) const
+RenderPtr<RenderElement> SVGMaskElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&)
 {
-    FloatRect maskBBox;
-    if (maskUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
-        maskBBox = FloatRect(x().valueAsPercentage() * objectBoundingBox.width() + objectBoundingBox.x(),
-                             y().valueAsPercentage() * objectBoundingBox.height() + objectBoundingBox.y(),
-                             width().valueAsPercentage() * objectBoundingBox.width(),
-                             height().valueAsPercentage() * objectBoundingBox.height());
-    else
-        maskBBox = FloatRect(x().value(this),
-                             y().value(this),
-                             width().value(this),
-                             height().value(this));
-
-    return maskBBox;
+    return createRenderer<RenderSVGResourceMasker>(*this, WTFMove(style));
 }
 
-RenderObject* SVGMaskElement::createRenderer(RenderArena* arena, RenderStyle*)
+Ref<SVGStringList> SVGMaskElement::requiredFeatures()
 {
-    return new (arena) RenderSVGResourceMasker(this);
+    return SVGTests::requiredFeatures(*this);
 }
 
-bool SVGMaskElement::selfHasRelativeLengths() const
-{
-    return x().isRelative()
-        || y().isRelative()
-        || width().isRelative()
-        || height().isRelative();
+Ref<SVGStringList> SVGMaskElement::requiredExtensions()
+{ 
+    return SVGTests::requiredExtensions(*this);
 }
 
+Ref<SVGStringList> SVGMaskElement::systemLanguage()
+{
+    return SVGTests::systemLanguage(*this);
 }
 
-#endif // ENABLE(SVG)
+}