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/. */
|
2004-07-07 02:52:03 +04:00
|
|
|
|
|
|
|
#ifndef nsIScriptElement_h___
|
|
|
|
#define nsIScriptElement_h___
|
|
|
|
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2006-11-04 08:45:02 +03:00
|
|
|
#include "nsIScriptLoaderObserver.h"
|
2009-10-15 15:29:11 +04:00
|
|
|
#include "nsWeakPtr.h"
|
|
|
|
#include "nsIParser.h"
|
2010-09-01 15:41:12 +04:00
|
|
|
#include "nsContentCreatorFunctions.h"
|
2010-10-27 16:04:09 +04:00
|
|
|
#include "nsIDOMHTMLScriptElement.h"
|
2012-03-10 20:13:52 +04:00
|
|
|
#include "mozilla/CORSMode.h"
|
2004-07-07 02:52:03 +04:00
|
|
|
|
2006-02-01 01:19:46 +03:00
|
|
|
#define NS_ISCRIPTELEMENT_IID \
|
2014-05-21 21:08:12 +04:00
|
|
|
{ 0xe60fca9b, 0x1b96, 0x4e4e, \
|
|
|
|
{ 0xa9, 0xb4, 0xdc, 0x98, 0x4f, 0x88, 0x3f, 0x9c } }
|
2004-07-07 02:52:03 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal interface implemented by script elements
|
|
|
|
*/
|
2017-05-08 09:25:03 +03:00
|
|
|
class nsIScriptElement : public nsIScriptLoaderObserver
|
|
|
|
{
|
2004-07-07 02:52:03 +04:00
|
|
|
public:
|
2005-11-11 17:36:26 +03:00
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(NS_ISCRIPTELEMENT_IID)
|
2004-07-07 02:52:03 +04:00
|
|
|
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit nsIScriptElement(mozilla::dom::FromParser aFromParser)
|
2014-10-23 15:06:00 +04:00
|
|
|
: mLineNumber(1),
|
2011-10-17 18:59:28 +04:00
|
|
|
mAlreadyStarted(false),
|
|
|
|
mMalformed(false),
|
2010-11-01 13:50:01 +03:00
|
|
|
mDoneAddingChildren(aFromParser == mozilla::dom::NOT_FROM_PARSER ||
|
|
|
|
aFromParser == mozilla::dom::FROM_PARSER_FRAGMENT),
|
2010-10-27 16:04:09 +04:00
|
|
|
mForceAsync(aFromParser == mozilla::dom::NOT_FROM_PARSER ||
|
|
|
|
aFromParser == mozilla::dom::FROM_PARSER_FRAGMENT),
|
2011-10-17 18:59:28 +04:00
|
|
|
mFrozen(false),
|
|
|
|
mDefer(false),
|
|
|
|
mAsync(false),
|
|
|
|
mExternal(false),
|
2010-10-27 11:18:24 +04:00
|
|
|
mParserCreated(aFromParser == mozilla::dom::FROM_PARSER_FRAGMENT ?
|
|
|
|
mozilla::dom::NOT_FROM_PARSER : aFromParser),
|
|
|
|
// Fragment parser-created scripts (if executable)
|
|
|
|
// behave like script-created scripts.
|
2012-07-30 18:20:58 +04:00
|
|
|
mCreatorParser(nullptr)
|
2006-11-04 08:45:02 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2004-07-07 02:52:03 +04:00
|
|
|
/**
|
|
|
|
* Content type identifying the scripting language. Can be empty, in
|
|
|
|
* which case javascript will be assumed.
|
2016-07-13 19:10:13 +03:00
|
|
|
* Return false if type attribute is not found.
|
2004-07-07 02:52:03 +04:00
|
|
|
*/
|
2016-07-13 19:10:13 +03:00
|
|
|
virtual bool GetScriptType(nsAString& type) = 0;
|
|
|
|
|
2004-07-07 02:52:03 +04:00
|
|
|
/**
|
|
|
|
* Location of script source text. Can return null, in which case
|
|
|
|
* this is assumed to be an inline script element.
|
|
|
|
*/
|
2009-11-17 11:52:30 +03:00
|
|
|
nsIURI* GetScriptURI()
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(mFrozen, "Not ready for this call yet!");
|
|
|
|
return mUri;
|
|
|
|
}
|
2016-07-13 19:10:13 +03:00
|
|
|
|
2004-07-07 02:52:03 +04:00
|
|
|
/**
|
|
|
|
* Script source text for inline script elements.
|
|
|
|
*/
|
|
|
|
virtual void GetScriptText(nsAString& text) = 0;
|
|
|
|
|
|
|
|
virtual void GetScriptCharset(nsAString& charset) = 0;
|
2008-07-26 06:42:12 +04:00
|
|
|
|
2009-11-17 11:52:30 +03:00
|
|
|
/**
|
|
|
|
* Freezes the return values of GetScriptDeferred(), GetScriptAsync() and
|
|
|
|
* GetScriptURI() so that subsequent modifications to the attributes don't
|
|
|
|
* change execution behavior.
|
|
|
|
*/
|
|
|
|
virtual void FreezeUriAsyncDefer() = 0;
|
|
|
|
|
2008-07-26 06:42:12 +04:00
|
|
|
/**
|
|
|
|
* Is the script deferred. Currently only supported by HTML scripts.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool GetScriptDeferred()
|
2009-11-17 11:52:30 +03:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(mFrozen, "Not ready for this call yet!");
|
|
|
|
return mDefer;
|
|
|
|
}
|
2008-07-26 06:42:12 +04:00
|
|
|
|
2009-11-10 04:04:24 +03:00
|
|
|
/**
|
|
|
|
* Is the script async. Currently only supported by HTML scripts.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool GetScriptAsync()
|
2009-11-17 11:52:30 +03:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(mFrozen, "Not ready for this call yet!");
|
2016-07-13 19:10:13 +03:00
|
|
|
return mAsync;
|
2009-11-17 11:52:30 +03:00
|
|
|
}
|
2009-11-10 04:04:24 +03:00
|
|
|
|
2010-10-27 11:03:10 +04:00
|
|
|
/**
|
|
|
|
* Is the script an external script?
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool GetScriptExternal()
|
2010-10-27 11:03:10 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(mFrozen, "Not ready for this call yet!");
|
|
|
|
return mExternal;
|
|
|
|
}
|
|
|
|
|
2010-09-01 15:41:12 +04:00
|
|
|
/**
|
2010-10-25 16:17:38 +04:00
|
|
|
* Returns how the element was created.
|
2010-09-01 15:41:12 +04:00
|
|
|
*/
|
2010-10-25 16:17:38 +04:00
|
|
|
mozilla::dom::FromParser GetParserCreated()
|
2010-09-01 15:41:12 +04:00
|
|
|
{
|
|
|
|
return mParserCreated;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void SetScriptLineNumber(uint32_t aLineNumber)
|
2006-11-04 08:45:02 +03:00
|
|
|
{
|
|
|
|
mLineNumber = aLineNumber;
|
|
|
|
}
|
2017-05-08 09:25:03 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t GetScriptLineNumber()
|
2006-11-04 08:45:02 +03:00
|
|
|
{
|
|
|
|
return mLineNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetIsMalformed()
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mMalformed = true;
|
2006-11-04 08:45:02 +03:00
|
|
|
}
|
2017-05-08 09:25:03 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool IsMalformed()
|
2006-11-04 08:45:02 +03:00
|
|
|
{
|
|
|
|
return mMalformed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PreventExecution()
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mAlreadyStarted = true;
|
2010-09-01 15:41:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void LoseParserInsertedness()
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mFrozen = false;
|
2012-07-30 18:20:58 +04:00
|
|
|
mUri = nullptr;
|
|
|
|
mCreatorParser = nullptr;
|
2010-10-25 16:17:38 +04:00
|
|
|
mParserCreated = mozilla::dom::NOT_FROM_PARSER;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool async = false;
|
2010-10-27 16:04:09 +04:00
|
|
|
nsCOMPtr<nsIDOMHTMLScriptElement> htmlScript = do_QueryInterface(this);
|
|
|
|
if (htmlScript) {
|
|
|
|
htmlScript->GetAsync(&async);
|
|
|
|
}
|
|
|
|
mForceAsync = !async;
|
2006-11-04 08:45:02 +03:00
|
|
|
}
|
|
|
|
|
2009-10-15 15:29:11 +04:00
|
|
|
void SetCreatorParser(nsIParser* aParser)
|
|
|
|
{
|
2013-07-10 13:56:21 +04:00
|
|
|
mCreatorParser = do_GetWeakReference(aParser);
|
2009-10-15 15:29:11 +04:00
|
|
|
}
|
|
|
|
|
2012-01-20 15:16:27 +04:00
|
|
|
/**
|
|
|
|
* Unblocks the creator parser
|
|
|
|
*/
|
|
|
|
void UnblockParser()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIParser> parser = do_QueryReferent(mCreatorParser);
|
|
|
|
if (parser) {
|
|
|
|
parser->UnblockParser();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempts to resume parsing asynchronously
|
|
|
|
*/
|
|
|
|
void ContinueParserAsync()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIParser> parser = do_QueryReferent(mCreatorParser);
|
|
|
|
if (parser) {
|
|
|
|
parser->ContinueInterruptedParsingAsync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-15 15:29:11 +04:00
|
|
|
/**
|
|
|
|
* Informs the creator parser that the evaluation of this script is starting
|
|
|
|
*/
|
|
|
|
void BeginEvaluating()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIParser> parser = do_QueryReferent(mCreatorParser);
|
|
|
|
if (parser) {
|
2016-09-03 00:55:38 +03:00
|
|
|
parser->PushDefinedInsertionPoint();
|
2009-10-15 15:29:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Informs the creator parser that the evaluation of this script is ending
|
|
|
|
*/
|
|
|
|
void EndEvaluating()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIParser> parser = do_QueryReferent(mCreatorParser);
|
|
|
|
if (parser) {
|
2016-09-03 00:55:38 +03:00
|
|
|
parser->PopDefinedInsertionPoint();
|
2009-10-15 15:29:11 +04:00
|
|
|
}
|
|
|
|
}
|
2016-07-13 19:10:13 +03:00
|
|
|
|
2009-10-15 15:29:11 +04:00
|
|
|
/**
|
|
|
|
* Retrieves a pointer to the creator parser if this has one or null if not
|
|
|
|
*/
|
|
|
|
already_AddRefed<nsIParser> GetCreatorParser()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIParser> parser = do_QueryReferent(mCreatorParser);
|
|
|
|
return parser.forget();
|
|
|
|
}
|
|
|
|
|
2011-11-16 11:50:18 +04:00
|
|
|
/**
|
|
|
|
* This method is called when the parser finishes creating the script
|
|
|
|
* element's children, if any are present.
|
|
|
|
*
|
|
|
|
* @return whether the parser will be blocked while this script is being
|
|
|
|
* loaded
|
|
|
|
*/
|
|
|
|
bool AttemptToExecute()
|
|
|
|
{
|
|
|
|
mDoneAddingChildren = true;
|
2011-11-16 11:50:18 +04:00
|
|
|
bool block = MaybeProcessScript();
|
2011-11-16 11:50:18 +04:00
|
|
|
if (!mAlreadyStarted) {
|
|
|
|
// Need to lose parser-insertedness here to allow another script to cause
|
|
|
|
// execution later.
|
|
|
|
LoseParserInsertedness();
|
|
|
|
}
|
2011-11-16 11:50:18 +04:00
|
|
|
return block;
|
2011-11-16 11:50:18 +04:00
|
|
|
}
|
|
|
|
|
2012-03-10 20:13:52 +04:00
|
|
|
/**
|
|
|
|
* Get the CORS mode of the script element
|
|
|
|
*/
|
|
|
|
virtual mozilla::CORSMode GetCORSMode() const
|
|
|
|
{
|
|
|
|
/* Default to no CORS */
|
|
|
|
return mozilla::CORS_NONE;
|
|
|
|
}
|
|
|
|
|
2012-09-19 07:24:27 +04:00
|
|
|
/**
|
|
|
|
* Fire an error event
|
|
|
|
*/
|
|
|
|
virtual nsresult FireErrorEvent() = 0;
|
|
|
|
|
2006-11-04 08:45:02 +03:00
|
|
|
protected:
|
2011-11-16 11:50:18 +04:00
|
|
|
/**
|
|
|
|
* Processes the script if it's in the document-tree and links to or
|
|
|
|
* contains a script. Once it has been evaluated there is no way to make it
|
|
|
|
* reevaluate the script, you'll have to create a new element. This also means
|
|
|
|
* that when adding a src attribute to an element that already contains an
|
|
|
|
* inline script, the script referenced by the src attribute will not be
|
|
|
|
* loaded.
|
|
|
|
*
|
|
|
|
* In order to be able to use multiple childNodes, or to use the
|
|
|
|
* fallback mechanism of using both inline script and linked script you have
|
|
|
|
* to add all attributes and childNodes before adding the element to the
|
|
|
|
* document-tree.
|
2011-11-16 11:50:18 +04:00
|
|
|
*
|
|
|
|
* @return whether the parser will be blocked while this script is being
|
|
|
|
* loaded
|
2011-11-16 11:50:18 +04:00
|
|
|
*/
|
2011-11-16 11:50:18 +04:00
|
|
|
virtual bool MaybeProcessScript() = 0;
|
2011-11-16 11:50:18 +04:00
|
|
|
|
2009-11-17 11:52:30 +03:00
|
|
|
/**
|
|
|
|
* The start line number of the script.
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mLineNumber;
|
2016-07-13 19:10:13 +03:00
|
|
|
|
2009-11-17 11:52:30 +03:00
|
|
|
/**
|
|
|
|
* The "already started" flag per HTML5.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mAlreadyStarted;
|
2016-07-13 19:10:13 +03:00
|
|
|
|
2009-11-17 11:52:30 +03:00
|
|
|
/**
|
|
|
|
* The script didn't have an end tag.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mMalformed;
|
2016-07-13 19:10:13 +03:00
|
|
|
|
2009-11-17 11:52:30 +03:00
|
|
|
/**
|
|
|
|
* False if parser-inserted but the parser hasn't triggered running yet.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mDoneAddingChildren;
|
2009-11-17 11:52:30 +03:00
|
|
|
|
2010-10-27 16:04:09 +04:00
|
|
|
/**
|
|
|
|
* If true, the .async property returns true instead of reflecting the
|
|
|
|
* content attribute.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mForceAsync;
|
2010-10-27 16:04:09 +04:00
|
|
|
|
2009-11-17 11:52:30 +03:00
|
|
|
/**
|
|
|
|
* Whether src, defer and async are frozen.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mFrozen;
|
2016-07-13 19:10:13 +03:00
|
|
|
|
2009-11-17 11:52:30 +03:00
|
|
|
/**
|
|
|
|
* The effective deferredness.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mDefer;
|
2016-07-13 19:10:13 +03:00
|
|
|
|
2009-11-17 11:52:30 +03:00
|
|
|
/**
|
|
|
|
* The effective asyncness.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mAsync;
|
2016-07-13 19:10:13 +03:00
|
|
|
|
2010-10-27 11:03:10 +04:00
|
|
|
/**
|
|
|
|
* The effective externalness. A script can be external with mUri being null
|
|
|
|
* if the src attribute contained an invalid URL string.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mExternal;
|
2010-10-27 11:03:10 +04:00
|
|
|
|
2010-09-01 15:41:12 +04:00
|
|
|
/**
|
|
|
|
* Whether this element was parser-created.
|
|
|
|
*/
|
2010-10-25 16:17:38 +04:00
|
|
|
mozilla::dom::FromParser mParserCreated;
|
2010-09-01 15:41:12 +04:00
|
|
|
|
2009-11-17 11:52:30 +03:00
|
|
|
/**
|
|
|
|
* The effective src (or null if no src).
|
|
|
|
*/
|
|
|
|
nsCOMPtr<nsIURI> mUri;
|
2016-07-13 19:10:13 +03:00
|
|
|
|
2009-11-17 11:52:30 +03:00
|
|
|
/**
|
|
|
|
* The creator parser of a non-defer, non-async parser-inserted script.
|
|
|
|
*/
|
|
|
|
nsWeakPtr mCreatorParser;
|
2004-07-07 02:52:03 +04:00
|
|
|
};
|
|
|
|
|
2005-11-11 17:36:26 +03:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(nsIScriptElement, NS_ISCRIPTELEMENT_IID)
|
|
|
|
|
2004-07-07 02:52:03 +04:00
|
|
|
#endif // nsIScriptElement_h___
|