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 "ExceptionCode.h"
31 CSSMediaRule::CSSMediaRule(CSSStyleSheet* parent, PassRefPtr<MediaList> media, PassRefPtr<CSSRuleList> rules)
32 : CSSRule(parent, CSSRule::MEDIA_RULE)
34 , m_lstCSSRules(rules)
36 m_lstMedia->setParentStyleSheet(parent);
37 int length = m_lstCSSRules->length();
38 for (int i = 0; i < length; i++)
39 m_lstCSSRules->item(i)->setParentRule(this);
42 CSSMediaRule::~CSSMediaRule()
45 m_lstMedia->setParentStyleSheet(0);
47 int length = m_lstCSSRules->length();
48 for (int i = 0; i < length; i++)
49 m_lstCSSRules->item(i)->setParentRule(0);
52 unsigned CSSMediaRule::append(CSSRule* rule)
57 rule->setParentRule(this);
58 return m_lstCSSRules->insertRule(rule, m_lstCSSRules->length());
61 unsigned CSSMediaRule::insertRule(const String& rule, unsigned index, ExceptionCode& ec)
63 if (index > m_lstCSSRules->length()) {
64 // INDEX_SIZE_ERR: Raised if the specified index is not a valid insertion point.
69 CSSParser p(useStrictParsing());
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 unsigned returnedIndex = m_lstCSSRules->insertRule(newRule.get(), index);
92 if (CSSStyleSheet* styleSheet = parentStyleSheet())
93 styleSheet->styleSheetChanged();
98 void CSSMediaRule::deleteRule(unsigned index, ExceptionCode& ec)
100 if (index >= m_lstCSSRules->length()) {
101 // INDEX_SIZE_ERR: Raised if the specified index does not correspond to a
102 // rule in the media rule list.
107 m_lstCSSRules->item(index)->setParentRule(0);
108 m_lstCSSRules->deleteRule(index);
110 if (CSSStyleSheet* styleSheet = parentStyleSheet())
111 styleSheet->styleSheetChanged();
114 String CSSMediaRule::cssText() const
116 String result = "@media ";
118 result += m_lstMedia->mediaText();
124 result += m_lstCSSRules->rulesText();
130 } // namespace WebCore