/src/mozilla-central/dom/script/ScriptElement.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "ScriptElement.h" |
8 | | #include "ScriptLoader.h" |
9 | | #include "mozilla/BasicEvents.h" |
10 | | #include "mozilla/EventDispatcher.h" |
11 | | #include "mozilla/dom/Element.h" |
12 | | #include "nsContentUtils.h" |
13 | | #include "nsPresContext.h" |
14 | | #include "nsIParser.h" |
15 | | #include "nsGkAtoms.h" |
16 | | #include "nsContentSink.h" |
17 | | |
18 | | using namespace mozilla; |
19 | | using namespace mozilla::dom; |
20 | | |
21 | | NS_IMETHODIMP |
22 | | ScriptElement::ScriptAvailable(nsresult aResult, |
23 | | nsIScriptElement* aElement, |
24 | | bool aIsInlineClassicScript, |
25 | | nsIURI* aURI, |
26 | | int32_t aLineNo) |
27 | 0 | { |
28 | 0 | if (!aIsInlineClassicScript && NS_FAILED(aResult)) { |
29 | 0 | nsCOMPtr<nsIParser> parser = do_QueryReferent(mCreatorParser); |
30 | 0 | if (parser) { |
31 | 0 | parser->PushDefinedInsertionPoint(); |
32 | 0 | } |
33 | 0 | nsresult rv = FireErrorEvent(); |
34 | 0 | if (parser) { |
35 | 0 | parser->PopDefinedInsertionPoint(); |
36 | 0 | } |
37 | 0 | return rv; |
38 | 0 | } |
39 | 0 | return NS_OK; |
40 | 0 | } |
41 | | |
42 | | /* virtual */ nsresult |
43 | | ScriptElement::FireErrorEvent() |
44 | 0 | { |
45 | 0 | nsCOMPtr<nsIContent> cont = |
46 | 0 | do_QueryInterface((nsIScriptElement*) this); |
47 | 0 |
|
48 | 0 | return nsContentUtils::DispatchTrustedEvent(cont->OwnerDoc(), |
49 | 0 | cont, |
50 | 0 | NS_LITERAL_STRING("error"), |
51 | 0 | CanBubble::eNo, |
52 | 0 | Cancelable::eNo); |
53 | 0 | } |
54 | | |
55 | | NS_IMETHODIMP |
56 | | ScriptElement::ScriptEvaluated(nsresult aResult, |
57 | | nsIScriptElement* aElement, |
58 | | bool aIsInline) |
59 | 0 | { |
60 | 0 | nsresult rv = NS_OK; |
61 | 0 | if (!aIsInline) { |
62 | 0 | nsCOMPtr<nsIContent> cont = |
63 | 0 | do_QueryInterface((nsIScriptElement*) this); |
64 | 0 |
|
65 | 0 | RefPtr<nsPresContext> presContext = |
66 | 0 | nsContentUtils::GetContextForContent(cont); |
67 | 0 |
|
68 | 0 | nsEventStatus status = nsEventStatus_eIgnore; |
69 | 0 | EventMessage message = NS_SUCCEEDED(aResult) ? eLoad : eLoadError; |
70 | 0 | WidgetEvent event(true, message); |
71 | 0 | // Load event doesn't bubble. |
72 | 0 | event.mFlags.mBubbles = (message != eLoad); |
73 | 0 |
|
74 | 0 | EventDispatcher::Dispatch(cont, presContext, &event, nullptr, &status); |
75 | 0 | } |
76 | 0 |
|
77 | 0 | return rv; |
78 | 0 | } |
79 | | |
80 | | void |
81 | | ScriptElement::CharacterDataChanged(nsIContent* aContent, |
82 | | const CharacterDataChangeInfo&) |
83 | 0 | { |
84 | 0 | MaybeProcessScript(); |
85 | 0 | } |
86 | | |
87 | | void |
88 | | ScriptElement::AttributeChanged(Element* aElement, |
89 | | int32_t aNameSpaceID, |
90 | | nsAtom* aAttribute, |
91 | | int32_t aModType, |
92 | | const nsAttrValue* aOldValue) |
93 | 0 | { |
94 | 0 | MaybeProcessScript(); |
95 | 0 | } |
96 | | |
97 | | void |
98 | | ScriptElement::ContentAppended(nsIContent* aFirstNewContent) |
99 | 0 | { |
100 | 0 | MaybeProcessScript(); |
101 | 0 | } |
102 | | |
103 | | void |
104 | | ScriptElement::ContentInserted(nsIContent* aChild) |
105 | 0 | { |
106 | 0 | MaybeProcessScript(); |
107 | 0 | } |
108 | | |
109 | | bool |
110 | | ScriptElement::MaybeProcessScript() |
111 | 0 | { |
112 | 0 | nsCOMPtr<nsIContent> cont = |
113 | 0 | do_QueryInterface((nsIScriptElement*) this); |
114 | 0 |
|
115 | 0 | NS_ASSERTION(cont->DebugGetSlots()->mMutationObservers.Contains(this), |
116 | 0 | "You forgot to add self as observer"); |
117 | 0 |
|
118 | 0 | if (mAlreadyStarted || !mDoneAddingChildren || |
119 | 0 | !cont->GetComposedDoc() || mMalformed || !HasScriptContent()) { |
120 | 0 | return false; |
121 | 0 | } |
122 | 0 | |
123 | 0 | nsIDocument* ownerDoc = cont->OwnerDoc(); |
124 | 0 | FreezeExecutionAttrs(ownerDoc); |
125 | 0 |
|
126 | 0 | mAlreadyStarted = true; |
127 | 0 |
|
128 | 0 | nsCOMPtr<nsIParser> parser = ((nsIScriptElement*) this)->GetCreatorParser(); |
129 | 0 | if (parser) { |
130 | 0 | nsCOMPtr<nsIContentSink> sink = parser->GetContentSink(); |
131 | 0 | if (sink) { |
132 | 0 | nsCOMPtr<nsIDocument> parserDoc = do_QueryInterface(sink->GetTarget()); |
133 | 0 | if (ownerDoc != parserDoc) { |
134 | 0 | // Willful violation of HTML5 as of 2010-12-01 |
135 | 0 | return false; |
136 | 0 | } |
137 | 0 | } |
138 | 0 | } |
139 | 0 | |
140 | 0 | RefPtr<ScriptLoader> loader = ownerDoc->ScriptLoader(); |
141 | 0 | return loader->ProcessScriptElement(this); |
142 | 0 | } |