Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: third_party/WebKit/Source/core/dom/ScriptLoader.cpp

Issue 2821803002: Introduce ScriptLoader::script_type_ (Closed)
Patch Set: Add missing include Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org) 4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights 5 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights
6 * reserved. 6 * reserved.
7 * Copyright (C) 2008 Nikolas Zimmermann <zimmermann@kde.org> 7 * Copyright (C) 2008 Nikolas Zimmermann <zimmermann@kde.org>
8 * 8 *
9 * This library is free software; you can redistribute it and/or 9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public 10 * modify it under the terms of the GNU Library General Public
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 150
151 void ScriptLoader::DispatchErrorEvent() { 151 void ScriptLoader::DispatchErrorEvent() {
152 element_->DispatchErrorEvent(); 152 element_->DispatchErrorEvent();
153 } 153 }
154 154
155 void ScriptLoader::DispatchLoadEvent() { 155 void ScriptLoader::DispatchLoadEvent() {
156 element_->DispatchLoadEvent(); 156 element_->DispatchLoadEvent();
157 SetHaveFiredLoadEvent(true); 157 SetHaveFiredLoadEvent(true);
158 } 158 }
159 159
160 bool ScriptLoader::IsValidScriptTypeAndLanguage( 160 namespace {
161
162 bool IsValidClassicScriptTypeAndLanguage(
161 const String& type, 163 const String& type,
162 const String& language, 164 const String& language,
163 LegacyTypeSupport support_legacy_types) { 165 ScriptLoader::LegacyTypeSupport support_legacy_types) {
164 // FIXME: isLegacySupportedJavaScriptLanguage() is not valid HTML5. It is used 166 // FIXME: isLegacySupportedJavaScriptLanguage() is not valid HTML5. It is used
165 // here to maintain backwards compatibility with existing layout tests. The 167 // here to maintain backwards compatibility with existing layout tests. The
166 // specific violations are: 168 // specific violations are:
167 // - Allowing type=javascript. type= should only support MIME types, such as 169 // - Allowing type=javascript. type= should only support MIME types, such as
168 // text/javascript. 170 // text/javascript.
169 // - Allowing a different set of languages for language= and type=. language= 171 // - Allowing a different set of languages for language= and type=. language=
170 // supports Javascript 1.1 and 1.4-1.6, but type= does not. 172 // supports Javascript 1.1 and 1.4-1.6, but type= does not.
171 if (type.IsEmpty()) { 173 if (type.IsEmpty()) {
172 return language.IsEmpty() || // assume text/javascript. 174 return language.IsEmpty() || // assume text/javascript.
173 MIMETypeRegistry::IsSupportedJavaScriptMIMEType("text/" + 175 MIMETypeRegistry::IsSupportedJavaScriptMIMEType("text/" +
174 language) || 176 language) ||
175 MIMETypeRegistry::IsLegacySupportedJavaScriptLanguage(language); 177 MIMETypeRegistry::IsLegacySupportedJavaScriptLanguage(language);
176 } else if (RuntimeEnabledFeatures::moduleScriptsEnabled() &&
177 type == "module") {
178 return true;
179 } else if (MIMETypeRegistry::IsSupportedJavaScriptMIMEType( 178 } else if (MIMETypeRegistry::IsSupportedJavaScriptMIMEType(
180 type.StripWhiteSpace()) || 179 type.StripWhiteSpace()) ||
181 (support_legacy_types == kAllowLegacyTypeInTypeAttribute && 180 (support_legacy_types ==
181 ScriptLoader::kAllowLegacyTypeInTypeAttribute &&
182 MIMETypeRegistry::IsLegacySupportedJavaScriptLanguage(type))) { 182 MIMETypeRegistry::IsLegacySupportedJavaScriptLanguage(type))) {
183 return true; 183 return true;
184 } 184 }
185 185
186 return false; 186 return false;
187 } 187 }
188 188
189 bool ScriptLoader::IsScriptTypeSupported( 189 } // namespace
190 LegacyTypeSupport support_legacy_types) const { 190
191 // Step 6 of https://html.spec.whatwg.org/#prepare-a-script
192 bool ScriptLoader::IsValidScriptTypeAndLanguage(
193 const String& type,
194 const String& language,
195 LegacyTypeSupport support_legacy_types,
196 ScriptType& out_script_type) {
197 if (IsValidClassicScriptTypeAndLanguage(type, language,
198 support_legacy_types)) {
199 // - "If the script block's type string is an ASCII case-insensitive match
200 // for any JavaScript MIME type, the script's type is "classic"."
201 // TODO(hiroshige): Annotate and/or cleanup this step.
202 out_script_type = ScriptType::kClassic;
203 return true;
204 }
205
206 if (RuntimeEnabledFeatures::moduleScriptsEnabled() && type == "module") {
207 // - "If the script block's type string is an ASCII case-insensitive match
208 // for the string "module", the script's type is "module"."
209 out_script_type = ScriptType::kModule;
210 return true;
211 }
212
213 // - "If neither of the above conditions are true, then abort these steps
214 // at this point. No script is executed."
215 return false;
216 }
217
218 bool ScriptLoader::IsScriptTypeSupported(LegacyTypeSupport support_legacy_types,
219 ScriptType& out_script_type) const {
191 return IsValidScriptTypeAndLanguage(element_->TypeAttributeValue(), 220 return IsValidScriptTypeAndLanguage(element_->TypeAttributeValue(),
192 element_->LanguageAttributeValue(), 221 element_->LanguageAttributeValue(),
193 support_legacy_types); 222 support_legacy_types, out_script_type);
194 } 223 }
195 224
196 // https://html.spec.whatwg.org/#prepare-a-script 225 // https://html.spec.whatwg.org/#prepare-a-script
197 bool ScriptLoader::PrepareScript(const TextPosition& script_start_position, 226 bool ScriptLoader::PrepareScript(const TextPosition& script_start_position,
198 LegacyTypeSupport support_legacy_types) { 227 LegacyTypeSupport support_legacy_types) {
199 // 1. "If the script element is marked as having "already started", then 228 // 1. "If the script element is marked as having "already started", then
200 // abort these steps at this point. The script is not executed." 229 // abort these steps at this point. The script is not executed."
201 if (already_started_) 230 if (already_started_)
202 return false; 231 return false;
203 232
(...skipping 20 matching lines...) Expand all
224 // FIXME: HTML5 spec says we should check that all children are either 253 // FIXME: HTML5 spec says we should check that all children are either
225 // comments or empty text nodes. 254 // comments or empty text nodes.
226 if (!element_->HasSourceAttribute() && !element_->HasChildren()) 255 if (!element_->HasSourceAttribute() && !element_->HasChildren())
227 return false; 256 return false;
228 257
229 // 5. "If the element is not connected, then abort these steps. 258 // 5. "If the element is not connected, then abort these steps.
230 // The script is not executed." 259 // The script is not executed."
231 if (!element_->IsConnected()) 260 if (!element_->IsConnected())
232 return false; 261 return false;
233 262
234 // 6. 263 // 6. "Determine the script's type as follows:"
235 // TODO(hiroshige): Annotate and/or cleanup this step. 264 // |script_type_| is set here.
236 if (!IsScriptTypeSupported(support_legacy_types)) 265 if (!IsScriptTypeSupported(support_legacy_types, script_type_))
237 return false; 266 return false;
238 267
239 // 7. "If was-parser-inserted is true, 268 // 7. "If was-parser-inserted is true,
240 // then flag the element as "parser-inserted" again, 269 // then flag the element as "parser-inserted" again,
241 // and set the element's "non-blocking" flag to false." 270 // and set the element's "non-blocking" flag to false."
242 if (was_parser_inserted) { 271 if (was_parser_inserted) {
243 parser_inserted_ = true; 272 parser_inserted_ = true;
244 non_blocking_ = false; 273 non_blocking_ = false;
245 } 274 }
246 275
(...skipping 10 matching lines...) Expand all
257 if (!element_document.ExecutingFrame()) 286 if (!element_document.ExecutingFrame())
258 return false; 287 return false;
259 if (!context_document || !context_document->ExecutingFrame()) 288 if (!context_document || !context_document->ExecutingFrame())
260 return false; 289 return false;
261 290
262 // 10. "If scripting is disabled for the script element, then abort these 291 // 10. "If scripting is disabled for the script element, then abort these
263 // steps at this point. The script is not executed." 292 // steps at this point. The script is not executed."
264 if (!context_document->CanExecuteScripts(kAboutToExecuteScript)) 293 if (!context_document->CanExecuteScripts(kAboutToExecuteScript))
265 return false; 294 return false;
266 295
296 // 11. "If the script element has a nomodule content attribute
297 // and the script's type is "classic", then abort these steps.
298 // The script is not executed."
299 // TODO(japhet): Implement this step.
300
267 // 13. 301 // 13.
268 if (!IsScriptForEventSupported()) 302 if (!IsScriptForEventSupported())
269 return false; 303 return false;
270 304
271 // 14. "If the script element has a charset attribute, 305 // 14. "If the script element has a charset attribute,
272 // then let encoding be the result of 306 // then let encoding be the result of
273 // getting an encoding from the value of the charset attribute." 307 // getting an encoding from the value of the charset attribute."
274 // "If the script element does not have a charset attribute, 308 // "If the script element does not have a charset attribute,
275 // or if getting an encoding failed, let encoding 309 // or if getting an encoding failed, let encoding
276 // be the same as the encoding of the script element's node document." 310 // be the same as the encoding of the script element's node document."
(...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 // then abort these steps at this point. The script is not executed. 815 // then abort these steps at this point. The script is not executed.
782 return DeprecatedEqualIgnoringCase(event_attribute, "onload") || 816 return DeprecatedEqualIgnoringCase(event_attribute, "onload") ||
783 DeprecatedEqualIgnoringCase(event_attribute, "onload()"); 817 DeprecatedEqualIgnoringCase(event_attribute, "onload()");
784 } 818 }
785 819
786 String ScriptLoader::ScriptContent() const { 820 String ScriptLoader::ScriptContent() const {
787 return element_->TextFromChildren(); 821 return element_->TextFromChildren();
788 } 822 }
789 823
790 } // namespace blink 824 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/dom/ScriptLoader.h ('k') | third_party/WebKit/Source/core/html/HTMLScriptElement.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698