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

Side by Side Diff: Source/core/dom/ScriptElement.cpp

Issue 14852011: Implement document.currentScript (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: rebase Created 7 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « Source/core/dom/Document.idl ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 reserv ed. 5 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserv ed.
6 * Copyright (C) 2008 Nikolas Zimmermann <zimmermann@kde.org> 6 * Copyright (C) 2008 Nikolas Zimmermann <zimmermann@kde.org>
7 * 7 *
8 * This library is free software; you can redistribute it and/or 8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public 9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either 10 * License as published by the Free Software Foundation; either
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 } 268 }
269 269
270 if (m_cachedScript) { 270 if (m_cachedScript) {
271 return true; 271 return true;
272 } 272 }
273 273
274 dispatchErrorEvent(); 274 dispatchErrorEvent();
275 return false; 275 return false;
276 } 276 }
277 277
278 bool isHTMLScriptElement(Element* element)
279 {
280 return element->isHTMLElement() && element->hasTagName(HTMLNames::scriptTag) ;
abarth-chromium 2013/06/11 17:45:20 The isHTMLElement check isn't redundant with hasTa
281 }
282
278 void ScriptElement::executeScript(const ScriptSourceCode& sourceCode) 283 void ScriptElement::executeScript(const ScriptSourceCode& sourceCode)
279 { 284 {
280 ASSERT(m_alreadyStarted); 285 ASSERT(m_alreadyStarted);
281 286
282 if (sourceCode.isEmpty()) 287 if (sourceCode.isEmpty())
283 return; 288 return;
284 289
285 RefPtr<Document> document = m_element->document(); 290 RefPtr<Document> document = m_element->document();
286 Frame* frame = document->frame(); 291 Frame* frame = document->frame();
287 292
288 bool shouldBypassMainWorldContentSecurityPolicy = (frame && frame->script()- >shouldBypassMainWorldContentSecurityPolicy()) || document->contentSecurityPolic y()->allowScriptNonce(m_element->fastGetAttribute(HTMLNames::nonceAttr)); 293 bool shouldBypassMainWorldContentSecurityPolicy = (frame && frame->script()- >shouldBypassMainWorldContentSecurityPolicy()) || document->contentSecurityPolic y()->allowScriptNonce(m_element->fastGetAttribute(HTMLNames::nonceAttr));
289 294
290 if (!m_isExternalScript && (!shouldBypassMainWorldContentSecurityPolicy && ! document->contentSecurityPolicy()->allowInlineScript(document->url(), m_startLin eNumber))) 295 if (!m_isExternalScript && (!shouldBypassMainWorldContentSecurityPolicy && ! document->contentSecurityPolicy()->allowInlineScript(document->url(), m_startLin eNumber)))
291 return; 296 return;
292 297
293 if (m_isExternalScript && m_cachedScript && !m_cachedScript->mimeTypeAllowed ByNosniff()) { 298 if (m_isExternalScript && m_cachedScript && !m_cachedScript->mimeTypeAllowed ByNosniff()) {
294 document->addConsoleMessage(SecurityMessageSource, ErrorMessageLevel, "R efused to execute script from '" + m_cachedScript->url().elidedString() + "' bec ause its MIME type ('" + m_cachedScript->mimeType() + "') is not executable, and strict MIME type checking is enabled."); 299 document->addConsoleMessage(SecurityMessageSource, ErrorMessageLevel, "R efused to execute script from '" + m_cachedScript->url().elidedString() + "' bec ause its MIME type ('" + m_cachedScript->mimeType() + "') is not executable, and strict MIME type checking is enabled.");
295 return; 300 return;
296 } 301 }
297 302
298 if (frame) { 303 if (frame) {
299 { 304 {
300 IgnoreDestructiveWriteCountIncrementer ignoreDesctructiveWriteCountI ncrementer(m_isExternalScript ? document.get() : 0); 305 IgnoreDestructiveWriteCountIncrementer ignoreDesctructiveWriteCountI ncrementer(m_isExternalScript ? document.get() : 0);
306
307 if (isHTMLScriptElement(m_element))
308 document->pushCurrentScript(static_cast<HTMLScriptElement*>(m_el ement));
abarth-chromium 2013/06/11 17:45:20 We should have a toHTMLScriptElement that contains
309
301 // Create a script from the script element node, using the script 310 // Create a script from the script element node, using the script
302 // block's source and the script block's type. 311 // block's source and the script block's type.
303 // Note: This is where the script is compiled and actually executed. 312 // Note: This is where the script is compiled and actually executed.
304 frame->script()->executeScriptInMainWorld(sourceCode); 313 frame->script()->executeScriptInMainWorld(sourceCode);
314
315 if (isHTMLScriptElement(m_element)) {
316 ASSERT(document->currentScript() == m_element);
317 document->popCurrentScript();
318 }
305 } 319 }
306 } 320 }
307 } 321 }
308 322
309 void ScriptElement::stopLoadRequest() 323 void ScriptElement::stopLoadRequest()
310 { 324 {
311 if (m_cachedScript) { 325 if (m_cachedScript) {
312 if (!m_willBeParserExecuted) 326 if (!m_willBeParserExecuted)
313 m_cachedScript->removeClient(this); 327 m_cachedScript->removeClient(this);
314 m_cachedScript = 0; 328 m_cachedScript = 0;
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 if (firstTextNode && !foundMultipleTextNodes) { 417 if (firstTextNode && !foundMultipleTextNodes) {
404 firstTextNode->atomize(); 418 firstTextNode->atomize();
405 return firstTextNode->data(); 419 return firstTextNode->data();
406 } 420 }
407 421
408 return content.toString(); 422 return content.toString();
409 } 423 }
410 424
411 ScriptElement* toScriptElementIfPossible(Element* element) 425 ScriptElement* toScriptElementIfPossible(Element* element)
412 { 426 {
413 if (element->isHTMLElement() && element->hasTagName(HTMLNames::scriptTag)) 427 if (isHTMLScriptElement(element))
414 return static_cast<HTMLScriptElement*>(element); 428 return static_cast<HTMLScriptElement*>(element);
415 429
416 if (element->isSVGElement() && element->hasTagName(SVGNames::scriptTag)) 430 if (element->isSVGElement() && element->hasTagName(SVGNames::scriptTag))
417 return static_cast<SVGScriptElement*>(element); 431 return static_cast<SVGScriptElement*>(element);
418 432
419 return 0; 433 return 0;
420 } 434 }
421 435
422 } 436 }
OLDNEW
« no previous file with comments | « Source/core/dom/Document.idl ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698