1 /*
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 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 "CSSImportRule.h"
24
25 #include "CachedCSSStyleSheet.h"
26 #include "DocLoader.h"
27 #include "Document.h"
28 #include "MediaList.h"
29
30 namespace WebCore {
31
CSSImportRule(CSSStyleSheet * parent,const String & href,PassRefPtr<MediaList> media)32 CSSImportRule::CSSImportRule(CSSStyleSheet* parent, const String& href, PassRefPtr<MediaList> media)
33 : CSSRule(parent)
34 , m_strHref(href)
35 , m_lstMedia(media)
36 , m_cachedSheet(0)
37 , m_loading(false)
38 {
39 if (m_lstMedia)
40 m_lstMedia->setParent(this);
41 else
42 m_lstMedia = MediaList::create(this, String());
43 }
44
~CSSImportRule()45 CSSImportRule::~CSSImportRule()
46 {
47 if (m_lstMedia)
48 m_lstMedia->setParent(0);
49 if (m_styleSheet)
50 m_styleSheet->setParent(0);
51 if (m_cachedSheet)
52 m_cachedSheet->removeClient(this);
53 }
54
setCSSStyleSheet(const String & url,const String & charset,const CachedCSSStyleSheet * sheet)55 void CSSImportRule::setCSSStyleSheet(const String& url, const String& charset, const CachedCSSStyleSheet* sheet)
56 {
57 if (m_styleSheet)
58 m_styleSheet->setParent(0);
59 m_styleSheet = CSSStyleSheet::create(this, url, charset);
60
61 CSSStyleSheet* parent = parentStyleSheet();
62 bool strict = !parent || parent->useStrictParsing();
63 m_styleSheet->parseString(sheet->sheetText(strict), strict);
64 m_loading = false;
65
66 if (parent)
67 parent->checkLoaded();
68 }
69
isLoading() const70 bool CSSImportRule::isLoading() const
71 {
72 return m_loading || (m_styleSheet && m_styleSheet->isLoading());
73 }
74
insertedIntoParent()75 void CSSImportRule::insertedIntoParent()
76 {
77 CSSStyleSheet* parentSheet = parentStyleSheet();
78 if (!parentSheet)
79 return;
80
81 DocLoader* docLoader = parentSheet->doc()->docLoader();
82 if (!docLoader)
83 return;
84
85 String absHref = m_strHref;
86 if (!parentSheet->href().isNull())
87 // use parent styleheet's URL as the base URL
88 absHref = KURL(KURL(parentSheet->href()), m_strHref).string();
89
90 // Check for a cycle in our import chain. If we encounter a stylesheet
91 // in our parent chain with the same URL, then just bail.
92 StyleBase* root = this;
93 for (StyleBase* curr = parent(); curr; curr = curr->parent()) {
94 if (curr->isCSSStyleSheet() && absHref == static_cast<CSSStyleSheet*>(curr)->href())
95 return;
96 root = curr;
97 }
98
99 m_cachedSheet = docLoader->requestCSSStyleSheet(absHref, parentSheet->charset());
100 if (m_cachedSheet) {
101 // if the import rule is issued dynamically, the sheet may be
102 // removed from the pending sheet count, so let the doc know
103 // the sheet being imported is pending.
104 if (parentSheet && parentSheet->loadCompleted() && root == parentSheet)
105 parentSheet->doc()->addPendingSheet();
106 m_loading = true;
107 m_cachedSheet->addClient(this);
108 }
109 }
110
cssText() const111 String CSSImportRule::cssText() const
112 {
113 String result = "@import url(\"";
114 result += m_strHref;
115 result += "\")";
116
117 if (m_lstMedia) {
118 result += " ";
119 result += m_lstMedia->mediaText();
120 }
121 result += ";";
122
123 return result;
124 }
125
addSubresourceStyleURLs(ListHashSet<KURL> & urls)126 void CSSImportRule::addSubresourceStyleURLs(ListHashSet<KURL>& urls)
127 {
128 if (m_styleSheet)
129 addSubresourceURL(urls, m_styleSheet->baseURL());
130 }
131
132 } // namespace WebCore
133