1/*
2 * Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4 * Copyright (C) 2005 Alexander Kellett <lypanov@kde.org>
5 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
6 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
7 * Copyright (C) 2014 Adobe Systems Incorporated. All rights reserved.
8 * Copyright (C) 2018-2019 Apple Inc. All rights reserved.
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
19 *
20 * You should have received a copy of the GNU Library General Public License
21 * along with this library; see the file COPYING.LIB. If not, write to
22 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
24 */
25
26#include "config.h"
27#include "SVGMaskElement.h"
28
29#include "RenderSVGResourceMasker.h"
30#include "SVGNames.h"
31#include "SVGRenderSupport.h"
32#include "SVGStringList.h"
33#include "SVGUnitTypes.h"
34#include "StyleResolver.h"
35#include <wtf/IsoMallocInlines.h>
36#include <wtf/NeverDestroyed.h>
37
38namespace WebCore {
39
40WTF_MAKE_ISO_ALLOCATED_IMPL(SVGMaskElement);
41
42inline SVGMaskElement::SVGMaskElement(const QualifiedName& tagName, Document& document)
43 : SVGElement(tagName, document)
44 , SVGExternalResourcesRequired(this)
45 , SVGTests(this)
46{
47 // Spec: If the x/y attribute is not specified, the effect is as if a value of "-10%" were specified.
48 // Spec: If the width/height attribute is not specified, the effect is as if a value of "120%" were specified.
49 ASSERT(hasTagName(SVGNames::maskTag));
50
51 static std::once_flag onceFlag;
52 std::call_once(onceFlag, [] {
53 PropertyRegistry::registerProperty<SVGNames::xAttr, &SVGMaskElement::m_x>();
54 PropertyRegistry::registerProperty<SVGNames::yAttr, &SVGMaskElement::m_y>();
55 PropertyRegistry::registerProperty<SVGNames::widthAttr, &SVGMaskElement::m_width>();
56 PropertyRegistry::registerProperty<SVGNames::heightAttr, &SVGMaskElement::m_height>();
57 PropertyRegistry::registerProperty<SVGNames::maskUnitsAttr, SVGUnitTypes::SVGUnitType, &SVGMaskElement::m_maskUnits>();
58 PropertyRegistry::registerProperty<SVGNames::maskContentUnitsAttr, SVGUnitTypes::SVGUnitType, &SVGMaskElement::m_maskContentUnits>();
59 });
60}
61
62Ref<SVGMaskElement> SVGMaskElement::create(const QualifiedName& tagName, Document& document)
63{
64 return adoptRef(*new SVGMaskElement(tagName, document));
65}
66
67void SVGMaskElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
68{
69 if (name == SVGNames::maskUnitsAttr) {
70 auto propertyValue = SVGPropertyTraits<SVGUnitTypes::SVGUnitType>::fromString(value);
71 if (propertyValue > 0)
72 m_maskUnits->setBaseValInternal<SVGUnitTypes::SVGUnitType>(propertyValue);
73 return;
74 }
75 if (name == SVGNames::maskContentUnitsAttr) {
76 auto propertyValue = SVGPropertyTraits<SVGUnitTypes::SVGUnitType>::fromString(value);
77 if (propertyValue > 0)
78 m_maskContentUnits->setBaseValInternal<SVGUnitTypes::SVGUnitType>(propertyValue);
79 return;
80 }
81
82 SVGParsingError parseError = NoError;
83
84 if (name == SVGNames::xAttr)
85 m_x->setBaseValInternal(SVGLengthValue::construct(LengthModeWidth, value, parseError));
86 else if (name == SVGNames::yAttr)
87 m_y->setBaseValInternal(SVGLengthValue::construct(LengthModeHeight, value, parseError));
88 else if (name == SVGNames::widthAttr)
89 m_width->setBaseValInternal(SVGLengthValue::construct(LengthModeWidth, value, parseError));
90 else if (name == SVGNames::heightAttr)
91 m_height->setBaseValInternal(SVGLengthValue::construct(LengthModeHeight, value, parseError));
92
93 reportAttributeParsingError(parseError, name, value);
94
95 SVGElement::parseAttribute(name, value);
96 SVGTests::parseAttribute(name, value);
97 SVGExternalResourcesRequired::parseAttribute(name, value);
98}
99
100void SVGMaskElement::svgAttributeChanged(const QualifiedName& attrName)
101{
102 if (PropertyRegistry::isAnimatedLengthAttribute(attrName)) {
103 InstanceInvalidationGuard guard(*this);
104 invalidateSVGPresentationAttributeStyle();
105 return;
106 }
107
108 if (PropertyRegistry::isKnownAttribute(attrName)) {
109 if (auto* renderer = this->renderer())
110 renderer->setNeedsLayout();
111 return;
112 }
113
114 SVGElement::svgAttributeChanged(attrName);
115 SVGExternalResourcesRequired::svgAttributeChanged(attrName);
116}
117
118void SVGMaskElement::childrenChanged(const ChildChange& change)
119{
120 SVGElement::childrenChanged(change);
121
122 if (change.source == ChildChangeSource::Parser)
123 return;
124
125 if (RenderObject* object = renderer())
126 object->setNeedsLayout();
127}
128
129RenderPtr<RenderElement> SVGMaskElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&)
130{
131 return createRenderer<RenderSVGResourceMasker>(*this, WTFMove(style));
132}
133
134}
135