1 /** 2 * (C) 1999-2003 Lars Knoll (knoll (at) kde.org) 3 * (C) 2002-2003 Dirk Mueller (mueller (at) kde.org) 4 * Copyright (C) 2002, 2005, 2006 Apple Computer, Inc. 5 * Copyright (C) 2006 Samuel Weinig (sam (at) 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 } 37 38 CSSMediaRule::~CSSMediaRule() 39 { 40 if (m_lstMedia) 41 m_lstMedia->setParent(0); 42 43 int length = m_lstCSSRules->length(); 44 for (int i = 0; i < length; i++) 45 m_lstCSSRules->item(i)->setParent(0); 46 } 47 48 unsigned CSSMediaRule::append(CSSRule* rule) 49 { 50 if (!rule) 51 return 0; 52 53 rule->setParent(this); 54 return m_lstCSSRules->insertRule(rule, m_lstCSSRules->length()); 55 } 56 57 unsigned CSSMediaRule::insertRule(const String& rule, unsigned index, ExceptionCode& ec) 58 { 59 if (index > m_lstCSSRules->length()) { 60 // INDEX_SIZE_ERR: Raised if the specified index is not a valid insertion point. 61 ec = INDEX_SIZE_ERR; 62 return 0; 63 } 64 65 CSSParser p(useStrictParsing()); 66 RefPtr<CSSRule> newRule = p.parseRule(parentStyleSheet(), rule); 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 85 newRule->setParent(this); 86 unsigned returnedIndex = m_lstCSSRules->insertRule(newRule.get(), index); 87 88 // stylesheet() can only return 0 for computed style declarations. 89 stylesheet()->styleSheetChanged(); 90 91 return returnedIndex; 92 } 93 94 void CSSMediaRule::deleteRule(unsigned index, ExceptionCode& ec) 95 { 96 if (index >= m_lstCSSRules->length()) { 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 m_lstCSSRules->deleteRule(index); 104 105 // stylesheet() can only return 0 for computed style declarations. 106 stylesheet()->styleSheetChanged(); 107 } 108 109 String CSSMediaRule::cssText() const 110 { 111 String result = "@media "; 112 if (m_lstMedia) { 113 result += m_lstMedia->mediaText(); 114 result += " "; 115 } 116 result += "{ \n"; 117 118 if (m_lstCSSRules) { 119 unsigned len = m_lstCSSRules->length(); 120 for (unsigned i = 0; i < len; i++) { 121 result += " "; 122 result += m_lstCSSRules->item(i)->cssText(); 123 result += "\n"; 124 } 125 } 126 127 result += "}"; 128 return result; 129 } 130 131 } // namespace WebCore 132