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

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

Issue 2821803002: Introduce ScriptLoader::script_type_ (Closed)
Patch Set: 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 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 149
150 void ScriptLoader::DispatchErrorEvent() { 150 void ScriptLoader::DispatchErrorEvent() {
151 element_->DispatchErrorEvent(); 151 element_->DispatchErrorEvent();
152 } 152 }
153 153
154 void ScriptLoader::DispatchLoadEvent() { 154 void ScriptLoader::DispatchLoadEvent() {
155 element_->DispatchLoadEvent(); 155 element_->DispatchLoadEvent();
156 SetHaveFiredLoadEvent(true); 156 SetHaveFiredLoadEvent(true);
157 } 157 }
158 158
159 bool ScriptLoader::IsValidScriptTypeAndLanguage( 159 namespace {
160
161 bool IsValidClassicScriptTypeAndLanguage(
160 const String& type, 162 const String& type,
161 const String& language, 163 const String& language,
162 LegacyTypeSupport support_legacy_types) { 164 ScriptLoader::LegacyTypeSupport support_legacy_types) {
163 // FIXME: isLegacySupportedJavaScriptLanguage() is not valid HTML5. It is used 165 // FIXME: isLegacySupportedJavaScriptLanguage() is not valid HTML5. It is used
164 // here to maintain backwards compatibility with existing layout tests. The 166 // here to maintain backwards compatibility with existing layout tests. The
165 // specific violations are: 167 // specific violations are:
166 // - Allowing type=javascript. type= should only support MIME types, such as 168 // - Allowing type=javascript. type= should only support MIME types, such as
167 // text/javascript. 169 // text/javascript.
168 // - Allowing a different set of languages for language= and type=. language= 170 // - Allowing a different set of languages for language= and type=. language=
169 // supports Javascript 1.1 and 1.4-1.6, but type= does not. 171 // supports Javascript 1.1 and 1.4-1.6, but type= does not.
170 if (type.IsEmpty()) { 172 if (type.IsEmpty()) {
171 return language.IsEmpty() || // assume text/javascript. 173 return language.IsEmpty() || // assume text/javascript.
172 MIMETypeRegistry::IsSupportedJavaScriptMIMEType("text/" + 174 MIMETypeRegistry::IsSupportedJavaScriptMIMEType("text/" +
173 language) || 175 language) ||
174 MIMETypeRegistry::IsLegacySupportedJavaScriptLanguage(language); 176 MIMETypeRegistry::IsLegacySupportedJavaScriptLanguage(language);
175 } else if (RuntimeEnabledFeatures::moduleScriptsEnabled() &&
176 type == "module") {
177 return true;
178 } else if (MIMETypeRegistry::IsSupportedJavaScriptMIMEType( 177 } else if (MIMETypeRegistry::IsSupportedJavaScriptMIMEType(
179 type.StripWhiteSpace()) || 178 type.StripWhiteSpace()) ||
180 (support_legacy_types == kAllowLegacyTypeInTypeAttribute && 179 (support_legacy_types ==
180 ScriptLoader::kAllowLegacyTypeInTypeAttribute &&
181 MIMETypeRegistry::IsLegacySupportedJavaScriptLanguage(type))) { 181 MIMETypeRegistry::IsLegacySupportedJavaScriptLanguage(type))) {
182 return true; 182 return true;
183 } 183 }
184 184
185 return false; 185 return false;
186 } 186 }
187 187
188 bool ScriptLoader::IsScriptTypeSupported( 188 } // namespace
189 LegacyTypeSupport support_legacy_types) const { 189
190 // Step 6 of https://html.spec.whatwg.org/#prepare-a-script
191 bool ScriptLoader::IsValidScriptTypeAndLanguage(
kouhei (in TOK) 2017/04/15 01:18:23 rename this to DetermineScriptType and have this r
192 const String& type,
193 const String& language,
194 LegacyTypeSupport support_legacy_types,
195 ScriptType& out_script_type) {
196 if (IsValidClassicScriptTypeAndLanguage(type, language,
197 support_legacy_types)) {
198 // - "If the script block's type string is an ASCII case-insensitive match
199 // for any JavaScript MIME type, the script's type is "classic"."
200 // TODO(hiroshige): Annotate and/or cleanup this step.
201 out_script_type = ScriptType::kClassic;
202 return true;
203 }
204
205 if (RuntimeEnabledFeatures::moduleScriptsEnabled() && type == "module") {
206 // - "If the script block's type string is an ASCII case-insensitive match
207 // for the string "module", the script's type is "module"."
208 out_script_type = ScriptType::kModule;
209 return true;
210 }
211
212 // - "If neither of the above conditions are true, then abort these steps
213 // at this point. No script is executed."
214 return false;
215 }
216
217 bool ScriptLoader::IsScriptTypeSupported(LegacyTypeSupport support_legacy_types,
kouhei (in TOK) 2017/04/15 01:18:23 DetermineScriptTypeForElement -> return ScriptTYpe
218 ScriptType& out_script_type) const {
190 return IsValidScriptTypeAndLanguage(element_->TypeAttributeValue(), 219 return IsValidScriptTypeAndLanguage(element_->TypeAttributeValue(),
191 element_->LanguageAttributeValue(), 220 element_->LanguageAttributeValue(),
192 support_legacy_types); 221 support_legacy_types, out_script_type);
193 } 222 }
194 223
195 // https://html.spec.whatwg.org/#prepare-a-script 224 // https://html.spec.whatwg.org/#prepare-a-script
196 bool ScriptLoader::PrepareScript(const TextPosition& script_start_position, 225 bool ScriptLoader::PrepareScript(const TextPosition& script_start_position,
197 LegacyTypeSupport support_legacy_types) { 226 LegacyTypeSupport support_legacy_types) {
198 // 1. "If the script element is marked as having "already started", then 227 // 1. "If the script element is marked as having "already started", then
199 // abort these steps at this point. The script is not executed." 228 // abort these steps at this point. The script is not executed."
200 if (already_started_) 229 if (already_started_)
201 return false; 230 return false;
202 231
(...skipping 20 matching lines...) Expand all
223 // FIXME: HTML5 spec says we should check that all children are either 252 // FIXME: HTML5 spec says we should check that all children are either
224 // comments or empty text nodes. 253 // comments or empty text nodes.
225 if (!element_->HasSourceAttribute() && !element_->HasChildren()) 254 if (!element_->HasSourceAttribute() && !element_->HasChildren())
226 return false; 255 return false;
227 256
228 // 5. "If the element is not connected, then abort these steps. 257 // 5. "If the element is not connected, then abort these steps.
229 // The script is not executed." 258 // The script is not executed."
230 if (!element_->IsConnected()) 259 if (!element_->IsConnected())
231 return false; 260 return false;
232 261
233 // 6. 262 // 6. "Determine the script's type as follows:"
234 // TODO(hiroshige): Annotate and/or cleanup this step. 263 // |script_type_| is set here.
235 if (!IsScriptTypeSupported(support_legacy_types)) 264 if (!IsScriptTypeSupported(support_legacy_types, script_type_))
236 return false; 265 return false;
237 266
238 // 7. "If was-parser-inserted is true, 267 // 7. "If was-parser-inserted is true,
239 // then flag the element as "parser-inserted" again, 268 // then flag the element as "parser-inserted" again,
240 // and set the element's "non-blocking" flag to false." 269 // and set the element's "non-blocking" flag to false."
241 if (was_parser_inserted) { 270 if (was_parser_inserted) {
242 parser_inserted_ = true; 271 parser_inserted_ = true;
243 non_blocking_ = false; 272 non_blocking_ = false;
244 } 273 }
245 274
(...skipping 10 matching lines...) Expand all
256 if (!element_document.ExecutingFrame()) 285 if (!element_document.ExecutingFrame())
257 return false; 286 return false;
258 if (!context_document || !context_document->ExecutingFrame()) 287 if (!context_document || !context_document->ExecutingFrame())
259 return false; 288 return false;
260 289
261 // 10. "If scripting is disabled for the script element, then abort these 290 // 10. "If scripting is disabled for the script element, then abort these
262 // steps at this point. The script is not executed." 291 // steps at this point. The script is not executed."
263 if (!context_document->CanExecuteScripts(kAboutToExecuteScript)) 292 if (!context_document->CanExecuteScripts(kAboutToExecuteScript))
264 return false; 293 return false;
265 294
295 // 11. "If the script element has a nomodule content attribute
296 // and the script's type is "classic", then abort these steps.
297 // The script is not executed."
298 // TODO(japhet): Implement this step.
299
266 // 13. 300 // 13.
267 if (!IsScriptForEventSupported()) 301 if (!IsScriptForEventSupported())
268 return false; 302 return false;
269 303
270 // 14. "If the script element has a charset attribute, 304 // 14. "If the script element has a charset attribute,
271 // then let encoding be the result of 305 // then let encoding be the result of
272 // getting an encoding from the value of the charset attribute." 306 // getting an encoding from the value of the charset attribute."
273 // "If the script element does not have a charset attribute, 307 // "If the script element does not have a charset attribute,
274 // or if getting an encoding failed, let encoding 308 // or if getting an encoding failed, let encoding
275 // be the same as the encoding of the script element's node document." 309 // be the same as the encoding of the script element's node document."
(...skipping 505 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

Powered by Google App Engine
This is Rietveld 408576698