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 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 "DocLoader.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->doc() && parent->doc()->settings() && parent->doc()->settings()->needsSiteSpecificQuirks();
70 #if defined(BUILDING_ON_TIGER) || defined(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->doc() || !parent->doc()->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();
118 DocLoader* docLoader = parentSheet->doc()->docLoader();
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 if (parentSheet->isUserStyleSheet())
138 m_cachedSheet = docLoader->requestUserCSSStyleSheet(absHref, parentSheet->charset());
140 m_cachedSheet = docLoader->requestCSSStyleSheet(absHref, parentSheet->charset());
142 // if the import rule is issued dynamically, the sheet may be
143 // removed from the pending sheet count, so let the doc know
144 // the sheet being imported is pending.
145 if (parentSheet && parentSheet->loadCompleted() && root == parentSheet)
146 parentSheet->doc()->addPendingSheet();
148 m_cachedSheet->addClient(this);
152 String CSSImportRule::cssText() const
154 String result = "@import url(\"";
160 result += m_lstMedia->mediaText();
167 void CSSImportRule::addSubresourceStyleURLs(ListHashSet<KURL>& urls)
170 addSubresourceURL(urls, m_styleSheet->baseURL());
173 #ifdef ANDROID_INSTRUMENT
174 void* CSSImportRule::operator new(size_t size)
176 return StyleBase::operator new(size);
179 void* CSSImportRule::operator new[](size_t size)
181 return StyleBase::operator new[](size);
184 void CSSImportRule::operator delete(void* p, size_t size)
186 StyleBase::operator delete(p, size);
189 void CSSImportRule::operator delete[](void* p, size_t size)
191 StyleBase::operator delete[](p, size);
195 } // namespace WebCore