1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/src/nsScriptElement.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,141 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsScriptElement.h" 1.10 +#include "mozilla/BasicEvents.h" 1.11 +#include "mozilla/EventDispatcher.h" 1.12 +#include "mozilla/dom/Element.h" 1.13 +#include "nsContentUtils.h" 1.14 +#include "nsPresContext.h" 1.15 +#include "nsScriptLoader.h" 1.16 +#include "nsIParser.h" 1.17 +#include "nsAutoPtr.h" 1.18 +#include "nsGkAtoms.h" 1.19 +#include "nsContentSink.h" 1.20 + 1.21 +using namespace mozilla; 1.22 +using namespace mozilla::dom; 1.23 + 1.24 +NS_IMETHODIMP 1.25 +nsScriptElement::ScriptAvailable(nsresult aResult, 1.26 + nsIScriptElement *aElement, 1.27 + bool aIsInline, 1.28 + nsIURI *aURI, 1.29 + int32_t aLineNo) 1.30 +{ 1.31 + if (!aIsInline && NS_FAILED(aResult)) { 1.32 + return FireErrorEvent(); 1.33 + } 1.34 + return NS_OK; 1.35 +} 1.36 + 1.37 +/* virtual */ nsresult 1.38 +nsScriptElement::FireErrorEvent() 1.39 +{ 1.40 + nsCOMPtr<nsIContent> cont = 1.41 + do_QueryInterface((nsIScriptElement*) this); 1.42 + 1.43 + return nsContentUtils::DispatchTrustedEvent(cont->OwnerDoc(), 1.44 + cont, 1.45 + NS_LITERAL_STRING("error"), 1.46 + false /* bubbles */, 1.47 + false /* cancelable */); 1.48 +} 1.49 + 1.50 +NS_IMETHODIMP 1.51 +nsScriptElement::ScriptEvaluated(nsresult aResult, 1.52 + nsIScriptElement *aElement, 1.53 + bool aIsInline) 1.54 +{ 1.55 + nsresult rv = NS_OK; 1.56 + if (!aIsInline) { 1.57 + nsCOMPtr<nsIContent> cont = 1.58 + do_QueryInterface((nsIScriptElement*) this); 1.59 + 1.60 + nsRefPtr<nsPresContext> presContext = 1.61 + nsContentUtils::GetContextForContent(cont); 1.62 + 1.63 + nsEventStatus status = nsEventStatus_eIgnore; 1.64 + uint32_t type = NS_SUCCEEDED(aResult) ? NS_LOAD : NS_LOAD_ERROR; 1.65 + WidgetEvent event(true, type); 1.66 + // Load event doesn't bubble. 1.67 + event.mFlags.mBubbles = (type != NS_LOAD); 1.68 + 1.69 + EventDispatcher::Dispatch(cont, presContext, &event, nullptr, &status); 1.70 + } 1.71 + 1.72 + return rv; 1.73 +} 1.74 + 1.75 +void 1.76 +nsScriptElement::CharacterDataChanged(nsIDocument *aDocument, 1.77 + nsIContent* aContent, 1.78 + CharacterDataChangeInfo* aInfo) 1.79 +{ 1.80 + MaybeProcessScript(); 1.81 +} 1.82 + 1.83 +void 1.84 +nsScriptElement::AttributeChanged(nsIDocument* aDocument, 1.85 + Element* aElement, 1.86 + int32_t aNameSpaceID, 1.87 + nsIAtom* aAttribute, 1.88 + int32_t aModType) 1.89 +{ 1.90 + MaybeProcessScript(); 1.91 +} 1.92 + 1.93 +void 1.94 +nsScriptElement::ContentAppended(nsIDocument* aDocument, 1.95 + nsIContent* aContainer, 1.96 + nsIContent* aFirstNewContent, 1.97 + int32_t aNewIndexInContainer) 1.98 +{ 1.99 + MaybeProcessScript(); 1.100 +} 1.101 + 1.102 +void 1.103 +nsScriptElement::ContentInserted(nsIDocument *aDocument, 1.104 + nsIContent* aContainer, 1.105 + nsIContent* aChild, 1.106 + int32_t aIndexInContainer) 1.107 +{ 1.108 + MaybeProcessScript(); 1.109 +} 1.110 + 1.111 +bool 1.112 +nsScriptElement::MaybeProcessScript() 1.113 +{ 1.114 + nsCOMPtr<nsIContent> cont = 1.115 + do_QueryInterface((nsIScriptElement*) this); 1.116 + 1.117 + NS_ASSERTION(cont->DebugGetSlots()->mMutationObservers.Contains(this), 1.118 + "You forgot to add self as observer"); 1.119 + 1.120 + if (mAlreadyStarted || !mDoneAddingChildren || !cont->IsInDoc() || 1.121 + mMalformed || !HasScriptContent()) { 1.122 + return false; 1.123 + } 1.124 + 1.125 + FreezeUriAsyncDefer(); 1.126 + 1.127 + mAlreadyStarted = true; 1.128 + 1.129 + nsIDocument* ownerDoc = cont->OwnerDoc(); 1.130 + nsCOMPtr<nsIParser> parser = ((nsIScriptElement*) this)->GetCreatorParser(); 1.131 + if (parser) { 1.132 + nsCOMPtr<nsIContentSink> sink = parser->GetContentSink(); 1.133 + if (sink) { 1.134 + nsCOMPtr<nsIDocument> parserDoc = do_QueryInterface(sink->GetTarget()); 1.135 + if (ownerDoc != parserDoc) { 1.136 + // Willful violation of HTML5 as of 2010-12-01 1.137 + return false; 1.138 + } 1.139 + } 1.140 + } 1.141 + 1.142 + nsRefPtr<nsScriptLoader> loader = ownerDoc->ScriptLoader(); 1.143 + return loader->ProcessScriptElement(this); 1.144 +}