2013-03-01 06:53:49 +04: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-06-23 10:57:01 +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/. */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Base class for all DOM nodes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsINode.h"
|
|
|
|
|
2013-02-23 08:46:05 +04:00
|
|
|
#include "AccessCheck.h"
|
2012-06-23 10:57:01 +04:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "mozAutoDocUpdate.h"
|
2014-03-17 10:56:54 +04:00
|
|
|
#include "mozilla/AsyncEventDispatcher.h"
|
2012-06-23 10:57:01 +04:00
|
|
|
#include "mozilla/CORSMode.h"
|
2014-03-18 08:48:21 +04:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2014-03-17 10:56:53 +04:00
|
|
|
#include "mozilla/EventListenerManager.h"
|
2017-08-04 00:18:50 +03:00
|
|
|
#include "mozilla/HTMLEditor.h"
|
2014-02-27 14:51:15 +04:00
|
|
|
#include "mozilla/InternalMutationEvent.h"
|
2012-10-26 17:32:10 +04:00
|
|
|
#include "mozilla/Likely.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2016-02-24 04:28:50 +03:00
|
|
|
#include "mozilla/ServoBindings.h"
|
2012-06-23 10:57:01 +04:00
|
|
|
#include "mozilla/Telemetry.h"
|
2017-06-22 09:21:31 +03:00
|
|
|
#include "mozilla/TextEditor.h"
|
2014-07-16 04:02:31 +04:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2018-02-01 07:04:04 +03:00
|
|
|
#ifdef MOZ_OLD_STYLE
|
2015-11-05 11:44:09 +03:00
|
|
|
#include "mozilla/css/StyleRule.h"
|
2018-02-01 07:04:04 +03:00
|
|
|
#endif
|
2018-03-13 23:24:01 +03:00
|
|
|
#include "mozilla/dom/DocumentType.h"
|
2014-03-05 04:37:43 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
#include "mozilla/dom/Event.h"
|
2018-02-22 01:07:53 +03:00
|
|
|
#include "mozilla/dom/L10nUtilsBinding.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
|
|
|
#include "mozilla/dom/PromiseNativeHandler.h"
|
2014-03-05 04:37:43 +04:00
|
|
|
#include "mozilla/dom/ShadowRoot.h"
|
2012-06-23 10:57:01 +04:00
|
|
|
#include "nsAttrValueOrString.h"
|
|
|
|
#include "nsBindingManager.h"
|
|
|
|
#include "nsCCUncollectableMarker.h"
|
|
|
|
#include "nsContentCreatorFunctions.h"
|
|
|
|
#include "nsContentList.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsDocument.h"
|
2013-04-09 19:29:44 +04:00
|
|
|
#include "mozilla/dom/Attr.h"
|
2012-06-23 10:57:01 +04:00
|
|
|
#include "nsDOMAttributeMap.h"
|
|
|
|
#include "nsDOMCID.h"
|
|
|
|
#include "nsDOMCSSAttrDeclaration.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2012-06-23 10:57:01 +04:00
|
|
|
#include "nsDOMMutationObserver.h"
|
|
|
|
#include "nsDOMString.h"
|
|
|
|
#include "nsDOMTokenList.h"
|
|
|
|
#include "nsFocusManager.h"
|
|
|
|
#include "nsFrameSelection.h"
|
|
|
|
#include "nsGenericHTMLElement.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsIAnonymousContentCreator.h"
|
2017-10-03 01:05:19 +03:00
|
|
|
#include "nsAtom.h"
|
2012-06-23 10:57:01 +04:00
|
|
|
#include "nsIBaseWindow.h"
|
|
|
|
#include "nsICategoryManager.h"
|
|
|
|
#include "nsIContentIterator.h"
|
|
|
|
#include "nsIControllers.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIDOMEvent.h"
|
|
|
|
#include "nsIDOMEventListener.h"
|
|
|
|
#include "nsIDOMNodeList.h"
|
|
|
|
#include "nsILinkHandler.h"
|
2014-06-20 06:01:40 +04:00
|
|
|
#include "mozilla/dom/NodeInfo.h"
|
|
|
|
#include "mozilla/dom/NodeInfoInlines.h"
|
2012-06-23 10:57:01 +04:00
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsIScriptError.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
#include "nsIScrollableFrame.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIURL.h"
|
2013-01-03 17:23:11 +04:00
|
|
|
#include "nsView.h"
|
2013-01-05 07:12:24 +04:00
|
|
|
#include "nsViewManager.h"
|
2012-06-23 10:57:01 +04:00
|
|
|
#include "nsIWebNavigation.h"
|
|
|
|
#include "nsIWidget.h"
|
|
|
|
#include "nsLayoutUtils.h"
|
2014-02-28 03:04:46 +04:00
|
|
|
#include "nsNameSpaceManager.h"
|
2012-06-23 10:57:01 +04:00
|
|
|
#include "nsNodeInfoManager.h"
|
|
|
|
#include "nsNodeUtils.h"
|
|
|
|
#include "nsPIBoxObject.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsPresContext.h"
|
2018-02-01 07:04:04 +03:00
|
|
|
#ifdef MOZ_OLD_STYLE
|
2012-06-23 10:57:01 +04:00
|
|
|
#include "nsRuleProcessorData.h"
|
2018-02-01 07:04:04 +03:00
|
|
|
#endif
|
2012-06-23 10:57:01 +04:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsStyleConsts.h"
|
|
|
|
#include "nsSVGUtils.h"
|
|
|
|
#include "nsTextNode.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nsXBLBinding.h"
|
|
|
|
#include "nsXBLPrototypeBinding.h"
|
2014-07-03 17:23:14 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-06-23 10:57:01 +04:00
|
|
|
#include "xpcpublic.h"
|
2018-02-01 07:04:04 +03:00
|
|
|
#ifdef MOZ_OLD_STYLE
|
2012-08-30 21:10:13 +04:00
|
|
|
#include "nsCSSRuleProcessor.h"
|
2018-02-01 07:04:04 +03:00
|
|
|
#endif
|
2012-08-30 21:10:13 +04:00
|
|
|
#include "nsCSSParser.h"
|
2013-02-15 20:55:53 +04:00
|
|
|
#include "HTMLLegendElement.h"
|
2012-06-23 10:57:01 +04:00
|
|
|
#include "nsWrapperCacheInlines.h"
|
2012-09-13 00:29:30 +04:00
|
|
|
#include "WrapperFactory.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2013-05-31 01:46:39 +04:00
|
|
|
#include "nsGlobalWindow.h"
|
2013-09-11 23:43:01 +04:00
|
|
|
#include "nsDOMMutationObserver.h"
|
2014-03-12 05:11:38 +04:00
|
|
|
#include "GeometryUtils.h"
|
2015-03-14 08:34:40 +03:00
|
|
|
#include "nsIAnimationObserver.h"
|
2015-05-19 13:33:00 +03:00
|
|
|
#include "nsChildContentList.h"
|
2017-01-15 20:46:00 +03:00
|
|
|
#include "mozilla/dom/NodeBinding.h"
|
2017-02-01 23:43:36 +03:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
2012-06-23 10:57:01 +04:00
|
|
|
|
2017-06-15 04:49:17 +03:00
|
|
|
#include "XPathGenerator.h"
|
|
|
|
|
2016-08-23 21:15:15 +03:00
|
|
|
#ifdef ACCESSIBILITY
|
|
|
|
#include "mozilla/dom/AccessibleNode.h"
|
|
|
|
#endif
|
|
|
|
|
2012-06-23 10:57:01 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2015-05-19 13:33:00 +03:00
|
|
|
nsINode::nsSlots::nsSlots()
|
2015-08-14 20:52:38 +03:00
|
|
|
: mWeakReference(nullptr),
|
|
|
|
mEditableDescendantCount(0)
|
2015-05-19 13:33:00 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-06-23 10:57:01 +04:00
|
|
|
nsINode::nsSlots::~nsSlots()
|
|
|
|
{
|
|
|
|
if (mChildNodes) {
|
|
|
|
mChildNodes->DropReference();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mWeakReference) {
|
|
|
|
mWeakReference->NoticeNodeDestruction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsINode::nsSlots::Traverse(nsCycleCollectionTraversalCallback &cb)
|
|
|
|
{
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mSlots->mChildNodes");
|
|
|
|
cb.NoteXPCOMChild(mChildNodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsINode::nsSlots::Unlink()
|
|
|
|
{
|
|
|
|
if (mChildNodes) {
|
|
|
|
mChildNodes->DropReference();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsINode::~nsINode()
|
|
|
|
{
|
2013-07-09 21:30:58 +04:00
|
|
|
MOZ_ASSERT(!HasSlots(), "nsNodeUtils::LastRelease was not called?");
|
|
|
|
MOZ_ASSERT(mSubtreeRoot == this, "Didn't restore state properly?");
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
2017-10-03 01:05:19 +03:00
|
|
|
nsINode::GetProperty(uint16_t aCategory, nsAtom *aPropertyName,
|
2012-06-23 10:57:01 +04:00
|
|
|
nsresult *aStatus) const
|
|
|
|
{
|
2017-04-25 11:15:37 +03:00
|
|
|
if (!HasProperties()) { // a fast HasFlag() test
|
|
|
|
if (aStatus) {
|
|
|
|
*aStatus = NS_PROPTABLE_PROP_NOT_THERE;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-06-23 10:57:01 +04:00
|
|
|
return OwnerDoc()->PropertyTable(aCategory)->GetProperty(this, aPropertyName,
|
|
|
|
aStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2017-10-03 01:05:19 +03:00
|
|
|
nsINode::SetProperty(uint16_t aCategory, nsAtom *aPropertyName, void *aValue,
|
2012-06-23 10:57:01 +04:00
|
|
|
NSPropertyDtorFunc aDtor, bool aTransfer,
|
|
|
|
void **aOldValue)
|
|
|
|
{
|
|
|
|
nsresult rv = OwnerDoc()->PropertyTable(aCategory)->SetProperty(this,
|
|
|
|
aPropertyName,
|
|
|
|
aValue, aDtor,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr,
|
2012-06-23 10:57:01 +04:00
|
|
|
aTransfer,
|
|
|
|
aOldValue);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
SetFlags(NODE_HAS_PROPERTIES);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-10-03 01:05:19 +03:00
|
|
|
nsINode::DeleteProperty(uint16_t aCategory, nsAtom *aPropertyName)
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
|
|
|
OwnerDoc()->PropertyTable(aCategory)->DeleteProperty(this, aPropertyName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
2017-10-03 01:05:19 +03:00
|
|
|
nsINode::UnsetProperty(uint16_t aCategory, nsAtom *aPropertyName,
|
2012-06-23 10:57:01 +04:00
|
|
|
nsresult *aStatus)
|
|
|
|
{
|
|
|
|
return OwnerDoc()->PropertyTable(aCategory)->UnsetProperty(this,
|
|
|
|
aPropertyName,
|
|
|
|
aStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsINode::nsSlots*
|
|
|
|
nsINode::CreateSlots()
|
|
|
|
{
|
|
|
|
return new nsSlots();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsINode::IsEditableInternal() const
|
|
|
|
{
|
|
|
|
if (HasFlag(NODE_IS_EDITABLE)) {
|
|
|
|
// The node is in an editable contentEditable subtree.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-02 22:45:44 +04:00
|
|
|
nsIDocument *doc = GetUncomposedDoc();
|
2012-06-23 10:57:01 +04:00
|
|
|
|
|
|
|
// Check if the node is in a document and the document is in designMode.
|
|
|
|
return doc && doc->HasFlag(NODE_IS_EDITABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIContent*
|
2017-06-22 09:21:31 +03:00
|
|
|
nsINode::GetTextEditorRootContent(TextEditor** aTextEditor)
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
2017-06-22 09:21:31 +03:00
|
|
|
if (aTextEditor) {
|
|
|
|
*aTextEditor = nullptr;
|
|
|
|
}
|
2012-10-09 16:31:24 +04:00
|
|
|
for (nsINode* node = this; node; node = node->GetParentNode()) {
|
2012-06-23 10:57:01 +04:00
|
|
|
if (!node->IsElement() ||
|
2015-03-03 14:08:59 +03:00
|
|
|
!node->IsHTMLElement())
|
2012-06-23 10:57:01 +04:00
|
|
|
continue;
|
|
|
|
|
2017-06-22 09:21:31 +03:00
|
|
|
RefPtr<TextEditor> textEditor =
|
|
|
|
static_cast<nsGenericHTMLElement*>(node)->GetTextEditorInternal();
|
|
|
|
if (!textEditor) {
|
2012-06-23 10:57:01 +04:00
|
|
|
continue;
|
2017-06-22 09:21:31 +03:00
|
|
|
}
|
2012-06-23 10:57:01 +04:00
|
|
|
|
2017-06-22 09:21:31 +03:00
|
|
|
MOZ_ASSERT(!textEditor->AsHTMLEditor(),
|
|
|
|
"If it were an HTML editor, needs to use GetRootElement()");
|
|
|
|
Element* rootElement = textEditor->GetRoot();
|
|
|
|
if (aTextEditor) {
|
|
|
|
textEditor.forget(aTextEditor);
|
|
|
|
}
|
|
|
|
return rootElement;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
2017-01-15 20:46:00 +03:00
|
|
|
nsINode* nsINode::GetRootNode(const GetRootNodeOptions& aOptions)
|
|
|
|
{
|
|
|
|
if (aOptions.mComposed) {
|
|
|
|
if (IsInComposedDoc() && GetComposedDoc()) {
|
|
|
|
return OwnerDoc();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsINode* node = this;
|
|
|
|
while(node) {
|
|
|
|
node = node->SubtreeRoot();
|
2017-12-31 22:57:32 +03:00
|
|
|
ShadowRoot* shadow = ShadowRoot::FromNode(node);
|
|
|
|
if (!shadow) {
|
|
|
|
break;
|
2017-01-15 20:46:00 +03:00
|
|
|
}
|
2017-12-31 22:57:32 +03:00
|
|
|
node = shadow->GetHost();
|
2017-01-15 20:46:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SubtreeRoot();
|
|
|
|
}
|
|
|
|
|
2018-03-06 03:34:40 +03:00
|
|
|
nsINode*
|
|
|
|
nsINode::GetParentOrHostNode() const
|
|
|
|
{
|
|
|
|
if (mParent) {
|
|
|
|
return mParent;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ShadowRoot* shadowRoot = ShadowRoot::FromNode(this);
|
|
|
|
return shadowRoot ? shadowRoot->GetHost() : nullptr;
|
|
|
|
}
|
|
|
|
|
2014-06-07 12:42:53 +04:00
|
|
|
nsINode*
|
|
|
|
nsINode::SubtreeRoot() const
|
|
|
|
{
|
|
|
|
// There are four cases of interest here. nsINodes that are really:
|
|
|
|
// 1. nsIDocument nodes - Are always in the document.
|
|
|
|
// 2.a nsIContent nodes not in a shadow tree - Are either in the document,
|
|
|
|
// or mSubtreeRoot is updated in BindToTree/UnbindFromTree.
|
|
|
|
// 2.b nsIContent nodes in a shadow tree - Are never in the document,
|
|
|
|
// ignore mSubtreeRoot and return the containing shadow root.
|
|
|
|
// 4. nsIAttribute nodes - Are never in the document, and mSubtreeRoot
|
|
|
|
// is always 'this' (as set in nsINode's ctor).
|
|
|
|
nsINode* node;
|
2016-03-31 13:58:25 +03:00
|
|
|
if (IsInUncomposedDoc()) {
|
2014-06-07 12:42:53 +04:00
|
|
|
node = OwnerDocAsNode();
|
|
|
|
} else if (IsContent()) {
|
|
|
|
ShadowRoot* containingShadow = AsContent()->GetContainingShadow();
|
|
|
|
node = containingShadow ? containingShadow : mSubtreeRoot;
|
|
|
|
} else {
|
|
|
|
node = mSubtreeRoot;
|
|
|
|
}
|
|
|
|
NS_ASSERTION(node, "Should always have a node here!");
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
const nsINode* slowNode = this;
|
|
|
|
const nsINode* iter = slowNode;
|
|
|
|
while ((iter = iter->GetParentNode())) {
|
|
|
|
slowNode = iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(slowNode == node, "These should always be in sync!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2012-06-23 10:57:01 +04:00
|
|
|
static nsIContent* GetRootForContentSubtree(nsIContent* aContent)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(aContent, nullptr);
|
2013-12-02 14:26:11 +04:00
|
|
|
|
|
|
|
// Special case for ShadowRoot because the ShadowRoot itself is
|
|
|
|
// the root. This is necessary to prevent selection from crossing
|
|
|
|
// the ShadowRoot boundary.
|
|
|
|
ShadowRoot* containingShadow = aContent->GetContainingShadow();
|
|
|
|
if (containingShadow) {
|
|
|
|
return containingShadow;
|
|
|
|
}
|
|
|
|
|
2012-06-23 10:57:01 +04:00
|
|
|
nsIContent* stop = aContent->GetBindingParent();
|
|
|
|
while (aContent) {
|
|
|
|
nsIContent* parent = aContent->GetParent();
|
|
|
|
if (parent == stop) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
aContent = parent;
|
|
|
|
}
|
|
|
|
return aContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIContent*
|
|
|
|
nsINode::GetSelectionRootContent(nsIPresShell* aPresShell)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(aPresShell, nullptr);
|
2012-06-23 10:57:01 +04:00
|
|
|
|
|
|
|
if (IsNodeOfType(eDOCUMENT))
|
|
|
|
return static_cast<nsIDocument*>(this)->GetRootElement();
|
2017-10-25 18:19:11 +03:00
|
|
|
if (!IsContent())
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-06-23 10:57:01 +04:00
|
|
|
|
2016-03-31 15:20:14 +03:00
|
|
|
if (GetComposedDoc() != aPresShell->GetDocument()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (static_cast<nsIContent*>(this)->HasIndependentSelection()) {
|
|
|
|
// This node should be a descendant of input/textarea editor.
|
|
|
|
nsIContent* content = GetTextEditorRootContent();
|
|
|
|
if (content)
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPresContext* presContext = aPresShell->GetPresContext();
|
|
|
|
if (presContext) {
|
2017-08-07 11:00:44 +03:00
|
|
|
HTMLEditor* htmlEditor = nsContentUtils::GetHTMLEditor(presContext);
|
|
|
|
if (htmlEditor) {
|
2012-06-23 10:57:01 +04:00
|
|
|
// This node is in HTML editor.
|
2016-03-31 15:20:14 +03:00
|
|
|
nsIDocument* doc = GetComposedDoc();
|
2012-06-23 10:57:01 +04:00
|
|
|
if (!doc || doc->HasFlag(NODE_IS_EDITABLE) ||
|
|
|
|
!HasFlag(NODE_IS_EDITABLE)) {
|
2017-08-07 11:00:44 +03:00
|
|
|
nsIContent* editorRoot = htmlEditor->GetRoot();
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(editorRoot, nullptr);
|
2012-06-23 10:57:01 +04:00
|
|
|
return nsContentUtils::IsInSameAnonymousTree(this, editorRoot) ?
|
|
|
|
editorRoot :
|
|
|
|
GetRootForContentSubtree(static_cast<nsIContent*>(this));
|
|
|
|
}
|
|
|
|
// If the document isn't editable but this is editable, this is in
|
|
|
|
// contenteditable. Use the editing host element for selection root.
|
|
|
|
return static_cast<nsIContent*>(this)->GetEditingHost();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsFrameSelection> fs = aPresShell->FrameSelection();
|
2012-06-23 10:57:01 +04:00
|
|
|
nsIContent* content = fs->GetLimiter();
|
|
|
|
if (!content) {
|
|
|
|
content = fs->GetAncestorLimiter();
|
|
|
|
if (!content) {
|
|
|
|
nsIDocument* doc = aPresShell->GetDocument();
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(doc, nullptr);
|
2012-06-23 10:57:01 +04:00
|
|
|
content = doc->GetRootElement();
|
|
|
|
if (!content)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This node might be in another subtree, if so, we should find this subtree's
|
|
|
|
// root. Otherwise, we can return the content simply.
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(content, nullptr);
|
2013-12-02 14:26:11 +04:00
|
|
|
if (!nsContentUtils::IsInSameAnonymousTree(this, content)) {
|
|
|
|
content = GetRootForContentSubtree(static_cast<nsIContent*>(this));
|
|
|
|
// Fixup for ShadowRoot because the ShadowRoot itself does not have a frame.
|
|
|
|
// Use the host as the root.
|
2017-12-31 22:57:32 +03:00
|
|
|
if (ShadowRoot* shadowRoot = ShadowRoot::FromNode(content)) {
|
2013-12-02 14:26:11 +04:00
|
|
|
content = shadowRoot->GetHost();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return content;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsINodeList*
|
2012-10-09 16:31:24 +04:00
|
|
|
nsINode::ChildNodes()
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
2012-08-11 02:27:28 +04:00
|
|
|
nsSlots* slots = Slots();
|
2015-04-17 18:49:59 +03:00
|
|
|
if (!slots->mChildNodes) {
|
2017-10-23 10:04:14 +03:00
|
|
|
// Check |!IsElement()| first to catch the common case
|
|
|
|
// without virtual call |IsNodeOfType|
|
|
|
|
slots->mChildNodes = !IsElement() && IsNodeOfType(nsINode::eATTRIBUTE) ?
|
|
|
|
new nsAttrChildContentList(this) :
|
|
|
|
new nsParentNodeChildContentList(this);
|
2015-04-17 18:49:59 +03:00
|
|
|
}
|
2012-06-23 10:57:01 +04:00
|
|
|
|
|
|
|
return slots->mChildNodes;
|
|
|
|
}
|
|
|
|
|
2017-10-23 10:04:14 +03:00
|
|
|
void
|
|
|
|
nsINode::InvalidateChildNodes()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsElement() || !IsNodeOfType(nsINode::eATTRIBUTE));
|
|
|
|
|
|
|
|
nsSlots* slots = GetExistingSlots();
|
|
|
|
if (!slots || !slots->mChildNodes) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto childNodes =
|
|
|
|
static_cast<nsParentNodeChildContentList*>(slots->mChildNodes.get());
|
|
|
|
childNodes->InvalidateCache();
|
|
|
|
}
|
|
|
|
|
2012-11-27 12:41:02 +04:00
|
|
|
void
|
2017-01-18 07:52:29 +03:00
|
|
|
nsINode::GetTextContentInternal(nsAString& aTextContent, OOMReporter& aError)
|
2012-11-27 12:41:02 +04:00
|
|
|
{
|
|
|
|
SetDOMStringToNull(aTextContent);
|
|
|
|
}
|
|
|
|
|
2014-06-07 12:42:53 +04:00
|
|
|
nsIDocument*
|
2014-07-03 21:02:07 +04:00
|
|
|
nsINode::GetComposedDocInternal() const
|
2014-06-07 12:42:53 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(HasFlag(NODE_IS_IN_SHADOW_TREE) && IsContent(),
|
|
|
|
"Should only be caled on nodes in the shadow tree.");
|
|
|
|
|
|
|
|
ShadowRoot* containingShadow = AsContent()->GetContainingShadow();
|
2015-02-09 21:01:23 +03:00
|
|
|
return containingShadow->IsComposedDocParticipant() ? OwnerDoc() : nullptr;
|
2014-06-07 12:42:53 +04:00
|
|
|
}
|
|
|
|
|
2012-06-23 10:57:01 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
|
|
|
nsINode::CheckNotNativeAnonymous() const
|
|
|
|
{
|
2017-10-25 18:19:11 +03:00
|
|
|
if (!IsContent())
|
2012-06-23 10:57:01 +04:00
|
|
|
return;
|
|
|
|
nsIContent* content = static_cast<const nsIContent *>(this)->GetBindingParent();
|
|
|
|
while (content) {
|
|
|
|
if (content->IsRootOfNativeAnonymousSubtree()) {
|
|
|
|
NS_ERROR("Element not marked to be in native anonymous subtree!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
content = content->GetBindingParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-04-06 23:32:38 +04:00
|
|
|
bool
|
|
|
|
nsINode::IsInAnonymousSubtree() const
|
|
|
|
{
|
|
|
|
if (!IsContent()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AsContent()->IsInAnonymousSubtree();
|
|
|
|
}
|
|
|
|
|
2015-07-08 22:58:07 +03:00
|
|
|
std::ostream&
|
|
|
|
operator<<(std::ostream& aStream, const nsINode& aNode)
|
|
|
|
{
|
|
|
|
nsAutoString elemDesc;
|
|
|
|
const nsINode* curr = &aNode;
|
|
|
|
while (curr) {
|
|
|
|
const nsString& localName = curr->LocalName();
|
|
|
|
nsString id;
|
|
|
|
if (curr->IsElement()) {
|
|
|
|
curr->AsElement()->GetId(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!elemDesc.IsEmpty()) {
|
|
|
|
elemDesc = elemDesc + NS_LITERAL_STRING(".");
|
|
|
|
}
|
|
|
|
|
|
|
|
elemDesc = elemDesc + localName;
|
|
|
|
|
|
|
|
if (!id.IsEmpty()) {
|
|
|
|
elemDesc = elemDesc + NS_LITERAL_STRING("['") + id +
|
|
|
|
NS_LITERAL_STRING("']");
|
|
|
|
}
|
|
|
|
|
|
|
|
curr = curr->GetParentNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 str(elemDesc);
|
|
|
|
return aStream << str.get();
|
|
|
|
}
|
|
|
|
|
2014-04-15 07:38:54 +04:00
|
|
|
bool
|
|
|
|
nsINode::IsAnonymousContentInSVGUseSubtree() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsInAnonymousSubtree());
|
|
|
|
nsIContent* parent = AsContent()->GetBindingParent();
|
|
|
|
// Watch out for parentless native-anonymous subtrees.
|
2015-03-03 14:08:59 +03:00
|
|
|
return parent && parent->IsSVGElement(nsGkAtoms::use);
|
2014-04-15 07:38:54 +04:00
|
|
|
}
|
|
|
|
|
2012-11-27 12:41:02 +04:00
|
|
|
void
|
|
|
|
nsINode::GetNodeValueInternal(nsAString& aNodeValue)
|
|
|
|
{
|
|
|
|
SetDOMStringToNull(aNodeValue);
|
|
|
|
}
|
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
nsINode*
|
|
|
|
nsINode::RemoveChild(nsINode& aOldChild, ErrorResult& aError)
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
|
|
|
if (IsNodeOfType(eDATA_NODE)) {
|
2012-08-04 11:43:59 +04:00
|
|
|
// aOldChild can't be one of our children.
|
2012-10-09 16:31:24 +04:00
|
|
|
aError.Throw(NS_ERROR_DOM_NOT_FOUND_ERR);
|
|
|
|
return nullptr;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
if (aOldChild.GetParentNode() == this) {
|
|
|
|
nsContentUtils::MaybeFireNodeRemoved(&aOldChild, this, OwnerDoc());
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
2018-01-23 16:30:18 +03:00
|
|
|
int32_t index = ComputeIndexOf(&aOldChild);
|
2012-06-23 10:57:01 +04:00
|
|
|
if (index == -1) {
|
|
|
|
// aOldChild isn't one of our children.
|
2012-10-09 16:31:24 +04:00
|
|
|
aError.Throw(NS_ERROR_DOM_NOT_FOUND_ERR);
|
|
|
|
return nullptr;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
2018-01-03 16:01:03 +03:00
|
|
|
RemoveChildAt_Deprecated(index, true);
|
2012-10-09 16:31:24 +04:00
|
|
|
return &aOldChild;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
void
|
2012-06-23 10:57:01 +04:00
|
|
|
nsINode::Normalize()
|
|
|
|
{
|
|
|
|
// First collect list of nodes to be removed
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<nsCOMPtr<nsIContent>, 50> nodes;
|
2012-06-23 10:57:01 +04:00
|
|
|
|
|
|
|
bool canMerge = false;
|
|
|
|
for (nsIContent* node = this->GetFirstChild();
|
|
|
|
node;
|
|
|
|
node = node->GetNextNode(this)) {
|
2018-01-30 07:10:53 +03:00
|
|
|
if (node->NodeType() != TEXT_NODE) {
|
2012-06-23 10:57:01 +04:00
|
|
|
canMerge = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (canMerge || node->TextLength() == 0) {
|
|
|
|
// No need to touch canMerge. That way we can merge across empty
|
|
|
|
// textnodes if and only if the node before is a textnode
|
|
|
|
nodes.AppendElement(node);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
canMerge = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's no following sibling, then we need to ensure that we don't
|
|
|
|
// collect following siblings of our (grand)parent as to-be-removed
|
|
|
|
canMerge = canMerge && !!node->GetNextSibling();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nodes.IsEmpty()) {
|
2012-10-09 16:31:24 +04:00
|
|
|
return;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// We're relying on mozAutoSubtreeModified to keep the doc alive here.
|
|
|
|
nsIDocument* doc = OwnerDoc();
|
|
|
|
|
|
|
|
// Batch possible DOMSubtreeModified events.
|
2012-07-30 18:20:58 +04:00
|
|
|
mozAutoSubtreeModified subtree(doc, nullptr);
|
2012-06-23 10:57:01 +04:00
|
|
|
|
|
|
|
// Fire all DOMNodeRemoved events. Optimize the common case of there being
|
|
|
|
// no listeners
|
|
|
|
bool hasRemoveListeners = nsContentUtils::
|
|
|
|
HasMutationListeners(doc, NS_EVENT_BITS_MUTATION_NODEREMOVED);
|
|
|
|
if (hasRemoveListeners) {
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < nodes.Length(); ++i) {
|
2013-12-02 22:38:51 +04:00
|
|
|
nsINode* parentNode = nodes[i]->GetParentNode();
|
|
|
|
if (parentNode) { // Node may have already been removed.
|
|
|
|
nsContentUtils::MaybeFireNodeRemoved(nodes[i], parentNode,
|
|
|
|
doc);
|
|
|
|
}
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mozAutoDocUpdate batch(doc, UPDATE_CONTENT_MODEL, true);
|
|
|
|
|
|
|
|
// Merge and remove all nodes
|
|
|
|
nsAutoString tmpStr;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < nodes.Length(); ++i) {
|
2012-06-23 10:57:01 +04:00
|
|
|
nsIContent* node = nodes[i];
|
|
|
|
// Merge with previous node unless empty
|
|
|
|
const nsTextFragment* text = node->GetText();
|
|
|
|
if (text->GetLength()) {
|
|
|
|
nsIContent* target = node->GetPreviousSibling();
|
2018-01-30 07:10:53 +03:00
|
|
|
NS_ASSERTION((target && target->NodeType() == TEXT_NODE) ||
|
2012-06-23 10:57:01 +04:00
|
|
|
hasRemoveListeners,
|
|
|
|
"Should always have a previous text sibling unless "
|
|
|
|
"mutation events messed us up");
|
|
|
|
if (!hasRemoveListeners ||
|
2018-01-30 07:10:53 +03:00
|
|
|
(target && target->NodeType() == TEXT_NODE)) {
|
2012-06-23 10:57:01 +04:00
|
|
|
nsTextNode* t = static_cast<nsTextNode*>(target);
|
|
|
|
if (text->Is2b()) {
|
|
|
|
t->AppendTextForNormalize(text->Get2b(), text->GetLength(), true, node);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tmpStr.Truncate();
|
|
|
|
text->AppendTo(tmpStr);
|
|
|
|
t->AppendTextForNormalize(tmpStr.get(), tmpStr.Length(), true, node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove node
|
2012-10-09 16:31:24 +04:00
|
|
|
nsCOMPtr<nsINode> parent = node->GetParentNode();
|
2012-06-23 10:57:01 +04:00
|
|
|
NS_ASSERTION(parent || hasRemoveListeners,
|
|
|
|
"Should always have a parent unless "
|
|
|
|
"mutation events messed us up");
|
|
|
|
if (parent) {
|
2018-01-23 16:30:18 +03:00
|
|
|
parent->RemoveChildAt_Deprecated(parent->ComputeIndexOf(node), true);
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-20 15:02:33 +03:00
|
|
|
nsresult
|
2012-10-09 16:31:24 +04:00
|
|
|
nsINode::GetBaseURI(nsAString &aURI) const
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> baseURI = GetBaseURI();
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString spec;
|
2012-06-23 10:57:01 +04:00
|
|
|
if (baseURI) {
|
2016-09-20 15:02:33 +03:00
|
|
|
nsresult rv = baseURI->GetSpec(spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CopyUTF8toUTF16(spec, aURI);
|
2016-09-20 15:02:33 +03:00
|
|
|
return NS_OK;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
2013-09-25 01:56:52 +04:00
|
|
|
void
|
2017-02-01 23:43:36 +03:00
|
|
|
nsINode::GetBaseURIFromJS(nsAString& aURI,
|
|
|
|
CallerType aCallerType,
|
|
|
|
ErrorResult& aRv) const
|
2013-09-25 01:56:52 +04:00
|
|
|
{
|
2017-02-01 23:43:36 +03:00
|
|
|
nsCOMPtr<nsIURI> baseURI = GetBaseURI(aCallerType == CallerType::System);
|
2013-09-25 01:56:52 +04:00
|
|
|
nsAutoCString spec;
|
|
|
|
if (baseURI) {
|
2016-09-08 07:17:58 +03:00
|
|
|
nsresult res = baseURI->GetSpec(spec);
|
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
aRv.Throw(res);
|
|
|
|
return;
|
|
|
|
}
|
2013-09-25 01:56:52 +04:00
|
|
|
}
|
|
|
|
CopyUTF8toUTF16(spec, aURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIURI>
|
|
|
|
nsINode::GetBaseURIObject() const
|
|
|
|
{
|
|
|
|
return GetBaseURI(true);
|
|
|
|
}
|
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
void
|
2012-06-23 10:57:01 +04:00
|
|
|
nsINode::LookupPrefix(const nsAString& aNamespaceURI, nsAString& aPrefix)
|
|
|
|
{
|
|
|
|
Element *element = GetNameSpaceElement();
|
|
|
|
if (element) {
|
|
|
|
// XXX Waiting for DOM spec to list error codes.
|
2016-03-25 13:23:53 +03:00
|
|
|
|
2012-06-23 10:57:01 +04:00
|
|
|
// Trace up the content parent chain looking for the namespace
|
|
|
|
// declaration that defines the aNamespaceURI namespace. Once found,
|
|
|
|
// return the prefix (i.e. the attribute localName).
|
|
|
|
for (nsIContent* content = element; content;
|
|
|
|
content = content->GetParent()) {
|
2017-12-05 20:05:51 +03:00
|
|
|
if (!content->IsElement()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Element* element = content->AsElement();
|
|
|
|
uint32_t attrCount = element->GetAttrCount();
|
2016-03-25 13:23:53 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < attrCount; ++i) {
|
2017-12-05 20:05:51 +03:00
|
|
|
const nsAttrName* name = element->GetAttrNameAt(i);
|
2016-03-25 13:23:53 +03:00
|
|
|
|
2012-06-23 10:57:01 +04:00
|
|
|
if (name->NamespaceEquals(kNameSpaceID_XMLNS) &&
|
2017-12-05 20:05:51 +03:00
|
|
|
element->AttrValueIs(kNameSpaceID_XMLNS, name->LocalName(),
|
2012-06-23 10:57:01 +04:00
|
|
|
aNamespaceURI, eCaseMatters)) {
|
|
|
|
// If the localName is "xmlns", the prefix we output should be
|
|
|
|
// null.
|
2017-12-05 20:05:51 +03:00
|
|
|
nsAtom* localName = name->LocalName();
|
2016-03-25 13:23:53 +03:00
|
|
|
|
2012-06-23 10:57:01 +04:00
|
|
|
if (localName != nsGkAtoms::xmlns) {
|
|
|
|
localName->ToString(aPrefix);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
SetDOMStringToNull(aPrefix);
|
|
|
|
}
|
2012-10-09 16:31:24 +04:00
|
|
|
return;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetDOMStringToNull(aPrefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult
|
2017-10-03 01:05:19 +03:00
|
|
|
SetUserDataProperty(uint16_t aCategory, nsINode *aNode, nsAtom *aKey,
|
2012-06-23 10:57:01 +04:00
|
|
|
nsISupports* aValue, void** aOldValue)
|
|
|
|
{
|
|
|
|
nsresult rv = aNode->SetProperty(aCategory, aKey, aValue,
|
|
|
|
nsPropertyTable::SupportsDtorFunc, true,
|
|
|
|
aOldValue);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Property table owns it now.
|
|
|
|
NS_ADDREF(aValue);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-09-11 14:22:30 +04:00
|
|
|
nsINode::SetUserData(const nsAString &aKey, nsIVariant *aData, nsIVariant **aResult)
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
2013-06-11 02:41:42 +04:00
|
|
|
OwnerDoc()->WarnOnceAbout(nsIDocument::eGetSetUserData);
|
2012-07-30 18:20:58 +04:00
|
|
|
*aResult = nullptr;
|
2012-06-23 10:57:01 +04:00
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> key = NS_Atomize(aKey);
|
2012-06-23 10:57:01 +04:00
|
|
|
if (!key) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
void *data;
|
|
|
|
if (aData) {
|
|
|
|
rv = SetUserDataProperty(DOM_USER_DATA, this, key, aData, &data);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
data = UnsetProperty(DOM_USER_DATA, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take over ownership of the old data from the property table.
|
|
|
|
nsCOMPtr<nsIVariant> oldData = dont_AddRef(static_cast<nsIVariant*>(data));
|
|
|
|
oldData.swap(*aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-12 00:26:52 +04:00
|
|
|
void
|
2013-05-31 01:46:48 +04:00
|
|
|
nsINode::SetUserData(JSContext* aCx, const nsAString& aKey,
|
|
|
|
JS::Handle<JS::Value> aData,
|
2014-06-12 00:26:52 +04:00
|
|
|
JS::MutableHandle<JS::Value> aRetval,
|
|
|
|
ErrorResult& aError)
|
2012-10-09 16:31:24 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIVariant> data;
|
2014-06-12 00:26:52 +04:00
|
|
|
aError = nsContentUtils::XPConnect()->JSValToVariant(aCx, aData, getter_AddRefs(data));
|
2012-10-09 16:31:24 +04:00
|
|
|
if (aError.Failed()) {
|
2014-06-12 00:26:52 +04:00
|
|
|
return;
|
2012-10-09 16:31:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIVariant> oldData;
|
2014-09-11 14:22:30 +04:00
|
|
|
aError = SetUserData(aKey, data, getter_AddRefs(oldData));
|
2012-10-09 16:31:24 +04:00
|
|
|
if (aError.Failed()) {
|
2014-06-12 00:26:52 +04:00
|
|
|
return;
|
2012-10-09 16:31:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!oldData) {
|
2014-06-12 00:26:52 +04:00
|
|
|
aRetval.setNull();
|
|
|
|
return;
|
2012-10-09 16:31:24 +04:00
|
|
|
}
|
|
|
|
|
2012-12-30 10:13:27 +04:00
|
|
|
JSAutoCompartment ac(aCx, GetWrapper());
|
2012-10-09 16:31:24 +04:00
|
|
|
aError = nsContentUtils::XPConnect()->VariantToJS(aCx, GetWrapper(), oldData,
|
2014-06-12 00:26:52 +04:00
|
|
|
aRetval);
|
2012-10-09 16:31:24 +04:00
|
|
|
}
|
|
|
|
|
2013-06-11 02:41:42 +04:00
|
|
|
nsIVariant*
|
|
|
|
nsINode::GetUserData(const nsAString& aKey)
|
|
|
|
{
|
|
|
|
OwnerDoc()->WarnOnceAbout(nsIDocument::eGetSetUserData);
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> key = NS_Atomize(aKey);
|
2013-06-11 02:41:42 +04:00
|
|
|
if (!key) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return static_cast<nsIVariant*>(GetProperty(DOM_USER_DATA, key));
|
|
|
|
}
|
|
|
|
|
2014-06-12 00:26:52 +04:00
|
|
|
void
|
|
|
|
nsINode::GetUserData(JSContext* aCx, const nsAString& aKey,
|
|
|
|
JS::MutableHandle<JS::Value> aRetval, ErrorResult& aError)
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
2012-10-09 16:31:24 +04:00
|
|
|
nsIVariant* data = GetUserData(aKey);
|
|
|
|
if (!data) {
|
2014-06-12 00:26:52 +04:00
|
|
|
aRetval.setNull();
|
|
|
|
return;
|
2012-10-09 16:31:24 +04:00
|
|
|
}
|
|
|
|
|
2012-12-30 10:13:27 +04:00
|
|
|
JSAutoCompartment ac(aCx, GetWrapper());
|
2012-10-09 16:31:24 +04:00
|
|
|
aError = nsContentUtils::XPConnect()->VariantToJS(aCx, GetWrapper(), data,
|
2014-06-12 00:26:52 +04:00
|
|
|
aRetval);
|
2012-10-09 16:31:24 +04:00
|
|
|
}
|
2012-06-23 10:57:01 +04:00
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
uint16_t
|
|
|
|
nsINode::CompareDocumentPosition(nsINode& aOtherNode) const
|
|
|
|
{
|
|
|
|
if (this == &aOtherNode) {
|
2012-06-23 10:57:01 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2013-11-28 16:07:43 +04:00
|
|
|
if (GetPreviousSibling() == &aOtherNode) {
|
|
|
|
MOZ_ASSERT(GetParentNode() == aOtherNode.GetParentNode());
|
2018-01-30 07:10:50 +03:00
|
|
|
return NodeBinding::DOCUMENT_POSITION_PRECEDING;
|
2013-11-28 16:07:43 +04:00
|
|
|
}
|
|
|
|
if (GetNextSibling() == &aOtherNode) {
|
|
|
|
MOZ_ASSERT(GetParentNode() == aOtherNode.GetParentNode());
|
2018-01-30 07:10:50 +03:00
|
|
|
return NodeBinding::DOCUMENT_POSITION_FOLLOWING;
|
2013-11-28 16:07:43 +04:00
|
|
|
}
|
2012-06-23 10:57:01 +04:00
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<const nsINode*, 32> parents1, parents2;
|
2012-06-23 10:57:01 +04:00
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
const nsINode *node1 = &aOtherNode, *node2 = this;
|
2012-06-23 10:57:01 +04:00
|
|
|
|
|
|
|
// Check if either node is an attribute
|
2014-04-10 20:09:50 +04:00
|
|
|
const Attr* attr1 = nullptr;
|
2012-06-23 10:57:01 +04:00
|
|
|
if (node1->IsNodeOfType(nsINode::eATTRIBUTE)) {
|
2014-04-10 20:09:50 +04:00
|
|
|
attr1 = static_cast<const Attr*>(node1);
|
|
|
|
const Element* elem = attr1->GetElement();
|
2012-06-23 10:57:01 +04:00
|
|
|
// If there is an owner element add the attribute
|
|
|
|
// to the chain and walk up to the element
|
|
|
|
if (elem) {
|
|
|
|
node1 = elem;
|
2012-10-09 16:31:24 +04:00
|
|
|
parents1.AppendElement(attr1);
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (node2->IsNodeOfType(nsINode::eATTRIBUTE)) {
|
2014-04-10 20:09:50 +04:00
|
|
|
const Attr* attr2 = static_cast<const Attr*>(node2);
|
|
|
|
const Element* elem = attr2->GetElement();
|
2012-06-23 10:57:01 +04:00
|
|
|
if (elem == node1 && attr1) {
|
|
|
|
// Both nodes are attributes on the same element.
|
|
|
|
// Compare position between the attributes.
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i;
|
2012-06-23 10:57:01 +04:00
|
|
|
const nsAttrName* attrName;
|
|
|
|
for (i = 0; (attrName = elem->GetAttrNameAt(i)); ++i) {
|
|
|
|
if (attrName->Equals(attr1->NodeInfo())) {
|
|
|
|
NS_ASSERTION(!attrName->Equals(attr2->NodeInfo()),
|
|
|
|
"Different attrs at same position");
|
2018-01-30 07:10:50 +03:00
|
|
|
return NodeBinding::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC |
|
|
|
|
NodeBinding::DOCUMENT_POSITION_PRECEDING;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
if (attrName->Equals(attr2->NodeInfo())) {
|
2018-01-30 07:10:50 +03:00
|
|
|
return NodeBinding::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC |
|
|
|
|
NodeBinding::DOCUMENT_POSITION_FOLLOWING;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_NOTREACHED("neither attribute in the element");
|
2018-01-30 07:10:50 +03:00
|
|
|
return NodeBinding::DOCUMENT_POSITION_DISCONNECTED;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (elem) {
|
|
|
|
node2 = elem;
|
2012-10-09 16:31:24 +04:00
|
|
|
parents2.AppendElement(attr2);
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We now know that both nodes are either nsIContents or nsIDocuments.
|
|
|
|
// If either node started out as an attribute, that attribute will have
|
|
|
|
// the same relative position as its ownerElement, except if the
|
|
|
|
// ownerElement ends up being the container for the other node
|
|
|
|
|
|
|
|
// Build the chain of parents
|
|
|
|
do {
|
|
|
|
parents1.AppendElement(node1);
|
2012-10-09 16:31:24 +04:00
|
|
|
node1 = node1->GetParentNode();
|
2012-06-23 10:57:01 +04:00
|
|
|
} while (node1);
|
|
|
|
do {
|
|
|
|
parents2.AppendElement(node2);
|
2012-10-09 16:31:24 +04:00
|
|
|
node2 = node2->GetParentNode();
|
2012-06-23 10:57:01 +04:00
|
|
|
} while (node2);
|
|
|
|
|
|
|
|
// Check if the nodes are disconnected.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t pos1 = parents1.Length();
|
|
|
|
uint32_t pos2 = parents2.Length();
|
2012-10-09 16:31:24 +04:00
|
|
|
const nsINode* top1 = parents1.ElementAt(--pos1);
|
|
|
|
const nsINode* top2 = parents2.ElementAt(--pos2);
|
2012-06-23 10:57:01 +04:00
|
|
|
if (top1 != top2) {
|
|
|
|
return top1 < top2 ?
|
2018-01-30 07:10:50 +03:00
|
|
|
(NodeBinding::DOCUMENT_POSITION_PRECEDING |
|
|
|
|
NodeBinding::DOCUMENT_POSITION_DISCONNECTED |
|
|
|
|
NodeBinding::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC) :
|
|
|
|
(NodeBinding::DOCUMENT_POSITION_FOLLOWING |
|
|
|
|
NodeBinding::DOCUMENT_POSITION_DISCONNECTED |
|
|
|
|
NodeBinding::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC);
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find where the parent chain differs and check indices in the parent.
|
2012-10-09 16:31:24 +04:00
|
|
|
const nsINode* parent = top1;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t len;
|
2013-01-15 16:22:03 +04:00
|
|
|
for (len = std::min(pos1, pos2); len > 0; --len) {
|
2012-10-09 16:31:24 +04:00
|
|
|
const nsINode* child1 = parents1.ElementAt(--pos1);
|
|
|
|
const nsINode* child2 = parents2.ElementAt(--pos2);
|
2012-06-23 10:57:01 +04:00
|
|
|
if (child1 != child2) {
|
|
|
|
// child1 or child2 can be an attribute here. This will work fine since
|
2018-01-23 16:30:18 +03:00
|
|
|
// ComputeIndexOf will return -1 for the attribute making the
|
|
|
|
// attribute be considered before any child.
|
|
|
|
return parent->ComputeIndexOf(child1) < parent->ComputeIndexOf(child2) ?
|
2018-01-30 07:10:50 +03:00
|
|
|
NodeBinding::DOCUMENT_POSITION_PRECEDING :
|
|
|
|
NodeBinding::DOCUMENT_POSITION_FOLLOWING;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
parent = child1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We hit the end of one of the parent chains without finding a difference
|
|
|
|
// between the chains. That must mean that one node is an ancestor of the
|
|
|
|
// other. The one with the shortest chain must be the ancestor.
|
|
|
|
return pos1 < pos2 ?
|
2018-01-30 07:10:50 +03:00
|
|
|
(NodeBinding::DOCUMENT_POSITION_PRECEDING |
|
|
|
|
NodeBinding::DOCUMENT_POSITION_CONTAINS) :
|
|
|
|
(NodeBinding::DOCUMENT_POSITION_FOLLOWING |
|
|
|
|
NodeBinding::DOCUMENT_POSITION_CONTAINED_BY);
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
2016-03-25 13:23:50 +03:00
|
|
|
bool
|
|
|
|
nsINode::IsSameNode(nsINode *other)
|
|
|
|
{
|
|
|
|
return other == this;
|
|
|
|
}
|
|
|
|
|
2012-06-23 10:57:01 +04:00
|
|
|
bool
|
2012-10-09 16:31:24 +04:00
|
|
|
nsINode::IsEqualNode(nsINode* aOther)
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
|
|
|
if (!aOther) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString string1, string2;
|
|
|
|
|
|
|
|
nsINode* node1 = this;
|
|
|
|
nsINode* node2 = aOther;
|
|
|
|
do {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t nodeType = node1->NodeType();
|
2012-06-23 10:57:01 +04:00
|
|
|
if (nodeType != node2->NodeType()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-20 06:01:40 +04:00
|
|
|
mozilla::dom::NodeInfo* nodeInfo1 = node1->mNodeInfo;
|
|
|
|
mozilla::dom::NodeInfo* nodeInfo2 = node2->mNodeInfo;
|
2012-06-23 10:57:01 +04:00
|
|
|
if (!nodeInfo1->Equals(nodeInfo2) ||
|
|
|
|
nodeInfo1->GetExtraName() != nodeInfo2->GetExtraName()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(nodeType) {
|
2018-01-30 07:10:53 +03:00
|
|
|
case ELEMENT_NODE:
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
|
|
|
// Both are elements (we checked that their nodeinfos are equal). Do the
|
|
|
|
// check on attributes.
|
|
|
|
Element* element1 = node1->AsElement();
|
|
|
|
Element* element2 = node2->AsElement();
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t attrCount = element1->GetAttrCount();
|
2012-06-23 10:57:01 +04:00
|
|
|
if (attrCount != element2->GetAttrCount()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over attributes.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < attrCount; ++i) {
|
2012-06-23 10:57:01 +04:00
|
|
|
const nsAttrName* attrName = element1->GetAttrNameAt(i);
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool hasAttr =
|
|
|
|
#endif
|
|
|
|
element1->GetAttr(attrName->NamespaceID(), attrName->LocalName(),
|
|
|
|
string1);
|
|
|
|
NS_ASSERTION(hasAttr, "Why don't we have an attr?");
|
2016-03-25 13:23:53 +03:00
|
|
|
|
2012-06-23 10:57:01 +04:00
|
|
|
if (!element2->AttrValueIs(attrName->NamespaceID(),
|
|
|
|
attrName->LocalName(),
|
|
|
|
string1,
|
|
|
|
eCaseMatters)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-01-30 07:10:53 +03:00
|
|
|
case TEXT_NODE:
|
|
|
|
case COMMENT_NODE:
|
|
|
|
case CDATA_SECTION_NODE:
|
|
|
|
case PROCESSING_INSTRUCTION_NODE:
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
|
|
|
string1.Truncate();
|
|
|
|
static_cast<nsIContent*>(node1)->AppendTextTo(string1);
|
|
|
|
string2.Truncate();
|
|
|
|
static_cast<nsIContent*>(node2)->AppendTextTo(string2);
|
|
|
|
|
|
|
|
if (!string1.Equals(string2)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2018-01-30 07:10:53 +03:00
|
|
|
case DOCUMENT_NODE:
|
|
|
|
case DOCUMENT_FRAGMENT_NODE:
|
2012-06-23 10:57:01 +04:00
|
|
|
break;
|
2018-01-30 07:10:53 +03:00
|
|
|
case ATTRIBUTE_NODE:
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(node1 == this && node2 == aOther,
|
|
|
|
"Did we come upon an attribute node while walking a "
|
|
|
|
"subtree?");
|
2012-10-09 16:31:24 +04:00
|
|
|
node1->GetNodeValue(string1);
|
|
|
|
node2->GetNodeValue(string2);
|
2016-03-25 13:23:53 +03:00
|
|
|
|
2012-06-23 10:57:01 +04:00
|
|
|
// Returning here as to not bother walking subtree. And there is no
|
|
|
|
// risk that we're half way through walking some other subtree since
|
|
|
|
// attribute nodes doesn't appear in subtrees.
|
|
|
|
return string1.Equals(string2);
|
|
|
|
}
|
2018-01-30 07:10:53 +03:00
|
|
|
case DOCUMENT_TYPE_NODE:
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
2018-03-13 23:24:01 +03:00
|
|
|
DocumentType* docType1 = static_cast<DocumentType*>(node1);
|
|
|
|
DocumentType* docType2 = static_cast<DocumentType*>(node2);
|
2012-06-23 10:57:01 +04:00
|
|
|
|
|
|
|
// Public ID
|
|
|
|
docType1->GetPublicId(string1);
|
|
|
|
docType2->GetPublicId(string2);
|
|
|
|
if (!string1.Equals(string2)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-25 13:23:53 +03:00
|
|
|
|
2012-06-23 10:57:01 +04:00
|
|
|
// System ID
|
|
|
|
docType1->GetSystemId(string1);
|
|
|
|
docType2->GetSystemId(string2);
|
|
|
|
if (!string1.Equals(string2)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(false, "Unknown node type");
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsINode* nextNode = node1->GetFirstChild();
|
|
|
|
if (nextNode) {
|
|
|
|
node1 = nextNode;
|
|
|
|
node2 = node2->GetFirstChild();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (node2->GetFirstChild()) {
|
|
|
|
// node2 has a firstChild, but node1 doesn't
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find next sibling, possibly walking parent chain.
|
|
|
|
while (1) {
|
|
|
|
if (node1 == this) {
|
|
|
|
NS_ASSERTION(node2 == aOther, "Should have reached the start node "
|
|
|
|
"for both trees at the same time");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nextNode = node1->GetNextSibling();
|
|
|
|
if (nextNode) {
|
|
|
|
node1 = nextNode;
|
|
|
|
node2 = node2->GetNextSibling();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node2->GetNextSibling()) {
|
|
|
|
// node2 has a nextSibling, but node1 doesn't
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-25 13:23:53 +03:00
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
node1 = node1->GetParentNode();
|
|
|
|
node2 = node2->GetParentNode();
|
2012-06-23 10:57:01 +04:00
|
|
|
NS_ASSERTION(node1 && node2, "no parent while walking subtree");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while(node2);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
void
|
2012-06-23 10:57:01 +04:00
|
|
|
nsINode::LookupNamespaceURI(const nsAString& aNamespacePrefix,
|
|
|
|
nsAString& aNamespaceURI)
|
|
|
|
{
|
|
|
|
Element *element = GetNameSpaceElement();
|
|
|
|
if (!element ||
|
|
|
|
NS_FAILED(element->LookupNamespaceURIInternal(aNamespacePrefix,
|
|
|
|
aNamespaceURI))) {
|
|
|
|
SetDOMStringToNull(aNamespaceURI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_DOMTARGET_DEFAULTS(nsINode)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsINode::AddEventListener(const nsAString& aType,
|
|
|
|
nsIDOMEventListener *aListener,
|
|
|
|
bool aUseCapture,
|
|
|
|
bool aWantsUntrusted,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t aOptionalArgc)
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!aWantsUntrusted || aOptionalArgc > 1,
|
|
|
|
"Won't check if this is chrome, you want to set "
|
|
|
|
"aWantsUntrusted to false or make the aWantsUntrusted "
|
|
|
|
"explicit by making aOptionalArgc non-zero.");
|
|
|
|
|
|
|
|
if (!aWantsUntrusted &&
|
|
|
|
(aOptionalArgc < 2 &&
|
|
|
|
!nsContentUtils::IsChromeDoc(OwnerDoc()))) {
|
|
|
|
aWantsUntrusted = true;
|
|
|
|
}
|
|
|
|
|
2014-03-17 10:56:53 +04:00
|
|
|
EventListenerManager* listener_manager = GetOrCreateListenerManager();
|
2012-06-23 10:57:01 +04:00
|
|
|
NS_ENSURE_STATE(listener_manager);
|
|
|
|
listener_manager->AddEventListener(aType, aListener, aUseCapture,
|
|
|
|
aWantsUntrusted);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-17 01:16:08 +04:00
|
|
|
void
|
|
|
|
nsINode::AddEventListener(const nsAString& aType,
|
2013-09-24 04:25:17 +04:00
|
|
|
EventListener* aListener,
|
2016-04-26 11:23:17 +03:00
|
|
|
const AddEventListenerOptionsOrBoolean& aOptions,
|
2013-04-17 01:16:08 +04:00
|
|
|
const Nullable<bool>& aWantsUntrusted,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
bool wantsUntrusted;
|
|
|
|
if (aWantsUntrusted.IsNull()) {
|
|
|
|
wantsUntrusted = !nsContentUtils::IsChromeDoc(OwnerDoc());
|
|
|
|
} else {
|
|
|
|
wantsUntrusted = aWantsUntrusted.Value();
|
|
|
|
}
|
|
|
|
|
2014-03-17 10:56:53 +04:00
|
|
|
EventListenerManager* listener_manager = GetOrCreateListenerManager();
|
2013-04-17 01:16:08 +04:00
|
|
|
if (!listener_manager) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return;
|
|
|
|
}
|
2016-04-26 11:23:17 +03:00
|
|
|
|
|
|
|
listener_manager->AddEventListener(aType, aListener, aOptions,
|
2013-04-17 01:16:08 +04:00
|
|
|
wantsUntrusted);
|
|
|
|
}
|
|
|
|
|
2012-06-23 10:57:01 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsINode::AddSystemEventListener(const nsAString& aType,
|
|
|
|
nsIDOMEventListener *aListener,
|
|
|
|
bool aUseCapture,
|
|
|
|
bool aWantsUntrusted,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t aOptionalArgc)
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!aWantsUntrusted || aOptionalArgc > 1,
|
|
|
|
"Won't check if this is chrome, you want to set "
|
|
|
|
"aWantsUntrusted to false or make the aWantsUntrusted "
|
|
|
|
"explicit by making aOptionalArgc non-zero.");
|
|
|
|
|
|
|
|
if (!aWantsUntrusted &&
|
|
|
|
(aOptionalArgc < 2 &&
|
|
|
|
!nsContentUtils::IsChromeDoc(OwnerDoc()))) {
|
|
|
|
aWantsUntrusted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_AddSystemEventListener(this, aType, aListener, aUseCapture,
|
|
|
|
aWantsUntrusted);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsINode::RemoveEventListener(const nsAString& aType,
|
|
|
|
nsIDOMEventListener* aListener,
|
|
|
|
bool aUseCapture)
|
|
|
|
{
|
2014-03-17 10:56:53 +04:00
|
|
|
EventListenerManager* elm = GetExistingListenerManager();
|
2012-06-23 10:57:01 +04:00
|
|
|
if (elm) {
|
|
|
|
elm->RemoveEventListener(aType, aListener, aUseCapture);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_REMOVE_SYSTEM_EVENT_LISTENER(nsINode)
|
|
|
|
|
|
|
|
nsresult
|
2016-10-21 05:11:07 +03:00
|
|
|
nsINode::GetEventTargetParent(EventChainPreVisitor& aVisitor)
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
2017-10-25 09:52:56 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("GetEventTargetParent is only here so that we can "
|
|
|
|
"use the NS_DECL_NSIDOMTARGET macro");
|
2012-06-23 10:57:01 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2014-03-12 05:11:38 +04:00
|
|
|
void
|
|
|
|
nsINode::GetBoxQuads(const BoxQuadOptions& aOptions,
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<DOMQuad> >& aResult,
|
2017-02-01 23:43:59 +03:00
|
|
|
CallerType aCallerType,
|
2014-03-12 05:11:38 +04:00
|
|
|
mozilla::ErrorResult& aRv)
|
|
|
|
{
|
2017-02-01 23:43:59 +03:00
|
|
|
mozilla::GetBoxQuads(this, aOptions, aResult, aCallerType, aRv);
|
2014-03-12 05:11:38 +04:00
|
|
|
}
|
|
|
|
|
2014-03-12 05:11:39 +04:00
|
|
|
already_AddRefed<DOMQuad>
|
|
|
|
nsINode::ConvertQuadFromNode(DOMQuad& aQuad,
|
|
|
|
const GeometryNode& aFrom,
|
|
|
|
const ConvertCoordinateOptions& aOptions,
|
2017-02-01 23:43:59 +03:00
|
|
|
CallerType aCallerType,
|
2014-03-12 05:11:39 +04:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2017-02-01 23:43:59 +03:00
|
|
|
return mozilla::ConvertQuadFromNode(this, aQuad, aFrom, aOptions, aCallerType,
|
|
|
|
aRv);
|
2014-03-12 05:11:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<DOMQuad>
|
|
|
|
nsINode::ConvertRectFromNode(DOMRectReadOnly& aRect,
|
|
|
|
const GeometryNode& aFrom,
|
|
|
|
const ConvertCoordinateOptions& aOptions,
|
2017-02-01 23:43:59 +03:00
|
|
|
CallerType aCallerType,
|
2014-03-12 05:11:39 +04:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2017-02-01 23:43:59 +03:00
|
|
|
return mozilla::ConvertRectFromNode(this, aRect, aFrom, aOptions, aCallerType,
|
|
|
|
aRv);
|
2014-03-12 05:11:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<DOMPoint>
|
|
|
|
nsINode::ConvertPointFromNode(const DOMPointInit& aPoint,
|
|
|
|
const GeometryNode& aFrom,
|
|
|
|
const ConvertCoordinateOptions& aOptions,
|
2017-02-01 23:43:59 +03:00
|
|
|
CallerType aCallerType,
|
2014-03-12 05:11:39 +04:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2017-02-01 23:43:59 +03:00
|
|
|
return mozilla::ConvertPointFromNode(this, aPoint, aFrom, aOptions,
|
|
|
|
aCallerType, aRv);
|
2014-03-12 05:11:39 +04:00
|
|
|
}
|
|
|
|
|
2012-06-23 10:57:01 +04:00
|
|
|
nsresult
|
|
|
|
nsINode::DispatchEvent(nsIDOMEvent *aEvent, bool* aRetVal)
|
|
|
|
{
|
|
|
|
// XXX sXBL/XBL2 issue -- do we really want the owner here? What
|
|
|
|
// if that's the XBL document? Would we want its presshell? Or what?
|
|
|
|
nsCOMPtr<nsIDocument> document = OwnerDoc();
|
|
|
|
|
|
|
|
// Do nothing if the element does not belong to a document
|
|
|
|
if (!document) {
|
|
|
|
*aRetVal = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Obtain a presentation shell
|
2018-02-21 01:00:10 +03:00
|
|
|
RefPtr<nsPresContext> context = document->GetPresContext();
|
2012-06-23 10:57:01 +04:00
|
|
|
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
nsresult rv =
|
2014-03-18 08:48:21 +04:00
|
|
|
EventDispatcher::DispatchDOMEvent(this, nullptr, aEvent, context, &status);
|
2012-06-23 10:57:01 +04:00
|
|
|
*aRetVal = (status != nsEventStatus_eConsumeNoDefault);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-03-18 08:48:20 +04:00
|
|
|
nsINode::PostHandleEvent(EventChainPostVisitor& /*aVisitor*/)
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-17 10:56:53 +04:00
|
|
|
EventListenerManager*
|
2013-10-23 03:32:04 +04:00
|
|
|
nsINode::GetOrCreateListenerManager()
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
2013-10-23 03:32:04 +04:00
|
|
|
return nsContentUtils::GetListenerManagerForNode(this);
|
|
|
|
}
|
|
|
|
|
2014-03-17 10:56:53 +04:00
|
|
|
EventListenerManager*
|
2013-10-23 03:32:04 +04:00
|
|
|
nsINode::GetExistingListenerManager() const
|
|
|
|
{
|
|
|
|
return nsContentUtils::GetExistingListenerManagerForNode(this);
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIScriptContext*
|
|
|
|
nsINode::GetContextForEventHandlers(nsresult* aRv)
|
|
|
|
{
|
|
|
|
return nsContentUtils::GetContextForEventHandlers(this, aRv);
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowOuter*
|
2015-05-12 22:56:39 +03:00
|
|
|
nsINode::GetOwnerGlobalForBindings()
|
2013-05-31 01:46:39 +04:00
|
|
|
{
|
|
|
|
bool dummy;
|
2017-11-04 01:25:38 +03:00
|
|
|
auto* window = static_cast<nsGlobalWindowInner*>(OwnerDoc()->GetScriptHandlingObject(dummy));
|
2016-01-30 20:05:36 +03:00
|
|
|
return window ? nsPIDOMWindowOuter::GetFromCurrentInner(window->AsInner()) : nullptr;
|
2013-05-31 01:46:39 +04:00
|
|
|
}
|
|
|
|
|
2015-05-12 22:56:39 +03:00
|
|
|
nsIGlobalObject*
|
|
|
|
nsINode::GetOwnerGlobal() const
|
|
|
|
{
|
|
|
|
bool dummy;
|
|
|
|
return OwnerDoc()->GetScriptHandlingObject(dummy);
|
|
|
|
}
|
|
|
|
|
2015-08-14 20:52:38 +03:00
|
|
|
void
|
|
|
|
nsINode::ChangeEditableDescendantCount(int32_t aDelta)
|
|
|
|
{
|
|
|
|
if (aDelta == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSlots* s = Slots();
|
|
|
|
MOZ_ASSERT(aDelta > 0 ||
|
|
|
|
s->mEditableDescendantCount >= (uint32_t) (-1 * aDelta));
|
|
|
|
s->mEditableDescendantCount += aDelta;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsINode::ResetEditableDescendantCount()
|
|
|
|
{
|
|
|
|
nsSlots* s = GetExistingSlots();
|
|
|
|
if (s) {
|
|
|
|
s->mEditableDescendantCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
nsINode::EditableDescendantCount()
|
|
|
|
{
|
|
|
|
nsSlots* s = GetExistingSlots();
|
|
|
|
if (s) {
|
|
|
|
return s->mEditableDescendantCount;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-23 10:58:04 +04:00
|
|
|
bool
|
|
|
|
nsINode::UnoptimizableCCNode() const
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
2013-09-24 23:28:32 +04:00
|
|
|
const uintptr_t problematicFlags = (NODE_IS_ANONYMOUS_ROOT |
|
2014-04-06 23:32:38 +04:00
|
|
|
NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE |
|
2012-10-02 12:24:11 +04:00
|
|
|
NODE_IS_NATIVE_ANONYMOUS_ROOT |
|
2014-04-25 20:12:26 +04:00
|
|
|
NODE_MAY_BE_IN_BINDING_MNGR |
|
|
|
|
NODE_IS_IN_SHADOW_TREE);
|
2012-06-23 10:58:04 +04:00
|
|
|
return HasFlag(problematicFlags) ||
|
2018-01-30 07:10:53 +03:00
|
|
|
NodeType() == ATTRIBUTE_NODE ||
|
2012-06-23 10:57:01 +04:00
|
|
|
// For strange cases like xbl:content/xbl:children
|
2012-06-23 10:58:04 +04:00
|
|
|
(IsElement() &&
|
|
|
|
AsElement()->IsInNamespace(kNameSpaceID_XBL));
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
bool
|
|
|
|
nsINode::Traverse(nsINode *tmp, nsCycleCollectionTraversalCallback &cb)
|
|
|
|
{
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_LIKELY(!cb.WantAllTraces())) {
|
2014-10-02 22:45:44 +04:00
|
|
|
nsIDocument *currentDoc = tmp->GetUncomposedDoc();
|
2012-06-23 10:57:01 +04:00
|
|
|
if (currentDoc &&
|
|
|
|
nsCCUncollectableMarker::InGeneration(currentDoc->GetMarkedCCGeneration())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nsCCUncollectableMarker::sGeneration) {
|
|
|
|
// If we're black no need to traverse.
|
2017-01-25 04:38:58 +03:00
|
|
|
if (tmp->HasKnownLiveWrapper() || tmp->InCCBlackTree()) {
|
2012-06-23 10:57:01 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-23 10:58:04 +04:00
|
|
|
if (!tmp->UnoptimizableCCNode()) {
|
2012-06-23 10:57:01 +04:00
|
|
|
// If we're in a black document, return early.
|
2017-01-25 04:38:58 +03:00
|
|
|
if ((currentDoc && currentDoc->HasKnownLiveWrapper())) {
|
2012-06-23 10:57:01 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// If we're not in anonymous content and we have a black parent,
|
|
|
|
// return early.
|
|
|
|
nsIContent* parent = tmp->GetParent();
|
2017-01-25 04:38:58 +03:00
|
|
|
if (parent && !parent->UnoptimizableCCNode() &&
|
|
|
|
parent->HasKnownLiveWrapper()) {
|
2018-01-23 16:30:18 +03:00
|
|
|
MOZ_ASSERT(parent->ComputeIndexOf(tmp) >= 0, "Parent doesn't own us?");
|
2012-06-23 10:57:01 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-15 11:32:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mNodeInfo)
|
2012-06-23 10:57:01 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_RAWPTR(GetParent())
|
|
|
|
|
|
|
|
nsSlots *slots = tmp->GetExistingSlots();
|
|
|
|
if (slots) {
|
|
|
|
slots->Traverse(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp->HasProperties()) {
|
|
|
|
nsNodeUtils::TraverseUserData(tmp, cb);
|
|
|
|
nsCOMArray<nsISupports>* objects =
|
|
|
|
static_cast<nsCOMArray<nsISupports>*>(tmp->GetProperty(nsGkAtoms::keepobjectsalive));
|
|
|
|
if (objects) {
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < objects->Count(); ++i) {
|
2012-06-23 10:57:01 +04:00
|
|
|
cb.NoteXPCOMChild(objects->ObjectAt(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 07:10:53 +03:00
|
|
|
if (tmp->NodeType() != DOCUMENT_NODE &&
|
2012-06-23 10:57:01 +04:00
|
|
|
tmp->HasFlag(NODE_HAS_LISTENERMANAGER)) {
|
|
|
|
nsContentUtils::TraverseListenerManager(tmp, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
void
|
2013-08-02 11:04:01 +04:00
|
|
|
nsINode::Unlink(nsINode* tmp)
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
2013-08-02 11:04:01 +04:00
|
|
|
tmp->ReleaseWrapper(tmp);
|
2012-06-23 10:57:01 +04:00
|
|
|
|
|
|
|
nsSlots *slots = tmp->GetExistingSlots();
|
|
|
|
if (slots) {
|
|
|
|
slots->Unlink();
|
|
|
|
}
|
|
|
|
|
2018-01-30 07:10:53 +03:00
|
|
|
if (tmp->NodeType() != DOCUMENT_NODE &&
|
2012-06-23 10:57:01 +04:00
|
|
|
tmp->HasFlag(NODE_HAS_LISTENERMANAGER)) {
|
|
|
|
nsContentUtils::RemoveListenerManager(tmp);
|
|
|
|
tmp->UnsetFlags(NODE_HAS_LISTENERMANAGER);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp->HasProperties()) {
|
|
|
|
nsNodeUtils::UnlinkUserData(tmp);
|
|
|
|
tmp->DeleteProperty(nsGkAtoms::keepobjectsalive);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 20:34:55 +03:00
|
|
|
static void
|
|
|
|
AdoptNodeIntoOwnerDoc(nsINode *aParent, nsINode *aNode, ErrorResult& aError)
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
2012-10-09 16:31:24 +04:00
|
|
|
NS_ASSERTION(!aNode->GetParentNode(),
|
2012-06-23 10:57:01 +04:00
|
|
|
"Should have removed from parent already");
|
|
|
|
|
|
|
|
nsIDocument *doc = aParent->OwnerDoc();
|
|
|
|
|
2017-09-13 20:34:55 +03:00
|
|
|
DebugOnly<nsINode*> adoptedNode = doc->AdoptNode(*aNode, aError);
|
2012-06-23 10:57:01 +04:00
|
|
|
|
2017-09-13 20:34:55 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (!aError.Failed()) {
|
|
|
|
MOZ_ASSERT(aParent->OwnerDoc() == doc,
|
2012-06-23 10:57:01 +04:00
|
|
|
"ownerDoc chainged while adopting");
|
2017-09-13 20:34:55 +03:00
|
|
|
MOZ_ASSERT(adoptedNode == aNode, "Uh, adopt node changed nodes?");
|
|
|
|
MOZ_ASSERT(aParent->OwnerDoc() == aNode->OwnerDoc(),
|
2012-06-23 10:57:01 +04:00
|
|
|
"ownerDocument changed again after adopting!");
|
2017-09-13 20:34:55 +03:00
|
|
|
}
|
|
|
|
#endif // DEBUG
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
2017-09-13 20:34:55 +03:00
|
|
|
static void
|
|
|
|
CheckForOutdatedParent(nsINode* aParent, nsINode* aNode, ErrorResult& aError)
|
2013-08-16 01:39:28 +04:00
|
|
|
{
|
2013-11-20 19:37:30 +04:00
|
|
|
if (JSObject* existingObjUnrooted = aNode->GetWrapper()) {
|
2016-08-11 15:39:22 +03:00
|
|
|
JS::Rooted<JSObject*> existingObj(RootingCx(), existingObjUnrooted);
|
2014-07-18 20:37:42 +04:00
|
|
|
|
2013-11-20 19:37:30 +04:00
|
|
|
AutoJSContext cx;
|
2013-08-16 01:39:28 +04:00
|
|
|
nsIGlobalObject* global = aParent->OwnerDoc()->GetScopeObject();
|
|
|
|
MOZ_ASSERT(global);
|
|
|
|
|
|
|
|
if (js::GetGlobalForObjectCrossCompartment(existingObj) !=
|
|
|
|
global->GetGlobalJSObject()) {
|
2014-04-09 02:27:15 +04:00
|
|
|
JSAutoCompartment ac(cx, existingObj);
|
2017-09-13 20:34:55 +03:00
|
|
|
ReparentWrapper(cx, existingObj, aError);
|
2013-08-16 01:39:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-23 10:57:01 +04:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsINode::doInsertChildAt(nsIContent* aKid, uint32_t aIndex,
|
2012-06-23 10:57:01 +04:00
|
|
|
bool aNotify, nsAttrAndChildArray& aChildArray)
|
|
|
|
{
|
2017-10-23 10:04:14 +03:00
|
|
|
MOZ_ASSERT(!aKid->GetParentNode(), "Inserting node that already has parent");
|
|
|
|
MOZ_ASSERT(!IsNodeOfType(nsINode::eATTRIBUTE));
|
2017-08-18 06:56:38 +03:00
|
|
|
|
2012-06-23 10:57:01 +04:00
|
|
|
// The id-handling code, and in the future possibly other code, need to
|
|
|
|
// react to unexpected attribute changes.
|
|
|
|
nsMutationGuard::DidMutate();
|
|
|
|
|
|
|
|
// Do this before checking the child-count since this could cause mutations
|
2014-10-02 22:45:44 +04:00
|
|
|
nsIDocument* doc = GetUncomposedDoc();
|
2016-03-31 15:20:14 +03:00
|
|
|
mozAutoDocUpdate updateBatch(GetComposedDoc(), UPDATE_CONTENT_MODEL, aNotify);
|
2012-06-23 10:57:01 +04:00
|
|
|
|
2013-08-02 14:24:41 +04:00
|
|
|
if (OwnerDoc() != aKid->OwnerDoc()) {
|
2017-09-13 20:34:55 +03:00
|
|
|
ErrorResult error;
|
|
|
|
AdoptNodeIntoOwnerDoc(this, aKid, error);
|
|
|
|
|
|
|
|
// Need to WouldReportJSException() if our callee can throw a JS
|
|
|
|
// exception (which it can) and we're neither propagating the
|
|
|
|
// error out nor unconditionally suppressing it.
|
|
|
|
error.WouldReportJSException();
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
|
|
return error.StealNSResult();
|
|
|
|
}
|
2013-08-16 01:39:28 +04:00
|
|
|
} else if (OwnerDoc()->DidDocumentOpen()) {
|
2017-09-13 20:34:55 +03:00
|
|
|
ErrorResult error;
|
|
|
|
CheckForOutdatedParent(this, aKid, error);
|
|
|
|
|
|
|
|
// Need to WouldReportJSException() if our callee can throw a JS
|
|
|
|
// exception (which it can) and we're neither propagating the
|
|
|
|
// error out nor unconditionally suppressing it.
|
|
|
|
error.WouldReportJSException();
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
|
|
return error.StealNSResult();
|
|
|
|
}
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t childCount = aChildArray.ChildCount();
|
2012-06-23 10:57:01 +04:00
|
|
|
NS_ENSURE_TRUE(aIndex <= childCount, NS_ERROR_ILLEGAL_VALUE);
|
|
|
|
bool isAppend = (aIndex == childCount);
|
|
|
|
|
2017-09-13 20:34:55 +03:00
|
|
|
nsresult rv = aChildArray.InsertChildAt(aKid, aIndex);
|
2012-06-23 10:57:01 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (aIndex == 0) {
|
|
|
|
mFirstChild = aKid;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIContent* parent =
|
2012-07-30 18:20:58 +04:00
|
|
|
IsNodeOfType(eDOCUMENT) ? nullptr : static_cast<nsIContent*>(this);
|
2012-06-23 10:57:01 +04:00
|
|
|
|
|
|
|
rv = aKid->BindToTree(doc, parent,
|
2012-07-30 18:20:58 +04:00
|
|
|
parent ? parent->GetBindingParent() : nullptr,
|
2012-06-23 10:57:01 +04:00
|
|
|
true);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (GetFirstChild() == aKid) {
|
|
|
|
mFirstChild = aKid->GetNextSibling();
|
|
|
|
}
|
|
|
|
aChildArray.RemoveChildAt(aIndex);
|
|
|
|
aKid->UnbindFromTree();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-10-23 10:04:14 +03:00
|
|
|
// Invalidate cached array of child nodes
|
|
|
|
InvalidateChildNodes();
|
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
NS_ASSERTION(aKid->GetParentNode() == this,
|
2012-06-23 10:57:01 +04:00
|
|
|
"Did we run script inappropriately?");
|
|
|
|
|
|
|
|
if (aNotify) {
|
|
|
|
// Note that we always want to call ContentInserted when things are added
|
|
|
|
// as kids to documents
|
|
|
|
if (parent && isAppend) {
|
2017-07-27 16:49:52 +03:00
|
|
|
nsNodeUtils::ContentAppended(parent, aKid);
|
2012-06-23 10:57:01 +04:00
|
|
|
} else {
|
2017-07-27 16:49:52 +03:00
|
|
|
nsNodeUtils::ContentInserted(this, aKid);
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nsContentUtils::HasMutationListeners(aKid,
|
|
|
|
NS_EVENT_BITS_MUTATION_NODEINSERTED, this)) {
|
2015-09-07 17:55:51 +03:00
|
|
|
InternalMutationEvent mutation(true, eLegacyNodeInserted);
|
2012-06-23 10:57:01 +04:00
|
|
|
mutation.mRelatedNode = do_QueryInterface(this);
|
|
|
|
|
|
|
|
mozAutoSubtreeModified subtree(OwnerDoc(), this);
|
2014-03-17 10:56:54 +04:00
|
|
|
(new AsyncEventDispatcher(aKid, mutation))->RunDOMEventWhenSafe();
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-07-22 16:15:43 +04:00
|
|
|
Element*
|
|
|
|
nsINode::GetPreviousElementSibling() const
|
|
|
|
{
|
|
|
|
nsIContent* previousSibling = GetPreviousSibling();
|
|
|
|
while (previousSibling) {
|
|
|
|
if (previousSibling->IsElement()) {
|
|
|
|
return previousSibling->AsElement();
|
|
|
|
}
|
|
|
|
previousSibling = previousSibling->GetPreviousSibling();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Element*
|
|
|
|
nsINode::GetNextElementSibling() const
|
|
|
|
{
|
|
|
|
nsIContent* nextSibling = GetNextSibling();
|
|
|
|
while (nextSibling) {
|
|
|
|
if (nextSibling->IsElement()) {
|
|
|
|
return nextSibling->AsElement();
|
|
|
|
}
|
|
|
|
nextSibling = nextSibling->GetNextSibling();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-04-12 05:48:14 +03:00
|
|
|
static already_AddRefed<nsINode>
|
|
|
|
GetNodeFromNodeOrString(const OwningNodeOrString& aNode,
|
|
|
|
nsIDocument* aDocument)
|
|
|
|
{
|
|
|
|
if (aNode.IsNode()) {
|
|
|
|
nsCOMPtr<nsINode> node = aNode.GetAsNode();
|
|
|
|
return node.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aNode.IsString()){
|
|
|
|
RefPtr<nsTextNode> textNode =
|
|
|
|
aDocument->CreateTextNode(aNode.GetAsString());
|
|
|
|
return textNode.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_CRASH("Impossible type");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implement the algorithm specified at
|
|
|
|
* https://dom.spec.whatwg.org/#converting-nodes-into-a-node for |prepend()|,
|
|
|
|
* |append()|, |before()|, |after()|, and |replaceWith()| APIs.
|
|
|
|
*/
|
|
|
|
static already_AddRefed<nsINode>
|
|
|
|
ConvertNodesOrStringsIntoNode(const Sequence<OwningNodeOrString>& aNodes,
|
|
|
|
nsIDocument* aDocument,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (aNodes.Length() == 1) {
|
|
|
|
return GetNodeFromNodeOrString(aNodes[0], aDocument);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsINode> fragment = aDocument->CreateDocumentFragment();
|
|
|
|
|
|
|
|
for (const auto& node : aNodes) {
|
|
|
|
nsCOMPtr<nsINode> childNode = GetNodeFromNodeOrString(node, aDocument);
|
|
|
|
fragment->AppendChild(*childNode, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fragment.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InsertNodesIntoHashset(const Sequence<OwningNodeOrString>& aNodes,
|
|
|
|
nsTHashtable<nsPtrHashKey<nsINode>>& aHashset)
|
|
|
|
{
|
|
|
|
for (const auto& node : aNodes) {
|
|
|
|
if (node.IsNode()) {
|
|
|
|
aHashset.PutEntry(node.GetAsNode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsINode*
|
|
|
|
FindViablePreviousSibling(const nsINode& aNode,
|
|
|
|
const Sequence<OwningNodeOrString>& aNodes)
|
|
|
|
{
|
|
|
|
nsTHashtable<nsPtrHashKey<nsINode>> nodeSet(16);
|
|
|
|
InsertNodesIntoHashset(aNodes, nodeSet);
|
|
|
|
|
|
|
|
nsINode* viablePreviousSibling = nullptr;
|
|
|
|
for (nsINode* sibling = aNode.GetPreviousSibling(); sibling;
|
|
|
|
sibling = sibling->GetPreviousSibling()) {
|
|
|
|
if (!nodeSet.Contains(sibling)) {
|
|
|
|
viablePreviousSibling = sibling;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return viablePreviousSibling;
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsINode*
|
|
|
|
FindViableNextSibling(const nsINode& aNode,
|
|
|
|
const Sequence<OwningNodeOrString>& aNodes)
|
|
|
|
{
|
|
|
|
nsTHashtable<nsPtrHashKey<nsINode>> nodeSet(16);
|
|
|
|
InsertNodesIntoHashset(aNodes, nodeSet);
|
|
|
|
|
|
|
|
nsINode* viableNextSibling = nullptr;
|
|
|
|
for (nsINode* sibling = aNode.GetNextSibling(); sibling;
|
|
|
|
sibling = sibling->GetNextSibling()) {
|
|
|
|
if (!nodeSet.Contains(sibling)) {
|
|
|
|
viableNextSibling = sibling;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return viableNextSibling;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsINode::Before(const Sequence<OwningNodeOrString>& aNodes,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsINode> parent = GetParentNode();
|
|
|
|
if (!parent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-27 22:36:41 +03:00
|
|
|
nsCOMPtr<nsINode> viablePreviousSibling =
|
|
|
|
FindViablePreviousSibling(*this, aNodes);
|
2016-04-12 05:48:14 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsINode> node =
|
|
|
|
ConvertNodesOrStringsIntoNode(aNodes, OwnerDoc(), aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
viablePreviousSibling = viablePreviousSibling ?
|
|
|
|
viablePreviousSibling->GetNextSibling() : parent->GetFirstChild();
|
|
|
|
|
|
|
|
parent->InsertBefore(*node, viablePreviousSibling, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsINode::After(const Sequence<OwningNodeOrString>& aNodes,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsINode> parent = GetParentNode();
|
|
|
|
if (!parent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-27 22:36:41 +03:00
|
|
|
nsCOMPtr<nsINode> viableNextSibling = FindViableNextSibling(*this, aNodes);
|
2016-04-12 05:48:14 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsINode> node =
|
|
|
|
ConvertNodesOrStringsIntoNode(aNodes, OwnerDoc(), aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent->InsertBefore(*node, viableNextSibling, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsINode::ReplaceWith(const Sequence<OwningNodeOrString>& aNodes,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsINode> parent = GetParentNode();
|
|
|
|
if (!parent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-27 22:36:41 +03:00
|
|
|
nsCOMPtr<nsINode> viableNextSibling = FindViableNextSibling(*this, aNodes);
|
2016-04-12 05:48:14 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsINode> node =
|
|
|
|
ConvertNodesOrStringsIntoNode(aNodes, OwnerDoc(), aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent == GetParentNode()) {
|
|
|
|
parent->ReplaceChild(*node, *this, aRv);
|
|
|
|
} else {
|
|
|
|
parent->InsertBefore(*node, viableNextSibling, aRv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-13 11:08:47 +04:00
|
|
|
void
|
|
|
|
nsINode::Remove()
|
|
|
|
{
|
2013-04-24 22:09:25 +04:00
|
|
|
nsCOMPtr<nsINode> parent = GetParentNode();
|
2013-04-13 11:08:47 +04:00
|
|
|
if (!parent) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-08 21:05:34 +03:00
|
|
|
|
2018-02-01 22:21:14 +03:00
|
|
|
parent->RemoveChild(*this, IgnoreErrors());
|
2013-04-13 11:08:47 +04:00
|
|
|
}
|
|
|
|
|
2013-07-22 16:15:43 +04:00
|
|
|
Element*
|
|
|
|
nsINode::GetFirstElementChild() const
|
|
|
|
{
|
|
|
|
for (nsIContent* child = GetFirstChild();
|
|
|
|
child;
|
|
|
|
child = child->GetNextSibling()) {
|
|
|
|
if (child->IsElement()) {
|
|
|
|
return child->AsElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Element*
|
|
|
|
nsINode::GetLastElementChild() const
|
|
|
|
{
|
|
|
|
for (nsIContent* child = GetLastChild();
|
|
|
|
child;
|
|
|
|
child = child->GetPreviousSibling()) {
|
|
|
|
if (child->IsElement()) {
|
|
|
|
return child->AsElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-04-12 05:48:14 +03:00
|
|
|
void
|
|
|
|
nsINode::Prepend(const Sequence<OwningNodeOrString>& aNodes,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsINode> node =
|
|
|
|
ConvertNodesOrStringsIntoNode(aNodes, OwnerDoc(), aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-13 00:34:25 +03:00
|
|
|
nsCOMPtr<nsINode> refNode = mFirstChild;
|
|
|
|
InsertBefore(*node, refNode, aRv);
|
2016-04-12 05:48:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsINode::Append(const Sequence<OwningNodeOrString>& aNodes,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsINode> node =
|
|
|
|
ConvertNodesOrStringsIntoNode(aNodes, OwnerDoc(), aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AppendChild(*node, aRv);
|
|
|
|
}
|
|
|
|
|
2012-06-23 10:57:01 +04:00
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
nsINode::doRemoveChildAt(uint32_t aIndex, bool aNotify,
|
2012-06-23 10:57:01 +04:00
|
|
|
nsIContent* aKid, nsAttrAndChildArray& aChildArray)
|
|
|
|
{
|
2017-05-03 02:08:02 +03:00
|
|
|
// NOTE: This function must not trigger any calls to
|
|
|
|
// nsIDocument::GetRootElement() calls until *after* it has removed aKid from
|
|
|
|
// aChildArray. Any calls before then could potentially restore a stale
|
2018-01-03 16:01:03 +03:00
|
|
|
// value for our cached root element, per note in
|
2018-01-15 19:18:38 +03:00
|
|
|
// nsDocument::RemoveChildNode().
|
2017-10-23 10:04:14 +03:00
|
|
|
MOZ_ASSERT(aKid && aKid->GetParentNode() == this &&
|
2018-01-03 15:59:54 +03:00
|
|
|
aKid == GetChildAt_Deprecated(aIndex) &&
|
2018-01-23 16:30:18 +03:00
|
|
|
ComputeIndexOf(aKid) == (int32_t)aIndex, "Bogus aKid");
|
2017-10-23 10:04:14 +03:00
|
|
|
MOZ_ASSERT(!IsNodeOfType(nsINode::eATTRIBUTE));
|
2012-06-23 10:57:01 +04:00
|
|
|
|
|
|
|
nsMutationGuard::DidMutate();
|
2016-03-31 15:20:14 +03:00
|
|
|
mozAutoDocUpdate updateBatch(GetComposedDoc(), UPDATE_CONTENT_MODEL, aNotify);
|
2012-06-23 10:57:01 +04:00
|
|
|
|
|
|
|
nsIContent* previousSibling = aKid->GetPreviousSibling();
|
|
|
|
|
|
|
|
if (GetFirstChild() == aKid) {
|
|
|
|
mFirstChild = aKid->GetNextSibling();
|
|
|
|
}
|
|
|
|
|
|
|
|
aChildArray.RemoveChildAt(aIndex);
|
|
|
|
|
2017-10-23 10:04:14 +03:00
|
|
|
// Invalidate cached array of child nodes
|
|
|
|
InvalidateChildNodes();
|
|
|
|
|
2012-06-23 10:57:01 +04:00
|
|
|
if (aNotify) {
|
2017-07-27 16:49:52 +03:00
|
|
|
nsNodeUtils::ContentRemoved(this, aKid, previousSibling);
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
aKid->UnbindFromTree();
|
|
|
|
}
|
|
|
|
|
|
|
|
// When replacing, aRefChild is the content being replaced; when
|
|
|
|
// inserting it's the content before which we're inserting. In the
|
|
|
|
// latter case it may be null.
|
|
|
|
static
|
|
|
|
bool IsAllowedAsChild(nsIContent* aNewChild, nsINode* aParent,
|
|
|
|
bool aIsReplace, nsINode* aRefChild)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aNewChild, "Must have new child");
|
|
|
|
MOZ_ASSERT_IF(aIsReplace, aRefChild);
|
|
|
|
MOZ_ASSERT(aParent);
|
|
|
|
MOZ_ASSERT(aParent->IsNodeOfType(nsINode::eDOCUMENT) ||
|
|
|
|
aParent->IsNodeOfType(nsINode::eDOCUMENT_FRAGMENT) ||
|
|
|
|
aParent->IsElement(),
|
|
|
|
"Nodes that are not documents, document fragments or elements "
|
|
|
|
"can't be parents!");
|
|
|
|
|
|
|
|
// A common case is that aNewChild has no kids, in which case
|
|
|
|
// aParent can't be a descendant of aNewChild unless they're
|
|
|
|
// actually equal to each other. Fast-path that case, since aParent
|
|
|
|
// could be pretty deep in the DOM tree.
|
|
|
|
if (aNewChild == aParent ||
|
2013-03-26 11:15:23 +04:00
|
|
|
((aNewChild->GetFirstChild() ||
|
2013-12-02 14:26:11 +04:00
|
|
|
// HTML template elements and ShadowRoot hosts need
|
|
|
|
// to be checked to ensure that they are not inserted into
|
|
|
|
// the hosted content.
|
2015-03-03 14:09:00 +03:00
|
|
|
aNewChild->NodeInfo()->NameAtom() == nsGkAtoms::_template ||
|
2013-12-02 14:26:11 +04:00
|
|
|
aNewChild->GetShadowRoot()) &&
|
2013-03-26 11:15:23 +04:00
|
|
|
nsContentUtils::ContentIsHostIncludingDescendantOf(aParent,
|
|
|
|
aNewChild))) {
|
2012-06-23 10:57:01 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The allowed child nodes differ for documents and elements
|
|
|
|
switch (aNewChild->NodeType()) {
|
2018-01-30 07:10:53 +03:00
|
|
|
case nsINode::COMMENT_NODE :
|
|
|
|
case nsINode::PROCESSING_INSTRUCTION_NODE :
|
2012-06-23 10:57:01 +04:00
|
|
|
// OK in both cases
|
|
|
|
return true;
|
2018-01-30 07:10:53 +03:00
|
|
|
case nsINode::TEXT_NODE :
|
|
|
|
case nsINode::CDATA_SECTION_NODE :
|
|
|
|
case nsINode::ENTITY_REFERENCE_NODE :
|
2012-06-23 10:57:01 +04:00
|
|
|
// Allowed under Elements and DocumentFragments
|
2018-01-30 07:10:53 +03:00
|
|
|
return aParent->NodeType() != nsINode::DOCUMENT_NODE;
|
|
|
|
case nsINode::ELEMENT_NODE :
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
|
|
|
if (!aParent->IsNodeOfType(nsINode::eDOCUMENT)) {
|
|
|
|
// Always ok to have elements under other elements or document fragments
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDocument* parentDocument = static_cast<nsIDocument*>(aParent);
|
|
|
|
Element* rootElement = parentDocument->GetRootElement();
|
|
|
|
if (rootElement) {
|
|
|
|
// Already have a documentElement, so this is only OK if we're
|
|
|
|
// replacing it.
|
|
|
|
return aIsReplace && rootElement == aRefChild;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't have a documentElement yet. Our one remaining constraint is
|
|
|
|
// that the documentElement must come after the doctype.
|
|
|
|
if (!aRefChild) {
|
|
|
|
// Appending is just fine.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-12-22 12:27:27 +04:00
|
|
|
nsIContent* docTypeContent = parentDocument->GetDoctype();
|
2012-06-23 10:57:01 +04:00
|
|
|
if (!docTypeContent) {
|
|
|
|
// It's all good.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-23 16:30:18 +03:00
|
|
|
int32_t doctypeIndex = aParent->ComputeIndexOf(docTypeContent);
|
|
|
|
int32_t insertIndex = aParent->ComputeIndexOf(aRefChild);
|
2012-06-23 10:57:01 +04:00
|
|
|
|
|
|
|
// Now we're OK in the following two cases only:
|
|
|
|
// 1) We're replacing something that's not before the doctype
|
2016-03-25 13:23:53 +03:00
|
|
|
// 2) We're inserting before something that comes after the doctype
|
2012-06-23 10:57:01 +04:00
|
|
|
return aIsReplace ? (insertIndex >= doctypeIndex) :
|
|
|
|
insertIndex > doctypeIndex;
|
|
|
|
}
|
2018-01-30 07:10:53 +03:00
|
|
|
case nsINode::DOCUMENT_TYPE_NODE :
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
|
|
|
if (!aParent->IsNodeOfType(nsINode::eDOCUMENT)) {
|
|
|
|
// doctypes only allowed under documents
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDocument* parentDocument = static_cast<nsIDocument*>(aParent);
|
2012-12-22 12:27:27 +04:00
|
|
|
nsIContent* docTypeContent = parentDocument->GetDoctype();
|
2012-06-23 10:57:01 +04:00
|
|
|
if (docTypeContent) {
|
|
|
|
// Already have a doctype, so this is only OK if we're replacing it
|
|
|
|
return aIsReplace && docTypeContent == aRefChild;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't have a doctype yet. Our one remaining constraint is
|
|
|
|
// that the doctype must come before the documentElement.
|
|
|
|
Element* rootElement = parentDocument->GetRootElement();
|
|
|
|
if (!rootElement) {
|
|
|
|
// It's all good
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aRefChild) {
|
|
|
|
// Trying to append a doctype, but have a documentElement
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-23 16:30:18 +03:00
|
|
|
int32_t rootIndex = aParent->ComputeIndexOf(rootElement);
|
|
|
|
int32_t insertIndex = aParent->ComputeIndexOf(aRefChild);
|
2012-06-23 10:57:01 +04:00
|
|
|
|
|
|
|
// Now we're OK if and only if insertIndex <= rootIndex. Indeed, either
|
|
|
|
// we end up replacing aRefChild or we end up before it. Either one is
|
|
|
|
// ok as long as aRefChild is not after rootElement.
|
|
|
|
return insertIndex <= rootIndex;
|
|
|
|
}
|
2018-01-30 07:10:53 +03:00
|
|
|
case nsINode::DOCUMENT_FRAGMENT_NODE :
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
|
|
|
// Note that for now we only allow nodes inside document fragments if
|
|
|
|
// they're allowed inside elements. If we ever change this to allow
|
|
|
|
// doctype nodes in document fragments, we'll need to update this code.
|
|
|
|
// Also, there's a version of this code in ReplaceOrInsertBefore. If you
|
|
|
|
// change this code, change that too.
|
|
|
|
if (!aParent->IsNodeOfType(nsINode::eDOCUMENT)) {
|
|
|
|
// All good here
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool sawElement = false;
|
|
|
|
for (nsIContent* child = aNewChild->GetFirstChild();
|
|
|
|
child;
|
|
|
|
child = child->GetNextSibling()) {
|
|
|
|
if (child->IsElement()) {
|
|
|
|
if (sawElement) {
|
|
|
|
// Can't put two elements into a document
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sawElement = true;
|
|
|
|
}
|
|
|
|
// If we can put this content at the the right place, we might be ok;
|
|
|
|
// if not, we bail out.
|
|
|
|
if (!IsAllowedAsChild(child, aParent, aIsReplace, aRefChild)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Everything in the fragment checked out ok, so we can stick it in here
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
/*
|
|
|
|
* aNewChild is of invalid type.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-07 16:07:39 +03:00
|
|
|
void
|
|
|
|
nsINode::EnsurePreInsertionValidity(nsINode& aNewChild, nsINode* aRefChild,
|
|
|
|
ErrorResult& aError)
|
|
|
|
{
|
|
|
|
EnsurePreInsertionValidity1(aNewChild, aRefChild, aError);
|
|
|
|
if (aError.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
EnsurePreInsertionValidity2(false, aNewChild, aRefChild, aError);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsINode::EnsurePreInsertionValidity1(nsINode& aNewChild, nsINode* aRefChild,
|
|
|
|
ErrorResult& aError)
|
|
|
|
{
|
|
|
|
if ((!IsNodeOfType(eDOCUMENT) &&
|
|
|
|
!IsNodeOfType(eDOCUMENT_FRAGMENT) &&
|
|
|
|
!IsElement()) ||
|
2017-10-25 18:19:11 +03:00
|
|
|
!aNewChild.IsContent()) {
|
2015-10-07 16:07:39 +03:00
|
|
|
aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsINode::EnsurePreInsertionValidity2(bool aReplace, nsINode& aNewChild,
|
|
|
|
nsINode* aRefChild, ErrorResult& aError)
|
|
|
|
{
|
|
|
|
nsIContent* newContent = aNewChild.AsContent();
|
|
|
|
if (newContent->IsRootOfAnonymousSubtree()) {
|
|
|
|
// This is anonymous content. Don't allow its insertion
|
|
|
|
// anywhere, since it might have UnbindFromTree calls coming
|
|
|
|
// its way.
|
|
|
|
aError.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that the inserted node is allowed as a child of its new parent.
|
|
|
|
if (!IsAllowedAsChild(newContent, this, aReplace, aRefChild)) {
|
|
|
|
aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
nsINode*
|
2012-06-23 10:57:01 +04:00
|
|
|
nsINode::ReplaceOrInsertBefore(bool aReplace, nsINode* aNewChild,
|
2012-10-09 16:31:24 +04:00
|
|
|
nsINode* aRefChild, ErrorResult& aError)
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
|
|
|
// XXXbz I wish I could assert that nsContentUtils::IsSafeToRunScript() so we
|
|
|
|
// could rely on scriptblockers going out of scope to actually run XBL
|
|
|
|
// teardown, but various crud adds nodes under scriptblockers (e.g. native
|
|
|
|
// anonymous content). The only good news is those insertions can't trigger
|
|
|
|
// the bad XBL cases.
|
2012-10-09 16:31:24 +04:00
|
|
|
MOZ_ASSERT_IF(aReplace, aRefChild);
|
2012-06-23 10:57:01 +04:00
|
|
|
|
2015-10-07 16:07:39 +03:00
|
|
|
EnsurePreInsertionValidity1(*aNewChild, aRefChild, aError);
|
|
|
|
if (aError.Failed()) {
|
2012-10-09 16:31:24 +04:00
|
|
|
return nullptr;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t nodeType = aNewChild->NodeType();
|
2012-06-23 10:57:01 +04:00
|
|
|
|
|
|
|
// Before we do anything else, fire all DOMNodeRemoved mutation events
|
|
|
|
// We do this up front as to avoid having to deal with script running
|
|
|
|
// at random places further down.
|
|
|
|
// Scope firing mutation events so that we don't carry any state that
|
|
|
|
// might be stale
|
|
|
|
{
|
2018-01-23 16:30:18 +03:00
|
|
|
// This check happens again further down (though then using
|
|
|
|
// ComputeIndexOf).
|
2012-06-23 10:57:01 +04:00
|
|
|
// We're only checking this here to avoid firing mutation events when
|
|
|
|
// none should be fired.
|
|
|
|
// It's ok that we do the check twice in the case when firing mutation
|
|
|
|
// events as we need to recheck after running script anyway.
|
2012-10-09 16:31:24 +04:00
|
|
|
if (aRefChild && aRefChild->GetParentNode() != this) {
|
|
|
|
aError.Throw(NS_ERROR_DOM_NOT_FOUND_ERR);
|
|
|
|
return nullptr;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we're replacing, fire for node-to-be-replaced.
|
|
|
|
// If aRefChild == aNewChild then we'll fire for it in check below
|
|
|
|
if (aReplace && aRefChild != aNewChild) {
|
|
|
|
nsContentUtils::MaybeFireNodeRemoved(aRefChild, this, OwnerDoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the new node already has a parent, fire for removing from old
|
|
|
|
// parent
|
2012-10-09 16:31:24 +04:00
|
|
|
nsINode* oldParent = aNewChild->GetParentNode();
|
2012-06-23 10:57:01 +04:00
|
|
|
if (oldParent) {
|
|
|
|
nsContentUtils::MaybeFireNodeRemoved(aNewChild, oldParent,
|
|
|
|
aNewChild->OwnerDoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're inserting a fragment, fire for all the children of the
|
|
|
|
// fragment
|
2018-01-30 07:10:53 +03:00
|
|
|
if (nodeType == DOCUMENT_FRAGMENT_NODE) {
|
2012-11-15 02:10:08 +04:00
|
|
|
static_cast<FragmentOrElement*>(aNewChild)->FireNodeRemovedForChildren();
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
2012-09-20 20:13:09 +04:00
|
|
|
// Verify that our aRefChild is still sensible
|
2012-10-09 16:31:24 +04:00
|
|
|
if (aRefChild && aRefChild->GetParentNode() != this) {
|
|
|
|
aError.Throw(NS_ERROR_DOM_NOT_FOUND_ERR);
|
|
|
|
return nullptr;
|
2012-09-20 20:13:09 +04:00
|
|
|
}
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
2015-10-07 16:07:39 +03:00
|
|
|
EnsurePreInsertionValidity2(aReplace, *aNewChild, aRefChild, aError);
|
|
|
|
if (aError.Failed()) {
|
2012-10-09 16:31:24 +04:00
|
|
|
return nullptr;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Record the node to insert before, if any
|
|
|
|
nsINode* nodeToInsertBefore;
|
|
|
|
if (aReplace) {
|
|
|
|
nodeToInsertBefore = aRefChild->GetNextSibling();
|
|
|
|
} else {
|
|
|
|
nodeToInsertBefore = aRefChild;
|
|
|
|
}
|
|
|
|
if (nodeToInsertBefore == aNewChild) {
|
|
|
|
// We're going to remove aNewChild from its parent, so use its next sibling
|
|
|
|
// as the node to insert before.
|
|
|
|
nodeToInsertBefore = nodeToInsertBefore->GetNextSibling();
|
|
|
|
}
|
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
Maybe<AutoTArray<nsCOMPtr<nsIContent>, 50> > fragChildren;
|
2012-06-23 10:57:01 +04:00
|
|
|
|
|
|
|
// Remove the new child from the old parent if one exists
|
2015-10-07 16:07:39 +03:00
|
|
|
nsIContent* newContent = aNewChild->AsContent();
|
2012-10-09 16:31:24 +04:00
|
|
|
nsCOMPtr<nsINode> oldParent = newContent->GetParentNode();
|
2012-06-23 10:57:01 +04:00
|
|
|
if (oldParent) {
|
2018-01-23 16:30:18 +03:00
|
|
|
int32_t removeIndex = oldParent->ComputeIndexOf(newContent);
|
2012-06-23 10:57:01 +04:00
|
|
|
if (removeIndex < 0) {
|
|
|
|
// newContent is anonymous. We can't deal with this, so just bail
|
|
|
|
NS_ERROR("How come our flags didn't catch this?");
|
2012-10-09 16:31:24 +04:00
|
|
|
aError.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return nullptr;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hold a strong ref to nodeToInsertBefore across the removal of newContent
|
|
|
|
nsCOMPtr<nsINode> kungFuDeathGrip = nodeToInsertBefore;
|
|
|
|
|
|
|
|
// Removing a child can run script, via XBL destructors.
|
|
|
|
nsMutationGuard guard;
|
|
|
|
|
|
|
|
// Scope for the mutation batch and scriptblocker, so they go away
|
|
|
|
// while kungFuDeathGrip is still alive.
|
|
|
|
{
|
2014-10-02 22:45:44 +04:00
|
|
|
mozAutoDocUpdate batch(newContent->GetComposedDoc(),
|
2012-06-23 10:57:01 +04:00
|
|
|
UPDATE_CONTENT_MODEL, true);
|
|
|
|
nsAutoMutationBatch mb(oldParent, true, true);
|
2018-01-03 16:01:03 +03:00
|
|
|
oldParent->RemoveChildAt_Deprecated(removeIndex, true);
|
2012-06-23 10:57:01 +04:00
|
|
|
if (nsAutoMutationBatch::GetCurrentBatch() == &mb) {
|
|
|
|
mb.RemovalDone();
|
2018-01-03 15:59:54 +03:00
|
|
|
mb.SetPrevSibling(oldParent->GetChildAt_Deprecated(removeIndex - 1));
|
|
|
|
mb.SetNextSibling(oldParent->GetChildAt_Deprecated(removeIndex));
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect one mutation (the removal) to have happened.
|
|
|
|
if (guard.Mutated(1)) {
|
|
|
|
// XBL destructors, yuck.
|
2016-03-25 13:23:53 +03:00
|
|
|
|
2012-06-23 10:57:01 +04:00
|
|
|
// Verify that nodeToInsertBefore, if non-null, is still our child. If
|
|
|
|
// it's not, there's no way we can do this insert sanely; just bail out.
|
|
|
|
if (nodeToInsertBefore && nodeToInsertBefore->GetParent() != this) {
|
2012-10-09 16:31:24 +04:00
|
|
|
aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR);
|
|
|
|
return nullptr;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that newContent has no parent.
|
2013-08-21 20:08:23 +04:00
|
|
|
if (newContent->GetParentNode()) {
|
2012-10-09 16:31:24 +04:00
|
|
|
aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR);
|
|
|
|
return nullptr;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// And verify that newContent is still allowed as our child.
|
|
|
|
if (aNewChild == aRefChild) {
|
|
|
|
// We've already removed aRefChild. So even if we were doing a replace,
|
|
|
|
// now we're doing a simple insert before nodeToInsertBefore.
|
|
|
|
if (!IsAllowedAsChild(newContent, this, false, nodeToInsertBefore)) {
|
2012-10-09 16:31:24 +04:00
|
|
|
aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR);
|
|
|
|
return nullptr;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ((aRefChild && aRefChild->GetParent() != this) ||
|
|
|
|
!IsAllowedAsChild(newContent, this, aReplace, aRefChild)) {
|
2012-10-09 16:31:24 +04:00
|
|
|
aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR);
|
|
|
|
return nullptr;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
// And recompute nodeToInsertBefore, just in case.
|
|
|
|
if (aReplace) {
|
|
|
|
nodeToInsertBefore = aRefChild->GetNextSibling();
|
|
|
|
} else {
|
|
|
|
nodeToInsertBefore = aRefChild;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-30 07:10:53 +03:00
|
|
|
} else if (nodeType == DOCUMENT_FRAGMENT_NODE) {
|
2012-06-23 10:57:01 +04:00
|
|
|
// Make sure to remove all the fragment's kids. We need to do this before
|
|
|
|
// we start inserting anything, so we will run out XBL destructors and
|
|
|
|
// binding teardown (GOD, I HATE THESE THINGS) before we insert anything
|
|
|
|
// into the DOM.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count = newContent->GetChildCount();
|
2012-06-23 10:57:01 +04:00
|
|
|
|
2014-08-14 02:39:41 +04:00
|
|
|
fragChildren.emplace();
|
2012-06-23 10:57:01 +04:00
|
|
|
|
|
|
|
// Copy the children into a separate array to avoid having to deal with
|
|
|
|
// mutations to the fragment later on here.
|
2014-08-14 02:39:41 +04:00
|
|
|
fragChildren->SetCapacity(count);
|
2012-06-23 10:57:01 +04:00
|
|
|
for (nsIContent* child = newContent->GetFirstChild();
|
|
|
|
child;
|
|
|
|
child = child->GetNextSibling()) {
|
2014-10-02 22:45:44 +04:00
|
|
|
NS_ASSERTION(child->GetComposedDoc() == nullptr,
|
2012-06-23 10:57:01 +04:00
|
|
|
"How did we get a child with a current doc?");
|
2014-08-14 02:39:41 +04:00
|
|
|
fragChildren->AppendElement(child);
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hold a strong ref to nodeToInsertBefore across the removals
|
|
|
|
nsCOMPtr<nsINode> kungFuDeathGrip = nodeToInsertBefore;
|
|
|
|
|
|
|
|
nsMutationGuard guard;
|
|
|
|
|
|
|
|
// Scope for the mutation batch and scriptblocker, so they go away
|
|
|
|
// while kungFuDeathGrip is still alive.
|
|
|
|
{
|
2014-10-02 22:45:44 +04:00
|
|
|
mozAutoDocUpdate batch(newContent->GetComposedDoc(),
|
2012-06-23 10:57:01 +04:00
|
|
|
UPDATE_CONTENT_MODEL, true);
|
|
|
|
nsAutoMutationBatch mb(newContent, false, true);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = count; i > 0;) {
|
2018-01-03 16:01:03 +03:00
|
|
|
newContent->RemoveChildAt_Deprecated(--i, true);
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect |count| removals
|
|
|
|
if (guard.Mutated(count)) {
|
|
|
|
// XBL destructors, yuck.
|
2016-03-25 13:23:53 +03:00
|
|
|
|
2012-06-23 10:57:01 +04:00
|
|
|
// Verify that nodeToInsertBefore, if non-null, is still our child. If
|
|
|
|
// it's not, there's no way we can do this insert sanely; just bail out.
|
|
|
|
if (nodeToInsertBefore && nodeToInsertBefore->GetParent() != this) {
|
2012-10-09 16:31:24 +04:00
|
|
|
aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR);
|
|
|
|
return nullptr;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that all the things in fragChildren have no parent.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2014-08-14 02:39:41 +04:00
|
|
|
if (fragChildren->ElementAt(i)->GetParentNode()) {
|
2012-10-09 16:31:24 +04:00
|
|
|
aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR);
|
|
|
|
return nullptr;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that unlike the single-element case above, none of our kids can
|
|
|
|
// be aRefChild, so we can always pass through aReplace in the
|
|
|
|
// IsAllowedAsChild checks below and don't have to worry about whether
|
|
|
|
// recomputing nodeToInsertBefore is OK.
|
|
|
|
|
|
|
|
// Verify that our aRefChild is still sensible
|
|
|
|
if (aRefChild && aRefChild->GetParent() != this) {
|
2012-10-09 16:31:24 +04:00
|
|
|
aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR);
|
|
|
|
return nullptr;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Recompute nodeToInsertBefore, just in case.
|
|
|
|
if (aReplace) {
|
|
|
|
nodeToInsertBefore = aRefChild->GetNextSibling();
|
|
|
|
} else {
|
|
|
|
nodeToInsertBefore = aRefChild;
|
2016-03-25 13:23:53 +03:00
|
|
|
}
|
2012-06-23 10:57:01 +04:00
|
|
|
|
|
|
|
// And verify that newContent is still allowed as our child. Sadly, we
|
|
|
|
// need to reimplement the relevant part of IsAllowedAsChild() because
|
|
|
|
// now our nodes are in an array and all. If you change this code,
|
|
|
|
// change the code there.
|
|
|
|
if (IsNodeOfType(nsINode::eDOCUMENT)) {
|
|
|
|
bool sawElement = false;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2014-08-14 02:39:41 +04:00
|
|
|
nsIContent* child = fragChildren->ElementAt(i);
|
2012-06-23 10:57:01 +04:00
|
|
|
if (child->IsElement()) {
|
|
|
|
if (sawElement) {
|
|
|
|
// No good
|
2012-10-09 16:31:24 +04:00
|
|
|
aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR);
|
|
|
|
return nullptr;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
sawElement = true;
|
|
|
|
}
|
|
|
|
if (!IsAllowedAsChild(child, this, aReplace, aRefChild)) {
|
2012-10-09 16:31:24 +04:00
|
|
|
aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR);
|
|
|
|
return nullptr;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 15:20:14 +03:00
|
|
|
mozAutoDocUpdate batch(GetComposedDoc(), UPDATE_CONTENT_MODEL, true);
|
2012-06-23 10:57:01 +04:00
|
|
|
nsAutoMutationBatch mb;
|
|
|
|
|
|
|
|
// Figure out which index we want to insert at. Note that we use
|
|
|
|
// nodeToInsertBefore to determine this, because it's possible that
|
|
|
|
// aRefChild == aNewChild, in which case we just removed it from the
|
|
|
|
// parent list.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t insPos;
|
2012-06-23 10:57:01 +04:00
|
|
|
if (nodeToInsertBefore) {
|
2018-01-23 16:30:18 +03:00
|
|
|
insPos = ComputeIndexOf(nodeToInsertBefore);
|
2012-06-23 10:57:01 +04:00
|
|
|
if (insPos < 0) {
|
|
|
|
// XXXbz How the heck would _that_ happen, exactly?
|
2012-10-09 16:31:24 +04:00
|
|
|
aError.Throw(NS_ERROR_DOM_NOT_FOUND_ERR);
|
|
|
|
return nullptr;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
insPos = GetChildCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're replacing and we haven't removed aRefChild yet, do so now
|
|
|
|
if (aReplace && aRefChild != aNewChild) {
|
|
|
|
mb.Init(this, true, true);
|
|
|
|
|
|
|
|
// Since aRefChild is never null in the aReplace case, we know that at
|
|
|
|
// this point nodeToInsertBefore is the next sibling of aRefChild.
|
|
|
|
NS_ASSERTION(aRefChild->GetNextSibling() == nodeToInsertBefore,
|
|
|
|
"Unexpected nodeToInsertBefore");
|
|
|
|
|
|
|
|
// An since nodeToInsertBefore is at index insPos, we want to remove
|
|
|
|
// at the previous index.
|
|
|
|
NS_ASSERTION(insPos >= 1, "insPos too small");
|
2018-01-03 16:01:03 +03:00
|
|
|
RemoveChildAt_Deprecated(insPos-1, true);
|
2012-06-23 10:57:01 +04:00
|
|
|
--insPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move new child over to our document if needed. Do this after removing
|
|
|
|
// it from its parent so that AdoptNode doesn't fire DOMNodeRemoved
|
|
|
|
// DocumentType nodes are the only nodes that can have a null
|
|
|
|
// ownerDocument according to the DOM spec, and we need to allow
|
|
|
|
// inserting them w/o calling AdoptNode().
|
2015-10-07 16:07:39 +03:00
|
|
|
nsIDocument* doc = OwnerDoc();
|
2013-08-16 01:39:28 +04:00
|
|
|
if (doc != newContent->OwnerDoc()) {
|
2017-09-13 20:34:55 +03:00
|
|
|
AdoptNodeIntoOwnerDoc(this, aNewChild, aError);
|
2012-10-09 16:31:24 +04:00
|
|
|
if (aError.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-08-16 01:39:28 +04:00
|
|
|
} else if (doc->DidDocumentOpen()) {
|
2017-09-13 20:34:55 +03:00
|
|
|
CheckForOutdatedParent(this, aNewChild, aError);
|
2013-08-16 01:39:28 +04:00
|
|
|
if (aError.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if we're inserting a document fragment. If we are, we need
|
|
|
|
* to actually add its children individually (i.e. we don't add the
|
|
|
|
* actual document fragment).
|
|
|
|
*/
|
2012-10-09 16:31:24 +04:00
|
|
|
nsINode* result = aReplace ? aRefChild : aNewChild;
|
2018-01-30 07:10:53 +03:00
|
|
|
if (nodeType == DOCUMENT_FRAGMENT_NODE) {
|
2012-06-23 10:57:01 +04:00
|
|
|
if (!aReplace) {
|
|
|
|
mb.Init(this, true, true);
|
|
|
|
}
|
|
|
|
nsAutoMutationBatch* mutationBatch = nsAutoMutationBatch::GetCurrentBatch();
|
|
|
|
if (mutationBatch) {
|
|
|
|
mutationBatch->RemovalDone();
|
2018-01-03 15:59:54 +03:00
|
|
|
mutationBatch->SetPrevSibling(GetChildAt_Deprecated(insPos - 1));
|
|
|
|
mutationBatch->SetNextSibling(GetChildAt_Deprecated(insPos));
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
2014-08-14 02:39:41 +04:00
|
|
|
uint32_t count = fragChildren->Length();
|
2012-10-12 03:01:40 +04:00
|
|
|
if (!count) {
|
2012-10-09 16:31:24 +04:00
|
|
|
return result;
|
2012-10-12 03:01:40 +04:00
|
|
|
}
|
|
|
|
|
2012-06-23 10:57:01 +04:00
|
|
|
bool appending =
|
2012-08-22 19:56:38 +04:00
|
|
|
!IsNodeOfType(eDOCUMENT) && uint32_t(insPos) == GetChildCount();
|
2014-08-14 02:39:41 +04:00
|
|
|
nsIContent* firstInsertedContent = fragChildren->ElementAt(0);
|
2012-06-23 10:57:01 +04:00
|
|
|
|
|
|
|
// Iterate through the fragment's children, and insert them in the new
|
|
|
|
// parent
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < count; ++i, ++insPos) {
|
2012-06-23 10:57:01 +04:00
|
|
|
// XXXbz how come no reparenting here? That seems odd...
|
|
|
|
// Insert the child.
|
2018-01-23 16:30:18 +03:00
|
|
|
aError = InsertChildAt_Deprecated(fragChildren->ElementAt(i), insPos,
|
|
|
|
!appending);
|
2012-10-09 16:31:24 +04:00
|
|
|
if (aError.Failed()) {
|
2012-06-23 10:57:01 +04:00
|
|
|
// Make sure to notify on any children that we did succeed to insert
|
|
|
|
if (appending && i != 0) {
|
|
|
|
nsNodeUtils::ContentAppended(static_cast<nsIContent*>(this),
|
2017-07-27 16:49:52 +03:00
|
|
|
firstInsertedContent);
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
2012-10-09 16:31:24 +04:00
|
|
|
return nullptr;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mutationBatch && !appending) {
|
|
|
|
mutationBatch->NodesAdded();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify and fire mutation events when appending
|
|
|
|
if (appending) {
|
|
|
|
nsNodeUtils::ContentAppended(static_cast<nsIContent*>(this),
|
2017-07-27 16:49:52 +03:00
|
|
|
firstInsertedContent);
|
2012-06-23 10:57:01 +04:00
|
|
|
if (mutationBatch) {
|
|
|
|
mutationBatch->NodesAdded();
|
|
|
|
}
|
|
|
|
// Optimize for the case when there are no listeners
|
|
|
|
if (nsContentUtils::
|
|
|
|
HasMutationListeners(doc, NS_EVENT_BITS_MUTATION_NODEINSERTED)) {
|
2014-08-14 02:39:41 +04:00
|
|
|
Element::FireNodeInserted(doc, this, *fragChildren);
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Not inserting a fragment but rather a single node.
|
|
|
|
|
|
|
|
// FIXME https://bugzilla.mozilla.org/show_bug.cgi?id=544654
|
|
|
|
// We need to reparent here for nodes for which the parent of their
|
|
|
|
// wrapper is not the wrapper for their ownerDocument (XUL elements,
|
|
|
|
// form controls, ...). Also applies in the fragment code above.
|
|
|
|
|
|
|
|
if (nsAutoMutationBatch::GetCurrentBatch() == &mb) {
|
|
|
|
mb.RemovalDone();
|
2018-01-03 15:59:54 +03:00
|
|
|
mb.SetPrevSibling(GetChildAt_Deprecated(insPos - 1));
|
|
|
|
mb.SetNextSibling(GetChildAt_Deprecated(insPos));
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
2018-01-23 16:30:18 +03:00
|
|
|
aError = InsertChildAt_Deprecated(newContent, insPos, true);
|
2012-10-09 16:31:24 +04:00
|
|
|
if (aError.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
return result;
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsINode::BindObject(nsISupports* aObject)
|
|
|
|
{
|
|
|
|
nsCOMArray<nsISupports>* objects =
|
|
|
|
static_cast<nsCOMArray<nsISupports>*>(GetProperty(nsGkAtoms::keepobjectsalive));
|
|
|
|
if (!objects) {
|
|
|
|
objects = new nsCOMArray<nsISupports>();
|
2014-03-25 17:25:47 +04:00
|
|
|
SetProperty(nsGkAtoms::keepobjectsalive, objects,
|
|
|
|
nsINode::DeleteProperty< nsCOMArray<nsISupports> >, true);
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
objects->AppendObject(aObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsINode::UnbindObject(nsISupports* aObject)
|
|
|
|
{
|
|
|
|
nsCOMArray<nsISupports>* objects =
|
|
|
|
static_cast<nsCOMArray<nsISupports>*>(GetProperty(nsGkAtoms::keepobjectsalive));
|
|
|
|
if (objects) {
|
|
|
|
objects->RemoveObject(aObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-11 23:43:01 +04:00
|
|
|
void
|
2015-10-18 08:24:48 +03:00
|
|
|
nsINode::GetBoundMutationObservers(nsTArray<RefPtr<nsDOMMutationObserver> >& aResult)
|
2013-09-11 23:43:01 +04:00
|
|
|
{
|
|
|
|
nsCOMArray<nsISupports>* objects =
|
|
|
|
static_cast<nsCOMArray<nsISupports>*>(GetProperty(nsGkAtoms::keepobjectsalive));
|
|
|
|
if (objects) {
|
|
|
|
for (int32_t i = 0; i < objects->Count(); ++i) {
|
|
|
|
nsCOMPtr<nsDOMMutationObserver> mo = do_QueryInterface(objects->ObjectAt(i));
|
|
|
|
if (mo) {
|
|
|
|
MOZ_ASSERT(!aResult.Contains(mo));
|
2014-03-15 21:56:00 +04:00
|
|
|
aResult.AppendElement(mo.forget());
|
2013-09-11 23:43:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-23 21:15:15 +03:00
|
|
|
already_AddRefed<AccessibleNode>
|
|
|
|
nsINode::GetAccessibleNode()
|
|
|
|
{
|
|
|
|
#ifdef ACCESSIBILITY
|
|
|
|
RefPtr<AccessibleNode> anode = new AccessibleNode(this);
|
|
|
|
return anode.forget();
|
2017-10-26 12:40:12 +03:00
|
|
|
#else
|
2016-08-23 21:15:15 +03:00
|
|
|
return nullptr;
|
2017-10-26 12:40:12 +03:00
|
|
|
#endif
|
2016-08-23 21:15:15 +03:00
|
|
|
}
|
|
|
|
|
Bug 1387956 - Overhaul ComputedValues measurement, and add style structs measurement. r=bholley.
This patch moves measurement of ComputedValues objects from Rust to C++.
Measurement now happens (a) via DOM elements and (b) remaining elements via
the frame tree. Likewise for the style structs hanging off ComputedValues
objects.
Here is an example of the output.
> ├──27,600,448 B (26.49%) -- active/window(https://en.wikipedia.org/wiki/Barack_Obama)
> │ ├──12,772,544 B (12.26%) -- layout
> │ │ ├───4,483,744 B (04.30%) -- frames
> │ │ │ ├──1,653,552 B (01.59%) ── nsInlineFrame
> │ │ │ ├──1,415,760 B (01.36%) ── nsTextFrame
> │ │ │ ├────431,376 B (00.41%) ── nsBlockFrame
> │ │ │ ├────340,560 B (00.33%) ── nsHTMLScrollFrame
> │ │ │ ├────302,544 B (00.29%) ── nsContinuingTextFrame
> │ │ │ ├────156,408 B (00.15%) ── nsBulletFrame
> │ │ │ ├─────73,024 B (00.07%) ── nsPlaceholderFrame
> │ │ │ ├─────27,656 B (00.03%) ── sundries
> │ │ │ ├─────23,520 B (00.02%) ── nsTableCellFrame
> │ │ │ ├─────16,704 B (00.02%) ── nsImageFrame
> │ │ │ ├─────15,488 B (00.01%) ── nsTableRowFrame
> │ │ │ ├─────13,776 B (00.01%) ── nsTableColFrame
> │ │ │ └─────13,376 B (00.01%) ── nsTableFrame
> │ │ ├───3,412,192 B (03.28%) -- servo-style-structs
> │ │ │ ├──1,288,224 B (01.24%) ── Display
> │ │ │ ├────742,400 B (00.71%) ── Position
> │ │ │ ├────308,736 B (00.30%) ── Font
> │ │ │ ├────226,512 B (00.22%) ── Background
> │ │ │ ├────218,304 B (00.21%) ── TextReset
> │ │ │ ├────214,896 B (00.21%) ── Text
> │ │ │ ├────130,560 B (00.13%) ── Border
> │ │ │ ├─────81,408 B (00.08%) ── UIReset
> │ │ │ ├─────61,440 B (00.06%) ── Padding
> │ │ │ ├─────38,176 B (00.04%) ── UserInterface
> │ │ │ ├─────29,232 B (00.03%) ── Margin
> │ │ │ ├─────21,824 B (00.02%) ── sundries
> │ │ │ ├─────20,080 B (00.02%) ── Color
> │ │ │ ├─────20,080 B (00.02%) ── Column
> │ │ │ └─────10,320 B (00.01%) ── Effects
> │ │ ├───2,227,680 B (02.14%) -- computed-values
> │ │ │ ├──1,182,928 B (01.14%) ── non-dom
> │ │ │ └──1,044,752 B (01.00%) ── dom
> │ │ ├───1,500,016 B (01.44%) ── text-runs
> │ │ ├─────492,640 B (00.47%) ── line-boxes
> │ │ ├─────326,688 B (00.31%) ── frame-properties
> │ │ ├─────301,760 B (00.29%) ── pres-shell
> │ │ ├──────27,648 B (00.03%) ── pres-contexts
> │ │ └─────────176 B (00.00%) ── style-sets
The 'servo-style-structs' and 'computed-values' sub-trees are new. (Prior to
this patch, ComputedValues under DOM elements were tallied under the the
'dom/element-nodes' sub-tree, and ComputedValues not under DOM element were
ignored.) 'servo-style-structs/sundries' aggregates all the style structs that
are smaller than 8 KiB.
Other notable things done by the patch are as follows.
- It significantly changes the signatures of the methods measuring nsINode and
its subclasses, in order to handle the tallying of style structs separately
from element-nodes. Likewise for nsIFrame.
- It renames the 'layout/style-structs' sub-tree as
'layout/gecko-style-structs', to clearly distinguish it from the new
'layout/servo-style-structs' sub-tree.
- It adds some FFI functions to access various Rust-side data structures from
C++ code.
- There is a nasty hack used twice to measure Arcs, by stepping backwards from
an interior pointer to a base pointer. It works, but I want to replace it
with something better eventually. The "XXX WARNING" comments have details.
- It makes DMD print a line to the console if it sees a pointer it doesn't
recognise. This is useful for detecting when we are measuring an interior
pointer instead of a base pointer, which is bad but easy to do when Arcs are
involved.
- It removes the Rust code for measuring CVs, because it's now all done on the
C++ side.
MozReview-Commit-ID: BKebACLKtCi
--HG--
extra : rebase_source : 4d9a8c6b198a0ff025b811759a6bfa9f33a260ba
2017-08-11 09:37:33 +03:00
|
|
|
void
|
2017-08-25 07:47:54 +03:00
|
|
|
nsINode::AddSizeOfExcludingThis(nsWindowSizes& aSizes, size_t* aNodeSize) const
|
2012-06-23 10:57:01 +04:00
|
|
|
{
|
2014-03-17 10:56:53 +04:00
|
|
|
EventListenerManager* elm = GetExistingListenerManager();
|
2012-06-23 10:57:01 +04:00
|
|
|
if (elm) {
|
2017-08-25 07:47:54 +03:00
|
|
|
*aNodeSize += elm->SizeOfIncludingThis(aSizes.mState.mMallocSizeOf);
|
2012-06-23 10:57:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Measurement of the following members may be added later if DMD finds it is
|
|
|
|
// worthwhile:
|
2013-03-01 06:53:49 +04:00
|
|
|
// - mNodeInfo
|
2012-06-23 10:57:01 +04:00
|
|
|
// - mSlots
|
|
|
|
//
|
|
|
|
// The following members are not measured:
|
|
|
|
// - mParent, mNextSibling, mPreviousSibling, mFirstChild: because they're
|
|
|
|
// non-owning
|
|
|
|
}
|
|
|
|
|
2012-11-13 17:37:39 +04:00
|
|
|
#define EVENT(name_, id_, type_, struct_) \
|
|
|
|
EventHandlerNonNull* nsINode::GetOn##name_() { \
|
2014-03-17 10:56:53 +04:00
|
|
|
EventListenerManager *elm = GetExistingListenerManager(); \
|
2013-08-16 14:06:24 +04:00
|
|
|
return elm ? elm->GetEventHandler(nsGkAtoms::on##name_, EmptyString()) \
|
|
|
|
: nullptr; \
|
2012-11-13 17:37:39 +04:00
|
|
|
} \
|
2013-09-17 15:01:28 +04:00
|
|
|
void nsINode::SetOn##name_(EventHandlerNonNull* handler) \
|
|
|
|
{ \
|
2014-03-17 10:56:53 +04:00
|
|
|
EventListenerManager *elm = GetOrCreateListenerManager(); \
|
2012-11-13 17:37:39 +04:00
|
|
|
if (elm) { \
|
2013-09-17 15:01:28 +04:00
|
|
|
elm->SetEventHandler(nsGkAtoms::on##name_, EmptyString(), handler); \
|
2012-11-13 17:37:39 +04:00
|
|
|
} \
|
2012-11-09 20:00:25 +04:00
|
|
|
}
|
2012-06-23 10:57:01 +04:00
|
|
|
#define TOUCH_EVENT EVENT
|
|
|
|
#define DOCUMENT_ONLY_EVENT EVENT
|
2014-04-01 15:42:12 +04:00
|
|
|
#include "mozilla/EventNameList.h"
|
2012-06-23 10:57:01 +04:00
|
|
|
#undef DOCUMENT_ONLY_EVENT
|
|
|
|
#undef TOUCH_EVENT
|
|
|
|
#undef EVENT
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsINode::Contains(const nsINode* aOther) const
|
|
|
|
{
|
|
|
|
if (aOther == this) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!aOther ||
|
|
|
|
OwnerDoc() != aOther->OwnerDoc() ||
|
2016-03-31 13:58:25 +03:00
|
|
|
IsInUncomposedDoc() != aOther->IsInUncomposedDoc() ||
|
2017-10-25 18:19:11 +03:00
|
|
|
!aOther->IsContent() ||
|
2012-06-23 10:57:01 +04:00
|
|
|
!GetFirstChild()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsIContent* other = static_cast<const nsIContent*>(aOther);
|
|
|
|
if (this == OwnerDoc()) {
|
|
|
|
// document.contains(aOther) returns true if aOther is in the document,
|
|
|
|
// but is not in any anonymous subtree.
|
2016-03-31 13:58:25 +03:00
|
|
|
// IsInUncomposedDoc() check is done already before this.
|
2012-06-23 10:57:01 +04:00
|
|
|
return !other->IsInAnonymousSubtree();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsElement() && !IsNodeOfType(nsINode::eDOCUMENT_FRAGMENT)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsIContent* thisContent = static_cast<const nsIContent*>(this);
|
|
|
|
if (thisContent->GetBindingParent() != other->GetBindingParent()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsContentUtils::ContentIsDescendantOf(other, this);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
2012-06-23 10:57:01 +04:00
|
|
|
nsINode::Length() const
|
|
|
|
{
|
|
|
|
switch (NodeType()) {
|
2018-01-30 07:10:53 +03:00
|
|
|
case DOCUMENT_TYPE_NODE:
|
2012-06-23 10:57:01 +04:00
|
|
|
return 0;
|
|
|
|
|
2018-01-30 07:10:53 +03:00
|
|
|
case TEXT_NODE:
|
|
|
|
case CDATA_SECTION_NODE:
|
|
|
|
case PROCESSING_INSTRUCTION_NODE:
|
|
|
|
case COMMENT_NODE:
|
2017-10-25 18:19:11 +03:00
|
|
|
MOZ_ASSERT(IsContent());
|
|
|
|
return AsContent()->TextLength();
|
2012-06-23 10:57:01 +04:00
|
|
|
|
|
|
|
default:
|
|
|
|
return GetChildCount();
|
|
|
|
}
|
|
|
|
}
|
2012-08-30 21:10:13 +04:00
|
|
|
|
2017-10-02 19:14:15 +03:00
|
|
|
const RawServoSelectorList*
|
|
|
|
nsINode::ParseServoSelectorList(
|
|
|
|
const nsAString& aSelectorString,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsIDocument* doc = OwnerDoc();
|
|
|
|
MOZ_ASSERT(doc->IsStyledByServo());
|
|
|
|
|
2017-10-12 11:07:25 +03:00
|
|
|
nsIDocument::SelectorCache& cache =
|
|
|
|
doc->GetSelectorCache(mozilla::StyleBackendType::Servo);
|
2017-10-02 19:14:15 +03:00
|
|
|
nsIDocument::SelectorCache::SelectorList* list =
|
|
|
|
cache.GetList(aSelectorString);
|
|
|
|
if (list) {
|
|
|
|
if (!*list) {
|
|
|
|
// Invalid selector.
|
|
|
|
aRv.ThrowDOMException(NS_ERROR_DOM_SYNTAX_ERR,
|
|
|
|
NS_LITERAL_CSTRING("'") + NS_ConvertUTF16toUTF8(aSelectorString) +
|
|
|
|
NS_LITERAL_CSTRING("' is not a valid selector")
|
|
|
|
);
|
2017-10-06 09:16:13 +03:00
|
|
|
return nullptr;
|
2017-10-02 19:14:15 +03:00
|
|
|
}
|
|
|
|
|
2017-10-12 11:07:25 +03:00
|
|
|
return list->AsServo();
|
2017-10-02 19:14:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 selectorString(aSelectorString);
|
|
|
|
|
|
|
|
auto* selectorList = Servo_SelectorList_Parse(&selectorString);
|
|
|
|
if (!selectorList) {
|
|
|
|
aRv.ThrowDOMException(NS_ERROR_DOM_SYNTAX_ERR,
|
|
|
|
NS_LITERAL_CSTRING("'") + selectorString +
|
|
|
|
NS_LITERAL_CSTRING("' is not a valid selector")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
cache.CacheList(aSelectorString, UniquePtr<RawServoSelectorList>(selectorList));
|
|
|
|
return selectorList;
|
|
|
|
}
|
|
|
|
|
2018-02-01 07:04:04 +03:00
|
|
|
#ifdef MOZ_OLD_STYLE
|
2013-12-16 22:06:36 +04:00
|
|
|
nsCSSSelectorList*
|
|
|
|
nsINode::ParseSelectorList(const nsAString& aSelectorString,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsIDocument* doc = OwnerDoc();
|
2017-10-12 11:07:25 +03:00
|
|
|
nsIDocument::SelectorCache& cache =
|
|
|
|
doc->GetSelectorCache(mozilla::StyleBackendType::Gecko);
|
2017-10-02 16:32:07 +03:00
|
|
|
nsIDocument::SelectorCache::SelectorList* list =
|
|
|
|
cache.GetList(aSelectorString);
|
|
|
|
if (list) {
|
|
|
|
if (!*list) {
|
2013-12-16 22:06:36 +04:00
|
|
|
// Invalid selector.
|
2016-09-01 01:31:59 +03:00
|
|
|
aRv.ThrowDOMException(NS_ERROR_DOM_SYNTAX_ERR,
|
|
|
|
NS_LITERAL_CSTRING("'") + NS_ConvertUTF16toUTF8(aSelectorString) +
|
|
|
|
NS_LITERAL_CSTRING("' is not a valid selector")
|
|
|
|
);
|
2017-10-06 09:16:13 +03:00
|
|
|
return nullptr;
|
2013-12-16 22:06:36 +04:00
|
|
|
}
|
2017-10-02 19:14:15 +03:00
|
|
|
|
2017-10-12 11:07:25 +03:00
|
|
|
return list->AsGecko();
|
2013-12-16 22:06:36 +04:00
|
|
|
}
|
|
|
|
|
2012-08-30 21:10:13 +04:00
|
|
|
nsCSSParser parser(doc->CSSLoader());
|
|
|
|
|
2017-10-02 16:32:07 +03:00
|
|
|
nsCSSSelectorList* selectorList = nullptr;
|
2013-12-16 22:06:36 +04:00
|
|
|
aRv = parser.ParseSelectorString(aSelectorString,
|
|
|
|
doc->GetDocumentURI(),
|
|
|
|
0, // XXXbz get the line number!
|
|
|
|
&selectorList);
|
|
|
|
if (aRv.Failed()) {
|
2012-12-20 19:59:01 +04:00
|
|
|
// We hit this for syntax errors, which are quite common, so don't
|
|
|
|
// use NS_ENSURE_SUCCESS. (For example, jQuery has an extended set
|
|
|
|
// of selectors, but it sees if we can parse them first.)
|
2015-04-27 16:18:52 +03:00
|
|
|
MOZ_ASSERT(aRv.ErrorCodeIs(NS_ERROR_DOM_SYNTAX_ERR),
|
2013-12-16 22:06:36 +04:00
|
|
|
"Unexpected error, so cached version won't return it");
|
2016-09-01 01:31:59 +03:00
|
|
|
|
|
|
|
// Change the error message to match above.
|
|
|
|
aRv.ThrowDOMException(NS_ERROR_DOM_SYNTAX_ERR,
|
|
|
|
NS_LITERAL_CSTRING("'") + NS_ConvertUTF16toUTF8(aSelectorString) +
|
|
|
|
NS_LITERAL_CSTRING("' is not a valid selector")
|
|
|
|
);
|
|
|
|
|
2017-10-02 16:32:07 +03:00
|
|
|
cache.CacheList(aSelectorString, UniquePtr<nsCSSSelectorList>());
|
2013-12-16 22:06:36 +04:00
|
|
|
return nullptr;
|
2012-12-20 19:59:01 +04:00
|
|
|
}
|
2012-08-30 21:10:13 +04:00
|
|
|
|
|
|
|
// Filter out pseudo-element selectors from selectorList
|
|
|
|
nsCSSSelectorList** slot = &selectorList;
|
|
|
|
do {
|
|
|
|
nsCSSSelectorList* cur = *slot;
|
|
|
|
if (cur->mSelectors->IsPseudoElement()) {
|
|
|
|
*slot = cur->mNext;
|
|
|
|
cur->mNext = nullptr;
|
|
|
|
delete cur;
|
|
|
|
} else {
|
|
|
|
slot = &cur->mNext;
|
|
|
|
}
|
|
|
|
} while (*slot);
|
|
|
|
|
2013-12-16 22:06:36 +04:00
|
|
|
if (selectorList) {
|
|
|
|
NS_ASSERTION(selectorList->mSelectors,
|
|
|
|
"How can we not have any selectors?");
|
2017-10-02 16:32:07 +03:00
|
|
|
cache.CacheList(aSelectorString, UniquePtr<nsCSSSelectorList>(selectorList));
|
2013-12-16 22:06:36 +04:00
|
|
|
} else {
|
|
|
|
// This is the "only pseudo-element selectors" case, which is
|
|
|
|
// not common, so just don't worry about caching it. That way a
|
|
|
|
// null cached value can always indicate an invalid selector.
|
|
|
|
}
|
|
|
|
|
2013-12-16 22:06:36 +04:00
|
|
|
return selectorList;
|
2012-08-30 21:10:13 +04:00
|
|
|
}
|
|
|
|
|
2012-12-05 08:15:47 +04:00
|
|
|
static void
|
|
|
|
AddScopeElements(TreeMatchContext& aMatchContext,
|
|
|
|
nsINode* aMatchContextNode)
|
|
|
|
{
|
|
|
|
if (aMatchContextNode->IsElement()) {
|
|
|
|
aMatchContext.SetHasSpecifiedScope();
|
|
|
|
aMatchContext.AddScopeElement(aMatchContextNode->AsElement());
|
|
|
|
}
|
|
|
|
}
|
2018-02-01 07:04:04 +03:00
|
|
|
#endif
|
2012-12-05 08:15:47 +04:00
|
|
|
|
2013-11-01 23:39:24 +04:00
|
|
|
namespace {
|
|
|
|
struct SelectorMatchInfo {
|
2018-02-01 07:04:04 +03:00
|
|
|
#ifdef MOZ_OLD_STYLE
|
2013-11-01 23:39:24 +04:00
|
|
|
nsCSSSelectorList* const mSelectorList;
|
|
|
|
TreeMatchContext& mMatchContext;
|
2018-02-01 07:04:04 +03:00
|
|
|
#endif
|
2013-11-01 23:39:24 +04:00
|
|
|
};
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2013-11-01 23:39:24 +04:00
|
|
|
|
|
|
|
// Given an id, find elements with that id under aRoot that match aMatchInfo if
|
|
|
|
// any is provided. If no SelectorMatchInfo is provided, just find the ones
|
|
|
|
// with the given id. aRoot must be in the document.
|
|
|
|
template<bool onlyFirstMatch, class T>
|
|
|
|
inline static void
|
|
|
|
FindMatchingElementsWithId(const nsAString& aId, nsINode* aRoot,
|
|
|
|
SelectorMatchInfo* aMatchInfo,
|
|
|
|
T& aList)
|
|
|
|
{
|
2016-03-31 13:58:25 +03:00
|
|
|
MOZ_ASSERT(aRoot->IsInUncomposedDoc(),
|
2013-11-01 23:39:24 +04:00
|
|
|
"Don't call me if the root is not in the document");
|
|
|
|
MOZ_ASSERT(aRoot->IsElement() || aRoot->IsNodeOfType(nsINode::eDOCUMENT),
|
|
|
|
"The optimization below to check ContentIsDescendantOf only for "
|
|
|
|
"elements depends on aRoot being either an element or a "
|
|
|
|
"document if it's in the document. Note that document fragments "
|
2016-03-31 13:58:25 +03:00
|
|
|
"can't be IsInUncomposedDoc(), so should never show up here.");
|
2013-11-01 23:39:24 +04:00
|
|
|
|
2015-05-06 20:57:37 +03:00
|
|
|
const nsTArray<Element*>* elements = aRoot->OwnerDoc()->GetAllElementsForId(aId);
|
2013-11-01 23:39:24 +04:00
|
|
|
if (!elements) {
|
|
|
|
// Nothing to do; we're done
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXXbz: Should we fall back to the tree walk if aRoot is not the
|
|
|
|
// document and |elements| is long, for some value of "long"?
|
2015-05-06 20:57:37 +03:00
|
|
|
for (size_t i = 0; i < elements->Length(); ++i) {
|
|
|
|
Element* element = (*elements)[i];
|
2013-11-01 23:39:24 +04:00
|
|
|
if (!aRoot->IsElement() ||
|
|
|
|
(element != aRoot &&
|
|
|
|
nsContentUtils::ContentIsDescendantOf(element, aRoot))) {
|
|
|
|
// We have an element with the right id and it's a strict descendant
|
|
|
|
// of aRoot. Make sure it really matches the selector.
|
2018-02-01 07:04:04 +03:00
|
|
|
if (!aMatchInfo
|
|
|
|
#ifdef MOZ_OLD_STYLE
|
|
|
|
|| nsCSSRuleProcessor::SelectorListMatches(element,
|
|
|
|
aMatchInfo->mMatchContext,
|
|
|
|
aMatchInfo->mSelectorList)
|
|
|
|
#endif
|
|
|
|
) {
|
2013-11-01 23:39:24 +04:00
|
|
|
aList.AppendElement(element);
|
|
|
|
if (onlyFirstMatch) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-01 07:04:04 +03:00
|
|
|
#ifdef MOZ_OLD_STYLE
|
2012-08-30 21:10:13 +04:00
|
|
|
// Actually find elements matching aSelectorList (which must not be
|
|
|
|
// null) and which are descendants of aRoot and put them in aList. If
|
|
|
|
// onlyFirstMatch, then stop once the first one is found.
|
2013-11-07 23:59:48 +04:00
|
|
|
template<bool onlyFirstMatch, class Collector, class T>
|
2013-12-16 22:06:36 +04:00
|
|
|
MOZ_ALWAYS_INLINE static void
|
2013-12-16 22:06:36 +04:00
|
|
|
FindMatchingElements(nsINode* aRoot, nsCSSSelectorList* aSelectorList, T &aList,
|
2013-12-16 22:06:36 +04:00
|
|
|
ErrorResult& aRv)
|
2012-08-30 21:10:13 +04:00
|
|
|
{
|
2013-12-16 22:06:36 +04:00
|
|
|
nsIDocument* doc = aRoot->OwnerDoc();
|
2012-08-30 21:10:13 +04:00
|
|
|
|
|
|
|
TreeMatchContext matchingContext(false, nsRuleWalker::eRelevantLinkUnvisited,
|
|
|
|
doc, TreeMatchContext::eNeverMatchVisited);
|
2012-12-05 08:15:47 +04:00
|
|
|
AddScopeElements(matchingContext, aRoot);
|
2012-08-30 21:10:13 +04:00
|
|
|
|
|
|
|
// Fast-path selectors involving IDs. We can only do this if aRoot
|
|
|
|
// is in the document and the document is not in quirks mode, since
|
|
|
|
// ID selectors are case-insensitive in quirks mode. Also, only do
|
2013-12-16 22:06:36 +04:00
|
|
|
// this if aSelectorList only has one selector, because otherwise
|
2012-08-30 21:10:13 +04:00
|
|
|
// ordering the elements correctly is a pain.
|
|
|
|
NS_ASSERTION(aRoot->IsElement() || aRoot->IsNodeOfType(nsINode::eDOCUMENT) ||
|
2016-03-31 13:58:25 +03:00
|
|
|
!aRoot->IsInUncomposedDoc(),
|
2012-08-30 21:10:13 +04:00
|
|
|
"The optimization below to check ContentIsDescendantOf only for "
|
|
|
|
"elements depends on aRoot being either an element or a "
|
|
|
|
"document if it's in the document.");
|
2016-03-31 13:58:25 +03:00
|
|
|
if (aRoot->IsInUncomposedDoc() &&
|
2012-08-30 21:10:13 +04:00
|
|
|
doc->GetCompatibilityMode() != eCompatibility_NavQuirks &&
|
2013-12-16 22:06:36 +04:00
|
|
|
!aSelectorList->mNext &&
|
|
|
|
aSelectorList->mSelectors->mIDList) {
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* id = aSelectorList->mSelectors->mIDList->mAtom;
|
2013-12-16 22:06:36 +04:00
|
|
|
SelectorMatchInfo info = { aSelectorList, matchingContext };
|
2013-11-01 23:39:24 +04:00
|
|
|
FindMatchingElementsWithId<onlyFirstMatch, T>(nsDependentAtomString(id),
|
|
|
|
aRoot, &info, aList);
|
2013-12-16 22:06:36 +04:00
|
|
|
return;
|
2012-08-30 21:10:13 +04:00
|
|
|
}
|
|
|
|
|
2013-11-07 23:59:48 +04:00
|
|
|
Collector results;
|
2012-08-30 21:10:13 +04:00
|
|
|
for (nsIContent* cur = aRoot->GetFirstChild();
|
|
|
|
cur;
|
|
|
|
cur = cur->GetNextNode(aRoot)) {
|
|
|
|
if (cur->IsElement() &&
|
|
|
|
nsCSSRuleProcessor::SelectorListMatches(cur->AsElement(),
|
|
|
|
matchingContext,
|
2013-12-16 22:06:36 +04:00
|
|
|
aSelectorList)) {
|
2012-08-30 21:10:13 +04:00
|
|
|
if (onlyFirstMatch) {
|
2013-11-07 23:59:48 +04:00
|
|
|
aList.AppendElement(cur->AsElement());
|
2013-12-16 22:06:36 +04:00
|
|
|
return;
|
2012-08-30 21:10:13 +04:00
|
|
|
}
|
2013-11-07 23:59:48 +04:00
|
|
|
results.AppendElement(cur->AsElement());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t len = results.Length();
|
|
|
|
if (len) {
|
|
|
|
aList.SetCapacity(len);
|
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
|
|
|
aList.AppendElement(results.ElementAt(i));
|
2012-08-30 21:10:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-01 07:04:04 +03:00
|
|
|
#endif
|
2012-08-30 21:10:13 +04:00
|
|
|
|
|
|
|
struct ElementHolder {
|
|
|
|
ElementHolder() : mElement(nullptr) {}
|
|
|
|
void AppendElement(Element* aElement) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(!mElement, "Should only get one element");
|
2012-11-15 02:10:08 +04:00
|
|
|
mElement = aElement;
|
2012-08-30 21:10:13 +04:00
|
|
|
}
|
2013-11-07 23:59:48 +04:00
|
|
|
void SetCapacity(uint32_t aCapacity) { MOZ_CRASH("Don't call me!"); }
|
|
|
|
uint32_t Length() { return 0; }
|
|
|
|
Element* ElementAt(uint32_t aIndex) { return nullptr; }
|
|
|
|
|
2012-11-15 02:10:08 +04:00
|
|
|
Element* mElement;
|
2012-08-30 21:10:13 +04:00
|
|
|
};
|
|
|
|
|
2012-11-15 02:10:08 +04:00
|
|
|
Element*
|
2012-10-16 15:51:00 +04:00
|
|
|
nsINode::QuerySelector(const nsAString& aSelector, ErrorResult& aResult)
|
2012-08-30 21:10:13 +04:00
|
|
|
{
|
2017-10-21 15:54:43 +03:00
|
|
|
return WithSelectorList<Element*>(
|
|
|
|
aSelector,
|
|
|
|
aResult,
|
|
|
|
[&](const RawServoSelectorList* aList) -> Element* {
|
|
|
|
if (!aList) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-10-30 21:23:59 +03:00
|
|
|
const bool useInvalidation = false;
|
|
|
|
return const_cast<Element*>(
|
|
|
|
Servo_SelectorList_QueryFirst(this, aList, useInvalidation));
|
2017-10-21 15:54:43 +03:00
|
|
|
},
|
|
|
|
[&](nsCSSSelectorList* aList) -> Element* {
|
2018-02-01 07:04:04 +03:00
|
|
|
#ifdef MOZ_OLD_STYLE
|
2017-10-21 15:54:43 +03:00
|
|
|
if (!aList) {
|
|
|
|
// Either we failed (and aResult already has the exception), or this
|
|
|
|
// is a pseudo-element-only selector that matches nothing.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
ElementHolder holder;
|
|
|
|
FindMatchingElements<true, ElementHolder>(this, aList, holder, aResult);
|
|
|
|
return holder.mElement;
|
2018-02-01 07:04:04 +03:00
|
|
|
#else
|
|
|
|
MOZ_CRASH("old style system disabled");
|
|
|
|
#endif
|
2017-10-21 15:54:43 +03:00
|
|
|
}
|
|
|
|
);
|
2012-08-30 21:10:13 +04:00
|
|
|
}
|
|
|
|
|
2012-10-16 15:51:00 +04:00
|
|
|
already_AddRefed<nsINodeList>
|
|
|
|
nsINode::QuerySelectorAll(const nsAString& aSelector, ErrorResult& aResult)
|
2012-08-30 21:10:13 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsSimpleContentList> contentList = new nsSimpleContentList(this);
|
2012-08-30 21:10:13 +04:00
|
|
|
|
2017-10-22 03:19:14 +03:00
|
|
|
WithSelectorList<void>(
|
|
|
|
aSelector,
|
|
|
|
aResult,
|
|
|
|
[&](const RawServoSelectorList* aList) {
|
|
|
|
if (!aList) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-30 21:23:59 +03:00
|
|
|
const bool useInvalidation = false;
|
|
|
|
Servo_SelectorList_QueryAll(
|
|
|
|
this, aList, contentList.get(), useInvalidation);
|
2017-10-22 03:19:14 +03:00
|
|
|
},
|
|
|
|
[&](nsCSSSelectorList* aList) {
|
2018-02-01 07:04:04 +03:00
|
|
|
#ifdef MOZ_OLD_STYLE
|
2017-10-22 03:19:14 +03:00
|
|
|
if (!aList) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
FindMatchingElements<false, AutoTArray<Element*, 128>>(
|
|
|
|
this, aList, *contentList, aResult);
|
2018-02-01 07:04:04 +03:00
|
|
|
#else
|
|
|
|
MOZ_CRASH("old style system disabled");
|
|
|
|
#endif
|
2017-10-22 03:19:14 +03:00
|
|
|
}
|
|
|
|
);
|
2012-08-30 21:10:13 +04:00
|
|
|
|
2012-10-16 15:51:00 +04:00
|
|
|
return contentList.forget();
|
2012-08-30 21:10:13 +04:00
|
|
|
}
|
|
|
|
|
2013-11-01 23:39:24 +04:00
|
|
|
Element*
|
|
|
|
nsINode::GetElementById(const nsAString& aId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsElement() || IsNodeOfType(eDOCUMENT_FRAGMENT),
|
|
|
|
"Bogus this object for GetElementById call");
|
2016-03-31 13:58:25 +03:00
|
|
|
if (IsInUncomposedDoc()) {
|
2013-11-01 23:39:24 +04:00
|
|
|
ElementHolder holder;
|
|
|
|
FindMatchingElementsWithId<true>(aId, this, nullptr, holder);
|
|
|
|
return holder.mElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (nsIContent* kid = GetFirstChild(); kid; kid = kid->GetNextNode(this)) {
|
|
|
|
if (!kid->IsElement()) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* id = kid->AsElement()->GetID();
|
2013-11-01 23:39:24 +04:00
|
|
|
if (id && id->Equals(aId)) {
|
|
|
|
return kid->AsElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
JSObject*
|
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
|
|
|
nsINode::WrapObject(JSContext *aCx, JS::Handle<JSObject*> aGivenProto)
|
2012-10-09 16:31:24 +04:00
|
|
|
{
|
|
|
|
// Make sure one of these is true
|
|
|
|
// (1) our owner document has a script handling object,
|
|
|
|
// (2) Our owner document has had a script handling object, or has been marked
|
|
|
|
// to have had one,
|
|
|
|
// (3) we are running a privileged script.
|
|
|
|
// Event handling is possible only if (1). If (2) event handling is
|
|
|
|
// prevented.
|
|
|
|
// If the document has never had a script handling object, untrusted
|
|
|
|
// scripts (3) shouldn't touch it!
|
|
|
|
bool hasHadScriptHandlingObject = false;
|
|
|
|
if (!OwnerDoc()->GetScriptHandlingObject(hasHadScriptHandlingObject) &&
|
|
|
|
!hasHadScriptHandlingObject &&
|
2017-02-01 23:43:36 +03:00
|
|
|
!nsContentUtils::IsSystemCaller(aCx)) {
|
2013-09-09 07:29:21 +04:00
|
|
|
Throw(aCx, NS_ERROR_UNEXPECTED);
|
2012-10-09 16:31:24 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
JS::Rooted<JSObject*> obj(aCx, WrapNode(aCx, aGivenProto));
|
2016-01-11 21:32:12 +03:00
|
|
|
MOZ_ASSERT_IF(obj && ChromeOnlyAccess(),
|
|
|
|
xpc::IsInContentXBLScope(obj) ||
|
2017-06-28 22:31:53 +03:00
|
|
|
!xpc::UseContentXBLScope(JS::GetObjectRealmOrNull(obj)));
|
2012-09-13 00:29:30 +04:00
|
|
|
return obj;
|
2012-10-09 16:31:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsINode>
|
|
|
|
nsINode::CloneNode(bool aDeep, ErrorResult& aError)
|
|
|
|
{
|
2017-09-13 20:34:55 +03:00
|
|
|
return nsNodeUtils::CloneNodeImpl(this, aDeep, aError);
|
2012-10-09 16:31:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMAttributeMap*
|
|
|
|
nsINode::GetAttributes()
|
|
|
|
{
|
|
|
|
if (!IsElement()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-03-10 11:58:47 +04:00
|
|
|
return AsElement()->Attributes();
|
2012-10-09 16:31:24 +04:00
|
|
|
}
|
|
|
|
|
2014-06-06 09:22:55 +04:00
|
|
|
Element*
|
|
|
|
nsINode::GetParentElementCrossingShadowRoot() const
|
|
|
|
{
|
|
|
|
if (!mParent) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mParent->IsElement()) {
|
|
|
|
return mParent->AsElement();
|
|
|
|
}
|
|
|
|
|
2017-12-31 22:57:32 +03:00
|
|
|
if (ShadowRoot* shadowRoot = ShadowRoot::FromNode(mParent)) {
|
|
|
|
MOZ_ASSERT(shadowRoot->GetHost(), "ShowRoots should always have a host");
|
|
|
|
return shadowRoot->GetHost();
|
2014-06-06 09:22:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-07-03 17:23:14 +04:00
|
|
|
|
|
|
|
bool
|
|
|
|
nsINode::HasBoxQuadsSupport(JSContext* aCx, JSObject* /* unused */)
|
|
|
|
{
|
|
|
|
return xpc::AccessCheck::isChrome(js::GetContextCompartment(aCx)) ||
|
2017-04-07 22:48:24 +03:00
|
|
|
nsContentUtils::GetBoxQuadsEnabled();
|
2014-07-03 17:23:14 +04:00
|
|
|
}
|
|
|
|
|
2014-10-31 00:38:48 +03:00
|
|
|
nsINode*
|
|
|
|
nsINode::GetScopeChainParent() const
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-03-14 08:34:40 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
nsINode::AddAnimationObserver(nsIAnimationObserver* aAnimationObserver)
|
|
|
|
{
|
|
|
|
AddMutationObserver(aAnimationObserver);
|
|
|
|
OwnerDoc()->SetMayHaveAnimationObservers();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsINode::AddAnimationObserverUnlessExists(
|
|
|
|
nsIAnimationObserver* aAnimationObserver)
|
|
|
|
{
|
|
|
|
AddMutationObserverUnlessExists(aAnimationObserver);
|
|
|
|
OwnerDoc()->SetMayHaveAnimationObservers();
|
|
|
|
}
|
2015-09-17 13:16:20 +03:00
|
|
|
|
2017-06-15 04:49:17 +03:00
|
|
|
void
|
|
|
|
nsINode::GenerateXPath(nsAString& aResult)
|
|
|
|
{
|
|
|
|
XPathGenerator::Generate(this, aResult);
|
|
|
|
}
|
|
|
|
|
2015-09-17 13:16:20 +03:00
|
|
|
bool
|
2016-05-06 13:39:10 +03:00
|
|
|
nsINode::IsApzAware() const
|
2015-09-17 13:16:20 +03:00
|
|
|
{
|
2016-05-06 13:39:10 +03:00
|
|
|
return IsNodeApzAware();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsINode::IsNodeApzAwareInternal() const
|
|
|
|
{
|
|
|
|
return EventTarget::IsApzAware();
|
2015-09-17 13:16:20 +03:00
|
|
|
}
|
2016-07-08 10:02:56 +03:00
|
|
|
|
|
|
|
#ifdef MOZ_STYLO
|
|
|
|
bool
|
|
|
|
nsINode::IsStyledByServo() const
|
|
|
|
{
|
|
|
|
return OwnerDoc()->IsStyledByServo();
|
|
|
|
}
|
|
|
|
#endif
|
2017-11-27 11:10:27 +03:00
|
|
|
|
|
|
|
DocGroup*
|
|
|
|
nsINode::GetDocGroup() const
|
|
|
|
{
|
|
|
|
return OwnerDoc()->GetDocGroup();
|
|
|
|
}
|
2018-02-22 01:07:53 +03:00
|
|
|
|
|
|
|
class LocalizationHandler : public PromiseNativeHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LocalizationHandler() = default;
|
|
|
|
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS(LocalizationHandler)
|
|
|
|
|
|
|
|
nsTArray<nsCOMPtr<Element>>& Elements() { return mElements; }
|
|
|
|
|
|
|
|
void SetReturnValuePromise(Promise* aReturnValuePromise)
|
|
|
|
{
|
|
|
|
mReturnValuePromise = aReturnValuePromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override
|
|
|
|
{
|
|
|
|
nsTArray<L10nValue> l10nData;
|
|
|
|
if (aValue.isObject()) {
|
|
|
|
JS::ForOfIterator iter(aCx);
|
|
|
|
if (!iter.init(aValue, JS::ForOfIterator::AllowNonIterable)) {
|
|
|
|
mReturnValuePromise->MaybeRejectWithUndefined();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!iter.valueIsIterable()) {
|
|
|
|
mReturnValuePromise->MaybeRejectWithUndefined();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> temp(aCx);
|
|
|
|
while (true) {
|
|
|
|
bool done;
|
|
|
|
if (!iter.next(&temp, &done)) {
|
|
|
|
mReturnValuePromise->MaybeRejectWithUndefined();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (done) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
L10nValue* slotPtr =
|
|
|
|
l10nData.AppendElement(mozilla::fallible);
|
|
|
|
if (!slotPtr) {
|
|
|
|
mReturnValuePromise->MaybeRejectWithUndefined();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!slotPtr->Init(aCx, temp)) {
|
|
|
|
mReturnValuePromise->MaybeRejectWithUndefined();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mElements.Length() != l10nData.Length()) {
|
|
|
|
mReturnValuePromise->MaybeRejectWithUndefined();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> untranslatedElements(aCx,
|
|
|
|
JS_NewArrayObject(aCx, mElements.Length()));
|
|
|
|
if (!untranslatedElements) {
|
|
|
|
mReturnValuePromise->MaybeRejectWithUndefined();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorResult rv;
|
|
|
|
for (size_t i = 0; i < l10nData.Length(); ++i) {
|
|
|
|
Element* elem = mElements[i];
|
|
|
|
nsString& content = l10nData[i].mValue;
|
|
|
|
if (!content.IsVoid()) {
|
|
|
|
elem->SetTextContent(content, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
mReturnValuePromise->MaybeRejectWithUndefined();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Nullable<Sequence<AttributeNameValue>>& attributes =
|
|
|
|
l10nData[i].mAttrs;
|
|
|
|
if (!attributes.IsNull()) {
|
|
|
|
for (size_t j = 0; j < attributes.Value().Length(); ++j) {
|
|
|
|
// Use SetAttribute here to validate the attribute name!
|
|
|
|
elem->SetAttribute(attributes.Value()[j].mName,
|
|
|
|
attributes.Value()[j].mValue,
|
|
|
|
rv);
|
|
|
|
if (rv.Failed()) {
|
|
|
|
mReturnValuePromise->MaybeRejectWithUndefined();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (content.IsVoid() && attributes.IsNull()) {
|
|
|
|
JS::Rooted<JS::Value> wrappedElem(aCx);
|
|
|
|
if (!ToJSValue(aCx, elem, &wrappedElem)) {
|
|
|
|
mReturnValuePromise->MaybeRejectWithUndefined();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_DefineElement(aCx, untranslatedElements, i, wrappedElem, JSPROP_ENUMERATE)) {
|
|
|
|
mReturnValuePromise->MaybeRejectWithUndefined();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mReturnValuePromise->MaybeResolve(untranslatedElements);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override
|
|
|
|
{
|
|
|
|
mReturnValuePromise->MaybeRejectWithUndefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~LocalizationHandler() = default;
|
|
|
|
|
|
|
|
nsTArray<nsCOMPtr<Element>> mElements;
|
|
|
|
RefPtr<Promise> mReturnValuePromise;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(LocalizationHandler)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(LocalizationHandler)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(LocalizationHandler)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(LocalizationHandler)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(LocalizationHandler)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mElements)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mReturnValuePromise)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(LocalizationHandler)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mElements)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mReturnValuePromise)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
nsINode::Localize(JSContext* aCx,
|
|
|
|
mozilla::dom::L10nCallback& aCallback,
|
|
|
|
mozilla::ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
Sequence<L10nElement> l10nElements;
|
|
|
|
SequenceRooter<L10nElement> rooter(aCx, &l10nElements);
|
|
|
|
RefPtr<LocalizationHandler> nativeHandler = new LocalizationHandler();
|
|
|
|
nsTArray<nsCOMPtr<Element>>& domElements = nativeHandler->Elements();
|
|
|
|
nsIContent* node = IsContent() ? AsContent() : GetFirstChild();
|
|
|
|
nsAutoString l10nId;
|
|
|
|
nsAutoString l10nArgs;
|
|
|
|
nsAutoString l10nAttrs;
|
|
|
|
nsAutoString type;
|
|
|
|
for (; node; node = node->GetNextNode(this)) {
|
|
|
|
if (!node->IsElement()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Element* domElement = node->AsElement();
|
|
|
|
if (!domElement->GetAttr(kNameSpaceID_None, nsGkAtoms::datal10nid, l10nId)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
domElement->GetAttr(kNameSpaceID_None, nsGkAtoms::datal10nargs, l10nArgs);
|
|
|
|
domElement->GetAttr(kNameSpaceID_None, nsGkAtoms::datal10nattrs, l10nAttrs);
|
|
|
|
L10nElement* element = l10nElements.AppendElement(fallible);
|
|
|
|
if (!element) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
domElements.AppendElement(domElement, fallible);
|
|
|
|
|
|
|
|
domElement->GetNamespaceURI(element->mNamespaceURI);
|
|
|
|
element->mLocalName = domElement->LocalName();
|
|
|
|
domElement->GetAttr(kNameSpaceID_None, nsGkAtoms::type, type);
|
|
|
|
if (!type.IsEmpty()) {
|
|
|
|
element->mType = type;
|
|
|
|
}
|
|
|
|
element->mL10nId = l10nId;
|
|
|
|
if (!l10nAttrs.IsEmpty()) {
|
|
|
|
element->mL10nAttrs = l10nAttrs;
|
|
|
|
}
|
|
|
|
if (!l10nArgs.IsEmpty()) {
|
|
|
|
JS::Rooted<JS::Value> json(aCx);
|
|
|
|
if (!JS_ParseJSON(aCx, l10nArgs.get(), l10nArgs.Length(), &json) ||
|
|
|
|
!json.isObject()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
element->mL10nArgs = &json.toObject();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Promise> callbackResult = aCallback.Call(l10nElements, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Promise> promise = Promise::Create(OwnerDoc()->GetParentObject(), aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nativeHandler->SetReturnValuePromise(promise);
|
|
|
|
callbackResult->AppendNativeHandler(nativeHandler);
|
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|