1/*
2 * Copyright (C) 2004, 2005 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4 * Copyright (C) 2006, 2013 Apple Inc. All rights reserved.
5 * Copyright (C) 2009 Cameron McCormack <cam@mcc.id.au>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB.  If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23#include "config.h"
24#include "SVGStyleElement.h"
25
26#include "Attribute.h"
27#include "CSSStyleSheet.h"
28#include "Document.h"
29#include "ExceptionCode.h"
30#include "SVGNames.h"
31#include <wtf/NeverDestroyed.h>
32#include <wtf/StdLibExtras.h>
33
34namespace WebCore {
35
36inline SVGStyleElement::SVGStyleElement(const QualifiedName& tagName, Document& document, bool createdByParser)
37    : SVGElement(tagName, document)
38    , m_styleSheetOwner(document, createdByParser)
39    , m_svgLoadEventTimer(this, &SVGElement::svgLoadEventTimerFired)
40{
41    ASSERT(hasTagName(SVGNames::styleTag));
42}
43
44SVGStyleElement::~SVGStyleElement()
45{
46    m_styleSheetOwner.clearDocumentData(document(), *this);
47}
48
49PassRefPtr<SVGStyleElement> SVGStyleElement::create(const QualifiedName& tagName, Document& document, bool createdByParser)
50{
51    return adoptRef(new SVGStyleElement(tagName, document, createdByParser));
52}
53
54bool SVGStyleElement::disabled() const
55{
56    return sheet() && sheet()->disabled();
57}
58
59void SVGStyleElement::setDisabled(bool setDisabled)
60{
61    if (CSSStyleSheet* styleSheet = sheet())
62        styleSheet->setDisabled(setDisabled);
63}
64
65const AtomicString& SVGStyleElement::type() const
66{
67    DEPRECATED_DEFINE_STATIC_LOCAL(const AtomicString, defaultValue, ("text/css", AtomicString::ConstructFromLiteral));
68    const AtomicString& n = getAttribute(SVGNames::typeAttr);
69    return n.isNull() ? defaultValue : n;
70}
71
72void SVGStyleElement::setType(const AtomicString& type, ExceptionCode&)
73{
74    setAttribute(SVGNames::typeAttr, type);
75}
76
77const AtomicString& SVGStyleElement::media() const
78{
79    DEPRECATED_DEFINE_STATIC_LOCAL(const AtomicString, defaultValue, ("all", AtomicString::ConstructFromLiteral));
80    const AtomicString& n = fastGetAttribute(SVGNames::mediaAttr);
81    return n.isNull() ? defaultValue : n;
82}
83
84void SVGStyleElement::setMedia(const AtomicString& media, ExceptionCode&)
85{
86    setAttribute(SVGNames::mediaAttr, media);
87}
88
89String SVGStyleElement::title() const
90{
91    return fastGetAttribute(SVGNames::titleAttr);
92}
93
94void SVGStyleElement::setTitle(const AtomicString& title, ExceptionCode&)
95{
96    setAttribute(SVGNames::titleAttr, title);
97}
98
99bool SVGStyleElement::isSupportedAttribute(const QualifiedName& attrName)
100{
101    static NeverDestroyed<HashSet<QualifiedName>> supportedAttributes;
102    if (supportedAttributes.get().isEmpty()) {
103        SVGLangSpace::addSupportedAttributes(supportedAttributes);
104        supportedAttributes.get().add(SVGNames::titleAttr);
105        supportedAttributes.get().add(SVGNames::mediaAttr);
106        supportedAttributes.get().add(SVGNames::typeAttr);
107    }
108    return supportedAttributes.get().contains<SVGAttributeHashTranslator>(attrName);
109}
110
111void SVGStyleElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
112{
113    if (!isSupportedAttribute(name)) {
114        SVGElement::parseAttribute(name, value);
115        return;
116    }
117    if (name == SVGNames::titleAttr) {
118        if (sheet())
119            sheet()->setTitle(value);
120        return;
121    }
122    if (name == SVGNames::typeAttr) {
123        m_styleSheetOwner.setContentType(value);
124        return;
125    }
126    if (name == SVGNames::mediaAttr) {
127        m_styleSheetOwner.setMedia(value);
128        return;
129    }
130    if (SVGLangSpace::parseAttribute(name, value))
131        return;
132
133    ASSERT_NOT_REACHED();
134}
135
136void SVGStyleElement::finishParsingChildren()
137{
138    m_styleSheetOwner.finishParsingChildren(*this);
139    SVGElement::finishParsingChildren();
140}
141
142Node::InsertionNotificationRequest SVGStyleElement::insertedInto(ContainerNode& rootParent)
143{
144    SVGElement::insertedInto(rootParent);
145    if (rootParent.inDocument())
146        m_styleSheetOwner.insertedIntoDocument(document(), *this);
147    return InsertionDone;
148}
149
150void SVGStyleElement::removedFrom(ContainerNode& rootParent)
151{
152    SVGElement::removedFrom(rootParent);
153    if (rootParent.inDocument())
154        m_styleSheetOwner.removedFromDocument(document(), *this);
155}
156
157void SVGStyleElement::childrenChanged(const ChildChange& change)
158{
159    SVGElement::childrenChanged(change);
160    m_styleSheetOwner.childrenChanged(*this);
161}
162
163}
164