1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * the Mozilla Foundation.
19 : * Portions created by the Initial Developer are Copyright (C) 2006
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Jonas Sicking <jonas@sicking.cc> (original developer)
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "nsScriptElement.h"
40 : #include "mozilla/dom/Element.h"
41 : #include "nsContentUtils.h"
42 : #include "nsGUIEvent.h"
43 : #include "nsEventDispatcher.h"
44 : #include "nsPresContext.h"
45 : #include "nsScriptLoader.h"
46 : #include "nsIParser.h"
47 : #include "nsAutoPtr.h"
48 : #include "nsGkAtoms.h"
49 : #include "nsContentSink.h"
50 :
51 : using namespace mozilla::dom;
52 :
53 : NS_IMETHODIMP
54 0 : nsScriptElement::ScriptAvailable(nsresult aResult,
55 : nsIScriptElement *aElement,
56 : bool aIsInline,
57 : nsIURI *aURI,
58 : PRInt32 aLineNo)
59 : {
60 0 : if (!aIsInline && NS_FAILED(aResult)) {
61 : nsCOMPtr<nsIContent> cont =
62 0 : do_QueryInterface((nsIScriptElement*) this);
63 :
64 : nsRefPtr<nsPresContext> presContext =
65 0 : nsContentUtils::GetContextForContent(cont);
66 :
67 0 : nsEventStatus status = nsEventStatus_eIgnore;
68 0 : nsScriptErrorEvent event(true, NS_LOAD_ERROR);
69 :
70 0 : event.lineNr = aLineNo;
71 :
72 0 : NS_NAMED_LITERAL_STRING(errorString, "Error loading script");
73 0 : event.errorMsg = errorString.get();
74 :
75 0 : nsCAutoString spec;
76 0 : aURI->GetSpec(spec);
77 :
78 0 : NS_ConvertUTF8toUTF16 fileName(spec);
79 0 : event.fileName = fileName.get();
80 :
81 0 : nsEventDispatcher::Dispatch(cont, presContext, &event, nsnull, &status);
82 : }
83 :
84 0 : return NS_OK;
85 : }
86 :
87 : NS_IMETHODIMP
88 0 : nsScriptElement::ScriptEvaluated(nsresult aResult,
89 : nsIScriptElement *aElement,
90 : bool aIsInline)
91 : {
92 0 : nsresult rv = NS_OK;
93 0 : if (!aIsInline) {
94 : nsCOMPtr<nsIContent> cont =
95 0 : do_QueryInterface((nsIScriptElement*) this);
96 :
97 : nsRefPtr<nsPresContext> presContext =
98 0 : nsContentUtils::GetContextForContent(cont);
99 :
100 0 : nsEventStatus status = nsEventStatus_eIgnore;
101 0 : PRUint32 type = NS_SUCCEEDED(aResult) ? NS_LOAD : NS_LOAD_ERROR;
102 0 : nsEvent event(true, type);
103 0 : if (type == NS_LOAD) {
104 : // Load event doesn't bubble.
105 0 : event.flags |= NS_EVENT_FLAG_CANT_BUBBLE;
106 : }
107 :
108 0 : nsEventDispatcher::Dispatch(cont, presContext, &event, nsnull, &status);
109 : }
110 :
111 0 : return rv;
112 : }
113 :
114 : void
115 0 : nsScriptElement::CharacterDataChanged(nsIDocument *aDocument,
116 : nsIContent* aContent,
117 : CharacterDataChangeInfo* aInfo)
118 : {
119 0 : MaybeProcessScript();
120 0 : }
121 :
122 : void
123 0 : nsScriptElement::AttributeChanged(nsIDocument* aDocument,
124 : Element* aElement,
125 : PRInt32 aNameSpaceID,
126 : nsIAtom* aAttribute,
127 : PRInt32 aModType)
128 : {
129 0 : MaybeProcessScript();
130 0 : }
131 :
132 : void
133 0 : nsScriptElement::ContentAppended(nsIDocument* aDocument,
134 : nsIContent* aContainer,
135 : nsIContent* aFirstNewContent,
136 : PRInt32 aNewIndexInContainer)
137 : {
138 0 : MaybeProcessScript();
139 0 : }
140 :
141 : void
142 0 : nsScriptElement::ContentInserted(nsIDocument *aDocument,
143 : nsIContent* aContainer,
144 : nsIContent* aChild,
145 : PRInt32 aIndexInContainer)
146 : {
147 0 : MaybeProcessScript();
148 0 : }
149 :
150 : bool
151 0 : nsScriptElement::MaybeProcessScript()
152 : {
153 : nsCOMPtr<nsIContent> cont =
154 0 : do_QueryInterface((nsIScriptElement*) this);
155 :
156 0 : NS_ASSERTION(cont->DebugGetSlots()->mMutationObservers.Contains(this),
157 : "You forgot to add self as observer");
158 :
159 0 : if (mAlreadyStarted || !mDoneAddingChildren || !cont->IsInDoc() ||
160 0 : mMalformed || !HasScriptContent()) {
161 0 : return false;
162 : }
163 :
164 0 : FreezeUriAsyncDefer();
165 :
166 0 : mAlreadyStarted = true;
167 :
168 0 : nsIDocument* ownerDoc = cont->OwnerDoc();
169 0 : nsCOMPtr<nsIParser> parser = ((nsIScriptElement*) this)->GetCreatorParser();
170 0 : if (parser) {
171 0 : nsCOMPtr<nsIContentSink> sink = parser->GetContentSink();
172 0 : if (sink) {
173 0 : nsCOMPtr<nsIDocument> parserDoc = do_QueryInterface(sink->GetTarget());
174 0 : if (ownerDoc != parserDoc) {
175 : // Willful violation of HTML5 as of 2010-12-01
176 0 : return false;
177 : }
178 : }
179 : }
180 :
181 0 : nsRefPtr<nsScriptLoader> loader = ownerDoc->ScriptLoader();
182 0 : return loader->ProcessScriptElement(this);
183 : }
|