michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsScriptElement.h" michael@0: #include "mozilla/BasicEvents.h" michael@0: #include "mozilla/EventDispatcher.h" michael@0: #include "mozilla/dom/Element.h" michael@0: #include "nsContentUtils.h" michael@0: #include "nsPresContext.h" michael@0: #include "nsScriptLoader.h" michael@0: #include "nsIParser.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsGkAtoms.h" michael@0: #include "nsContentSink.h" michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::dom; michael@0: michael@0: NS_IMETHODIMP michael@0: nsScriptElement::ScriptAvailable(nsresult aResult, michael@0: nsIScriptElement *aElement, michael@0: bool aIsInline, michael@0: nsIURI *aURI, michael@0: int32_t aLineNo) michael@0: { michael@0: if (!aIsInline && NS_FAILED(aResult)) { michael@0: return FireErrorEvent(); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* virtual */ nsresult michael@0: nsScriptElement::FireErrorEvent() michael@0: { michael@0: nsCOMPtr cont = michael@0: do_QueryInterface((nsIScriptElement*) this); michael@0: michael@0: return nsContentUtils::DispatchTrustedEvent(cont->OwnerDoc(), michael@0: cont, michael@0: NS_LITERAL_STRING("error"), michael@0: false /* bubbles */, michael@0: false /* cancelable */); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsScriptElement::ScriptEvaluated(nsresult aResult, michael@0: nsIScriptElement *aElement, michael@0: bool aIsInline) michael@0: { michael@0: nsresult rv = NS_OK; michael@0: if (!aIsInline) { michael@0: nsCOMPtr cont = michael@0: do_QueryInterface((nsIScriptElement*) this); michael@0: michael@0: nsRefPtr presContext = michael@0: nsContentUtils::GetContextForContent(cont); michael@0: michael@0: nsEventStatus status = nsEventStatus_eIgnore; michael@0: uint32_t type = NS_SUCCEEDED(aResult) ? NS_LOAD : NS_LOAD_ERROR; michael@0: WidgetEvent event(true, type); michael@0: // Load event doesn't bubble. michael@0: event.mFlags.mBubbles = (type != NS_LOAD); michael@0: michael@0: EventDispatcher::Dispatch(cont, presContext, &event, nullptr, &status); michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: void michael@0: nsScriptElement::CharacterDataChanged(nsIDocument *aDocument, michael@0: nsIContent* aContent, michael@0: CharacterDataChangeInfo* aInfo) michael@0: { michael@0: MaybeProcessScript(); michael@0: } michael@0: michael@0: void michael@0: nsScriptElement::AttributeChanged(nsIDocument* aDocument, michael@0: Element* aElement, michael@0: int32_t aNameSpaceID, michael@0: nsIAtom* aAttribute, michael@0: int32_t aModType) michael@0: { michael@0: MaybeProcessScript(); michael@0: } michael@0: michael@0: void michael@0: nsScriptElement::ContentAppended(nsIDocument* aDocument, michael@0: nsIContent* aContainer, michael@0: nsIContent* aFirstNewContent, michael@0: int32_t aNewIndexInContainer) michael@0: { michael@0: MaybeProcessScript(); michael@0: } michael@0: michael@0: void michael@0: nsScriptElement::ContentInserted(nsIDocument *aDocument, michael@0: nsIContent* aContainer, michael@0: nsIContent* aChild, michael@0: int32_t aIndexInContainer) michael@0: { michael@0: MaybeProcessScript(); michael@0: } michael@0: michael@0: bool michael@0: nsScriptElement::MaybeProcessScript() michael@0: { michael@0: nsCOMPtr cont = michael@0: do_QueryInterface((nsIScriptElement*) this); michael@0: michael@0: NS_ASSERTION(cont->DebugGetSlots()->mMutationObservers.Contains(this), michael@0: "You forgot to add self as observer"); michael@0: michael@0: if (mAlreadyStarted || !mDoneAddingChildren || !cont->IsInDoc() || michael@0: mMalformed || !HasScriptContent()) { michael@0: return false; michael@0: } michael@0: michael@0: FreezeUriAsyncDefer(); michael@0: michael@0: mAlreadyStarted = true; michael@0: michael@0: nsIDocument* ownerDoc = cont->OwnerDoc(); michael@0: nsCOMPtr parser = ((nsIScriptElement*) this)->GetCreatorParser(); michael@0: if (parser) { michael@0: nsCOMPtr sink = parser->GetContentSink(); michael@0: if (sink) { michael@0: nsCOMPtr parserDoc = do_QueryInterface(sink->GetTarget()); michael@0: if (ownerDoc != parserDoc) { michael@0: // Willful violation of HTML5 as of 2010-12-01 michael@0: return false; michael@0: } michael@0: } michael@0: } michael@0: michael@0: nsRefPtr loader = ownerDoc->ScriptLoader(); michael@0: return loader->ProcessScriptElement(this); michael@0: }