2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * (C) 2002-2003 Dirk Mueller (mueller@kde.org)
4 * Copyright (C) 2002, 2005, 2006 Apple Computer, Inc.
5 * Copyright (C) 2006 Samuel Weinig (sam@webkit.org)
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.
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.
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.
24 #include "CSSMediaRule.h"
26 #include "CSSParser.h"
27 #include "CSSRuleList.h"
28 #include "ExceptionCode.h"
29 #include <wtf/text/StringBuilder.h>
33 CSSMediaRule::CSSMediaRule(CSSStyleSheet* parent, PassRefPtr<MediaQuerySet> media, Vector<RefPtr<CSSRule> >& adoptRules)
34 : CSSRule(parent, CSSRule::MEDIA_RULE)
35 , m_mediaQueries(media)
37 m_childRules.swap(adoptRules);
39 unsigned size = m_childRules.size();
40 for (unsigned i = 0; i < size; i++)
41 m_childRules[i]->setParentRule(this);
44 CSSMediaRule::~CSSMediaRule()
46 unsigned size = m_childRules.size();
47 for (unsigned i = 0; i < size; i++)
48 m_childRules[i]->setParentRule(0);
51 unsigned CSSMediaRule::append(CSSRule* rule)
56 rule->setParentRule(this);
57 m_childRules.append(rule);
58 return m_childRules.size() - 1;
61 unsigned CSSMediaRule::insertRule(const String& rule, unsigned index, ExceptionCode& ec)
63 if (index > m_childRules.size()) {
64 // INDEX_SIZE_ERR: Raised if the specified index is not a valid insertion point.
69 CSSParser p(cssParserMode());
70 RefPtr<CSSRule> newRule = p.parseRule(parentStyleSheet(), rule);
72 // SYNTAX_ERR: Raised if the specified rule has a syntax error and is unparsable.
77 if (newRule->isImportRule()) {
78 // FIXME: an HIERARCHY_REQUEST_ERR should also be thrown for a @charset or a nested
79 // @media rule. They are currently not getting parsed, resulting in a SYNTAX_ERR
80 // to get raised above.
82 // HIERARCHY_REQUEST_ERR: Raised if the rule cannot be inserted at the specified
83 // index, e.g., if an @import rule is inserted after a standard rule set or other
85 ec = HIERARCHY_REQUEST_ERR;
89 newRule->setParentRule(this);
90 m_childRules.insert(index, newRule.get());
92 if (CSSStyleSheet* styleSheet = parentStyleSheet())
93 styleSheet->styleSheetChanged();
98 void CSSMediaRule::deleteRule(unsigned index, ExceptionCode& ec)
100 if (index >= m_childRules.size()) {
101 // INDEX_SIZE_ERR: Raised if the specified index does not correspond to a
102 // rule in the media rule list.
107 m_childRules[index]->setParentRule(0);
108 m_childRules.remove(index);
110 if (CSSStyleSheet* styleSheet = parentStyleSheet())
111 styleSheet->styleSheetChanged();
114 String CSSMediaRule::cssText() const
116 StringBuilder result;
117 result.append("@media ");
118 if (m_mediaQueries) {
119 result.append(m_mediaQueries->mediaText());
122 result.append("{ \n");
124 for (unsigned i = 0; i < m_childRules.size(); ++i) {
126 result.append(m_childRules[i]->cssText());
131 return result.toString();
134 CSSRuleList* CSSMediaRule::cssRules()
136 if (!m_ruleListCSSOMWrapper)
137 m_ruleListCSSOMWrapper = adoptPtr(new LiveCSSRuleList<CSSMediaRule>(this));
138 return m_ruleListCSSOMWrapper.get();
141 } // namespace WebCore