[CherryPick] flex-grow should be 1 when flex:auto
[framework/web/webkit-efl.git] / Source / WebCore / css / CSSMediaRule.cpp
1 /**
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)
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 #include "CSSMediaRule.h"
25
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>
32
33 namespace WebCore {
34
35 CSSMediaRule::CSSMediaRule(StyleRuleMedia* mediaRule, CSSStyleSheet* parent)
36     : CSSRule(parent, CSSRule::MEDIA_RULE)
37     , m_mediaRule(mediaRule)
38     , m_childRuleCSSOMWrappers(mediaRule->childRules().size())
39 {
40 }
41
42 CSSMediaRule::~CSSMediaRule()
43 {
44     ASSERT(m_childRuleCSSOMWrappers.size() == m_mediaRule->childRules().size());
45
46     for (unsigned i = 0; i < m_childRuleCSSOMWrappers.size(); ++i) {
47         if (m_childRuleCSSOMWrappers[i])
48             m_childRuleCSSOMWrappers[i]->setParentRule(0);
49     }
50     if (m_mediaCSSOMWrapper)
51         m_mediaCSSOMWrapper->clearParentRule();
52 }
53
54 unsigned CSSMediaRule::insertRule(const String& ruleString, unsigned index, ExceptionCode& ec)
55 {
56     ASSERT(m_childRuleCSSOMWrappers.size() == m_mediaRule->childRules().size());
57
58     if (index > m_mediaRule->childRules().size()) {
59         // INDEX_SIZE_ERR: Raised if the specified index is not a valid insertion point.
60         ec = INDEX_SIZE_ERR;
61         return 0;
62     }
63
64     CSSParser parser(parserContext());
65     CSSStyleSheet* styleSheet = parentStyleSheet();
66     RefPtr<StyleRuleBase> newRule = parser.parseRule(styleSheet ? styleSheet->contents() : 0, ruleString);
67     if (!newRule) {
68         // SYNTAX_ERR: Raised if the specified rule has a syntax error and is unparsable.
69         ec = SYNTAX_ERR;
70         return 0;
71     }
72
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.
77
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
80         // at-rule.
81         ec = HIERARCHY_REQUEST_ERR;
82         return 0;
83     }
84     CSSStyleSheet::RuleMutationScope mutationScope(this);
85
86     m_mediaRule->wrapperInsertRule(index, newRule);
87
88     m_childRuleCSSOMWrappers.insert(index, RefPtr<CSSRule>());
89     return index;
90 }
91
92 void CSSMediaRule::deleteRule(unsigned index, ExceptionCode& ec)
93 {
94     ASSERT(m_childRuleCSSOMWrappers.size() == m_mediaRule->childRules().size());
95
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.
99         ec = INDEX_SIZE_ERR;
100         return;
101     }
102
103     CSSStyleSheet::RuleMutationScope mutationScope(this);
104
105     m_mediaRule->wrapperRemoveRule(index);
106
107     if (m_childRuleCSSOMWrappers[index])
108         m_childRuleCSSOMWrappers[index]->setParentRule(0);
109     m_childRuleCSSOMWrappers.remove(index);
110 }
111
112 String CSSMediaRule::cssText() const
113 {
114     StringBuilder result;
115     result.append("@media ");
116     if (m_mediaRule->mediaQueries()) {
117         result.append(m_mediaRule->mediaQueries()->mediaText());
118         result.append(' ');
119     }
120     result.append("{ \n");
121     
122     unsigned size = length();
123     for (unsigned i = 0; i < size; ++i) {
124         result.append("  ");
125         result.append(item(i)->cssText());
126         result.append('\n');
127     }
128
129     result.append('}');
130     return result.toString();
131 }
132
133 MediaList* CSSMediaRule::media() const
134 {
135     if (!m_mediaRule->mediaQueries())
136         return 0;
137     if (!m_mediaCSSOMWrapper)
138         m_mediaCSSOMWrapper = MediaList::create(m_mediaRule->mediaQueries(), const_cast<CSSMediaRule*>(this));
139     return m_mediaCSSOMWrapper.get();
140 }
141
142 unsigned CSSMediaRule::length() const
143
144     return m_mediaRule->childRules().size(); 
145 }
146
147 CSSRule* CSSMediaRule::item(unsigned index) const
148
149     if (index >= length())
150         return 0;
151     ASSERT(m_childRuleCSSOMWrappers.size() == m_mediaRule->childRules().size());
152     RefPtr<CSSRule>& rule = m_childRuleCSSOMWrappers[index];
153     if (!rule)
154         rule = m_mediaRule->childRules()[index]->createCSSOMWrapper(const_cast<CSSMediaRule*>(this));
155     return rule.get();
156 }
157
158 CSSRuleList* CSSMediaRule::cssRules() const
159 {
160     if (!m_ruleListCSSOMWrapper)
161         m_ruleListCSSOMWrapper = adoptPtr(new LiveCSSRuleList<CSSMediaRule>(const_cast<CSSMediaRule*>(this)));
162     return m_ruleListCSSOMWrapper.get();
163 }
164
165 void CSSMediaRule::reattach(StyleRuleMedia* rule)
166 {
167     ASSERT(rule);
168     m_mediaRule = 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());
174     }
175 }
176
177 } // namespace WebCore