2 * Copyright (C) 2011 Adobe Systems Incorporated. All rights reserved.
3 * Copyright (C) 2012 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above
10 * copyright notice, this list of conditions and the following
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
26 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include "core/css/CSSGroupingRule.h"
34 #include "bindings/core/v8/ExceptionState.h"
35 #include "core/css/parser/BisonCSSParser.h"
36 #include "core/css/CSSRuleList.h"
37 #include "core/css/CSSStyleSheet.h"
38 #include "core/dom/ExceptionCode.h"
39 #include "core/frame/UseCounter.h"
40 #include "wtf/text/StringBuilder.h"
44 CSSGroupingRule::CSSGroupingRule(StyleRuleGroup* groupRule, CSSStyleSheet* parent)
46 , m_groupRule(groupRule)
47 , m_childRuleCSSOMWrappers(groupRule->childRules().size())
51 CSSGroupingRule::~CSSGroupingRule()
54 ASSERT(m_childRuleCSSOMWrappers.size() == m_groupRule->childRules().size());
55 for (unsigned i = 0; i < m_childRuleCSSOMWrappers.size(); ++i) {
56 if (m_childRuleCSSOMWrappers[i])
57 m_childRuleCSSOMWrappers[i]->setParentRule(0);
62 unsigned CSSGroupingRule::insertRule(const String& ruleString, unsigned index, ExceptionState& exceptionState)
64 ASSERT(m_childRuleCSSOMWrappers.size() == m_groupRule->childRules().size());
66 if (index > m_groupRule->childRules().size()) {
67 exceptionState.throwDOMException(IndexSizeError, "the index " + String::number(index) + " must be less than or equal to the length of the rule list.");
71 CSSStyleSheet* styleSheet = parentStyleSheet();
72 CSSParserContext context(parserContext(), UseCounter::getFrom(styleSheet));
73 BisonCSSParser parser(context);
74 RefPtrWillBeRawPtr<StyleRuleBase> newRule = parser.parseRule(styleSheet ? styleSheet->contents() : 0, ruleString);
76 exceptionState.throwDOMException(SyntaxError, "the rule '" + ruleString + "' is invalid and cannot be parsed.");
80 if (newRule->isImportRule()) {
81 // FIXME: an HierarchyRequestError should also be thrown for a @charset or a nested
82 // @media rule. They are currently not getting parsed, resulting in a SyntaxError
83 // to get raised above.
84 exceptionState.throwDOMException(HierarchyRequestError, "'@import' rules cannot be inserted inside a group rule.");
87 CSSStyleSheet::RuleMutationScope mutationScope(this);
89 m_groupRule->wrapperInsertRule(index, newRule);
91 m_childRuleCSSOMWrappers.insert(index, RefPtrWillBeMember<CSSRule>(nullptr));
95 void CSSGroupingRule::deleteRule(unsigned index, ExceptionState& exceptionState)
97 ASSERT(m_childRuleCSSOMWrappers.size() == m_groupRule->childRules().size());
99 if (index >= m_groupRule->childRules().size()) {
100 exceptionState.throwDOMException(IndexSizeError, "the index " + String::number(index) + " is greated than the length of the rule list.");
104 CSSStyleSheet::RuleMutationScope mutationScope(this);
106 m_groupRule->wrapperRemoveRule(index);
108 if (m_childRuleCSSOMWrappers[index])
109 m_childRuleCSSOMWrappers[index]->setParentRule(0);
110 m_childRuleCSSOMWrappers.remove(index);
113 void CSSGroupingRule::appendCSSTextForItems(StringBuilder& result) const
115 unsigned size = length();
116 for (unsigned i = 0; i < size; ++i) {
117 result.appendLiteral(" ");
118 result.append(item(i)->cssText());
123 unsigned CSSGroupingRule::length() const
125 return m_groupRule->childRules().size();
128 CSSRule* CSSGroupingRule::item(unsigned index) const
130 if (index >= length())
132 ASSERT(m_childRuleCSSOMWrappers.size() == m_groupRule->childRules().size());
133 RefPtrWillBeMember<CSSRule>& rule = m_childRuleCSSOMWrappers[index];
135 rule = m_groupRule->childRules()[index]->createCSSOMWrapper(const_cast<CSSGroupingRule*>(this));
139 CSSRuleList* CSSGroupingRule::cssRules() const
141 if (!m_ruleListCSSOMWrapper)
142 m_ruleListCSSOMWrapper = LiveCSSRuleList<CSSGroupingRule>::create(const_cast<CSSGroupingRule*>(this));
143 return m_ruleListCSSOMWrapper.get();
146 void CSSGroupingRule::reattach(StyleRuleBase* rule)
149 m_groupRule = static_cast<StyleRuleGroup*>(rule);
150 for (unsigned i = 0; i < m_childRuleCSSOMWrappers.size(); ++i) {
151 if (m_childRuleCSSOMWrappers[i])
152 m_childRuleCSSOMWrappers[i]->reattach(m_groupRule->childRules()[i].get());
156 void CSSGroupingRule::trace(Visitor* visitor)
158 CSSRule::trace(visitor);
160 visitor->trace(m_childRuleCSSOMWrappers);
162 visitor->trace(m_groupRule);
163 visitor->trace(m_ruleListCSSOMWrapper);