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 Apple Inc. All rights reserved.
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.
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.
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.
23 #include "CSSStyleRule.h"
25 #include "CSSParser.h"
26 #include "CSSSelector.h"
27 #include "CSSStyleSheet.h"
29 #include "PropertySetCSSStyleDeclaration.h"
30 #include "StylePropertySet.h"
31 #include "StyleRule.h"
32 #include <wtf/text/StringBuilder.h>
36 typedef HashMap<const CSSStyleRule*, String> SelectorTextCache;
37 static SelectorTextCache& selectorTextCache()
39 DEFINE_STATIC_LOCAL(SelectorTextCache, cache, ());
43 CSSStyleRule::CSSStyleRule(StyleRule* styleRule, CSSStyleSheet* parent)
44 : CSSRule(parent, CSSRule::STYLE_RULE)
45 , m_styleRule(styleRule)
49 CSSStyleRule::~CSSStyleRule()
51 if (m_propertiesCSSOMWrapper)
52 m_propertiesCSSOMWrapper->clearParentRule();
54 if (hasCachedSelectorText()) {
55 selectorTextCache().remove(this);
56 setHasCachedSelectorText(false);
60 CSSStyleDeclaration* CSSStyleRule::style() const
62 if (!m_propertiesCSSOMWrapper) {
63 m_propertiesCSSOMWrapper = StyleRuleCSSStyleDeclaration::create(m_styleRule->mutableProperties(), const_cast<CSSStyleRule*>(this));
65 return m_propertiesCSSOMWrapper.get();
68 String CSSStyleRule::generateSelectorText() const
70 StringBuilder builder;
71 for (CSSSelector* s = m_styleRule->selectorList().first(); s; s = CSSSelectorList::next(s)) {
72 if (s != m_styleRule->selectorList().first())
74 builder.append(s->selectorText());
76 return builder.toString();
79 String CSSStyleRule::selectorText() const
81 if (hasCachedSelectorText()) {
82 ASSERT(selectorTextCache().contains(this));
83 return selectorTextCache().get(this);
86 ASSERT(!selectorTextCache().contains(this));
87 String text = generateSelectorText();
88 selectorTextCache().set(this, text);
89 setHasCachedSelectorText(true);
93 void CSSStyleRule::setSelectorText(const String& selectorText)
95 CSSParser p(parserContext());
96 CSSSelectorList selectorList;
97 p.parseSelector(selectorText, selectorList);
98 if (!selectorList.first())
101 CSSStyleSheet::RuleMutationScope mutationScope(this);
103 String oldSelectorText = this->selectorText();
104 m_styleRule->wrapperAdoptSelectorList(selectorList);
106 if (hasCachedSelectorText()) {
107 ASSERT(selectorTextCache().contains(this));
108 selectorTextCache().set(this, generateSelectorText());
112 String CSSStyleRule::cssText() const
114 String result = selectorText();
117 result += m_styleRule->properties()->asText();
123 void CSSStyleRule::reattach(StyleRule* rule)
126 if (m_propertiesCSSOMWrapper)
127 m_propertiesCSSOMWrapper->reattach(m_styleRule->mutableProperties());
130 } // namespace WebCore