1 /**
2 * This file is part of the DOM implementation for KDE.
3 *
4 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
5 * (C) 2002-2003 Dirk Mueller (mueller@kde.org)
6 * Copyright (C) 2002, 2005, 2006 Apple Computer, Inc.
7 * Copyright (C) 2006 Samuel Weinig (sam@webkit.org)
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 */
24
25 #include "config.h"
26 #include "CSSMediaRule.h"
27
28 #include "CSSParser.h"
29 #include "ExceptionCode.h"
30
31 namespace WebCore {
32
CSSMediaRule(CSSStyleSheet * parent,PassRefPtr<MediaList> media,PassRefPtr<CSSRuleList> rules)33 CSSMediaRule::CSSMediaRule(CSSStyleSheet* parent, PassRefPtr<MediaList> media, PassRefPtr<CSSRuleList> rules)
34 : CSSRule(parent)
35 , m_lstMedia(media)
36 , m_lstCSSRules(rules)
37 {
38 }
39
~CSSMediaRule()40 CSSMediaRule::~CSSMediaRule()
41 {
42 if (m_lstMedia)
43 m_lstMedia->setParent(0);
44
45 int length = m_lstCSSRules->length();
46 for (int i = 0; i < length; i++)
47 m_lstCSSRules->item(i)->setParent(0);
48 }
49
append(CSSRule * rule)50 unsigned CSSMediaRule::append(CSSRule* rule)
51 {
52 if (!rule)
53 return 0;
54
55 rule->setParent(this);
56 return m_lstCSSRules->insertRule(rule, m_lstCSSRules->length());
57 }
58
insertRule(const String & rule,unsigned index,ExceptionCode & ec)59 unsigned CSSMediaRule::insertRule(const String& rule, unsigned index, ExceptionCode& ec)
60 {
61 if (index > m_lstCSSRules->length()) {
62 // INDEX_SIZE_ERR: Raised if the specified index is not a valid insertion point.
63 ec = INDEX_SIZE_ERR;
64 return 0;
65 }
66
67 CSSParser p(useStrictParsing());
68 RefPtr<CSSRule> newRule = p.parseRule(parentStyleSheet(), rule);
69 if (!newRule) {
70 // SYNTAX_ERR: Raised if the specified rule has a syntax error and is unparsable.
71 ec = SYNTAX_ERR;
72 return 0;
73 }
74
75 if (newRule->isImportRule()) {
76 // FIXME: an HIERARCHY_REQUEST_ERR should also be thrown for a @charset or a nested
77 // @media rule. They are currently not getting parsed, resulting in a SYNTAX_ERR
78 // to get raised above.
79
80 // HIERARCHY_REQUEST_ERR: Raised if the rule cannot be inserted at the specified
81 // index, e.g., if an @import rule is inserted after a standard rule set or other
82 // at-rule.
83 ec = HIERARCHY_REQUEST_ERR;
84 return 0;
85 }
86
87 newRule->setParent(this);
88 unsigned returnedIndex = m_lstCSSRules->insertRule(newRule.get(), index);
89
90 // stylesheet() can only return 0 for computed style declarations.
91 stylesheet()->styleSheetChanged();
92
93 return returnedIndex;
94 }
95
deleteRule(unsigned index,ExceptionCode & ec)96 void CSSMediaRule::deleteRule(unsigned index, ExceptionCode& ec)
97 {
98 if (index >= m_lstCSSRules->length()) {
99 // INDEX_SIZE_ERR: Raised if the specified index does not correspond to a
100 // rule in the media rule list.
101 ec = INDEX_SIZE_ERR;
102 return;
103 }
104
105 m_lstCSSRules->deleteRule(index);
106
107 // stylesheet() can only return 0 for computed style declarations.
108 stylesheet()->styleSheetChanged();
109 }
110
cssText() const111 String CSSMediaRule::cssText() const
112 {
113 String result = "@media ";
114 if (m_lstMedia) {
115 result += m_lstMedia->mediaText();
116 result += " ";
117 }
118 result += "{ \n";
119
120 if (m_lstCSSRules) {
121 unsigned len = m_lstCSSRules->length();
122 for (unsigned i = 0; i < len; i++) {
123 result += " ";
124 result += m_lstCSSRules->item(i)->cssText();
125 result += "\n";
126 }
127 }
128
129 result += "}";
130 return result;
131 }
132
133 } // namespace WebCore
134