2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * (C) 2002-2003 Dirk Mueller (mueller@kde.org)
4 * Copyright (C) 2002, 2005, 2006, 2012 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 "CSSStyleSheet.h"
29 #include "ExceptionCode.h"
30 #include "StyleRule.h"
31 #include <wtf/text/StringBuilder.h>
35 CSSMediaRule::CSSMediaRule(StyleRuleMedia* mediaRule, CSSStyleSheet* parent)
36 : CSSRule(parent, CSSRule::MEDIA_RULE)
37 , m_mediaRule(mediaRule)
38 , m_childRuleCSSOMWrappers(mediaRule->childRules().size())
42 CSSMediaRule::~CSSMediaRule()
44 ASSERT(m_childRuleCSSOMWrappers.size() == m_mediaRule->childRules().size());
46 for (unsigned i = 0; i < m_childRuleCSSOMWrappers.size(); ++i) {
47 if (m_childRuleCSSOMWrappers[i])
48 m_childRuleCSSOMWrappers[i]->setParentRule(0);
50 if (m_mediaCSSOMWrapper)
51 m_mediaCSSOMWrapper->clearParentRule();
54 unsigned CSSMediaRule::insertRule(const String& ruleString, unsigned index, ExceptionCode& ec)
56 ASSERT(m_childRuleCSSOMWrappers.size() == m_mediaRule->childRules().size());
58 if (index > m_mediaRule->childRules().size()) {
59 // INDEX_SIZE_ERR: Raised if the specified index is not a valid insertion point.
64 CSSParser parser(parserContext());
65 CSSStyleSheet* styleSheet = parentStyleSheet();
66 RefPtr<StyleRuleBase> newRule = parser.parseRule(styleSheet ? styleSheet->contents() : 0, ruleString);
68 // SYNTAX_ERR: Raised if the specified rule has a syntax error and is unparsable.
73 if (newRule->isImportRule()) {
74 // FIXME: an HIERARCHY_REQUEST_ERR should also be thrown for a @charset or a nested
75 // @media rule. They are currently not getting parsed, resulting in a SYNTAX_ERR
76 // to get raised above.
78 // HIERARCHY_REQUEST_ERR: Raised if the rule cannot be inserted at the specified
79 // index, e.g., if an @import rule is inserted after a standard rule set or other
81 ec = HIERARCHY_REQUEST_ERR;
84 CSSStyleSheet::RuleMutationScope mutationScope(this);
86 m_mediaRule->wrapperInsertRule(index, newRule);
88 m_childRuleCSSOMWrappers.insert(index, RefPtr<CSSRule>());
92 void CSSMediaRule::deleteRule(unsigned index, ExceptionCode& ec)
94 ASSERT(m_childRuleCSSOMWrappers.size() == m_mediaRule->childRules().size());
96 if (index >= m_mediaRule->childRules().size()) {
97 // INDEX_SIZE_ERR: Raised if the specified index does not correspond to a
98 // rule in the media rule list.
103 CSSStyleSheet::RuleMutationScope mutationScope(this);
105 m_mediaRule->wrapperRemoveRule(index);
107 if (m_childRuleCSSOMWrappers[index])
108 m_childRuleCSSOMWrappers[index]->setParentRule(0);
109 m_childRuleCSSOMWrappers.remove(index);
112 String CSSMediaRule::cssText() const
114 StringBuilder result;
115 result.append("@media ");
116 if (m_mediaRule->mediaQueries()) {
117 result.append(m_mediaRule->mediaQueries()->mediaText());
120 result.append("{ \n");
122 unsigned size = length();
123 for (unsigned i = 0; i < size; ++i) {
125 result.append(item(i)->cssText());
130 return result.toString();
133 MediaList* CSSMediaRule::media() const
135 if (!m_mediaRule->mediaQueries())
137 if (!m_mediaCSSOMWrapper)
138 m_mediaCSSOMWrapper = MediaList::create(m_mediaRule->mediaQueries(), const_cast<CSSMediaRule*>(this));
139 return m_mediaCSSOMWrapper.get();
142 unsigned CSSMediaRule::length() const
144 return m_mediaRule->childRules().size();
147 CSSRule* CSSMediaRule::item(unsigned index) const
149 if (index >= length())
151 ASSERT(m_childRuleCSSOMWrappers.size() == m_mediaRule->childRules().size());
152 RefPtr<CSSRule>& rule = m_childRuleCSSOMWrappers[index];
154 rule = m_mediaRule->childRules()[index]->createCSSOMWrapper(const_cast<CSSMediaRule*>(this));
158 CSSRuleList* CSSMediaRule::cssRules() const
160 if (!m_ruleListCSSOMWrapper)
161 m_ruleListCSSOMWrapper = adoptPtr(new LiveCSSRuleList<CSSMediaRule>(const_cast<CSSMediaRule*>(this)));
162 return m_ruleListCSSOMWrapper.get();
165 void CSSMediaRule::reattach(StyleRuleMedia* rule)
169 if (m_mediaCSSOMWrapper && m_mediaRule->mediaQueries())
170 m_mediaCSSOMWrapper->reattach(m_mediaRule->mediaQueries());
171 for (unsigned i = 0; i < m_childRuleCSSOMWrappers.size(); ++i) {
172 if (m_childRuleCSSOMWrappers[i])
173 m_childRuleCSSOMWrappers[i]->reattach(m_mediaRule->childRules()[i].get());
177 } // namespace WebCore