1 | /*
|
---|
2 | * (C) 1999-2003 Lars Knoll ([email protected])
|
---|
3 | * (C) 2002-2003 Dirk Mueller ([email protected])
|
---|
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 | auto selectorList = p.parseSelector(selectorText);
|
---|
95 | if (!selectorList)
|
---|
96 | return;
|
---|
97 |
|
---|
98 | // NOTE: The selector list has to fit into RuleData. <http://webkit.org/b/118369>
|
---|
99 | if (selectorList->componentCount() > Style::RuleData::maximumSelectorComponentCount)
|
---|
100 | return;
|
---|
101 |
|
---|
102 | CSSStyleSheet::RuleMutationScope mutationScope(this);
|
---|
103 |
|
---|
104 | m_styleRule->wrapperAdoptSelectorList(WTFMove(*selectorList));
|
---|
105 |
|
---|
106 | if (hasCachedSelectorText()) {
|
---|
107 | selectorTextCache().remove(this);
|
---|
108 | setHasCachedSelectorText(false);
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | String CSSStyleRule::cssText() const
|
---|
113 | {
|
---|
114 | String declarations = m_styleRule->properties().asText();
|
---|
115 | if (declarations.isEmpty())
|
---|
116 | return makeString(selectorText(), " { }");
|
---|
117 | return makeString(selectorText(), " { ", declarations, " }");
|
---|
118 | }
|
---|
119 |
|
---|
120 | void CSSStyleRule::reattach(StyleRuleBase& rule)
|
---|
121 | {
|
---|
122 | m_styleRule = downcast<StyleRule>(rule);
|
---|
123 | if (m_propertiesCSSOMWrapper)
|
---|
124 | m_propertiesCSSOMWrapper->reattach(m_styleRule->mutableProperties());
|
---|
125 | }
|
---|
126 |
|
---|
127 | } // namespace WebCore
|
---|