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, 2008, 2012, 2013 Apple Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB.  If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22#include "config.h"
23#include "CSSStyleRule.h"
24
25#include "CSSParser.h"
26#include "CSSSelector.h"
27#include "CSSStyleSheet.h"
28#include "Document.h"
29#include "PropertySetCSSStyleDeclaration.h"
30#include "RuleSet.h"
31#include "StylePropertySet.h"
32#include "StyleRule.h"
33#include <wtf/text/StringBuilder.h>
34
35namespace WebCore {
36
37typedef HashMap<const CSSStyleRule*, String> SelectorTextCache;
38static SelectorTextCache& selectorTextCache()
39{
40    DEFINE_STATIC_LOCAL(SelectorTextCache, cache, ());
41    return cache;
42}
43
44CSSStyleRule::CSSStyleRule(StyleRule* styleRule, CSSStyleSheet* parent)
45    : CSSRule(parent)
46    , m_styleRule(styleRule)
47{
48}
49
50CSSStyleRule::~CSSStyleRule()
51{
52    if (m_propertiesCSSOMWrapper)
53        m_propertiesCSSOMWrapper->clearParentRule();
54
55    if (hasCachedSelectorText()) {
56        selectorTextCache().remove(this);
57        setHasCachedSelectorText(false);
58    }
59}
60
61CSSStyleDeclaration* CSSStyleRule::style()
62{
63    if (!m_propertiesCSSOMWrapper) {
64        m_propertiesCSSOMWrapper = StyleRuleCSSStyleDeclaration::create(m_styleRule->mutableProperties(), this);
65    }
66    return m_propertiesCSSOMWrapper.get();
67}
68
69String CSSStyleRule::generateSelectorText() const
70{
71    StringBuilder builder;
72    for (const CSSSelector* selector = m_styleRule->selectorList().first(); selector; selector = CSSSelectorList::next(selector)) {
73        if (selector != m_styleRule->selectorList().first())
74            builder.appendLiteral(", ");
75        builder.append(selector->selectorText());
76    }
77    return builder.toString();
78}
79
80String CSSStyleRule::selectorText() const
81{
82    if (hasCachedSelectorText()) {
83        ASSERT(selectorTextCache().contains(this));
84        return selectorTextCache().get(this);
85    }
86
87    ASSERT(!selectorTextCache().contains(this));
88    String text = generateSelectorText();
89    selectorTextCache().set(this, text);
90    setHasCachedSelectorText(true);
91    return text;
92}
93
94void CSSStyleRule::setSelectorText(const String& selectorText)
95{
96    // FIXME: getMatchedCSSRules can return CSSStyleRules that are missing parent stylesheet pointer while
97    // referencing StyleRules that are part of stylesheet. Disallow mutations in this case.
98    if (!parentStyleSheet())
99        return;
100
101    CSSParser p(parserContext());
102    CSSSelectorList selectorList;
103    p.parseSelector(selectorText, selectorList);
104    if (!selectorList.isValid())
105        return;
106
107    // NOTE: The selector list has to fit into RuleData. <http://webkit.org/b/118369>
108    if (selectorList.componentCount() > RuleData::maximumSelectorComponentCount)
109        return;
110
111    CSSStyleSheet::RuleMutationScope mutationScope(this);
112
113    m_styleRule->wrapperAdoptSelectorList(selectorList);
114
115    if (hasCachedSelectorText()) {
116        selectorTextCache().remove(this);
117        setHasCachedSelectorText(false);
118    }
119}
120
121String CSSStyleRule::cssText() const
122{
123    StringBuilder result;
124    result.append(selectorText());
125    result.appendLiteral(" { ");
126    String decls = m_styleRule->properties()->asText();
127    result.append(decls);
128    if (!decls.isEmpty())
129        result.append(' ');
130    result.append('}');
131    return result.toString();
132}
133
134void CSSStyleRule::reattach(StyleRuleBase* rule)
135{
136    ASSERT(rule);
137    ASSERT_WITH_SECURITY_IMPLICATION(rule->isStyleRule());
138    m_styleRule = static_cast<StyleRule*>(rule);
139    if (m_propertiesCSSOMWrapper)
140        m_propertiesCSSOMWrapper->reattach(m_styleRule->mutableProperties());
141}
142
143} // namespace WebCore
144