initial import
[vuplus_webkit] / 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 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 "ExceptionCode.h"
28
29 namespace WebCore {
30
31 CSSMediaRule::CSSMediaRule(CSSStyleSheet* parent, PassRefPtr<MediaList> media, PassRefPtr<CSSRuleList> rules)
32     : CSSRule(parent)
33     , m_lstMedia(media)
34     , m_lstCSSRules(rules)
35 {
36     m_lstMedia->setParent(this);
37     int length = m_lstCSSRules->length();
38     for (int i = 0; i < length; i++)
39         m_lstCSSRules->item(i)->setParent(this);
40 }
41
42 CSSMediaRule::~CSSMediaRule()
43 {
44     if (m_lstMedia)
45         m_lstMedia->setParent(0);
46
47     int length = m_lstCSSRules->length();
48     for (int i = 0; i < length; i++)
49         m_lstCSSRules->item(i)->setParent(0);
50 }
51
52 unsigned CSSMediaRule::append(CSSRule* rule)
53 {
54     if (!rule)
55         return 0;
56
57     rule->setParent(this);
58     return m_lstCSSRules->insertRule(rule, m_lstCSSRules->length());
59 }
60
61 unsigned CSSMediaRule::insertRule(const String& rule, unsigned index, ExceptionCode& ec)
62 {
63     if (index > m_lstCSSRules->length()) {
64         // INDEX_SIZE_ERR: Raised if the specified index is not a valid insertion point.
65         ec = INDEX_SIZE_ERR;
66         return 0;
67     }
68
69     CSSParser p(useStrictParsing());
70     RefPtr<CSSRule> newRule = p.parseRule(parentStyleSheet(), rule);
71     if (!newRule) {
72         // SYNTAX_ERR: Raised if the specified rule has a syntax error and is unparsable.
73         ec = SYNTAX_ERR;
74         return 0;
75     }
76
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.
81
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
84         // at-rule.
85         ec = HIERARCHY_REQUEST_ERR;
86         return 0;
87     }
88
89     newRule->setParent(this);
90     unsigned returnedIndex = m_lstCSSRules->insertRule(newRule.get(), index);
91
92     if (stylesheet())
93         stylesheet()->styleSheetChanged();
94
95     return returnedIndex;
96 }
97
98 void CSSMediaRule::deleteRule(unsigned index, ExceptionCode& ec)
99 {
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.
103         ec = INDEX_SIZE_ERR;
104         return;
105     }
106
107     m_lstCSSRules->deleteRule(index);
108
109     if (stylesheet())
110         stylesheet()->styleSheetChanged();
111 }
112
113 String CSSMediaRule::cssText() const
114 {
115     String result = "@media ";
116     if (m_lstMedia) {
117         result += m_lstMedia->mediaText();
118         result += " ";
119     }
120     result += "{ \n";
121
122     if (m_lstCSSRules) {
123         unsigned len = m_lstCSSRules->length();
124         for (unsigned i = 0; i < len; i++) {
125             result += "  ";
126             result += m_lstCSSRules->item(i)->cssText();
127             result += "\n";
128         }
129     }
130
131     result += "}";
132     return result;
133 }
134
135 } // namespace WebCore