1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/*
* (C) 1999-2003 Lars Knoll (knoll@kde.org)
* (C) 2002-2003 Dirk Mueller (mueller@kde.org)
* Copyright (C) 2002-2024 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "CSSStyleRule.h"
#include "CSSGroupingRule.h"
#include "CSSParser.h"
#include "CSSParserEnum.h"
#include "CSSParserImpl.h"
#include "CSSRuleList.h"
#include "CSSSerializationContext.h"
#include "CSSStyleSheet.h"
#include "DeclaredStylePropertyMap.h"
#include "MutableStyleProperties.h"
#include "PropertySetCSSStyleDeclaration.h"
#include "RuleSet.h"
#include "StyleProperties.h"
#include "StyleRule.h"
#include <wtf/NeverDestroyed.h>
#include <wtf/text/StringBuilder.h>
namespace WebCore {
typedef UncheckedKeyHashMap<const CSSStyleRule*, String> SelectorTextCache;
static SelectorTextCache& selectorTextCache()
{
static NeverDestroyed<SelectorTextCache> cache;
return cache;
}
CSSStyleRule::CSSStyleRule(StyleRule& styleRule, CSSStyleSheet* parent)
: CSSRule(parent)
, m_styleRule(styleRule)
, m_styleMap(DeclaredStylePropertyMap::create(*this))
, m_childRuleCSSOMWrappers(0)
{
}
CSSStyleRule::CSSStyleRule(StyleRuleWithNesting& styleRule, CSSStyleSheet* parent)
: CSSRule(parent)
, m_styleRule(styleRule)
, m_styleMap(DeclaredStylePropertyMap::create(*this))
, m_childRuleCSSOMWrappers(styleRule.nestedRules().size())
{
}
CSSStyleRule::~CSSStyleRule()
{
if (m_propertiesCSSOMWrapper)
m_propertiesCSSOMWrapper->clearParentRule();
if (hasCachedSelectorText()) {
selectorTextCache().remove(this);
setHasCachedSelectorText(false);
}
}
CSSStyleDeclaration& CSSStyleRule::style()
{
if (!m_propertiesCSSOMWrapper)
m_propertiesCSSOMWrapper = StyleRuleCSSStyleDeclaration::create(m_styleRule->mutableProperties(), *this);
return *m_propertiesCSSOMWrapper;
}
StylePropertyMap& CSSStyleRule::styleMap()
{
return m_styleMap.get();
}
String CSSStyleRule::generateSelectorText() const
{
if (auto* styleRule = dynamicDowncast<StyleRuleWithNesting>(m_styleRule.get()))
return styleRule->originalSelectorList().selectorsText();
return m_styleRule->selectorList().selectorsText();
}
String CSSStyleRule::selectorText() const
{
if (hasCachedSelectorText()) {
ASSERT(selectorTextCache().contains(this));
return selectorTextCache().get(this);
}
ASSERT(!selectorTextCache().contains(this));
String text = generateSelectorText();
selectorTextCache().set(this, text);
setHasCachedSelectorText(true);
return text;
}
void CSSStyleRule::setSelectorText(const String& selectorText)
{
// FIXME: getMatchedCSSRules can return CSSStyleRules that are missing parent stylesheet pointer while
// referencing StyleRules that are part of stylesheet. Disallow mutations in this case.
if (!parentStyleSheet())
return;
CSSParser p(parserContext());
RefPtr sheet = parentStyleSheet();
auto selectorList = p.parseSelectorList(selectorText, sheet ? &sheet->contents() : nullptr, nestedContext());
if (!selectorList)
return;
// NOTE: The selector list has to fit into RuleData. <http://webkit.org/b/118369>
if (selectorList->componentCount() > Style::RuleData::maximumSelectorComponentCount)
return;
CSSStyleSheet::RuleMutationScope mutationScope(this);
if (auto* styleRule = dynamicDowncast<StyleRuleWithNesting>(m_styleRule.get()))
styleRule->wrapperAdoptOriginalSelectorList(WTFMove(*selectorList));
else
m_styleRule->wrapperAdoptSelectorList(WTFMove(*selectorList));
if (hasCachedSelectorText()) {
selectorTextCache().remove(this);
setHasCachedSelectorText(false);
}
}
Vector<Ref<StyleRuleBase>> CSSStyleRule::nestedRules() const
{
if (auto* styleRule = dynamicDowncast<StyleRuleWithNesting>(m_styleRule.get()))
return styleRule->nestedRules();
return { };
}
// https://w3c.github.io/csswg-drafts/cssom-1/#serialize-a-css-rule
String CSSStyleRule::cssText() const
{
auto declarationsString = m_styleRule->properties().asText(CSS::defaultSerializationContext());
StringBuilder declarations;
StringBuilder rules;
declarations.append(declarationsString);
cssTextForRules(rules);
return cssTextInternal(declarations, rules);
}
void CSSStyleRule::cssTextForRules(StringBuilder& rules) const
{
for (unsigned index = 0; index < length(); ++index) {
auto ruleText = item(index)->cssText();
if (!ruleText.isEmpty())
rules.append("\n "_s, WTFMove(ruleText));
}
}
String CSSStyleRule::cssText(const CSS::SerializationContext& context) const
{
StringBuilder declarations;
StringBuilder rules;
auto declarationsString = m_styleRule->properties().asText(context);
declarations.append(declarationsString);
cssTextForRulesWithReplacementURLs(rules, context);
return cssTextInternal(declarations, rules);
}
void CSSStyleRule::cssTextForRulesWithReplacementURLs(StringBuilder& rules, const CSS::SerializationContext& context) const
{
for (unsigned index = 0; index < length(); index++)
rules.append("\n "_s, item(index)->cssText(context));
}
String CSSStyleRule::cssTextInternal(StringBuilder& declarations, StringBuilder& rules) const
{
StringBuilder builder;
builder.append(selectorText(), " {"_s);
if (declarations.isEmpty() && rules.isEmpty()) {
builder.append(" }"_s);
return builder.toString();
}
if (rules.isEmpty()) {
builder.append(' ');
builder.append(declarations);
builder.append(" }"_s);
return builder.toString();
}
if (declarations.isEmpty()) {
builder.append(rules);
builder.append("\n}"_s);
return builder.toString();
}
builder.append("\n "_s);
builder.append(declarations);
builder.append(rules);
builder.append("\n}"_s);
return builder.toString();
}
// FIXME: share all methods below with CSSGroupingRule.
void CSSStyleRule::reattach(StyleRuleBase& rule)
{
if (auto* styleRuleWithNesting = dynamicDowncast<StyleRuleWithNesting>(rule))
m_styleRule = *styleRuleWithNesting;
else
m_styleRule = downcast<StyleRule>(rule);
if (m_propertiesCSSOMWrapper)
m_propertiesCSSOMWrapper->reattach(m_styleRule->mutableProperties());
}
ExceptionOr<unsigned> CSSStyleRule::insertRule(const String& ruleString, unsigned index)
{
ASSERT(m_childRuleCSSOMWrappers.size() == nestedRules().size());
if (index > nestedRules().size())
return Exception { ExceptionCode::IndexSizeError };
RefPtr styleSheet = parentStyleSheet();
RefPtr newRule = CSSParser::parseRule(parserContext(), styleSheet ? &styleSheet->contents() : nullptr, ruleString, CSSParserEnum::NestedContextType::Style);
if (!newRule) {
newRule = CSSParserImpl::parseNestedDeclarations(parserContext(), ruleString);
if (!newRule)
return Exception { ExceptionCode::SyntaxError };
}
// We only accepts style rule, nested group rule (@media,...) or nested declarations inside style rules.
if (!newRule->isStyleRule() && !newRule->isGroupRule() && !newRule->isNestedDeclarationsRule())
return Exception { ExceptionCode::HierarchyRequestError };
CSSStyleSheet::RuleMutationScope mutationScope(this);
if (!m_styleRule->isStyleRuleWithNesting()) {
// Call the parent rule (or parent stylesheet if top-level or nothing if it's an orphaned rule) to transform the current StyleRule to StyleRuleWithNesting.
RefPtr<StyleRuleWithNesting> styleRuleWithNesting;
if (RefPtr parent = parentRule())
styleRuleWithNesting = parent->prepareChildStyleRuleForNesting(m_styleRule.get());
else if (RefPtr parent = parentStyleSheet())
styleRuleWithNesting = parent->prepareChildStyleRuleForNesting(m_styleRule.get());
else
styleRuleWithNesting = StyleRuleWithNesting::create(WTFMove(m_styleRule.get()));
ASSERT(styleRuleWithNesting);
m_styleRule = *styleRuleWithNesting;
}
if (auto styleSheet = parentStyleSheet())
styleSheet->contents().clearHasNestingRulesCache();
downcast<StyleRuleWithNesting>(m_styleRule)->nestedRules().insert(index, newRule.releaseNonNull());
m_childRuleCSSOMWrappers.insert(index, RefPtr<CSSRule>());
return index;
}
ExceptionOr<void> CSSStyleRule::deleteRule(unsigned index)
{
ASSERT(m_childRuleCSSOMWrappers.size() == nestedRules().size());
if (index >= nestedRules().size()) {
// IndexSizeError: Raised if the specified index does not correspond to a
// rule in the media rule list.
return Exception { ExceptionCode::IndexSizeError };
}
auto& rules = downcast<StyleRuleWithNesting>(m_styleRule.get()).nestedRules();
CSSStyleSheet::RuleMutationScope mutationScope(this);
rules.remove(index);
if (m_childRuleCSSOMWrappers[index])
m_childRuleCSSOMWrappers[index]->setParentRule(nullptr);
m_childRuleCSSOMWrappers.remove(index);
return { };
}
unsigned CSSStyleRule::length() const
{
return nestedRules().size();
}
CSSRule* CSSStyleRule::item(unsigned index) const
{
if (index >= length())
return nullptr;
ASSERT(m_childRuleCSSOMWrappers.size() == nestedRules().size());
auto& rule = m_childRuleCSSOMWrappers[index];
if (!rule)
rule = nestedRules()[index]->createCSSOMWrapper(const_cast<CSSStyleRule&>(*this));
return rule.get();
}
CSSRuleList& CSSStyleRule::cssRules() const
{
if (!m_ruleListCSSOMWrapper)
m_ruleListCSSOMWrapper = makeUniqueWithoutRefCountedCheck<LiveCSSRuleList<CSSStyleRule>>(const_cast<CSSStyleRule&>(*this));
return *m_ruleListCSSOMWrapper;
}
void CSSStyleRule::getChildStyleSheets(UncheckedKeyHashSet<RefPtr<CSSStyleSheet>>& childStyleSheets)
{
for (unsigned index = 0; index < length(); ++index)
item(index)->getChildStyleSheets(childStyleSheets);
}
} // namespace WebCore
|