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

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: tests Created 7 years, 7 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
« Source/core/dom/Document.cpp ('K') | « 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 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 } 275 }
276 276
277 if (m_cachedScript) { 277 if (m_cachedScript) {
278 return true; 278 return true;
279 } 279 }
280 280
281 dispatchErrorEvent(); 281 dispatchErrorEvent();
282 return false; 282 return false;
283 } 283 }
284 284
285 bool isHTMLScriptElement(Element* element)
286 {
287 return element->isHTMLElement() && element->hasTagName(HTMLNames::scriptTag) ;
288 }
289
285 void ScriptElement::executeScript(const ScriptSourceCode& sourceCode) 290 void ScriptElement::executeScript(const ScriptSourceCode& sourceCode)
286 { 291 {
287 ASSERT(m_alreadyStarted); 292 ASSERT(m_alreadyStarted);
288 293
289 if (sourceCode.isEmpty()) 294 if (sourceCode.isEmpty())
290 return; 295 return;
291 296
292 RefPtr<Document> document = m_element->document(); 297 RefPtr<Document> document = m_element->document();
293 Frame* frame = document->frame(); 298 Frame* frame = document->frame();
294 299
295 bool shouldBypassMainWorldContentSecurityPolicy = (frame && frame->script()- >shouldBypassMainWorldContentSecurityPolicy()); 300 bool shouldBypassMainWorldContentSecurityPolicy = (frame && frame->script()- >shouldBypassMainWorldContentSecurityPolicy());
296 if (!shouldBypassMainWorldContentSecurityPolicy && !document->contentSecurit yPolicy()->allowScriptNonce(m_element->fastGetAttribute(HTMLNames::nonceAttr), d ocument->url(), m_startLineNumber)) 301 if (!shouldBypassMainWorldContentSecurityPolicy && !document->contentSecurit yPolicy()->allowScriptNonce(m_element->fastGetAttribute(HTMLNames::nonceAttr), d ocument->url(), m_startLineNumber))
297 return; 302 return;
298 303
299 if (!m_isExternalScript && (!shouldBypassMainWorldContentSecurityPolicy && ! document->contentSecurityPolicy()->allowInlineScript(document->url(), m_startLin eNumber))) 304 if (!m_isExternalScript && (!shouldBypassMainWorldContentSecurityPolicy && ! document->contentSecurityPolicy()->allowInlineScript(document->url(), m_startLin eNumber)))
300 return; 305 return;
301 306
302 if (m_isExternalScript && m_cachedScript && !m_cachedScript->mimeTypeAllowed ByNosniff()) { 307 if (m_isExternalScript && m_cachedScript && !m_cachedScript->mimeTypeAllowed ByNosniff()) {
303 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."); 308 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.");
304 return; 309 return;
305 } 310 }
306 311
307 if (frame) { 312 if (frame) {
308 { 313 {
309 IgnoreDestructiveWriteCountIncrementer ignoreDesctructiveWriteCountI ncrementer(m_isExternalScript ? document.get() : 0); 314 IgnoreDestructiveWriteCountIncrementer ignoreDesctructiveWriteCountI ncrementer(m_isExternalScript ? document.get() : 0);
315
316 if (isHTMLScriptElement(m_element))
317 document->setCurrentScript(static_cast<HTMLScriptElement*>(m_ele ment));
318
310 // Create a script from the script element node, using the script 319 // Create a script from the script element node, using the script
311 // block's source and the script block's type. 320 // block's source and the script block's type.
312 // Note: This is where the script is compiled and actually executed. 321 // Note: This is where the script is compiled and actually executed.
313 frame->script()->evaluate(sourceCode); 322 frame->script()->evaluate(sourceCode);
323
324 document->clearCurrentScript();
esprehn 2013/05/15 16:08:16 You're sure this is safe without checking if (docu
arv (Not doing code reviews) 2013/05/15 16:18:11 There is a case where this will fail. When you doc
314 } 325 }
315 } 326 }
316 } 327 }
317 328
318 void ScriptElement::stopLoadRequest() 329 void ScriptElement::stopLoadRequest()
319 { 330 {
320 if (m_cachedScript) { 331 if (m_cachedScript) {
321 if (!m_willBeParserExecuted) 332 if (!m_willBeParserExecuted)
322 m_cachedScript->removeClient(this); 333 m_cachedScript->removeClient(this);
323 m_cachedScript = 0; 334 m_cachedScript = 0;
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 } 421 }
411 422
412 if (firstTextNode && !foundMultipleTextNodes) 423 if (firstTextNode && !foundMultipleTextNodes)
413 return firstTextNode->data(); 424 return firstTextNode->data();
414 425
415 return content.toString(); 426 return content.toString();
416 } 427 }
417 428
418 ScriptElement* toScriptElementIfPossible(Element* element) 429 ScriptElement* toScriptElementIfPossible(Element* element)
419 { 430 {
420 if (element->isHTMLElement() && element->hasTagName(HTMLNames::scriptTag)) 431 if (isHTMLScriptElement(element))
421 return static_cast<HTMLScriptElement*>(element); 432 return static_cast<HTMLScriptElement*>(element);
422 433
423 #if ENABLE(SVG) 434 #if ENABLE(SVG)
424 if (element->isSVGElement() && element->hasTagName(SVGNames::scriptTag)) 435 if (element->isSVGElement() && element->hasTagName(SVGNames::scriptTag))
425 return static_cast<SVGScriptElement*>(element); 436 return static_cast<SVGScriptElement*>(element);
426 #endif 437 #endif
427 438
428 return 0; 439 return 0;
429 } 440 }
430 441
431 } 442 }
OLDNEW
« Source/core/dom/Document.cpp ('K') | « Source/core/dom/Document.idl ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698