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 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
25#if ENABLE(SVG)
26#include "SVGStyleElement.h"
27
28#include "Attribute.h"
29#include "CSSStyleSheet.h"
30#include "Document.h"
31#include "ExceptionCode.h"
32#include "SVGNames.h"
33#include <wtf/StdLibExtras.h>
34
35namespace WebCore {
36
37inline SVGStyleElement::SVGStyleElement(const QualifiedName& tagName, Document* document, bool createdByParser)
38    : SVGElement(tagName, document)
39    , StyleElement(document, createdByParser)
40    , m_svgLoadEventTimer(this, &SVGElement::svgLoadEventTimerFired)
41{
42    ASSERT(hasTagName(SVGNames::styleTag));
43}
44
45SVGStyleElement::~SVGStyleElement()
46{
47    StyleElement::clearDocumentData(document(), this);
48}
49
50PassRefPtr<SVGStyleElement> SVGStyleElement::create(const QualifiedName& tagName, Document* document, bool createdByParser)
51{
52    return adoptRef(new SVGStyleElement(tagName, document, createdByParser));
53}
54
55bool SVGStyleElement::disabled() const
56{
57    if (!m_sheet)
58        return false;
59
60    return m_sheet->disabled();
61}
62
63void SVGStyleElement::setDisabled(bool setDisabled)
64{
65    if (CSSStyleSheet* styleSheet = sheet())
66        styleSheet->setDisabled(setDisabled);
67}
68
69const AtomicString& SVGStyleElement::type() const
70{
71    DEFINE_STATIC_LOCAL(const AtomicString, defaultValue, ("text/css", AtomicString::ConstructFromLiteral));
72    const AtomicString& n = getAttribute(SVGNames::typeAttr);
73    return n.isNull() ? defaultValue : n;
74}
75
76void SVGStyleElement::setType(const AtomicString& type, ExceptionCode&)
77{
78    setAttribute(SVGNames::typeAttr, type);
79}
80
81const AtomicString& SVGStyleElement::media() const
82{
83    DEFINE_STATIC_LOCAL(const AtomicString, defaultValue, ("all", AtomicString::ConstructFromLiteral));
84    const AtomicString& n = fastGetAttribute(SVGNames::mediaAttr);
85    return n.isNull() ? defaultValue : n;
86}
87
88void SVGStyleElement::setMedia(const AtomicString& media, ExceptionCode&)
89{
90    setAttribute(SVGNames::mediaAttr, media);
91}
92
93String SVGStyleElement::title() const
94{
95    return fastGetAttribute(SVGNames::titleAttr);
96}
97
98void SVGStyleElement::setTitle(const AtomicString& title, ExceptionCode&)
99{
100    setAttribute(SVGNames::titleAttr, title);
101}
102
103bool SVGStyleElement::isSupportedAttribute(const QualifiedName& attrName)
104{
105    DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
106    if (supportedAttributes.isEmpty()) {
107        SVGLangSpace::addSupportedAttributes(supportedAttributes);
108        supportedAttributes.add(SVGNames::titleAttr);
109    }
110    return supportedAttributes.contains<QualifiedName, SVGAttributeHashTranslator>(attrName);
111}
112
113void SVGStyleElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
114{
115    if (!isSupportedAttribute(name)) {
116        SVGElement::parseAttribute(name, value);
117        return;
118    }
119
120    if (name == SVGNames::titleAttr) {
121        if (m_sheet)
122            m_sheet->setTitle(value);
123        return;
124    }
125
126    if (SVGLangSpace::parseAttribute(name, value))
127        return;
128
129    ASSERT_NOT_REACHED();
130}
131
132void SVGStyleElement::finishParsingChildren()
133{
134    StyleElement::finishParsingChildren(this);
135    SVGElement::finishParsingChildren();
136}
137
138Node::InsertionNotificationRequest SVGStyleElement::insertedInto(ContainerNode* rootParent)
139{
140    SVGElement::insertedInto(rootParent);
141    if (rootParent->inDocument())
142        StyleElement::insertedIntoDocument(document(), this);
143    return InsertionDone;
144}
145
146void SVGStyleElement::removedFrom(ContainerNode* rootParent)
147{
148    SVGElement::removedFrom(rootParent);
149    if (rootParent->inDocument())
150        StyleElement::removedFromDocument(document(), this);
151}
152
153void SVGStyleElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
154{
155    SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
156    StyleElement::childrenChanged(this);
157}
158
159}
160
161#endif // ENABLE(SVG)
162