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