2007-07-12 13:05:45 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsScriptElement.h"
|
2013-09-25 04:21:22 -07:00
|
|
|
#include "mozilla/BasicEvents.h"
|
2014-03-17 21:48:21 -07:00
|
|
|
#include "mozilla/EventDispatcher.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 "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
|
|
|
|
2013-10-01 20:46:04 -07:00
|
|
|
using namespace mozilla;
|
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,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aLineNo)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!aIsInline && NS_FAILED(aResult)) {
|
2012-09-18 20:24:27 -07:00
|
|
|
return FireErrorEvent();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-18 20:24:27 -07:00
|
|
|
/* virtual */ nsresult
|
|
|
|
nsScriptElement::FireErrorEvent()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> cont =
|
|
|
|
do_QueryInterface((nsIScriptElement*) this);
|
|
|
|
|
|
|
|
return nsContentUtils::DispatchTrustedEvent(cont->OwnerDoc(),
|
|
|
|
cont,
|
|
|
|
NS_LITERAL_STRING("error"),
|
|
|
|
false /* bubbles */,
|
|
|
|
false /* cancelable */);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
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;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t type = NS_SUCCEEDED(aResult) ? NS_LOAD : NS_LOAD_ERROR;
|
2013-10-01 20:46:04 -07:00
|
|
|
WidgetEvent event(true, type);
|
2012-12-15 17:26:03 -08:00
|
|
|
// Load event doesn't bubble.
|
|
|
|
event.mFlags.mBubbles = (type != NS_LOAD);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-03-17 21:48:21 -07:00
|
|
|
EventDispatcher::Dispatch(cont, presContext, &event, nullptr, &status);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aNameSpaceID,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIAtom* aAttribute,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t 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,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aNewIndexInContainer)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
MaybeProcessScript();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptElement::ContentInserted(nsIDocument *aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aIndexInContainer)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
MaybeProcessScript();
|
|
|
|
}
|
|
|
|
|
2011-11-15 23:50:18 -08:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsScriptElement::MaybeProcessScript()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> cont =
|
|
|
|
do_QueryInterface((nsIScriptElement*) this);
|
|
|
|
|
|
|
|
NS_ASSERTION(cont->DebugGetSlots()->mMutationObservers.Contains(this),
|
|
|
|
"You forgot to add self as observer");
|
|
|
|
|
2014-06-07 01:42:54 -07:00
|
|
|
if (mAlreadyStarted || !mDoneAddingChildren ||
|
|
|
|
!cont->GetCrossShadowCurrentDoc() || mMalformed || !HasScriptContent()) {
|
2011-11-15 23:50:18 -08:00
|
|
|
return false;
|
2009-01-14 04:49:18 -08:00
|
|
|
}
|
|
|
|
|
2009-11-17 00:52:30 -08:00
|
|
|
FreezeUriAsyncDefer();
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mAlreadyStarted = true;
|
2010-12-08 04:37:19 -08:00
|
|
|
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* ownerDoc = cont->OwnerDoc();
|
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
|
2011-11-15 23:50:18 -08:00
|
|
|
return false;
|
2011-04-19 00:10:48 -07:00
|
|
|
}
|
2010-12-08 04:37:19 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsScriptLoader> loader = ownerDoc->ScriptLoader();
|
2011-11-15 23:50:18 -08:00
|
|
|
return loader->ProcessScriptElement(this);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|