2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00: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/. */
|
2006-11-04 05:45:02 +00:00
|
|
|
|
|
|
|
#include "nsScriptElement.h"
|
2013-09-25 11:21:22 +00:00
|
|
|
#include "mozilla/BasicEvents.h"
|
2014-03-18 04:48:21 +00:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2010-08-24 07:05:56 +00:00
|
|
|
#include "mozilla/dom/Element.h"
|
2007-07-13 08:49:07 +00:00
|
|
|
#include "nsContentUtils.h"
|
2006-11-04 05:45:02 +00:00
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsScriptLoader.h"
|
|
|
|
#include "nsIParser.h"
|
|
|
|
#include "nsGkAtoms.h"
|
2010-12-08 12:37:19 +00:00
|
|
|
#include "nsContentSink.h"
|
2006-11-04 05:45:02 +00:00
|
|
|
|
2013-10-02 03:46:04 +00:00
|
|
|
using namespace mozilla;
|
2010-08-24 07:05:56 +00:00
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2006-11-04 05:45:02 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptElement::ScriptAvailable(nsresult aResult,
|
|
|
|
nsIScriptElement *aElement,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aIsInline,
|
2006-11-04 05:45:02 +00:00
|
|
|
nsIURI *aURI,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aLineNo)
|
2006-11-04 05:45:02 +00:00
|
|
|
{
|
|
|
|
if (!aIsInline && NS_FAILED(aResult)) {
|
2012-09-19 03:24:27 +00:00
|
|
|
return FireErrorEvent();
|
2006-11-04 05:45:02 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-19 03:24:27 +00: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 */);
|
|
|
|
}
|
|
|
|
|
2006-11-04 05:45:02 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptElement::ScriptEvaluated(nsresult aResult,
|
|
|
|
nsIScriptElement *aElement,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aIsInline)
|
2006-11-04 05:45:02 +00:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (!aIsInline) {
|
|
|
|
nsCOMPtr<nsIContent> cont =
|
|
|
|
do_QueryInterface((nsIScriptElement*) this);
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<nsPresContext> presContext =
|
2007-07-13 08:49:07 +00:00
|
|
|
nsContentUtils::GetContextForContent(cont);
|
2006-11-04 05:45:02 +00:00
|
|
|
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2015-09-02 06:07:59 +00:00
|
|
|
EventMessage message = NS_SUCCEEDED(aResult) ? eLoad : eLoadError;
|
2015-08-26 12:56:59 +00:00
|
|
|
WidgetEvent event(true, message);
|
2012-12-16 01:26:03 +00:00
|
|
|
// Load event doesn't bubble.
|
2015-09-02 06:07:59 +00:00
|
|
|
event.mFlags.mBubbles = (message != eLoad);
|
2006-11-04 05:45:02 +00:00
|
|
|
|
2014-03-18 04:48:21 +00:00
|
|
|
EventDispatcher::Dispatch(cont, presContext, &event, nullptr, &status);
|
2006-11-04 05:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptElement::CharacterDataChanged(nsIDocument *aDocument,
|
|
|
|
nsIContent* aContent,
|
2007-01-30 05:48:22 +00:00
|
|
|
CharacterDataChangeInfo* aInfo)
|
2006-11-04 05:45:02 +00:00
|
|
|
{
|
|
|
|
MaybeProcessScript();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptElement::AttributeChanged(nsIDocument* aDocument,
|
2010-08-24 07:05:56 +00:00
|
|
|
Element* aElement,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aNameSpaceID,
|
2006-11-04 05:45:02 +00:00
|
|
|
nsIAtom* aAttribute,
|
2015-07-25 06:01:19 +00:00
|
|
|
int32_t aModType,
|
|
|
|
const nsAttrValue* aOldValue)
|
2006-11-04 05:45:02 +00:00
|
|
|
{
|
2006-11-09 00:02:21 +00:00
|
|
|
MaybeProcessScript();
|
2006-11-04 05:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptElement::ContentAppended(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
2010-05-11 01:12:34 +00:00
|
|
|
nsIContent* aFirstNewContent,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aNewIndexInContainer)
|
2006-11-04 05:45:02 +00:00
|
|
|
{
|
|
|
|
MaybeProcessScript();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptElement::ContentInserted(nsIDocument *aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aIndexInContainer)
|
2006-11-04 05:45:02 +00:00
|
|
|
{
|
|
|
|
MaybeProcessScript();
|
|
|
|
}
|
|
|
|
|
2011-11-16 07:50:18 +00:00
|
|
|
bool
|
2006-11-04 05:45:02 +00: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 08:42:54 +00:00
|
|
|
if (mAlreadyStarted || !mDoneAddingChildren ||
|
2016-03-31 12:20:14 +00:00
|
|
|
!cont->GetComposedDoc() || mMalformed || !HasScriptContent()) {
|
2011-11-16 07:50:18 +00:00
|
|
|
return false;
|
2009-01-14 12:49:18 +00:00
|
|
|
}
|
|
|
|
|
2009-11-17 08:52:30 +00:00
|
|
|
FreezeUriAsyncDefer();
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mAlreadyStarted = true;
|
2010-12-08 12:37:19 +00:00
|
|
|
|
2011-10-18 10:53:36 +00:00
|
|
|
nsIDocument* ownerDoc = cont->OwnerDoc();
|
2011-04-19 07:10:48 +00:00
|
|
|
nsCOMPtr<nsIParser> parser = ((nsIScriptElement*) this)->GetCreatorParser();
|
2010-12-08 12:37:19 +00:00
|
|
|
if (parser) {
|
2011-04-19 07:10:48 +00: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-16 07:50:18 +00:00
|
|
|
return false;
|
2011-04-19 07:10:48 +00:00
|
|
|
}
|
2010-12-08 12:37:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<nsScriptLoader> loader = ownerDoc->ScriptLoader();
|
2011-11-16 07:50:18 +00:00
|
|
|
return loader->ProcessScriptElement(this);
|
2006-11-04 05:45:02 +00:00
|
|
|
}
|