1/*
2 * Copyright (C) 2004, 2005, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4 *
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.
9 *
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.
14 *
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.
19 */
20
21#include "config.h"
22#include "SVGStopElement.h"
23
24#include "Attribute.h"
25#include "Document.h"
26#include "RenderSVGGradientStop.h"
27#include "RenderSVGResource.h"
28#include "SVGElementInstance.h"
29#include "SVGGradientElement.h"
30#include "SVGNames.h"
31#include <wtf/NeverDestroyed.h>
32
33namespace WebCore {
34
35// Animated property definitions
36DEFINE_ANIMATED_NUMBER(SVGStopElement, SVGNames::offsetAttr, Offset, offset)
37
38BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGStopElement)
39    REGISTER_LOCAL_ANIMATED_PROPERTY(offset)
40    REGISTER_PARENT_ANIMATED_PROPERTIES(SVGElement)
41END_REGISTER_ANIMATED_PROPERTIES
42
43inline SVGStopElement::SVGStopElement(const QualifiedName& tagName, Document& document)
44    : SVGElement(tagName, document)
45    , m_offset(0)
46{
47    ASSERT(hasTagName(SVGNames::stopTag));
48    registerAnimatedPropertiesForSVGStopElement();
49}
50
51PassRefPtr<SVGStopElement> SVGStopElement::create(const QualifiedName& tagName, Document& document)
52{
53    return adoptRef(new SVGStopElement(tagName, document));
54}
55
56bool SVGStopElement::isSupportedAttribute(const QualifiedName& attrName)
57{
58    static NeverDestroyed<HashSet<QualifiedName>> supportedAttributes;
59    if (supportedAttributes.get().isEmpty())
60        supportedAttributes.get().add(SVGNames::offsetAttr);
61    return supportedAttributes.get().contains<SVGAttributeHashTranslator>(attrName);
62}
63
64void SVGStopElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
65{
66    if (!isSupportedAttribute(name)) {
67        SVGElement::parseAttribute(name, value);
68        return;
69    }
70
71    if (name == SVGNames::offsetAttr) {
72        if (value.endsWith('%'))
73            setOffsetBaseValue(value.string().left(value.length() - 1).toFloat() / 100.0f);
74        else
75            setOffsetBaseValue(value.toFloat());
76        return;
77    }
78
79    ASSERT_NOT_REACHED();
80}
81
82void SVGStopElement::svgAttributeChanged(const QualifiedName& attrName)
83{
84    if (!isSupportedAttribute(attrName)) {
85        SVGElement::svgAttributeChanged(attrName);
86        return;
87    }
88
89    SVGElementInstance::InvalidationGuard invalidationGuard(this);
90
91    if (attrName == SVGNames::offsetAttr) {
92        if (auto renderer = this->renderer())
93            RenderSVGResource::markForLayoutAndParentResourceInvalidation(*renderer);
94        return;
95    }
96
97    ASSERT_NOT_REACHED();
98}
99
100RenderPtr<RenderElement> SVGStopElement::createElementRenderer(PassRef<RenderStyle> style)
101{
102    return createRenderer<RenderSVGGradientStop>(*this, WTF::move(style));
103}
104
105bool SVGStopElement::rendererIsNeeded(const RenderStyle&)
106{
107    return true;
108}
109
110Color SVGStopElement::stopColorIncludingOpacity() const
111{
112    RenderStyle* style = renderer() ? &renderer()->style() : nullptr;
113    // FIXME: This check for null style exists to address Bug WK 90814, a rare crash condition in
114    // which the renderer or style is null. This entire class is scheduled for removal (Bug WK 86941)
115    // and we will tolerate this null check until then.
116    if (!style)
117        return Color(Color::transparent, true); // Transparent black.
118
119    const SVGRenderStyle& svgStyle = style->svgStyle();
120    return colorWithOverrideAlpha(svgStyle.stopColor().rgb(), svgStyle.stopOpacity());
121}
122
123}
124