| 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 "CSSStyleSheet.h" |
| 27 | #include "PropertySetCSSStyleDeclaration.h" |
| 28 | #include "RuleSet.h" |
| 29 | #include "StyleProperties.h" |
| 30 | #include "StyleRule.h" |
| 31 | #include <wtf/NeverDestroyed.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 | static NeverDestroyed<SelectorTextCache> cache; |
| 40 | return cache; |
| 41 | } |
| 42 | |
| 43 | CSSStyleRule::CSSStyleRule(StyleRule& styleRule, CSSStyleSheet* parent) |
| 44 | : CSSRule(parent) |
| 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() |
| 61 | { |
| 62 | if (!m_propertiesCSSOMWrapper) |
| 63 | m_propertiesCSSOMWrapper = StyleRuleCSSStyleDeclaration::create(m_styleRule->mutableProperties(), *this); |
| 64 | return *m_propertiesCSSOMWrapper; |
| 65 | } |
| 66 | |
| 67 | String CSSStyleRule::generateSelectorText() const |
| 68 | { |
| 69 | return m_styleRule->selectorList().selectorsText(); |
| 70 | } |
| 71 | |
| 72 | String CSSStyleRule::selectorText() const |
| 73 | { |
| 74 | if (hasCachedSelectorText()) { |
| 75 | ASSERT(selectorTextCache().contains(this)); |
| 76 | return selectorTextCache().get(this); |
| 77 | } |
| 78 | |
| 79 | ASSERT(!selectorTextCache().contains(this)); |
| 80 | String text = generateSelectorText(); |
| 81 | selectorTextCache().set(this, text); |
| 82 | setHasCachedSelectorText(true); |
| 83 | return text; |
| 84 | } |
| 85 | |
| 86 | void CSSStyleRule::setSelectorText(const String& selectorText) |
| 87 | { |
| 88 | // FIXME: getMatchedCSSRules can return CSSStyleRules that are missing parent stylesheet pointer while |
| 89 | // referencing StyleRules that are part of stylesheet. Disallow mutations in this case. |
| 90 | if (!parentStyleSheet()) |
| 91 | return; |
| 92 | |
| 93 | CSSParser p(parserContext()); |
| 94 | CSSSelectorList selectorList; |
| 95 | p.parseSelector(selectorText, selectorList); |
| 96 | if (!selectorList.isValid()) |
| 97 | return; |
| 98 | |
| 99 | // NOTE: The selector list has to fit into RuleData. <http://webkit.org/b/118369> |
| 100 | if (selectorList.componentCount() > RuleData::maximumSelectorComponentCount) |
| 101 | return; |
| 102 | |
| 103 | CSSStyleSheet::RuleMutationScope mutationScope(this); |
| 104 | |
| 105 | m_styleRule->wrapperAdoptSelectorList(WTFMove(selectorList)); |
| 106 | |
| 107 | if (hasCachedSelectorText()) { |
| 108 | selectorTextCache().remove(this); |
| 109 | setHasCachedSelectorText(false); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | String CSSStyleRule::cssText() const |
| 114 | { |
| 115 | StringBuilder result; |
| 116 | result.append(selectorText()); |
| 117 | result.appendLiteral(" { " ); |
| 118 | String decls = m_styleRule->properties().asText(); |
| 119 | result.append(decls); |
| 120 | if (!decls.isEmpty()) |
| 121 | result.append(' '); |
| 122 | result.append('}'); |
| 123 | return result.toString(); |
| 124 | } |
| 125 | |
| 126 | void CSSStyleRule::reattach(StyleRuleBase& rule) |
| 127 | { |
| 128 | m_styleRule = downcast<StyleRule>(rule); |
| 129 | if (m_propertiesCSSOMWrapper) |
| 130 | m_propertiesCSSOMWrapper->reattach(m_styleRule->mutableProperties()); |
| 131 | } |
| 132 | |
| 133 | } // namespace WebCore |
| 134 | |