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: */
|
2013-01-05 13:41:27 +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/. */
|
|
|
|
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsStyleConsts.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2013-01-05 13:41:27 +04:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsUnicharUtils.h" // for nsCaseInsensitiveStringComparator()
|
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsDOMJSUtils.h"
|
2017-03-09 19:54:26 +03:00
|
|
|
#include "nsIScriptError.h"
|
2013-08-08 00:23:08 +04:00
|
|
|
#include "nsISupportsImpl.h"
|
2013-01-05 13:41:27 +04:00
|
|
|
#include "mozilla/dom/HTMLScriptElement.h"
|
2013-01-05 13:41:28 +04:00
|
|
|
#include "mozilla/dom/HTMLScriptElementBinding.h"
|
2013-01-05 13:41:27 +04:00
|
|
|
|
|
|
|
NS_IMPL_NS_NEW_HTML_ELEMENT_CHECK_PARSER(Script)
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
namespace mozilla::dom {
|
2013-01-05 13:41:27 +04:00
|
|
|
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
JSObject* HTMLScriptElement::WrapNode(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return HTMLScriptElement_Binding::Wrap(aCx, this, aGivenProto);
|
2013-01-05 13:41:28 +04:00
|
|
|
}
|
|
|
|
|
2018-09-21 23:45:49 +03:00
|
|
|
HTMLScriptElement::HTMLScriptElement(
|
|
|
|
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
|
2013-01-05 13:41:27 +04:00
|
|
|
FromParser aFromParser)
|
2018-09-21 23:45:49 +03:00
|
|
|
: nsGenericHTMLElement(std::move(aNodeInfo)), ScriptElement(aFromParser) {
|
2013-01-05 13:41:27 +04:00
|
|
|
AddMutationObserver(this);
|
|
|
|
}
|
|
|
|
|
2020-02-20 19:19:15 +03:00
|
|
|
HTMLScriptElement::~HTMLScriptElement() = default;
|
2013-01-05 13:41:27 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(HTMLScriptElement, nsGenericHTMLElement,
|
|
|
|
nsIScriptLoaderObserver, nsIScriptElement,
|
|
|
|
nsIMutationObserver)
|
2013-01-05 13:41:27 +04:00
|
|
|
|
Bug 1555216 - Change the signature of BindToTree to be (BindContext&, nsINode& aParentNode). r=bzbarsky
BindContext was going to have way more information at first, but then I realized
that most of the things I wanted to know were basically a flag away using the
parent node.
Still I think it's worth it, now experimenting with BindToTree will only mean
adding a field to a struct that's included from a couple cpp files, instead of a
massive pain.
I also think this is clearer, and doing this highlights quite a few
inconsistencies in our code which I've left untouched, but commented with
FIXMEs.
Steps are:
$ for file in $(rg 'nsresult BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#nsresult BindToTree(Document\* aDocument, nsIContent\* aParent,#nsresult BindToTree(BindContext\&, nsINode\& aParent)#g' $file; done
$ for file in $(rg 'nsresult BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's# nsIContent\* aBindingParent) override#override#g' $file; done
$ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#::BindToTree(Document\* aDocument, nsIContent\* aParent,#::BindToTree(BindContext\& aContext, nsINode\& aParent)#g' $file; done
$ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#nsIContent\* aBindingParent)##g' $file; done
$ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#::BindToTree(aDocument, aParent, aBindingParent)#::BindToTree(aContext, aParent)#g' $file; done
$ ./mach clang-format
Then manual fixups.
Depends on D32948
Differential Revision: https://phabricator.services.mozilla.com/D32949
2019-05-29 07:27:04 +03:00
|
|
|
nsresult HTMLScriptElement::BindToTree(BindContext& aContext,
|
|
|
|
nsINode& aParent) {
|
|
|
|
nsresult rv = nsGenericHTMLElement::BindToTree(aContext, aParent);
|
2013-01-05 13:41:27 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2019-05-31 17:33:33 +03:00
|
|
|
if (IsInComposedDoc()) {
|
2013-01-05 13:41:27 +04:00
|
|
|
MaybeProcessScript();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HTMLScriptElement::ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
|
|
|
|
const nsAString& aValue,
|
2017-11-02 06:35:52 +03:00
|
|
|
nsIPrincipal* aMaybeScriptedPrincipal,
|
2013-01-05 13:41:27 +04:00
|
|
|
nsAttrValue& aResult) {
|
2015-08-13 06:19:11 +03:00
|
|
|
if (aNamespaceID == kNameSpaceID_None) {
|
|
|
|
if (aAttribute == nsGkAtoms::crossorigin) {
|
|
|
|
ParseCORSValue(aValue, aResult);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aAttribute == nsGkAtoms::integrity) {
|
|
|
|
aResult.ParseStringOrAtom(aValue);
|
|
|
|
return true;
|
|
|
|
}
|
2013-01-05 13:41:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
|
2017-11-02 06:35:52 +03:00
|
|
|
aMaybeScriptedPrincipal, aResult);
|
2013-01-05 13:41:27 +04:00
|
|
|
}
|
|
|
|
|
2018-08-09 02:58:44 +03:00
|
|
|
nsresult HTMLScriptElement::Clone(dom::NodeInfo* aNodeInfo,
|
|
|
|
nsINode** aResult) const {
|
2013-01-05 13:41:27 +04:00
|
|
|
*aResult = nullptr;
|
|
|
|
|
2020-03-17 17:53:08 +03:00
|
|
|
HTMLScriptElement* it = new (aNodeInfo->NodeInfoManager())
|
|
|
|
HTMLScriptElement(do_AddRef(aNodeInfo), NOT_FROM_PARSER);
|
2013-01-05 13:41:27 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsINode> kungFuDeathGrip = it;
|
2018-08-09 02:58:44 +03:00
|
|
|
nsresult rv = const_cast<HTMLScriptElement*>(this)->CopyInnerTo(it);
|
2013-01-05 13:41:27 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// The clone should be marked evaluated if we are.
|
|
|
|
it->mAlreadyStarted = mAlreadyStarted;
|
|
|
|
it->mLineNumber = mLineNumber;
|
|
|
|
it->mMalformed = mMalformed;
|
|
|
|
|
|
|
|
kungFuDeathGrip.swap(*aResult);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
nsresult HTMLScriptElement::AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
|
2017-05-19 00:09:01 +03:00
|
|
|
const nsAttrValue* aValue,
|
2017-10-10 00:33:38 +03:00
|
|
|
const nsAttrValue* aOldValue,
|
|
|
|
nsIPrincipal* aMaybeScriptedPrincipal,
|
|
|
|
bool aNotify) {
|
2013-01-05 13:41:27 +04:00
|
|
|
if (nsGkAtoms::async == aName && kNameSpaceID_None == aNamespaceID) {
|
|
|
|
mForceAsync = false;
|
|
|
|
}
|
2017-10-05 08:16:32 +03:00
|
|
|
if (nsGkAtoms::src == aName && kNameSpaceID_None == aNamespaceID) {
|
|
|
|
mSrcTriggeringPrincipal = nsContentUtils::GetAttrTriggeringPrincipal(
|
|
|
|
this, aValue ? aValue->GetStringValue() : EmptyString(),
|
|
|
|
aMaybeScriptedPrincipal);
|
|
|
|
}
|
2017-10-10 00:33:38 +03:00
|
|
|
return nsGenericHTMLElement::AfterSetAttr(
|
|
|
|
aNamespaceID, aName, aValue, aOldValue, aMaybeScriptedPrincipal, aNotify);
|
2013-01-05 13:41:27 +04:00
|
|
|
}
|
|
|
|
|
2018-03-26 23:41:43 +03:00
|
|
|
void HTMLScriptElement::GetInnerHTML(nsAString& aInnerHTML,
|
|
|
|
OOMReporter& aError) {
|
2015-05-22 21:16:20 +03:00
|
|
|
if (!nsContentUtils::GetNodeTextContent(this, false, aInnerHTML, fallible)) {
|
2018-03-26 23:41:43 +03:00
|
|
|
aError.ReportOOM();
|
2014-03-19 21:05:03 +04:00
|
|
|
}
|
2013-01-05 13:41:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void HTMLScriptElement::SetInnerHTML(const nsAString& aInnerHTML,
|
2017-12-21 01:43:18 +03:00
|
|
|
nsIPrincipal* aScriptedPrincipal,
|
2013-01-05 13:41:27 +04:00
|
|
|
ErrorResult& aError) {
|
|
|
|
aError = nsContentUtils::SetNodeTextContent(this, aInnerHTML, true);
|
|
|
|
}
|
|
|
|
|
2017-11-09 06:02:14 +03:00
|
|
|
void HTMLScriptElement::GetText(nsAString& aValue, ErrorResult& aRv) {
|
|
|
|
if (!nsContentUtils::GetNodeTextContent(this, false, aValue, fallible)) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTMLScriptElement::SetText(const nsAString& aValue, ErrorResult& aRv) {
|
|
|
|
aRv = nsContentUtils::SetNodeTextContent(this, aValue, true);
|
|
|
|
}
|
|
|
|
|
2020-07-11 05:20:20 +03:00
|
|
|
// variation of this code in SVGScriptElement - check if changes
|
2013-01-05 13:41:27 +04:00
|
|
|
// need to be transfered when modifying
|
|
|
|
|
2018-01-09 19:00:50 +03:00
|
|
|
bool HTMLScriptElement::GetScriptType(nsAString& aType) {
|
|
|
|
nsAutoString type;
|
|
|
|
if (!GetAttr(kNameSpaceID_None, nsGkAtoms::type, type)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ASCII whitespace https://infra.spec.whatwg.org/#ascii-whitespace:
|
|
|
|
// U+0009 TAB, U+000A LF, U+000C FF, U+000D CR, or U+0020 SPACE.
|
|
|
|
static const char kASCIIWhitespace[] = "\t\n\f\r ";
|
|
|
|
type.Trim(kASCIIWhitespace);
|
|
|
|
|
|
|
|
aType.Assign(type);
|
|
|
|
return true;
|
2013-01-05 13:41:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void HTMLScriptElement::GetScriptText(nsAString& text) {
|
2018-02-01 22:21:14 +03:00
|
|
|
GetText(text, IgnoreErrors());
|
2013-01-05 13:41:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void HTMLScriptElement::GetScriptCharset(nsAString& charset) {
|
|
|
|
GetCharset(charset);
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
void HTMLScriptElement::FreezeExecutionAttrs(Document* aOwnerDoc) {
|
2013-01-05 13:41:27 +04:00
|
|
|
if (mFrozen) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:17:34 +03:00
|
|
|
MOZ_ASSERT(!mIsModule && !mAsync && !mDefer && !mExternal);
|
|
|
|
|
|
|
|
// Determine whether this is a classic script or a module script.
|
|
|
|
nsAutoString type;
|
|
|
|
GetScriptType(type);
|
|
|
|
mIsModule = aOwnerDoc->ModuleScriptsEnabled() && !type.IsEmpty() &&
|
|
|
|
type.LowerCaseEqualsASCII("module");
|
|
|
|
|
2020-07-11 05:20:20 +03:00
|
|
|
// variation of this code in SVGScriptElement - check if changes
|
2016-09-07 21:21:36 +03:00
|
|
|
// need to be transfered when modifying. Note that we don't use GetSrc here
|
|
|
|
// because it will return the base URL when the attr value is "".
|
|
|
|
nsAutoString src;
|
|
|
|
if (GetAttr(kNameSpaceID_None, nsGkAtoms::src, src)) {
|
|
|
|
// Empty src should be treated as invalid URL.
|
|
|
|
if (!src.IsEmpty()) {
|
|
|
|
nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(mUri), src,
|
2019-07-15 21:29:05 +03:00
|
|
|
OwnerDoc(), GetBaseURI());
|
2017-03-09 19:54:26 +03:00
|
|
|
|
|
|
|
if (!mUri) {
|
2019-06-09 00:26:12 +03:00
|
|
|
AutoTArray<nsString, 2> params = {u"src"_ns, src};
|
2017-03-09 19:54:26 +03:00
|
|
|
|
|
|
|
nsContentUtils::ReportToConsole(
|
|
|
|
nsIScriptError::warningFlag, "HTML"_ns, OwnerDoc(),
|
|
|
|
nsContentUtils::eDOM_PROPERTIES, "ScriptSourceInvalidUri", params,
|
2020-09-23 18:17:15 +03:00
|
|
|
nullptr, u""_ns, GetScriptLineNumber(), GetScriptColumnNumber());
|
2017-03-09 19:54:26 +03:00
|
|
|
}
|
|
|
|
} else {
|
2019-06-09 00:26:12 +03:00
|
|
|
AutoTArray<nsString, 1> params = {u"src"_ns};
|
2017-03-09 19:54:26 +03:00
|
|
|
|
|
|
|
nsContentUtils::ReportToConsole(
|
|
|
|
nsIScriptError::warningFlag, "HTML"_ns, OwnerDoc(),
|
2019-06-09 00:26:12 +03:00
|
|
|
nsContentUtils::eDOM_PROPERTIES, "ScriptSourceEmpty", params, nullptr,
|
2020-09-23 18:17:15 +03:00
|
|
|
u""_ns, GetScriptLineNumber(), GetScriptColumnNumber());
|
2016-09-07 21:21:36 +03:00
|
|
|
}
|
|
|
|
|
2013-01-05 13:41:27 +04:00
|
|
|
// At this point mUri will be null for invalid URLs.
|
|
|
|
mExternal = true;
|
2018-01-08 18:17:34 +03:00
|
|
|
}
|
2013-01-05 13:41:27 +04:00
|
|
|
|
2018-01-08 18:17:34 +03:00
|
|
|
bool async = (mExternal || mIsModule) && Async();
|
|
|
|
bool defer = mExternal && Defer();
|
2013-01-05 13:41:27 +04:00
|
|
|
|
2018-01-08 18:17:34 +03:00
|
|
|
mDefer = !async && defer;
|
|
|
|
mAsync = async;
|
2013-01-05 13:41:27 +04:00
|
|
|
|
|
|
|
mFrozen = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CORSMode HTMLScriptElement::GetCORSMode() const {
|
|
|
|
return AttrValueToCORSMode(GetParsedAttr(nsGkAtoms::crossorigin));
|
|
|
|
}
|
|
|
|
|
2019-08-21 16:24:45 +03:00
|
|
|
mozilla::dom::ReferrerPolicy HTMLScriptElement::GetReferrerPolicy() {
|
2018-11-13 17:33:02 +03:00
|
|
|
return GetReferrerPolicyAsEnum();
|
|
|
|
}
|
|
|
|
|
2013-01-05 13:41:27 +04:00
|
|
|
bool HTMLScriptElement::HasScriptContent() {
|
|
|
|
return (mFrozen ? mExternal : HasAttr(kNameSpaceID_None, nsGkAtoms::src)) ||
|
|
|
|
nsContentUtils::HasNonEmptyTextContent(this);
|
|
|
|
}
|
|
|
|
|
2021-09-27 15:49:51 +03:00
|
|
|
// https://html.spec.whatwg.org/multipage/scripting.html#dom-script-supports
|
|
|
|
/* static */
|
|
|
|
bool HTMLScriptElement::Supports(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aType) {
|
|
|
|
return aType.EqualsLiteral("classic") || aType.EqualsLiteral("module");
|
|
|
|
}
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
} // namespace mozilla::dom
|