1/*
2 * Copyright (C) 2011 Adobe Systems Incorporated. All rights reserved.
3 * Copyright (C) 2012 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above
10 *    copyright notice, this list of conditions and the following
11 *    disclaimer.
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.
16 *
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
28 * SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "core/css/CSSGroupingRule.h"
33
34#include "bindings/v8/ExceptionState.h"
35#include "core/css/CSSParser.h"
36#include "core/css/CSSRuleList.h"
37#include "core/css/CSSStyleSheet.h"
38#include "core/dom/ExceptionCode.h"
39#include "wtf/text/StringBuilder.h"
40
41namespace WebCore {
42
43CSSGroupingRule::CSSGroupingRule(StyleRuleGroup* groupRule, CSSStyleSheet* parent)
44    : CSSRule(parent)
45    , m_groupRule(groupRule)
46    , m_childRuleCSSOMWrappers(groupRule->childRules().size())
47{
48}
49
50CSSGroupingRule::~CSSGroupingRule()
51{
52    ASSERT(m_childRuleCSSOMWrappers.size() == m_groupRule->childRules().size());
53    for (unsigned i = 0; i < m_childRuleCSSOMWrappers.size(); ++i) {
54        if (m_childRuleCSSOMWrappers[i])
55            m_childRuleCSSOMWrappers[i]->setParentRule(0);
56    }
57}
58
59unsigned CSSGroupingRule::insertRule(const String& ruleString, unsigned index, ExceptionState& exceptionState)
60{
61    ASSERT(m_childRuleCSSOMWrappers.size() == m_groupRule->childRules().size());
62
63    if (index > m_groupRule->childRules().size()) {
64        exceptionState.throwDOMException(IndexSizeError, "the index " + String::number(index) + " must be less than or equal to the length of the rule list.");
65        return 0;
66    }
67
68    CSSStyleSheet* styleSheet = parentStyleSheet();
69    CSSParser parser(parserContext(), UseCounter::getFrom(styleSheet));
70    RefPtr<StyleRuleBase> newRule = parser.parseRule(styleSheet ? styleSheet->contents() : 0, ruleString);
71    if (!newRule) {
72        exceptionState.throwDOMException(SyntaxError, "the rule '" + ruleString + "' is invalid and cannot be parsed.");
73        return 0;
74    }
75
76    if (newRule->isImportRule()) {
77        // FIXME: an HierarchyRequestError should also be thrown for a @charset or a nested
78        // @media rule. They are currently not getting parsed, resulting in a SyntaxError
79        // to get raised above.
80        exceptionState.throwDOMException(HierarchyRequestError, "'@import' rules cannot be inserted inside a group rule.");
81        return 0;
82    }
83    CSSStyleSheet::RuleMutationScope mutationScope(this);
84
85    m_groupRule->wrapperInsertRule(index, newRule);
86
87    m_childRuleCSSOMWrappers.insert(index, RefPtr<CSSRule>());
88    return index;
89}
90
91void CSSGroupingRule::deleteRule(unsigned index, ExceptionState& exceptionState)
92{
93    ASSERT(m_childRuleCSSOMWrappers.size() == m_groupRule->childRules().size());
94
95    if (index >= m_groupRule->childRules().size()) {
96        exceptionState.throwDOMException(IndexSizeError, "the index " + String::number(index) + " is greated than the length of the rule list.");
97        return;
98    }
99
100    CSSStyleSheet::RuleMutationScope mutationScope(this);
101
102    m_groupRule->wrapperRemoveRule(index);
103
104    if (m_childRuleCSSOMWrappers[index])
105        m_childRuleCSSOMWrappers[index]->setParentRule(0);
106    m_childRuleCSSOMWrappers.remove(index);
107}
108
109void CSSGroupingRule::appendCSSTextForItems(StringBuilder& result) const
110{
111    unsigned size = length();
112    for (unsigned i = 0; i < size; ++i) {
113        result.appendLiteral("  ");
114        result.append(item(i)->cssText());
115        result.append('\n');
116    }
117}
118
119unsigned CSSGroupingRule::length() const
120{
121    return m_groupRule->childRules().size();
122}
123
124CSSRule* CSSGroupingRule::item(unsigned index) const
125{
126    if (index >= length())
127        return 0;
128    ASSERT(m_childRuleCSSOMWrappers.size() == m_groupRule->childRules().size());
129    RefPtr<CSSRule>& rule = m_childRuleCSSOMWrappers[index];
130    if (!rule)
131        rule = m_groupRule->childRules()[index]->createCSSOMWrapper(const_cast<CSSGroupingRule*>(this));
132    return rule.get();
133}
134
135CSSRuleList* CSSGroupingRule::cssRules() const
136{
137    if (!m_ruleListCSSOMWrapper)
138        m_ruleListCSSOMWrapper = adoptPtr(new LiveCSSRuleList<CSSGroupingRule>(const_cast<CSSGroupingRule*>(this)));
139    return m_ruleListCSSOMWrapper.get();
140}
141
142void CSSGroupingRule::reattach(StyleRuleBase* rule)
143{
144    ASSERT(rule);
145    m_groupRule = static_cast<StyleRuleGroup*>(rule);
146    for (unsigned i = 0; i < m_childRuleCSSOMWrappers.size(); ++i) {
147        if (m_childRuleCSSOMWrappers[i])
148            m_childRuleCSSOMWrappers[i]->reattach(m_groupRule->childRules()[i].get());
149    }
150}
151
152} // namespace WebCore
153