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, 2009, 2010, 2012 Apple Inc. All rights reserved.
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.
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.
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.
23 #include "core/css/StyleRuleImport.h"
25 #include "core/FetchInitiatorTypeNames.h"
26 #include "core/css/StyleSheetContents.h"
27 #include "core/dom/Document.h"
28 #include "core/fetch/CSSStyleSheetResource.h"
29 #include "core/fetch/FetchRequest.h"
30 #include "core/fetch/ResourceFetcher.h"
34 PassRefPtrWillBeRawPtr<StyleRuleImport> StyleRuleImport::create(const String& href, PassRefPtrWillBeRawPtr<MediaQuerySet> media)
36 return adoptRefWillBeNoop(new StyleRuleImport(href, media));
39 StyleRuleImport::StyleRuleImport(const String& href, PassRefPtrWillBeRawPtr<MediaQuerySet> media)
40 : StyleRuleBase(Import)
41 , m_parentStyleSheet(nullptr)
42 , m_styleSheetClient(this)
44 , m_mediaQueries(media)
49 m_mediaQueries = MediaQuerySet::create(String());
52 StyleRuleImport::~StyleRuleImport()
56 m_styleSheet->clearOwnerRule();
59 m_resource->removeClient(&m_styleSheetClient);
62 void StyleRuleImport::traceAfterDispatch(Visitor* visitor)
64 visitor->trace(m_parentStyleSheet);
65 visitor->trace(m_mediaQueries);
66 visitor->trace(m_styleSheet);
67 StyleRuleBase::traceAfterDispatch(visitor);
70 void StyleRuleImport::setCSSStyleSheet(const String& href, const KURL& baseURL, const String& charset, const CSSStyleSheetResource* cachedStyleSheet)
73 m_styleSheet->clearOwnerRule();
75 CSSParserContext context = m_parentStyleSheet ? m_parentStyleSheet->parserContext() : strictCSSParserContext();
76 context.setCharset(charset);
77 Document* document = m_parentStyleSheet ? m_parentStyleSheet->singleOwnerDocument() : 0;
78 if (!baseURL.isNull()) {
79 context.setBaseURL(baseURL);
81 context.setReferrer(Referrer(baseURL.strippedForUseAsReferrer(), document->referrerPolicy()));
84 m_styleSheet = StyleSheetContents::create(this, href, context);
86 m_styleSheet->parseAuthorStyleSheet(cachedStyleSheet, document ? document->securityOrigin() : 0);
90 if (m_parentStyleSheet) {
91 m_parentStyleSheet->notifyLoadedSheet(cachedStyleSheet);
92 m_parentStyleSheet->checkLoaded();
96 bool StyleRuleImport::isLoading() const
98 return m_loading || (m_styleSheet && m_styleSheet->isLoading());
101 void StyleRuleImport::requestStyleSheet()
103 if (!m_parentStyleSheet)
105 Document* document = m_parentStyleSheet->singleOwnerDocument();
109 ResourceFetcher* fetcher = document->fetcher();
114 if (!m_parentStyleSheet->baseURL().isNull())
115 // use parent styleheet's URL as the base URL
116 absURL = KURL(m_parentStyleSheet->baseURL(), m_strHref);
118 absURL = document->completeURL(m_strHref);
120 // Check for a cycle in our import chain. If we encounter a stylesheet
121 // in our parent chain with the same URL, then just bail.
122 StyleSheetContents* rootSheet = m_parentStyleSheet;
123 for (StyleSheetContents* sheet = m_parentStyleSheet; sheet; sheet = sheet->parentStyleSheet()) {
124 if (equalIgnoringFragmentIdentifier(absURL, sheet->baseURL())
125 || equalIgnoringFragmentIdentifier(absURL, document->completeURL(sheet->originalURL())))
130 FetchRequest request(ResourceRequest(absURL), FetchInitiatorTypeNames::css, m_parentStyleSheet->charset());
131 m_resource = fetcher->fetchCSSStyleSheet(request);
133 // if the import rule is issued dynamically, the sheet may be
134 // removed from the pending sheet count, so let the doc know
135 // the sheet being imported is pending.
136 if (m_parentStyleSheet && m_parentStyleSheet->loadCompleted() && rootSheet == m_parentStyleSheet)
137 m_parentStyleSheet->startLoadingDynamicSheet();
139 m_resource->addClient(&m_styleSheetClient);