Merge "[CherryPick] [WEBGL] Rename WEBKIT_WEBGL_lose_context to WEBGL_lose_context...
[framework/web/webkit-efl.git] / Source / WebCore / css / CSSStyleRule.cpp
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 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 "StylePropertySet.h"
31 #include "StyleRule.h"
32 #include <wtf/text/StringBuilder.h>
33
34 namespace WebCore {
35
36 typedef HashMap<const CSSStyleRule*, String> SelectorTextCache;
37 static SelectorTextCache& selectorTextCache()
38 {
39     DEFINE_STATIC_LOCAL(SelectorTextCache, cache, ());
40     return cache;
41 }
42
43 CSSStyleRule::CSSStyleRule(StyleRule* styleRule, CSSStyleSheet* parent)
44     : CSSRule(parent, CSSRule::STYLE_RULE)
45     , m_styleRule(styleRule)
46 {
47 }
48
49 CSSStyleRule::~CSSStyleRule()
50 {
51     if (m_propertiesCSSOMWrapper)
52         m_propertiesCSSOMWrapper->clearParentRule();
53
54     if (hasCachedSelectorText()) {
55         selectorTextCache().remove(this);
56         setHasCachedSelectorText(false);
57     }
58 }
59
60 CSSStyleDeclaration* CSSStyleRule::style() const
61 {
62     if (!m_propertiesCSSOMWrapper) {
63         m_propertiesCSSOMWrapper = StyleRuleCSSStyleDeclaration::create(m_styleRule->mutableProperties(), const_cast<CSSStyleRule*>(this));
64     }
65     return m_propertiesCSSOMWrapper.get();
66 }
67
68 String CSSStyleRule::generateSelectorText() const
69 {
70     StringBuilder builder;
71     for (CSSSelector* s = m_styleRule->selectorList().first(); s; s = CSSSelectorList::next(s)) {
72         if (s != m_styleRule->selectorList().first())
73             builder.append(", ");
74         builder.append(s->selectorText());
75     }
76     return builder.toString();
77 }
78
79 String CSSStyleRule::selectorText() const
80 {
81     if (hasCachedSelectorText()) {
82         ASSERT(selectorTextCache().contains(this));
83         return selectorTextCache().get(this);
84     }
85
86     ASSERT(!selectorTextCache().contains(this));
87     String text = generateSelectorText();
88     selectorTextCache().set(this, text);
89     setHasCachedSelectorText(true);
90     return text;
91 }
92
93 void CSSStyleRule::setSelectorText(const String& selectorText)
94 {
95     CSSParser p(parserContext());
96     CSSSelectorList selectorList;
97     p.parseSelector(selectorText, selectorList);
98     if (!selectorList.first())
99         return;
100
101     CSSStyleSheet::RuleMutationScope mutationScope(this);
102
103     String oldSelectorText = this->selectorText();
104     m_styleRule->wrapperAdoptSelectorList(selectorList);
105
106     if (hasCachedSelectorText()) {
107         ASSERT(selectorTextCache().contains(this));
108         selectorTextCache().set(this, generateSelectorText());
109     }
110 }
111
112 String CSSStyleRule::cssText() const
113 {
114     String result = selectorText();
115
116     result += " { ";
117     result += m_styleRule->properties()->asText();
118     result += "}";
119
120     return result;
121 }
122
123 void CSSStyleRule::reattach(StyleRule* rule)
124 {
125     m_styleRule = rule;
126     if (m_propertiesCSSOMWrapper)
127         m_propertiesCSSOMWrapper->reattach(m_styleRule->mutableProperties());
128 }
129
130 } // namespace WebCore