• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
CSSMediaRule(CSSStyleSheet * parent,PassRefPtr<MediaList> media,PassRefPtr<CSSRuleList> rules)31 CSSMediaRule::CSSMediaRule(CSSStyleSheet* parent, PassRefPtr<MediaList> media, PassRefPtr<CSSRuleList> rules)
32     : CSSRule(parent)
33     , m_lstMedia(media)
34     , m_lstCSSRules(rules)
35 {
36     int length = m_lstCSSRules->length();
37     for (int i = 0; i < length; i++)
38         m_lstCSSRules->item(i)->setParent(this);
39 }
40 
~CSSMediaRule()41 CSSMediaRule::~CSSMediaRule()
42 {
43     if (m_lstMedia)
44         m_lstMedia->setParent(0);
45 
46     int length = m_lstCSSRules->length();
47     for (int i = 0; i < length; i++)
48         m_lstCSSRules->item(i)->setParent(0);
49 }
50 
append(CSSRule * rule)51 unsigned CSSMediaRule::append(CSSRule* rule)
52 {
53     if (!rule)
54         return 0;
55 
56     rule->setParent(this);
57     return m_lstCSSRules->insertRule(rule, m_lstCSSRules->length());
58 }
59 
insertRule(const String & rule,unsigned index,ExceptionCode & ec)60 unsigned CSSMediaRule::insertRule(const String& rule, unsigned index, ExceptionCode& ec)
61 {
62     if (index > m_lstCSSRules->length()) {
63         // INDEX_SIZE_ERR: Raised if the specified index is not a valid insertion point.
64         ec = INDEX_SIZE_ERR;
65         return 0;
66     }
67 
68     CSSParser p(useStrictParsing());
69     RefPtr<CSSRule> newRule = p.parseRule(parentStyleSheet(), rule);
70     if (!newRule) {
71         // SYNTAX_ERR: Raised if the specified rule has a syntax error and is unparsable.
72         ec = SYNTAX_ERR;
73         return 0;
74     }
75 
76     if (newRule->isImportRule()) {
77         // FIXME: an HIERARCHY_REQUEST_ERR should also be thrown for a @charset or a nested
78         // @media rule.  They are currently not getting parsed, resulting in a SYNTAX_ERR
79         // to get raised above.
80 
81         // HIERARCHY_REQUEST_ERR: Raised if the rule cannot be inserted at the specified
82         // index, e.g., if an @import rule is inserted after a standard rule set or other
83         // at-rule.
84         ec = HIERARCHY_REQUEST_ERR;
85         return 0;
86     }
87 
88     newRule->setParent(this);
89     unsigned returnedIndex = m_lstCSSRules->insertRule(newRule.get(), index);
90 
91     if (stylesheet())
92         stylesheet()->styleSheetChanged();
93 
94     return returnedIndex;
95 }
96 
deleteRule(unsigned index,ExceptionCode & ec)97 void CSSMediaRule::deleteRule(unsigned index, ExceptionCode& ec)
98 {
99     if (index >= m_lstCSSRules->length()) {
100         // INDEX_SIZE_ERR: Raised if the specified index does not correspond to a
101         // rule in the media rule list.
102         ec = INDEX_SIZE_ERR;
103         return;
104     }
105 
106     m_lstCSSRules->deleteRule(index);
107 
108     if (stylesheet())
109         stylesheet()->styleSheetChanged();
110 }
111 
cssText() const112 String CSSMediaRule::cssText() const
113 {
114     String result = "@media ";
115     if (m_lstMedia) {
116         result += m_lstMedia->mediaText();
117         result += " ";
118     }
119     result += "{ \n";
120 
121     if (m_lstCSSRules) {
122         unsigned len = m_lstCSSRules->length();
123         for (unsigned i = 0; i < len; i++) {
124             result += "  ";
125             result += m_lstCSSRules->item(i)->cssText();
126             result += "\n";
127         }
128     }
129 
130     result += "}";
131     return result;
132 }
133 
134 } // namespace WebCore
135