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 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
|
/*
* (C) 1999-2003 Lars Knoll (knoll@kde.org)
* Copyright (C) 2004, 2006, 2007, 2012 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 "core/css/StyleSheetContents.h"
#include "core/css/CSSStyleSheet.h"
#include "core/css/CSSTiming.h"
#include "core/css/StylePropertySet.h"
#include "core/css/StyleRule.h"
#include "core/css/StyleRuleImport.h"
#include "core/css/StyleRuleNamespace.h"
#include "core/css/parser/CSSParser.h"
#include "core/dom/Document.h"
#include "core/dom/Node.h"
#include "core/dom/StyleEngine.h"
#include "core/frame/UseCounter.h"
#include "core/inspector/InspectorTraceEvents.h"
#include "core/loader/resource/CSSStyleSheetResource.h"
#include "platform/Histogram.h"
#include "platform/instrumentation/tracing/TraceEvent.h"
#include "platform/weborigin/SecurityOrigin.h"
namespace blink {
// Rough size estimate for the memory cache.
unsigned StyleSheetContents::estimatedSizeInBytes() const {
// Note that this does not take into account size of the strings hanging from
// various objects. The assumption is that nearly all of of them are atomic
// and would exist anyway.
unsigned size = sizeof(*this);
// FIXME: This ignores the children of media rules.
// Most rules are StyleRules.
size += ruleCount() * StyleRule::averageSizeInBytes();
for (unsigned i = 0; i < m_importRules.size(); ++i) {
if (StyleSheetContents* sheet = m_importRules[i]->styleSheet())
size += sheet->estimatedSizeInBytes();
}
return size;
}
StyleSheetContents::StyleSheetContents(StyleRuleImport* ownerRule,
const String& originalURL,
const CSSParserContext* context)
: m_ownerRule(ownerRule),
m_originalURL(originalURL),
m_defaultNamespace(starAtom),
m_hasSyntacticallyValidCSSHeader(true),
m_didLoadErrorOccur(false),
m_isMutable(false),
m_hasFontFaceRule(false),
m_hasViewportRule(false),
m_hasMediaQueries(false),
m_hasSingleOwnerDocument(true),
m_isUsedFromTextCache(false),
m_parserContext(context) {}
StyleSheetContents::StyleSheetContents(const StyleSheetContents& o)
: m_ownerRule(nullptr),
m_originalURL(o.m_originalURL),
m_importRules(o.m_importRules.size()),
m_namespaceRules(o.m_namespaceRules.size()),
m_childRules(o.m_childRules.size()),
m_namespaces(o.m_namespaces),
m_defaultNamespace(o.m_defaultNamespace),
m_hasSyntacticallyValidCSSHeader(o.m_hasSyntacticallyValidCSSHeader),
m_didLoadErrorOccur(false),
m_isMutable(false),
m_hasFontFaceRule(o.m_hasFontFaceRule),
m_hasViewportRule(o.m_hasViewportRule),
m_hasMediaQueries(o.m_hasMediaQueries),
m_hasSingleOwnerDocument(true),
m_isUsedFromTextCache(false),
m_parserContext(o.m_parserContext) {
// FIXME: Copy import rules.
ASSERT(o.m_importRules.isEmpty());
for (unsigned i = 0; i < m_namespaceRules.size(); ++i) {
m_namespaceRules[i] =
static_cast<StyleRuleNamespace*>(o.m_namespaceRules[i]->copy());
}
// LazyParseCSS: Copying child rules is a strict point for lazy parsing, so
// there is no need to copy lazy parsing state here.
for (unsigned i = 0; i < m_childRules.size(); ++i)
m_childRules[i] = o.m_childRules[i]->copy();
}
StyleSheetContents::~StyleSheetContents() {}
void StyleSheetContents::setHasSyntacticallyValidCSSHeader(bool isValidCss) {
m_hasSyntacticallyValidCSSHeader = isValidCss;
}
bool StyleSheetContents::isCacheableForResource() const {
// This would require dealing with multiple clients for load callbacks.
if (!loadCompleted())
return false;
// FIXME: StyleSheets with media queries can't be cached because their RuleSet
// is processed differently based off the media queries, which might resolve
// differently depending on the context of the parent CSSStyleSheet (e.g.
// if they are in differently sized iframes). Once RuleSets are media query
// agnostic, we can restore sharing of StyleSheetContents with medea queries.
if (m_hasMediaQueries)
return false;
// FIXME: Support copying import rules.
if (!m_importRules.isEmpty())
return false;
// FIXME: Support cached stylesheets in import rules.
if (m_ownerRule)
return false;
if (m_didLoadErrorOccur)
return false;
// It is not the original sheet anymore.
if (m_isMutable)
return false;
// If the header is valid we are not going to need to check the
// SecurityOrigin.
// FIXME: Valid mime type avoids the check too.
if (!m_hasSyntacticallyValidCSSHeader)
return false;
return true;
}
bool StyleSheetContents::isCacheableForStyleElement() const {
// FIXME: Support copying import rules.
if (!importRules().isEmpty())
return false;
// Until import rules are supported in cached sheets it's not possible for
// loading to fail.
DCHECK(!didLoadErrorOccur());
// It is not the original sheet anymore.
if (isMutable())
return false;
if (!hasSyntacticallyValidCSSHeader())
return false;
return true;
}
void StyleSheetContents::parserAppendRule(StyleRuleBase* rule) {
if (rule->isImportRule()) {
// Parser enforces that @import rules come before anything else
ASSERT(m_childRules.isEmpty());
StyleRuleImport* importRule = toStyleRuleImport(rule);
if (importRule->mediaQueries())
setHasMediaQueries();
m_importRules.push_back(importRule);
m_importRules.back()->setParentStyleSheet(this);
m_importRules.back()->requestStyleSheet();
return;
}
if (rule->isNamespaceRule()) {
// Parser enforces that @namespace rules come before all rules other than
// import/charset rules
ASSERT(m_childRules.isEmpty());
StyleRuleNamespace& namespaceRule = toStyleRuleNamespace(*rule);
parserAddNamespace(namespaceRule.prefix(), namespaceRule.uri());
m_namespaceRules.push_back(&namespaceRule);
return;
}
m_childRules.push_back(rule);
}
void StyleSheetContents::setHasMediaQueries() {
m_hasMediaQueries = true;
if (parentStyleSheet())
parentStyleSheet()->setHasMediaQueries();
}
StyleRuleBase* StyleSheetContents::ruleAt(unsigned index) const {
SECURITY_DCHECK(index < ruleCount());
if (index < m_importRules.size())
return m_importRules[index].get();
index -= m_importRules.size();
if (index < m_namespaceRules.size())
return m_namespaceRules[index].get();
index -= m_namespaceRules.size();
return m_childRules[index].get();
}
unsigned StyleSheetContents::ruleCount() const {
return m_importRules.size() + m_namespaceRules.size() + m_childRules.size();
}
void StyleSheetContents::clearRules() {
for (unsigned i = 0; i < m_importRules.size(); ++i) {
ASSERT(m_importRules.at(i)->parentStyleSheet() == this);
m_importRules[i]->clearParentStyleSheet();
}
m_importRules.clear();
m_namespaceRules.clear();
m_childRules.clear();
}
bool StyleSheetContents::wrapperInsertRule(StyleRuleBase* rule,
unsigned index) {
ASSERT(m_isMutable);
SECURITY_DCHECK(index <= ruleCount());
if (index < m_importRules.size() ||
(index == m_importRules.size() && rule->isImportRule())) {
// Inserting non-import rule before @import is not allowed.
if (!rule->isImportRule())
return false;
StyleRuleImport* importRule = toStyleRuleImport(rule);
if (importRule->mediaQueries())
setHasMediaQueries();
m_importRules.insert(index, importRule);
m_importRules[index]->setParentStyleSheet(this);
m_importRules[index]->requestStyleSheet();
// FIXME: Stylesheet doesn't actually change meaningfully before the
// imported sheets are loaded.
return true;
}
// Inserting @import rule after a non-import rule is not allowed.
if (rule->isImportRule())
return false;
index -= m_importRules.size();
if (index < m_namespaceRules.size() ||
(index == m_namespaceRules.size() && rule->isNamespaceRule())) {
// Inserting non-namespace rules other than import rule before @namespace is
// not allowed.
if (!rule->isNamespaceRule())
return false;
// Inserting @namespace rule when rules other than import/namespace/charset
// are present is not allowed.
if (!m_childRules.isEmpty())
return false;
StyleRuleNamespace* namespaceRule = toStyleRuleNamespace(rule);
m_namespaceRules.insert(index, namespaceRule);
// For now to be compatible with IE and Firefox if namespace rule with same
// prefix is added irrespective of adding the rule at any index, last added
// rule's value is considered.
// TODO (ramya.v@samsung.com): As per spec last valid rule should be
// considered, which means if namespace rule is added in the middle of
// existing namespace rules, rule which comes later in rule list with same
// prefix needs to be considered.
parserAddNamespace(namespaceRule->prefix(), namespaceRule->uri());
return true;
}
if (rule->isNamespaceRule())
return false;
index -= m_namespaceRules.size();
m_childRules.insert(index, rule);
return true;
}
bool StyleSheetContents::wrapperDeleteRule(unsigned index) {
ASSERT(m_isMutable);
SECURITY_DCHECK(index < ruleCount());
if (index < m_importRules.size()) {
m_importRules[index]->clearParentStyleSheet();
if (m_importRules[index]->isFontFaceRule())
notifyRemoveFontFaceRule(toStyleRuleFontFace(m_importRules[index].get()));
m_importRules.remove(index);
return true;
}
index -= m_importRules.size();
if (index < m_namespaceRules.size()) {
if (!m_childRules.isEmpty())
return false;
m_namespaceRules.remove(index);
return true;
}
index -= m_namespaceRules.size();
if (m_childRules[index]->isFontFaceRule())
notifyRemoveFontFaceRule(toStyleRuleFontFace(m_childRules[index].get()));
m_childRules.remove(index);
return true;
}
void StyleSheetContents::parserAddNamespace(const AtomicString& prefix,
const AtomicString& uri) {
ASSERT(!uri.isNull());
if (prefix.isNull()) {
m_defaultNamespace = uri;
return;
}
PrefixNamespaceURIMap::AddResult result = m_namespaces.add(prefix, uri);
if (result.isNewEntry)
return;
result.storedValue->value = uri;
}
const AtomicString& StyleSheetContents::namespaceURIFromPrefix(
const AtomicString& prefix) {
return m_namespaces.get(prefix);
}
void StyleSheetContents::parseAuthorStyleSheet(
const CSSStyleSheetResource* cachedStyleSheet,
const SecurityOrigin* securityOrigin) {
TRACE_EVENT1("blink,devtools.timeline", "ParseAuthorStyleSheet", "data",
InspectorParseAuthorStyleSheetEvent::data(cachedStyleSheet));
double startTime = monotonicallyIncreasingTime();
bool isSameOriginRequest =
securityOrigin && securityOrigin->canRequest(baseURL());
// When the response was fetched via the Service Worker, the original URL may
// not be same as the base URL.
// TODO(horo): When we will use the original URL as the base URL, we can
// remove this check. crbug.com/553535
if (cachedStyleSheet->response().wasFetchedViaServiceWorker()) {
const KURL originalURL(
cachedStyleSheet->response().originalURLViaServiceWorker());
// |originalURL| is empty when the response is created in the SW.
if (!originalURL.isEmpty() && !securityOrigin->canRequest(originalURL))
isSameOriginRequest = false;
}
CSSStyleSheetResource::MIMETypeCheck mimeTypeCheck =
isQuirksModeBehavior(m_parserContext->mode()) && isSameOriginRequest
? CSSStyleSheetResource::MIMETypeCheck::Lax
: CSSStyleSheetResource::MIMETypeCheck::Strict;
String sheetText = cachedStyleSheet->sheetText(mimeTypeCheck);
const ResourceResponse& response = cachedStyleSheet->response();
m_sourceMapURL = response.httpHeaderField(HTTPNames::SourceMap);
if (m_sourceMapURL.isEmpty()) {
// Try to get deprecated header.
m_sourceMapURL = response.httpHeaderField(HTTPNames::X_SourceMap);
}
const CSSParserContext* context =
CSSParserContext::createWithStyleSheetContents(parserContext(), this);
CSSParser::parseSheet(context, this, sheetText,
RuntimeEnabledFeatures::lazyParseCSSEnabled());
DEFINE_STATIC_LOCAL(CustomCountHistogram, parseHistogram,
("Style.AuthorStyleSheet.ParseTime", 0, 10000000, 50));
double parseDurationSeconds = (monotonicallyIncreasingTime() - startTime);
parseHistogram.count(parseDurationSeconds * 1000 * 1000);
if (Document* document = singleOwnerDocument()) {
CSSTiming::from(*document).recordAuthorStyleSheetParseTime(
parseDurationSeconds);
}
}
void StyleSheetContents::parseString(const String& sheetText) {
parseStringAtPosition(sheetText, TextPosition::minimumPosition());
}
void StyleSheetContents::parseStringAtPosition(
const String& sheetText,
const TextPosition& startPosition) {
const CSSParserContext* context =
CSSParserContext::createWithStyleSheetContents(parserContext(), this);
CSSParser::parseSheet(context, this, sheetText);
}
bool StyleSheetContents::isLoading() const {
for (unsigned i = 0; i < m_importRules.size(); ++i) {
if (m_importRules[i]->isLoading())
return true;
}
return false;
}
bool StyleSheetContents::loadCompleted() const {
StyleSheetContents* parentSheet = parentStyleSheet();
if (parentSheet)
return parentSheet->loadCompleted();
StyleSheetContents* root = rootStyleSheet();
return root->m_loadingClients.isEmpty();
}
void StyleSheetContents::checkLoaded() {
if (isLoading())
return;
StyleSheetContents* parentSheet = parentStyleSheet();
if (parentSheet) {
parentSheet->checkLoaded();
return;
}
ASSERT(this == rootStyleSheet());
if (m_loadingClients.isEmpty())
return;
// Avoid |CSSSStyleSheet| and |ownerNode| being deleted by scripts that run
// via ScriptableDocumentParser::executeScriptsWaitingForResources(). Also
// protect the |CSSStyleSheet| from being deleted during iteration via the
// |sheetLoaded| method.
//
// When a sheet is loaded it is moved from the set of loading clients
// to the set of completed clients. We therefore need the copy in order to
// not modify the set while iterating it.
HeapVector<Member<CSSStyleSheet>> loadingClients;
copyToVector(m_loadingClients, loadingClients);
for (unsigned i = 0; i < loadingClients.size(); ++i) {
if (loadingClients[i]->loadCompleted())
continue;
// sheetLoaded might be invoked after its owner node is removed from
// document.
if (Node* ownerNode = loadingClients[i]->ownerNode()) {
if (loadingClients[i]->sheetLoaded())
ownerNode->notifyLoadedSheetAndAllCriticalSubresources(
m_didLoadErrorOccur ? Node::ErrorOccurredLoadingSubresource
: Node::NoErrorLoadingSubresource);
}
}
}
void StyleSheetContents::notifyLoadedSheet(const CSSStyleSheetResource* sheet) {
ASSERT(sheet);
m_didLoadErrorOccur |= sheet->errorOccurred();
// updateLayoutIgnorePendingStyleSheets can cause us to create the RuleSet on
// this sheet before its imports have loaded. So clear the RuleSet when the
// imports load since the import's subrules are flattened into its parent
// sheet's RuleSet.
clearRuleSet();
}
void StyleSheetContents::startLoadingDynamicSheet() {
StyleSheetContents* root = rootStyleSheet();
for (const auto& client : root->m_loadingClients)
client->startLoadingDynamicSheet();
// Copy the completed clients to a vector for iteration.
// startLoadingDynamicSheet will move the style sheet from the completed state
// to the loading state which modifies the set of completed clients. We
// therefore need the copy in order to not modify the set of completed clients
// while iterating it.
HeapVector<Member<CSSStyleSheet>> completedClients;
copyToVector(root->m_completedClients, completedClients);
for (unsigned i = 0; i < completedClients.size(); ++i)
completedClients[i]->startLoadingDynamicSheet();
}
StyleSheetContents* StyleSheetContents::rootStyleSheet() const {
const StyleSheetContents* root = this;
while (root->parentStyleSheet())
root = root->parentStyleSheet();
return const_cast<StyleSheetContents*>(root);
}
bool StyleSheetContents::hasSingleOwnerNode() const {
return rootStyleSheet()->hasOneClient();
}
Node* StyleSheetContents::singleOwnerNode() const {
StyleSheetContents* root = rootStyleSheet();
if (!root->hasOneClient())
return nullptr;
if (root->m_loadingClients.size())
return (*root->m_loadingClients.begin())->ownerNode();
return (*root->m_completedClients.begin())->ownerNode();
}
Document* StyleSheetContents::singleOwnerDocument() const {
StyleSheetContents* root = rootStyleSheet();
return root->clientSingleOwnerDocument();
}
Document* StyleSheetContents::anyOwnerDocument() const {
return rootStyleSheet()->clientAnyOwnerDocument();
}
static bool childRulesHaveFailedOrCanceledSubresources(
const HeapVector<Member<StyleRuleBase>>& rules) {
for (unsigned i = 0; i < rules.size(); ++i) {
const StyleRuleBase* rule = rules[i].get();
switch (rule->type()) {
case StyleRuleBase::Style:
if (toStyleRule(rule)->propertiesHaveFailedOrCanceledSubresources())
return true;
break;
case StyleRuleBase::FontFace:
if (toStyleRuleFontFace(rule)
->properties()
.hasFailedOrCanceledSubresources())
return true;
break;
case StyleRuleBase::Media:
if (childRulesHaveFailedOrCanceledSubresources(
toStyleRuleMedia(rule)->childRules()))
return true;
break;
case StyleRuleBase::Charset:
case StyleRuleBase::Import:
case StyleRuleBase::Namespace:
ASSERT_NOT_REACHED();
case StyleRuleBase::Page:
case StyleRuleBase::Keyframes:
case StyleRuleBase::Keyframe:
case StyleRuleBase::Supports:
case StyleRuleBase::Viewport:
break;
}
}
return false;
}
bool StyleSheetContents::hasFailedOrCanceledSubresources() const {
ASSERT(isCacheableForResource());
return childRulesHaveFailedOrCanceledSubresources(m_childRules);
}
Document* StyleSheetContents::clientAnyOwnerDocument() const {
if (clientSize() <= 0)
return nullptr;
if (m_loadingClients.size())
return (*m_loadingClients.begin())->ownerDocument();
return (*m_completedClients.begin())->ownerDocument();
}
Document* StyleSheetContents::clientSingleOwnerDocument() const {
return m_hasSingleOwnerDocument ? clientAnyOwnerDocument() : nullptr;
}
StyleSheetContents* StyleSheetContents::parentStyleSheet() const {
return m_ownerRule ? m_ownerRule->parentStyleSheet() : nullptr;
}
void StyleSheetContents::registerClient(CSSStyleSheet* sheet) {
ASSERT(!m_loadingClients.contains(sheet) &&
!m_completedClients.contains(sheet));
// InspectorCSSAgent::buildObjectForRule creates CSSStyleSheet without any
// owner node.
if (!sheet->ownerDocument())
return;
if (Document* document = clientSingleOwnerDocument()) {
if (sheet->ownerDocument() != document)
m_hasSingleOwnerDocument = false;
}
m_loadingClients.add(sheet);
}
void StyleSheetContents::unregisterClient(CSSStyleSheet* sheet) {
m_loadingClients.remove(sheet);
m_completedClients.remove(sheet);
if (!sheet->ownerDocument() || !m_loadingClients.isEmpty() ||
!m_completedClients.isEmpty())
return;
m_hasSingleOwnerDocument = true;
}
void StyleSheetContents::clientLoadCompleted(CSSStyleSheet* sheet) {
ASSERT(m_loadingClients.contains(sheet) || !sheet->ownerDocument());
m_loadingClients.remove(sheet);
// In m_ownerNode->sheetLoaded, the CSSStyleSheet might be detached.
// (i.e. clearOwnerNode was invoked.)
// In this case, we don't need to add the stylesheet to completed clients.
if (!sheet->ownerDocument())
return;
m_completedClients.add(sheet);
}
void StyleSheetContents::clientLoadStarted(CSSStyleSheet* sheet) {
ASSERT(m_completedClients.contains(sheet));
m_completedClients.remove(sheet);
m_loadingClients.add(sheet);
}
void StyleSheetContents::setReferencedFromResource(
CSSStyleSheetResource* resource) {
DCHECK(resource);
DCHECK(!isReferencedFromResource());
DCHECK(isCacheableForResource());
m_referencedFromResource = resource;
}
void StyleSheetContents::clearReferencedFromResource() {
DCHECK(isReferencedFromResource());
DCHECK(isCacheableForResource());
m_referencedFromResource = nullptr;
}
RuleSet& StyleSheetContents::ensureRuleSet(const MediaQueryEvaluator& medium,
AddRuleFlags addRuleFlags) {
if (!m_ruleSet) {
m_ruleSet = RuleSet::create();
m_ruleSet->addRulesFromSheet(this, medium, addRuleFlags);
}
return *m_ruleSet.get();
}
static void setNeedsActiveStyleUpdateForClients(
HeapHashSet<WeakMember<CSSStyleSheet>>& clients) {
for (const auto& sheet : clients) {
Document* document = sheet->ownerDocument();
Node* node = sheet->ownerNode();
if (!document || !node || !node->isConnected())
continue;
document->styleEngine().setNeedsActiveStyleUpdate(node->treeScope());
}
}
void StyleSheetContents::clearRuleSet() {
if (StyleSheetContents* parentSheet = parentStyleSheet())
parentSheet->clearRuleSet();
if (!m_ruleSet)
return;
m_ruleSet.clear();
setNeedsActiveStyleUpdateForClients(m_loadingClients);
setNeedsActiveStyleUpdateForClients(m_completedClients);
}
static void removeFontFaceRules(HeapHashSet<WeakMember<CSSStyleSheet>>& clients,
const StyleRuleFontFace* fontFaceRule) {
for (const auto& sheet : clients) {
if (Node* ownerNode = sheet->ownerNode())
ownerNode->document().styleEngine().removeFontFaceRules(
HeapVector<Member<const StyleRuleFontFace>>(1, fontFaceRule));
}
}
void StyleSheetContents::notifyRemoveFontFaceRule(
const StyleRuleFontFace* fontFaceRule) {
StyleSheetContents* root = rootStyleSheet();
removeFontFaceRules(root->m_loadingClients, fontFaceRule);
removeFontFaceRules(root->m_completedClients, fontFaceRule);
}
static void findFontFaceRulesFromRules(
const HeapVector<Member<StyleRuleBase>>& rules,
HeapVector<Member<const StyleRuleFontFace>>& fontFaceRules) {
for (unsigned i = 0; i < rules.size(); ++i) {
StyleRuleBase* rule = rules[i].get();
if (rule->isFontFaceRule()) {
fontFaceRules.push_back(toStyleRuleFontFace(rule));
} else if (rule->isMediaRule()) {
StyleRuleMedia* mediaRule = toStyleRuleMedia(rule);
// We cannot know whether the media rule matches or not, but
// for safety, remove @font-face in the media rule (if exists).
findFontFaceRulesFromRules(mediaRule->childRules(), fontFaceRules);
}
}
}
void StyleSheetContents::findFontFaceRules(
HeapVector<Member<const StyleRuleFontFace>>& fontFaceRules) {
for (unsigned i = 0; i < m_importRules.size(); ++i) {
if (!m_importRules[i]->styleSheet())
continue;
m_importRules[i]->styleSheet()->findFontFaceRules(fontFaceRules);
}
findFontFaceRulesFromRules(childRules(), fontFaceRules);
}
DEFINE_TRACE(StyleSheetContents) {
visitor->trace(m_ownerRule);
visitor->trace(m_importRules);
visitor->trace(m_namespaceRules);
visitor->trace(m_childRules);
visitor->trace(m_loadingClients);
visitor->trace(m_completedClients);
visitor->trace(m_ruleSet);
visitor->trace(m_referencedFromResource);
visitor->trace(m_parserContext);
}
} // namespace blink
|