2 * Copyright (C) 2008, 2010 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2009 Torch Mobile, Inc. http://www.torchmobile.com/
4 * Copyright (C) 2010 Google Inc. All Rights Reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "CSSPreloadScanner.h"
31 #include "CachedCSSStyleSheet.h"
32 #include "CachedResourceLoader.h"
34 #include "HTMLParserIdioms.h"
35 #include "HTMLToken.h"
39 CSSPreloadScanner::CSSPreloadScanner(Document* document)
41 , m_document(document)
45 void CSSPreloadScanner::reset()
52 void CSSPreloadScanner::scan(const HTMLToken& token, bool scanningBody)
54 m_scanningBody = scanningBody;
56 const HTMLToken::DataVector& characters = token.characters();
57 for (HTMLToken::DataVector::const_iterator iter = characters.begin(); iter != characters.end() && m_state != DoneParsingImportRules; ++iter)
61 inline void CSSPreloadScanner::tokenize(UChar c)
63 // We are just interested in @import rules, no need for real tokenization here
64 // Searching for other types of resources is probably low payoff.
72 m_state = MaybeComment;
74 m_state = DoneParsingImportRules;
84 m_state = MaybeCommentEnd;
95 if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
117 m_state = DoneParsingImportRules;
120 m_ruleValue.append(c);
125 m_state = AfterRuleValue;
129 m_ruleValue.append(c);
137 m_state = DoneParsingImportRules;
139 // FIXME: media rules
143 case DoneParsingImportRules:
144 ASSERT_NOT_REACHED();
149 static String parseCSSStringOrURL(const UChar* characters, size_t length)
152 size_t reducedLength = length;
154 while (reducedLength && isHTMLSpace(characters[offset])) {
158 while (reducedLength && isHTMLSpace(characters[offset + reducedLength - 1]))
161 if (reducedLength >= 5
162 && (characters[offset] == 'u' || characters[offset] == 'U')
163 && (characters[offset + 1] == 'r' || characters[offset + 1] == 'R')
164 && (characters[offset + 2] == 'l' || characters[offset + 2] == 'L')
165 && characters[offset + 3] == '('
166 && characters[offset + reducedLength - 1] == ')') {
171 while (reducedLength && isHTMLSpace(characters[offset])) {
175 while (reducedLength && isHTMLSpace(characters[offset + reducedLength - 1]))
178 if (reducedLength < 2 || characters[offset] != characters[offset + reducedLength - 1] || !(characters[offset] == '\'' || characters[offset] == '"'))
183 while (reducedLength && isHTMLSpace(characters[offset])) {
187 while (reducedLength && isHTMLSpace(characters[offset + reducedLength - 1]))
190 return String(characters + offset, reducedLength);
193 void CSSPreloadScanner::emitRule()
195 if (equalIgnoringCase("import", m_rule.characters(), m_rule.length())) {
196 String value = parseCSSStringOrURL(m_ruleValue.characters(), m_ruleValue.length());
197 if (!value.isEmpty()) {
198 ResourceRequest request(m_document->completeURL(value));
199 m_document->cachedResourceLoader()->preload(CachedResource::CSSStyleSheet, request, String(), m_scanningBody);
202 } else if (equalIgnoringCase("charset", m_rule.characters(), m_rule.length()))
205 m_state = DoneParsingImportRules;