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