1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsScriptElement.h"
7 #include "mozilla/BasicEvents.h"
8 #include "mozilla/EventDispatcher.h"
9 #include "mozilla/dom/Element.h"
10 #include "nsContentUtils.h"
11 #include "nsPresContext.h"
12 #include "nsScriptLoader.h"
13 #include "nsIParser.h"
14 #include "nsAutoPtr.h"
15 #include "nsGkAtoms.h"
16 #include "nsContentSink.h"
18 using namespace mozilla
;
19 using namespace mozilla::dom
;
22 nsScriptElement::ScriptAvailable(nsresult aResult
,
23 nsIScriptElement
*aElement
,
28 if (!aIsInline
&& NS_FAILED(aResult
)) {
29 return FireErrorEvent();
34 /* virtual */ nsresult
35 nsScriptElement::FireErrorEvent()
37 nsCOMPtr
<nsIContent
> cont
=
38 do_QueryInterface((nsIScriptElement
*) this);
40 return nsContentUtils::DispatchTrustedEvent(cont
->OwnerDoc(),
42 NS_LITERAL_STRING("error"),
44 false /* cancelable */);
48 nsScriptElement::ScriptEvaluated(nsresult aResult
,
49 nsIScriptElement
*aElement
,
54 nsCOMPtr
<nsIContent
> cont
=
55 do_QueryInterface((nsIScriptElement
*) this);
57 nsRefPtr
<nsPresContext
> presContext
=
58 nsContentUtils::GetContextForContent(cont
);
60 nsEventStatus status
= nsEventStatus_eIgnore
;
61 uint32_t type
= NS_SUCCEEDED(aResult
) ? NS_LOAD
: NS_LOAD_ERROR
;
62 WidgetEvent
event(true, type
);
63 // Load event doesn't bubble.
64 event
.mFlags
.mBubbles
= (type
!= NS_LOAD
);
66 EventDispatcher::Dispatch(cont
, presContext
, &event
, nullptr, &status
);
73 nsScriptElement::CharacterDataChanged(nsIDocument
*aDocument
,
75 CharacterDataChangeInfo
* aInfo
)
81 nsScriptElement::AttributeChanged(nsIDocument
* aDocument
,
91 nsScriptElement::ContentAppended(nsIDocument
* aDocument
,
92 nsIContent
* aContainer
,
93 nsIContent
* aFirstNewContent
,
94 int32_t aNewIndexInContainer
)
100 nsScriptElement::ContentInserted(nsIDocument
*aDocument
,
101 nsIContent
* aContainer
,
103 int32_t aIndexInContainer
)
105 MaybeProcessScript();
109 nsScriptElement::MaybeProcessScript()
111 nsCOMPtr
<nsIContent
> cont
=
112 do_QueryInterface((nsIScriptElement
*) this);
114 NS_ASSERTION(cont
->DebugGetSlots()->mMutationObservers
.Contains(this),
115 "You forgot to add self as observer");
117 if (mAlreadyStarted
|| !mDoneAddingChildren
||
118 !cont
->GetCrossShadowCurrentDoc() || mMalformed
|| !HasScriptContent()) {
122 FreezeUriAsyncDefer();
124 mAlreadyStarted
= true;
126 nsIDocument
* ownerDoc
= cont
->OwnerDoc();
127 nsCOMPtr
<nsIParser
> parser
= ((nsIScriptElement
*) this)->GetCreatorParser();
129 nsCOMPtr
<nsIContentSink
> sink
= parser
->GetContentSink();
131 nsCOMPtr
<nsIDocument
> parserDoc
= do_QueryInterface(sink
->GetTarget());
132 if (ownerDoc
!= parserDoc
) {
133 // Willful violation of HTML5 as of 2010-12-01
139 nsRefPtr
<nsScriptLoader
> loader
= ownerDoc
->ScriptLoader();
140 return loader
->ProcessScriptElement(this);