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 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 "CSSImportRule.h"
25 #include "CachedCSSStyleSheet.h"
26 #include "CachedResourceLoader.h"
28 #include "SecurityOrigin.h"
30 #include <wtf/StdLibExtras.h>
34 CSSImportRule::CSSImportRule(CSSStyleSheet* parent, const String& href, PassRefPtr<MediaList> media)
42 m_lstMedia->setParent(this);
44 m_lstMedia = MediaList::create(this, String());
47 CSSImportRule::~CSSImportRule()
50 m_lstMedia->setParent(0);
52 m_styleSheet->setParent(0);
54 m_cachedSheet->removeClient(this);
57 void CSSImportRule::setCSSStyleSheet(const String& href, const KURL& baseURL, const String& charset, const CachedCSSStyleSheet* sheet)
60 m_styleSheet->setParent(0);
61 m_styleSheet = CSSStyleSheet::create(this, href, baseURL, charset);
63 bool crossOriginCSS = false;
64 bool validMIMEType = false;
65 CSSStyleSheet* parent = parentStyleSheet();
66 bool strict = !parent || parent->useStrictParsing();
67 bool enforceMIMEType = strict;
68 bool needsSiteSpecificQuirks = parent && parent->document() && parent->document()->settings() && parent->document()->settings()->needsSiteSpecificQuirks();
70 #ifdef BUILDING_ON_LEOPARD
71 if (enforceMIMEType && needsSiteSpecificQuirks) {
72 // Covers both http and https, with or without "www."
73 if (baseURL.string().contains("mcafee.com/japan/", false))
74 enforceMIMEType = false;
78 String sheetText = sheet->sheetText(enforceMIMEType, &validMIMEType);
79 m_styleSheet->parseString(sheetText, strict);
81 if (!parent || !parent->document() || !parent->document()->securityOrigin()->canRequest(baseURL))
82 crossOriginCSS = true;
84 if (crossOriginCSS && !validMIMEType && !m_styleSheet->hasSyntacticallyValidCSSHeader())
85 m_styleSheet = CSSStyleSheet::create(this, href, baseURL, charset);
87 if (strict && needsSiteSpecificQuirks) {
88 // Work around <https://bugs.webkit.org/show_bug.cgi?id=28350>.
89 DEFINE_STATIC_LOCAL(const String, slashKHTMLFixesDotCss, ("/KHTMLFixes.css"));
90 DEFINE_STATIC_LOCAL(const String, mediaWikiKHTMLFixesStyleSheet, ("/* KHTML fix stylesheet */\n/* work around the horizontal scrollbars */\n#column-content { margin-left: 0; }\n\n"));
91 // There are two variants of KHTMLFixes.css. One is equal to mediaWikiKHTMLFixesStyleSheet,
92 // while the other lacks the second trailing newline.
93 if (baseURL.string().endsWith(slashKHTMLFixesDotCss) && !sheetText.isNull() && mediaWikiKHTMLFixesStyleSheet.startsWith(sheetText)
94 && sheetText.length() >= mediaWikiKHTMLFixesStyleSheet.length() - 1) {
95 ASSERT(m_styleSheet->length() == 1);
97 m_styleSheet->deleteRule(0, ec);
104 parent->checkLoaded();
107 bool CSSImportRule::isLoading() const
109 return m_loading || (m_styleSheet && m_styleSheet->isLoading());
112 void CSSImportRule::insertedIntoParent()
114 CSSStyleSheet* parentSheet = parentStyleSheet();
115 if (!parentSheet || !parentSheet->document())
118 CachedResourceLoader* cachedResourceLoader = parentSheet->document()->cachedResourceLoader();
119 if (!cachedResourceLoader)
122 String absHref = m_strHref;
123 if (!parentSheet->finalURL().isNull())
124 // use parent styleheet's URL as the base URL
125 absHref = KURL(parentSheet->finalURL(), m_strHref).string();
127 // Check for a cycle in our import chain. If we encounter a stylesheet
128 // in our parent chain with the same URL, then just bail.
129 StyleBase* root = this;
130 for (StyleBase* curr = parent(); curr; curr = curr->parent()) {
131 // FIXME: This is wrong if the finalURL was updated via document::updateBaseURL.
132 if (curr->isCSSStyleSheet() && absHref == static_cast<CSSStyleSheet*>(curr)->finalURL().string())
137 ResourceRequest request(parentSheet->document()->completeURL(absHref));
138 if (parentSheet->isUserStyleSheet())
139 m_cachedSheet = cachedResourceLoader->requestUserCSSStyleSheet(request, parentSheet->charset());
141 m_cachedSheet = cachedResourceLoader->requestCSSStyleSheet(request, parentSheet->charset());
143 // if the import rule is issued dynamically, the sheet may be
144 // removed from the pending sheet count, so let the doc know
145 // the sheet being imported is pending.
146 if (parentSheet && parentSheet->loadCompleted() && root == parentSheet)
147 parentSheet->startLoadingDynamicSheet();
149 m_cachedSheet->addClient(this);
153 String CSSImportRule::cssText() const
155 String result = "@import url(\"";
161 result += m_lstMedia->mediaText();
168 void CSSImportRule::addSubresourceStyleURLs(ListHashSet<KURL>& urls)
171 addSubresourceURL(urls, m_styleSheet->baseURL());
174 } // namespace WebCore