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
|
|
|
|
2017-05-08 06:24:22 +00:00
|
|
|
#include "ScriptElement.h"
|
|
|
|
#include "ScriptLoader.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 "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
|
2017-05-08 06:24:22 +00:00
|
|
|
ScriptElement::ScriptAvailable(nsresult aResult, nsIScriptElement* aElement,
|
2017-12-01 13:12:48 +00:00
|
|
|
bool aIsInlineClassicScript, nsIURI* aURI,
|
2017-05-08 06:24:22 +00:00
|
|
|
int32_t aLineNo) {
|
2017-12-01 13:12:48 +00:00
|
|
|
if (!aIsInlineClassicScript && NS_FAILED(aResult)) {
|
2016-09-02 21:55:38 +00:00
|
|
|
nsCOMPtr<nsIParser> parser = do_QueryReferent(mCreatorParser);
|
|
|
|
if (parser) {
|
2019-05-08 06:10:47 +00:00
|
|
|
parser->IncrementScriptNestingLevel();
|
2016-09-02 21:55:38 +00:00
|
|
|
}
|
|
|
|
nsresult rv = FireErrorEvent();
|
|
|
|
if (parser) {
|
2019-05-08 06:10:47 +00:00
|
|
|
parser->DecrementScriptNestingLevel();
|
2016-09-02 21:55:38 +00:00
|
|
|
}
|
|
|
|
return rv;
|
2006-11-04 05:45:02 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:05:29 +00:00
|
|
|
/* virtual */
|
|
|
|
nsresult ScriptElement::FireErrorEvent() {
|
2012-09-19 03:24:27 +00:00
|
|
|
nsCOMPtr<nsIContent> cont = do_QueryInterface((nsIScriptElement*)this);
|
|
|
|
|
|
|
|
return nsContentUtils::DispatchTrustedEvent(cont->OwnerDoc(), cont,
|
|
|
|
NS_LITERAL_STRING("error"),
|
2018-06-25 16:23:50 +00:00
|
|
|
CanBubble::eNo, Cancelable::eNo);
|
2012-09-19 03:24:27 +00:00
|
|
|
}
|
|
|
|
|
2006-11-04 05:45:02 +00:00
|
|
|
NS_IMETHODIMP
|
2017-05-08 06:24:22 +00:00
|
|
|
ScriptElement::ScriptEvaluated(nsresult aResult, nsIScriptElement* aElement,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-01 11:36:58 +00:00
|
|
|
void ScriptElement::CharacterDataChanged(nsIContent* aContent,
|
2018-02-27 14:30:27 +00:00
|
|
|
const CharacterDataChangeInfo&) {
|
2006-11-04 05:45:02 +00:00
|
|
|
MaybeProcessScript();
|
|
|
|
}
|
|
|
|
|
2018-03-01 11:36:58 +00:00
|
|
|
void ScriptElement::AttributeChanged(Element* aElement, int32_t aNameSpaceID,
|
2017-10-02 22:05:19 +00:00
|
|
|
nsAtom* aAttribute, int32_t aModType,
|
2017-05-08 06:24:22 +00:00
|
|
|
const nsAttrValue* aOldValue) {
|
2006-11-09 00:02:21 +00:00
|
|
|
MaybeProcessScript();
|
2006-11-04 05:45:02 +00:00
|
|
|
}
|
|
|
|
|
2018-03-01 11:36:58 +00:00
|
|
|
void ScriptElement::ContentAppended(nsIContent* aFirstNewContent) {
|
2006-11-04 05:45:02 +00:00
|
|
|
MaybeProcessScript();
|
|
|
|
}
|
|
|
|
|
2018-03-01 11:36:58 +00:00
|
|
|
void ScriptElement::ContentInserted(nsIContent* aChild) {
|
2006-11-04 05:45:02 +00:00
|
|
|
MaybeProcessScript();
|
|
|
|
}
|
|
|
|
|
2017-05-08 06:24:22 +00:00
|
|
|
bool ScriptElement::MaybeProcessScript() {
|
2006-11-04 05:45:02 +00:00
|
|
|
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 || !cont->GetComposedDoc() ||
|
2016-03-31 12:20:14 +00:00
|
|
|
mMalformed || !HasScriptContent()) {
|
2011-11-16 07:50:18 +00:00
|
|
|
return false;
|
2009-01-14 12:49:18 +00:00
|
|
|
}
|
|
|
|
|
2019-01-02 13:05:23 +00:00
|
|
|
Document* ownerDoc = cont->OwnerDoc();
|
2018-01-08 15:17:34 +00:00
|
|
|
FreezeExecutionAttrs(ownerDoc);
|
2009-11-17 08:52:30 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mAlreadyStarted = true;
|
2010-12-08 12:37:19 +00:00
|
|
|
|
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) {
|
2019-01-02 13:05:23 +00:00
|
|
|
nsCOMPtr<Document> parserDoc = do_QueryInterface(sink->GetTarget());
|
2011-04-19 07:10:48 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-08 06:24:22 +00:00
|
|
|
RefPtr<ScriptLoader> loader = ownerDoc->ScriptLoader();
|
2011-11-16 07:50:18 +00:00
|
|
|
return loader->ProcessScriptElement(this);
|
2006-11-04 05:45:02 +00:00
|
|
|
}
|