1 /* 2 * (C) 1999-2003 Lars Knoll (knoll (at) kde.org) 3 * (C) 2002-2003 Dirk Mueller (mueller (at) kde.org) 4 * Copyright (C) 2002, 2005, 2006, 2008, 2012 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 "core/css/CSSStyleRule.h" 24 25 #include "core/css/CSSSelector.h" 26 #include "core/css/CSSStyleSheet.h" 27 #include "core/css/PropertySetCSSStyleDeclaration.h" 28 #include "core/css/StylePropertySet.h" 29 #include "core/css/StyleRule.h" 30 #include "core/css/parser/CSSParser.h" 31 #include "wtf/text/StringBuilder.h" 32 33 namespace blink { 34 35 typedef HashMap<const CSSStyleRule*, String> SelectorTextCache; 36 static SelectorTextCache& selectorTextCache() 37 { 38 DEFINE_STATIC_LOCAL(SelectorTextCache, cache, ()); 39 return cache; 40 } 41 42 CSSStyleRule::CSSStyleRule(StyleRule* styleRule, CSSStyleSheet* parent) 43 : CSSRule(parent) 44 , m_styleRule(styleRule) 45 { 46 } 47 48 CSSStyleRule::~CSSStyleRule() 49 { 50 #if !ENABLE(OILPAN) 51 if (m_propertiesCSSOMWrapper) 52 m_propertiesCSSOMWrapper->clearParentRule(); 53 #endif 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 (const CSSSelector* selector = m_styleRule->selectorList().first(); selector; selector = CSSSelectorList::next(*selector)) { 72 if (selector != m_styleRule->selectorList().first()) 73 builder.appendLiteral(", "); 74 builder.append(selector->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 CSSParserContext context(parserContext(), 0); 96 CSSParser p(context); 97 CSSSelectorList selectorList; 98 p.parseSelector(selectorText, selectorList); 99 if (!selectorList.isValid()) 100 return; 101 102 CSSStyleSheet::RuleMutationScope mutationScope(this); 103 104 m_styleRule->wrapperAdoptSelectorList(selectorList); 105 106 if (hasCachedSelectorText()) { 107 selectorTextCache().remove(this); 108 setHasCachedSelectorText(false); 109 } 110 } 111 112 String CSSStyleRule::cssText() const 113 { 114 StringBuilder result; 115 result.append(selectorText()); 116 result.appendLiteral(" { "); 117 String decls = m_styleRule->properties().asText(); 118 result.append(decls); 119 if (!decls.isEmpty()) 120 result.append(' '); 121 result.append('}'); 122 return result.toString(); 123 } 124 125 void CSSStyleRule::reattach(StyleRuleBase* rule) 126 { 127 ASSERT(rule); 128 m_styleRule = toStyleRule(rule); 129 if (m_propertiesCSSOMWrapper) 130 m_propertiesCSSOMWrapper->reattach(m_styleRule->mutableProperties()); 131 } 132 133 void CSSStyleRule::trace(Visitor* visitor) 134 { 135 visitor->trace(m_styleRule); 136 visitor->trace(m_propertiesCSSOMWrapper); 137 CSSRule::trace(visitor); 138 } 139 140 } // namespace blink 141