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