2007-07-12 13:05:45 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2007-03-22 10:30:00 -07:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
2010-10-22 14:29:41 -07:00
|
|
|
* the Mozilla Foundation.
|
2007-03-22 10:30:00 -07:00
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2006
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Jonas Sicking <jonas@sicking.cc> (original developer)
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsScriptElement.h"
|
2010-08-24 00:05:56 -07:00
|
|
|
#include "mozilla/dom/Element.h"
|
2007-07-13 01:49:07 -07:00
|
|
|
#include "nsContentUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsGUIEvent.h"
|
|
|
|
#include "nsEventDispatcher.h"
|
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsScriptLoader.h"
|
|
|
|
#include "nsIParser.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsGkAtoms.h"
|
2010-12-08 04:37:19 -08:00
|
|
|
#include "nsContentSink.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-24 00:05:56 -07:00
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptElement::ScriptAvailable(nsresult aResult,
|
|
|
|
nsIScriptElement *aElement,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aIsInline,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIURI *aURI,
|
|
|
|
PRInt32 aLineNo)
|
|
|
|
{
|
|
|
|
if (!aIsInline && NS_FAILED(aResult)) {
|
|
|
|
nsCOMPtr<nsIContent> cont =
|
|
|
|
do_QueryInterface((nsIScriptElement*) this);
|
|
|
|
|
2010-03-25 06:17:11 -07:00
|
|
|
nsRefPtr<nsPresContext> presContext =
|
2007-07-13 01:49:07 -07:00
|
|
|
nsContentUtils::GetContextForContent(cont);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
nsScriptErrorEvent event(PR_TRUE, NS_LOAD_ERROR);
|
|
|
|
|
|
|
|
event.lineNr = aLineNo;
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_STRING(errorString, "Error loading script");
|
|
|
|
event.errorMsg = errorString.get();
|
|
|
|
|
|
|
|
nsCAutoString spec;
|
|
|
|
aURI->GetSpec(spec);
|
|
|
|
|
|
|
|
NS_ConvertUTF8toUTF16 fileName(spec);
|
|
|
|
event.fileName = fileName.get();
|
|
|
|
|
|
|
|
nsEventDispatcher::Dispatch(cont, presContext, &event, nsnull, &status);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptElement::ScriptEvaluated(nsresult aResult,
|
|
|
|
nsIScriptElement *aElement,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aIsInline)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (!aIsInline) {
|
|
|
|
nsCOMPtr<nsIContent> cont =
|
|
|
|
do_QueryInterface((nsIScriptElement*) this);
|
|
|
|
|
2010-03-25 06:17:11 -07:00
|
|
|
nsRefPtr<nsPresContext> presContext =
|
2007-07-13 01:49:07 -07:00
|
|
|
nsContentUtils::GetContextForContent(cont);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
PRUint32 type = NS_SUCCEEDED(aResult) ? NS_LOAD : NS_LOAD_ERROR;
|
|
|
|
nsEvent event(PR_TRUE, type);
|
|
|
|
if (type == NS_LOAD) {
|
|
|
|
// Load event doesn't bubble.
|
|
|
|
event.flags |= NS_EVENT_FLAG_CANT_BUBBLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventDispatcher::Dispatch(cont, presContext, &event, nsnull, &status);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptElement::CharacterDataChanged(nsIDocument *aDocument,
|
|
|
|
nsIContent* aContent,
|
|
|
|
CharacterDataChangeInfo* aInfo)
|
|
|
|
{
|
|
|
|
MaybeProcessScript();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptElement::AttributeChanged(nsIDocument* aDocument,
|
2010-08-24 00:05:56 -07:00
|
|
|
Element* aElement,
|
2007-03-22 10:30:00 -07:00
|
|
|
PRInt32 aNameSpaceID,
|
|
|
|
nsIAtom* aAttribute,
|
2009-12-10 14:36:04 -08:00
|
|
|
PRInt32 aModType)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
MaybeProcessScript();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptElement::ContentAppended(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
2010-05-10 18:12:34 -07:00
|
|
|
nsIContent* aFirstNewContent,
|
2007-03-22 10:30:00 -07:00
|
|
|
PRInt32 aNewIndexInContainer)
|
|
|
|
{
|
|
|
|
MaybeProcessScript();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptElement::ContentInserted(nsIDocument *aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
|
|
|
PRInt32 aIndexInContainer)
|
|
|
|
{
|
|
|
|
MaybeProcessScript();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScriptElement::MaybeProcessScript()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> cont =
|
|
|
|
do_QueryInterface((nsIScriptElement*) this);
|
|
|
|
|
|
|
|
NS_ASSERTION(cont->DebugGetSlots()->mMutationObservers.Contains(this),
|
|
|
|
"You forgot to add self as observer");
|
|
|
|
|
2010-09-01 04:41:12 -07:00
|
|
|
if (mAlreadyStarted || !mDoneAddingChildren || !cont->IsInDoc() ||
|
2009-01-14 04:49:18 -08:00
|
|
|
mMalformed || !HasScriptContent()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-17 00:52:30 -08:00
|
|
|
FreezeUriAsyncDefer();
|
|
|
|
|
2010-09-01 04:41:12 -07:00
|
|
|
mAlreadyStarted = PR_TRUE;
|
2010-12-08 04:37:19 -08:00
|
|
|
|
|
|
|
nsIDocument* ownerDoc = cont->GetOwnerDoc();
|
2011-04-19 00:10:48 -07:00
|
|
|
nsCOMPtr<nsIParser> parser = ((nsIScriptElement*) this)->GetCreatorParser();
|
2010-12-08 04:37:19 -08:00
|
|
|
if (parser) {
|
2011-04-19 00:10:48 -07:00
|
|
|
nsCOMPtr<nsIContentSink> sink = parser->GetContentSink();
|
|
|
|
if (sink) {
|
|
|
|
nsCOMPtr<nsIDocument> parserDoc = do_QueryInterface(sink->GetTarget());
|
|
|
|
if (ownerDoc != parserDoc) {
|
|
|
|
// Willful violation of HTML5 as of 2010-12-01
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-12-08 04:37:19 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsScriptLoader> loader = ownerDoc->ScriptLoader();
|
2010-10-22 14:29:41 -07:00
|
|
|
nsresult scriptresult = loader->ProcessScriptElement(this);
|
2007-05-30 13:43:41 -07:00
|
|
|
|
|
|
|
// The only error we don't ignore is NS_ERROR_HTMLPARSER_BLOCK
|
|
|
|
// However we don't want to override other success values
|
|
|
|
// (such as NS_CONTENT_SCRIPT_IS_EVENTHANDLER)
|
|
|
|
if (NS_FAILED(scriptresult) &&
|
|
|
|
scriptresult != NS_ERROR_HTMLPARSER_BLOCK) {
|
|
|
|
scriptresult = NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return scriptresult;
|
|
|
|
}
|