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 "StyleRuleImport.h"
25 #include "CSSStyleSheet.h"
26 #include "CachedCSSStyleSheet.h"
27 #include "CachedResourceLoader.h"
29 #include "SecurityOrigin.h"
30 #include "StyleSheetContents.h"
31 #include <wtf/StdLibExtras.h>
35 PassRefPtr<StyleRuleImport> StyleRuleImport::create(const String& href, PassRefPtr<MediaQuerySet> media)
37 return adoptRef(new StyleRuleImport(href, media));
40 StyleRuleImport::StyleRuleImport(const String& href, PassRefPtr<MediaQuerySet> media)
41 : StyleRuleBase(Import, 0)
42 , m_parentStyleSheet(0)
43 , m_styleSheetClient(this)
45 , m_mediaQueries(media)
50 m_mediaQueries = MediaQuerySet::create(String());
53 StyleRuleImport::~StyleRuleImport()
56 m_styleSheet->clearOwnerRule();
58 m_cachedSheet->removeClient(&m_styleSheetClient);
61 void StyleRuleImport::setCSSStyleSheet(const String& href, const KURL& baseURL, const String& charset, const CachedCSSStyleSheet* cachedStyleSheet)
64 m_styleSheet->clearOwnerRule();
66 CSSParserContext context = m_parentStyleSheet ? m_parentStyleSheet->parserContext() : CSSStrictMode;
67 context.charset = charset;
68 if (!baseURL.isNull())
69 context.baseURL = baseURL;
71 m_styleSheet = StyleSheetContents::create(this, href, baseURL, context);
73 Document* document = m_parentStyleSheet ? m_parentStyleSheet->singleOwnerDocument() : 0;
74 m_styleSheet->parseAuthorStyleSheet(cachedStyleSheet, document ? document->securityOrigin() : 0);
78 if (m_parentStyleSheet) {
79 m_parentStyleSheet->notifyLoadedSheet(cachedStyleSheet);
80 m_parentStyleSheet->checkLoaded();
84 bool StyleRuleImport::isLoading() const
86 return m_loading || (m_styleSheet && m_styleSheet->isLoading());
89 void StyleRuleImport::requestStyleSheet()
91 if (!m_parentStyleSheet)
93 Document* document = m_parentStyleSheet->singleOwnerDocument();
97 CachedResourceLoader* cachedResourceLoader = document->cachedResourceLoader();
98 if (!cachedResourceLoader)
101 String absHref = m_strHref;
102 if (!m_parentStyleSheet->finalURL().isNull())
103 // use parent styleheet's URL as the base URL
104 absHref = KURL(m_parentStyleSheet->finalURL(), m_strHref).string();
106 // Check for a cycle in our import chain. If we encounter a stylesheet
107 // in our parent chain with the same URL, then just bail.
108 StyleSheetContents* rootSheet = m_parentStyleSheet;
109 for (StyleSheetContents* sheet = m_parentStyleSheet; sheet; sheet = sheet->parentStyleSheet()) {
110 if (absHref == sheet->finalURL().string() || absHref == sheet->originalURL())
115 ResourceRequest request(document->completeURL(absHref));
116 if (m_parentStyleSheet->isUserStyleSheet())
117 m_cachedSheet = cachedResourceLoader->requestUserCSSStyleSheet(request, m_parentStyleSheet->charset());
119 m_cachedSheet = cachedResourceLoader->requestCSSStyleSheet(request, m_parentStyleSheet->charset());
121 // if the import rule is issued dynamically, the sheet may be
122 // removed from the pending sheet count, so let the doc know
123 // the sheet being imported is pending.
124 if (m_parentStyleSheet && m_parentStyleSheet->loadCompleted() && rootSheet == m_parentStyleSheet)
125 m_parentStyleSheet->startLoadingDynamicSheet();
127 m_cachedSheet->addClient(&m_styleSheetClient);
131 } // namespace WebCore