summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMoonchild <moonchild@palemoon.org>2020-06-14 10:01:06 +0000
committerGitHub <noreply@github.com>2020-06-14 10:01:06 +0000
commit7cc007d9bf8927fc4ce1d5ea80a843d6edfc17dd (patch)
tree7b3ba52faca3704faf53d8248cbe130a32481b21
parent59a5adc93415f6d6e8ce4352ba0cd2d34d5e7888 (diff)
parentbbd59105da97200947ae62b2dc949a1130a40d75 (diff)
downloaduxp-7cc007d9bf8927fc4ce1d5ea80a843d6edfc17dd.tar.gz
Merge pull request #1591 from MoonchildProductions/sr.activeElement-work
Implement ShadowRoot.activeElement
-rw-r--r--dom/base/ChromeNodeList.cpp1
-rw-r--r--dom/base/ChromeNodeList.h1
-rw-r--r--dom/base/DocumentOrShadowRoot.cpp149
-rw-r--r--dom/base/DocumentOrShadowRoot.h160
-rw-r--r--dom/base/Element.h1
-rw-r--r--dom/base/FragmentOrElement.cpp9
-rw-r--r--dom/base/ShadowRoot.cpp54
-rw-r--r--dom/base/ShadowRoot.h29
-rw-r--r--dom/base/StyleScope.cpp27
-rw-r--r--dom/base/StyleScope.h81
-rw-r--r--dom/base/StyleSheetList.cpp12
-rw-r--r--dom/base/StyleSheetList.h14
-rwxr-xr-xdom/base/moz.build4
-rw-r--r--dom/base/nsContentUtils.cpp8
-rw-r--r--dom/base/nsContentUtils.h2
-rw-r--r--dom/base/nsDocument.cpp117
-rw-r--r--dom/base/nsDocument.h19
-rw-r--r--dom/base/nsFocusManager.cpp7
-rw-r--r--dom/base/nsIContent.h8
-rw-r--r--dom/base/nsIDocument.h42
-rw-r--r--dom/base/nsIdentifierMapEntry.h106
-rw-r--r--dom/base/nsNameSpaceManager.h2
-rw-r--r--dom/html/nsHTMLDocument.cpp2
-rw-r--r--dom/html/nsHTMLDocument.h5
-rw-r--r--dom/webidl/Document.webidl4
-rw-r--r--dom/webidl/DocumentOrShadowRoot.webidl29
-rw-r--r--dom/webidl/ShadowRoot.webidl1
-rw-r--r--dom/webidl/moz.build1
-rw-r--r--dom/xul/XULDocument.cpp19
-rw-r--r--dom/xul/XULDocument.h5
-rw-r--r--editor/libeditor/TextEditor.cpp1
-rw-r--r--testing/web-platform/meta/shadow-dom/ShadowRoot-interface.html.ini29
-rw-r--r--testing/web-platform/meta/shadow-dom/untriaged/elements-and-dom-objects/shadowroot-object/shadowroot-attributes/activeElement-confirm-return-null.html.ini11
-rw-r--r--testing/web-platform/meta/shadow-dom/untriaged/elements-and-dom-objects/shadowroot-object/shadowroot-attributes/test-007.html.ini5
-rw-r--r--testing/web-platform/meta/shadow-dom/untriaged/user-interaction/active-element/test-001.html.ini5
-rw-r--r--testing/web-platform/meta/shadow-dom/untriaged/user-interaction/active-element/test-002.html.ini5
36 files changed, 556 insertions, 419 deletions
diff --git a/dom/base/ChromeNodeList.cpp b/dom/base/ChromeNodeList.cpp
index ffa1019718..2e9f058231 100644
--- a/dom/base/ChromeNodeList.cpp
+++ b/dom/base/ChromeNodeList.cpp
@@ -6,6 +6,7 @@
#include "mozilla/dom/ChromeNodeList.h"
#include "mozilla/dom/ChromeNodeListBinding.h"
+#include "nsPIDOMWindow.h"
using namespace mozilla;
using namespace mozilla::dom;
diff --git a/dom/base/ChromeNodeList.h b/dom/base/ChromeNodeList.h
index 9908808ac3..8dbd42be30 100644
--- a/dom/base/ChromeNodeList.h
+++ b/dom/base/ChromeNodeList.h
@@ -6,6 +6,7 @@
#include "nsCOMArray.h"
#include "nsContentList.h"
+#include "nsIDocument.h"
namespace mozilla {
class ErrorResult;
diff --git a/dom/base/DocumentOrShadowRoot.cpp b/dom/base/DocumentOrShadowRoot.cpp
new file mode 100644
index 0000000000..13ee3cb150
--- /dev/null
+++ b/dom/base/DocumentOrShadowRoot.cpp
@@ -0,0 +1,149 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "DocumentOrShadowRoot.h"
+#include "mozilla/dom/StyleSheetList.h"
+#include "nsDocument.h"
+#include "nsFocusManager.h"
+#include "ShadowRoot.h"
+#include "XULDocument.h"
+
+class nsINode;
+class nsIDocument;
+class ShadowRoot;
+
+namespace mozilla {
+namespace dom {
+
+DocumentOrShadowRoot::DocumentOrShadowRoot(mozilla::dom::ShadowRoot* aShadowRoot)
+ : mAsNode(aShadowRoot)
+ , mKind(Kind::ShadowRoot)
+{
+ MOZ_ASSERT(mAsNode);
+}
+
+DocumentOrShadowRoot::DocumentOrShadowRoot(nsIDocument* aDoc)
+ : mAsNode(aDoc)
+ , mKind(Kind::Document)
+{
+ MOZ_ASSERT(mAsNode);
+}
+
+StyleSheetList&
+DocumentOrShadowRoot::EnsureDOMStyleSheets()
+{
+ if (!mDOMStyleSheets) {
+ mDOMStyleSheets = new StyleSheetList(*this);
+ }
+ return *mDOMStyleSheets;
+}
+
+Element*
+DocumentOrShadowRoot::GetElementById(const nsAString& aElementId)
+{
+ if (MOZ_UNLIKELY(aElementId.IsEmpty())) {
+ nsContentUtils::ReportEmptyGetElementByIdArg(AsNode().OwnerDoc());
+ return nullptr;
+ }
+
+ if (nsIdentifierMapEntry* entry = mIdentifierMap.GetEntry(aElementId)) {
+ if (Element* el = entry->GetIdElement()) {
+ return el;
+ }
+ }
+
+ if (MOZ_UNLIKELY(mKind == Kind::Document &&
+ static_cast<nsIDocument&>(AsNode()).IsXULDocument())) {
+ return static_cast<XULDocument&>(AsNode()).GetRefById(aElementId);
+ }
+
+ return nullptr;
+}
+
+already_AddRefed<nsContentList>
+DocumentOrShadowRoot::GetElementsByTagNameNS(const nsAString& aNamespaceURI,
+ const nsAString& aLocalName)
+{
+ ErrorResult rv;
+ RefPtr<nsContentList> list =
+ GetElementsByTagNameNS(aNamespaceURI, aLocalName, rv);
+ if (rv.Failed()) {
+ return nullptr;
+ }
+ return list.forget();
+}
+
+already_AddRefed<nsContentList>
+DocumentOrShadowRoot::GetElementsByTagNameNS(const nsAString& aNamespaceURI,
+ const nsAString& aLocalName,
+ mozilla::ErrorResult& aResult)
+{
+ int32_t nameSpaceId = kNameSpaceID_Wildcard;
+
+ if (!aNamespaceURI.EqualsLiteral("*")) {
+ aResult =
+ nsContentUtils::NameSpaceManager()->RegisterNameSpace(aNamespaceURI,
+ nameSpaceId);
+ if (aResult.Failed()) {
+ return nullptr;
+ }
+ }
+
+ NS_ASSERTION(nameSpaceId != kNameSpaceID_Unknown, "Unexpected namespace ID!");
+ return NS_GetContentList(&AsNode(), nameSpaceId, aLocalName);
+}
+
+already_AddRefed<nsContentList>
+DocumentOrShadowRoot::GetElementsByClassName(const nsAString& aClasses)
+{
+ return nsContentUtils::GetElementsByClassName(&AsNode(), aClasses);
+}
+
+nsIContent*
+DocumentOrShadowRoot::Retarget(nsIContent* aContent) const
+{
+ for (nsIContent* cur = aContent;
+ cur;
+ cur = cur->GetContainingShadowHost()) {
+ if (cur->SubtreeRoot() == &AsNode()) {
+ return cur;
+ }
+ }
+ return nullptr;
+}
+
+Element*
+DocumentOrShadowRoot::GetRetargetedFocusedElement()
+{
+ if (nsCOMPtr<nsPIDOMWindowOuter> window = AsNode().OwnerDoc()->GetWindow()) {
+ nsCOMPtr<nsPIDOMWindowOuter> focusedWindow;
+ nsIContent* focusedContent =
+ nsFocusManager::GetFocusedDescendant(window,
+ false,
+ getter_AddRefs(focusedWindow));
+ // be safe and make sure the element is from this document
+ if (focusedContent && focusedContent->OwnerDoc() == AsNode().OwnerDoc()) {
+ if (focusedContent->ChromeOnlyAccess()) {
+ focusedContent = focusedContent->FindFirstNonChromeOnlyAccessContent();
+ }
+
+ if (focusedContent) {
+ if (!nsDocument::IsWebComponentsEnabled(focusedContent)) {
+ return focusedContent->AsElement();
+ }
+
+ if (nsIContent* retarget = Retarget(focusedContent)) {
+ return retarget->AsElement();
+ }
+ }
+ }
+ }
+
+ return nullptr;
+}
+
+}
+}
diff --git a/dom/base/DocumentOrShadowRoot.h b/dom/base/DocumentOrShadowRoot.h
new file mode 100644
index 0000000000..2092cd54f2
--- /dev/null
+++ b/dom/base/DocumentOrShadowRoot.h
@@ -0,0 +1,160 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* 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/. */
+
+#ifndef mozilla_dom_DocumentOrShadowRoot_h__
+#define mozilla_dom_DocumentOrShadowRoot_h__
+
+#include "nsTArray.h"
+#include "nsIdentifierMapEntry.h"
+#include "nsContentListDeclarations.h"
+#include "nsNameSpaceManager.h"
+#include "mozilla/dom/NameSpaceConstants.h"
+
+class nsContentList;
+class nsINode;
+
+namespace mozilla {
+class StyleSheet;
+
+namespace dom {
+
+class StyleSheetList;
+class ShadowRoot;
+
+/**
+ * A class meant to be shared by ShadowRoot and Document, that holds a list of
+ * stylesheets.
+ *
+ * TODO(emilio, bug 1418159): In the future this should hold most of the
+ * relevant style state, this should allow us to fix bug 548397.
+ */
+class DocumentOrShadowRoot
+{
+ enum class Kind {
+ Document,
+ ShadowRoot,
+ };
+
+public:
+ explicit DocumentOrShadowRoot(nsIDocument*);
+ explicit DocumentOrShadowRoot(mozilla::dom::ShadowRoot*);
+
+ nsINode& AsNode()
+ {
+ return *mAsNode;
+ }
+
+ const nsINode& AsNode() const
+ {
+ return *mAsNode;
+ }
+
+ StyleSheet* SheetAt(size_t aIndex) const
+ {
+ return mStyleSheets.SafeElementAt(aIndex);
+ }
+
+ size_t SheetCount() const
+ {
+ return mStyleSheets.Length();
+ }
+
+ int32_t IndexOfSheet(const StyleSheet& aSheet) const
+ {
+ return mStyleSheets.IndexOf(&aSheet);
+ }
+
+ void InsertSheetAt(size_t aIndex, StyleSheet& aSheet)
+ {
+ mStyleSheets.InsertElementAt(aIndex, &aSheet);
+ }
+
+ void RemoveSheet(StyleSheet& aSheet)
+ {
+ mStyleSheets.RemoveElement(&aSheet);
+ }
+
+ void AppendStyleSheet(StyleSheet& aSheet)
+ {
+ mStyleSheets.AppendElement(&aSheet);
+ }
+
+ StyleSheetList& EnsureDOMStyleSheets();
+
+ Element* GetElementById(const nsAString& aElementId);
+
+ /**
+ * This method returns _all_ the elements in this scope which have id
+ * aElementId, if there are any. Otherwise it returns null.
+ *
+ * This is useful for stuff like QuerySelector optimization and such.
+ */
+ inline const nsTArray<Element*>*
+ GetAllElementsForId(const nsAString& aElementId) const;
+
+ already_AddRefed<nsContentList>
+ GetElementsByTagName(const nsAString& aTagName)
+ {
+ return NS_GetContentList(&AsNode(), kNameSpaceID_Unknown, aTagName);
+ }
+
+ already_AddRefed<nsContentList>
+ GetElementsByTagNameNS(const nsAString& aNamespaceURI,
+ const nsAString& aLocalName);
+
+ already_AddRefed<nsContentList>
+ GetElementsByTagNameNS(const nsAString& aNamespaceURI,
+ const nsAString& aLocalName,
+ mozilla::ErrorResult&);
+
+ already_AddRefed<nsContentList>
+ GetElementsByClassName(const nsAString& aClasses);
+
+ ~DocumentOrShadowRoot() = default;
+
+protected:
+ nsIContent* Retarget(nsIContent* aContent) const;
+
+ /**
+ * If focused element's subtree root is this document or shadow root, return
+ * focused element, otherwise, get the shadow host recursively until the
+ * shadow host's subtree root is this document or shadow root.
+ */
+ Element* GetRetargetedFocusedElement();
+
+ nsTArray<RefPtr<mozilla::StyleSheet>> mStyleSheets;
+ RefPtr<mozilla::dom::StyleSheetList> mDOMStyleSheets;
+
+ /*
+ * mIdentifierMap works as follows for IDs:
+ * 1) Attribute changes affect the table immediately (removing and adding
+ * entries as needed).
+ * 2) Removals from the DOM affect the table immediately
+ * 3) Additions to the DOM always update existing entries for names, and add
+ * new ones for IDs.
+ */
+ nsTHashtable<nsIdentifierMapEntry> mIdentifierMap;
+
+ nsINode* mAsNode;
+ const Kind mKind;
+};
+
+inline const nsTArray<Element*>*
+DocumentOrShadowRoot::GetAllElementsForId(const nsAString& aElementId) const
+{
+ if (aElementId.IsEmpty()) {
+ return nullptr;
+ }
+
+ nsIdentifierMapEntry* entry = mIdentifierMap.GetEntry(aElementId);
+ return entry ? &entry->GetIdElements() : nullptr;
+}
+
+}
+
+}
+
+#endif
diff --git a/dom/base/Element.h b/dom/base/Element.h
index 3d9b80a989..fd4fa71084 100644
--- a/dom/base/Element.h
+++ b/dom/base/Element.h
@@ -139,6 +139,7 @@ class EventStateManager;
namespace dom {
+struct CustomElementDefinition;
class Animation;
class CustomElementRegistry;
class Link;
diff --git a/dom/base/FragmentOrElement.cpp b/dom/base/FragmentOrElement.cpp
index 6fbe04d251..e33d3c63a4 100644
--- a/dom/base/FragmentOrElement.cpp
+++ b/dom/base/FragmentOrElement.cpp
@@ -1138,6 +1138,15 @@ FragmentOrElement::GetAssignedSlot() const
return slots ? slots->mAssignedSlot.get() : nullptr;
}
+nsIContent*
+nsIContent::GetContainingShadowHost() const
+{
+ if (mozilla::dom::ShadowRoot* shadow = GetContainingShadow()) {
+ return shadow->GetHost();
+ }
+ return nullptr;
+}
+
void
FragmentOrElement::SetAssignedSlot(HTMLSlotElement* aSlot)
{
diff --git a/dom/base/ShadowRoot.cpp b/dom/base/ShadowRoot.cpp
index 2383c951a6..c354e04c18 100644
--- a/dom/base/ShadowRoot.cpp
+++ b/dom/base/ShadowRoot.cpp
@@ -58,6 +58,7 @@ ShadowRoot::ShadowRoot(Element* aElement, bool aClosed,
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
nsXBLPrototypeBinding* aProtoBinding)
: DocumentFragment(aNodeInfo)
+ , DocumentOrShadowRoot(this)
, mProtoBinding(aProtoBinding)
, mInsertionPointChanged(false)
, mIsComposedDocParticipant(false)
@@ -240,7 +241,7 @@ ShadowRoot::InsertSheet(StyleSheet* aSheet,
linkingElement->SetStyleSheet(aSheet); // This sets the ownerNode on the sheet
- MOZ_DIAGNOSTIC_ASSERT(mProtoBinding->SheetCount() == StyleScope::SheetCount());
+ MOZ_DIAGNOSTIC_ASSERT(mProtoBinding->SheetCount() == DocumentOrShadowRoot::SheetCount());
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
// FIXME(emilio, bug 1425759): For now we keep them duplicated, the proto
// binding will disappear soon (tm).
@@ -278,49 +279,17 @@ void
ShadowRoot::RemoveSheet(StyleSheet* aSheet)
{
mProtoBinding->RemoveStyleSheet(aSheet);
- StyleScope::RemoveSheet(*aSheet);
+ DocumentOrShadowRoot::RemoveSheet(*aSheet);
if (aSheet->IsApplicable()) {
StyleSheetChanged();
}
}
-Element*
-ShadowRoot::GetElementById(const nsAString& aElementId)
-{
- nsIdentifierMapEntry *entry = mIdentifierMap.GetEntry(aElementId);
- return entry ? entry->GetIdElement() : nullptr;
-}
-
-already_AddRefed<nsContentList>
-ShadowRoot::GetElementsByTagName(const nsAString& aTagName)
-{
- return NS_GetContentList(this, kNameSpaceID_Unknown, aTagName);
-}
-
-already_AddRefed<nsContentList>
-ShadowRoot::GetElementsByTagNameNS(const nsAString& aNamespaceURI,
- const nsAString& aLocalName)
-{
- int32_t nameSpaceId = kNameSpaceID_Wildcard;
-
- if (!aNamespaceURI.EqualsLiteral("*")) {
- nsresult rv =
- nsContentUtils::NameSpaceManager()->RegisterNameSpace(aNamespaceURI,
- nameSpaceId);
- NS_ENSURE_SUCCESS(rv, nullptr);
- }
-
- NS_ASSERTION(nameSpaceId != kNameSpaceID_Unknown, "Unexpected namespace ID!");
-
- return NS_GetContentList(this, nameSpaceId, aLocalName);
-}
-
void
ShadowRoot::AddToIdTable(Element* aElement, nsIAtom* aId)
{
- nsIdentifierMapEntry *entry =
- mIdentifierMap.PutEntry(nsDependentAtomString(aId));
+ nsIdentifierMapEntry* entry = mIdentifierMap.PutEntry(aId);
if (entry) {
entry->AddIdElement(aElement);
}
@@ -329,8 +298,7 @@ ShadowRoot::AddToIdTable(Element* aElement, nsIAtom* aId)
void
ShadowRoot::RemoveFromIdTable(Element* aElement, nsIAtom* aId)
{
- nsIdentifierMapEntry *entry =
- mIdentifierMap.GetEntry(nsDependentAtomString(aId));
+ nsIdentifierMapEntry* entry = mIdentifierMap.GetEntry(aId);
if (entry) {
entry->RemoveIdElement(aElement);
if (entry->IsEmpty()) {
@@ -339,12 +307,6 @@ ShadowRoot::RemoveFromIdTable(Element* aElement, nsIAtom* aId)
}
}
-already_AddRefed<nsContentList>
-ShadowRoot::GetElementsByClassName(const nsAString& aClasses)
-{
- return nsContentUtils::GetElementsByClassName(this, aClasses);
-}
-
nsresult
ShadowRoot::GetEventTargetParent(EventChainPreVisitor& aVisitor)
{
@@ -499,6 +461,12 @@ ShadowRoot::DistributeAllNodes()
DistributionChanged();
}
+Element*
+ShadowRoot::GetActiveElement()
+{
+ return GetRetargetedFocusedElement();
+}
+
void
ShadowRoot::GetInnerHTML(nsAString& aInnerHTML)
{
diff --git a/dom/base/ShadowRoot.h b/dom/base/ShadowRoot.h
index 21c6e1733d..f83a6f7740 100644
--- a/dom/base/ShadowRoot.h
+++ b/dom/base/ShadowRoot.h
@@ -8,7 +8,7 @@
#define mozilla_dom_shadowroot_h__
#include "mozilla/dom/DocumentFragment.h"
-#include "mozilla/dom/StyleScope.h"
+#include "mozilla/dom/DocumentOrShadowRoot.h"
#include "nsCOMPtr.h"
#include "nsCycleCollectionParticipant.h"
#include "nsIContentInlines.h"
@@ -29,7 +29,7 @@ namespace dom {
class Element;
class ShadowRoot final : public DocumentFragment,
- public StyleScope,
+ public DocumentOrShadowRoot,
public nsStubMutationObserver
{
public:
@@ -57,22 +57,14 @@ public:
return mMode == ShadowRootMode::Closed;
}
- // StyleScope.
- nsINode& AsNode() final
- {
- return *this;
- }
-
// [deprecated] Shadow DOM v0
- void AddToIdTable(Element* aElement, nsIAtom* aId);
- void RemoveFromIdTable(Element* aElement, nsIAtom* aId);
void InsertSheet(StyleSheet* aSheet, nsIContent* aLinkingContent);
void RemoveSheet(StyleSheet* aSheet);
bool ApplyAuthorStyles();
void SetApplyAuthorStyles(bool aApplyAuthorStyles);
StyleSheetList* StyleSheets()
{
- return &StyleScope::EnsureDOMStyleSheets();
+ return &DocumentOrShadowRoot::EnsureDOMStyleSheets();
}
/**
@@ -123,15 +115,13 @@ public:
static ShadowRoot* FromNode(nsINode* aNode);
+ void AddToIdTable(Element* aElement, nsIAtom* aId);
+ void RemoveFromIdTable(Element* aElement, nsIAtom* aId);
+
// WebIDL methods.
- Element* GetElementById(const nsAString& aElementId);
- already_AddRefed<nsContentList>
- GetElementsByTagName(const nsAString& aNamespaceURI);
- already_AddRefed<nsContentList>
- GetElementsByTagNameNS(const nsAString& aNamespaceURI,
- const nsAString& aLocalName);
- already_AddRefed<nsContentList>
- GetElementsByClassName(const nsAString& aClasses);
+ using mozilla::dom::DocumentOrShadowRoot::GetElementById;
+
+ Element* GetActiveElement();
void GetInnerHTML(nsAString& aInnerHTML);
void SetInnerHTML(const nsAString& aInnerHTML, ErrorResult& aError);
void StyleSheetChanged();
@@ -154,7 +144,6 @@ protected:
// are in the shadow tree and should be kept alive by its parent.
nsClassHashtable<nsStringHashKey, nsTArray<mozilla::dom::HTMLSlotElement*>> mSlotMap;
- nsTHashtable<nsIdentifierMapEntry> mIdentifierMap;
nsXBLPrototypeBinding* mProtoBinding;
// It is necessary to hold a reference to the associated nsXBLBinding
diff --git a/dom/base/StyleScope.cpp b/dom/base/StyleScope.cpp
deleted file mode 100644
index 6c708a8ba1..0000000000
--- a/dom/base/StyleScope.cpp
+++ /dev/null
@@ -1,27 +0,0 @@
-/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/* vim: set ts=8 sts=2 et sw=2 tw=80: */
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-#include "StyleScope.h"
-#include "mozilla/dom/StyleSheetList.h"
-
-namespace mozilla {
-namespace dom {
-
-StyleScope::~StyleScope()
-{
-}
-
-StyleSheetList&
-StyleScope::EnsureDOMStyleSheets()
-{
- if (!mDOMStyleSheets) {
- mDOMStyleSheets = new StyleSheetList(*this);
- }
- return *mDOMStyleSheets;
-}
-
-}
-}
diff --git a/dom/base/StyleScope.h b/dom/base/StyleScope.h
deleted file mode 100644
index c8957b069b..0000000000
--- a/dom/base/StyleScope.h
+++ /dev/null
@@ -1,81 +0,0 @@
-/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/* vim: set ts=8 sts=2 et sw=2 tw=80: */
-/* 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/. */
-
-#ifndef mozilla_dom_StyleScope_h__
-#define mozilla_dom_StyleScope_h__
-
-#include "nsTArray.h"
-
-class nsINode;
-
-namespace mozilla {
-class StyleSheet;
-
-namespace dom {
-
-class StyleSheetList;
-
-/**
- * A class meant to be shared by ShadowRoot and Document, that holds a list of
- * stylesheets.
- *
- * TODO(emilio, bug 1418159): In the future this should hold most of the
- * relevant style state, this should allow us to fix bug 548397.
- */
-class StyleScope
-{
-public:
- virtual nsINode& AsNode() = 0;
-
- const nsINode& AsNode() const
- {
- return const_cast<StyleScope&>(*this).AsNode();
- }
-
- StyleSheet* SheetAt(size_t aIndex) const
- {
- return mStyleSheets.SafeElementAt(aIndex);
- }
-
- size_t SheetCount() const
- {
- return mStyleSheets.Length();
- }
-
- int32_t IndexOfSheet(const StyleSheet& aSheet) const
- {
- return mStyleSheets.IndexOf(&aSheet);
- }
-
- void InsertSheetAt(size_t aIndex, StyleSheet& aSheet)
- {
- mStyleSheets.InsertElementAt(aIndex, &aSheet);
- }
-
- void RemoveSheet(StyleSheet& aSheet)
- {
- mStyleSheets.RemoveElement(&aSheet);
- }
-
- void AppendStyleSheet(StyleSheet& aSheet)
- {
- mStyleSheets.AppendElement(&aSheet);
- }
-
- StyleSheetList& EnsureDOMStyleSheets();
-
- ~StyleScope();
-
-protected:
- nsTArray<RefPtr<mozilla::StyleSheet>> mStyleSheets;
- RefPtr<mozilla::dom::StyleSheetList> mDOMStyleSheets;
-};
-
-}
-
-}
-
-#endif
diff --git a/dom/base/StyleSheetList.cpp b/dom/base/StyleSheetList.cpp
index 42db3179dd..2b41473ffa 100644
--- a/dom/base/StyleSheetList.cpp
+++ b/dom/base/StyleSheetList.cpp
@@ -48,19 +48,19 @@ StyleSheetList::SlowItem(uint32_t aIndex, nsIDOMStyleSheet** aItem)
void
StyleSheetList::NodeWillBeDestroyed(const nsINode* aNode)
{
- mStyleScope = nullptr;
+ mDocumentOrShadowRoot = nullptr;
}
-StyleSheetList::StyleSheetList(StyleScope& aScope)
- : mStyleScope(&aScope)
+StyleSheetList::StyleSheetList(DocumentOrShadowRoot& aScope)
+ : mDocumentOrShadowRoot(&aScope)
{
- mStyleScope->AsNode().AddMutationObserver(this);
+ mDocumentOrShadowRoot->AsNode().AddMutationObserver(this);
}
StyleSheetList::~StyleSheetList()
{
- if (mStyleScope) {
- mStyleScope->AsNode().RemoveMutationObserver(this);
+ if (mDocumentOrShadowRoot) {
+ mDocumentOrShadowRoot->AsNode().RemoveMutationObserver(this);
}
}
diff --git a/dom/base/StyleSheetList.h b/dom/base/StyleSheetList.h
index ea5c33a983..f8f371a81b 100644
--- a/dom/base/StyleSheetList.h
+++ b/dom/base/StyleSheetList.h
@@ -7,7 +7,7 @@
#ifndef mozilla_dom_StyleSheetList_h
#define mozilla_dom_StyleSheetList_h
-#include "mozilla/dom/StyleScope.h"
+#include "mozilla/dom/DocumentOrShadowRoot.h"
#include "nsIDOMStyleSheetList.h"
#include "nsWrapperCache.h"
#include "nsStubDocumentObserver.h"
@@ -31,28 +31,28 @@ public:
NS_DECL_NSIMUTATIONOBSERVER_NODEWILLBEDESTROYED
- explicit StyleSheetList(StyleScope& aScope);
+ explicit StyleSheetList(DocumentOrShadowRoot& aScope);
virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override final;
nsINode* GetParentObject() const
{
- return mStyleScope ? &mStyleScope->AsNode() : nullptr;
+ return mDocumentOrShadowRoot ? &mDocumentOrShadowRoot->AsNode() : nullptr;
}
uint32_t Length() const
{
- return mStyleScope ? mStyleScope->SheetCount() : 0;
+ return mDocumentOrShadowRoot ? mDocumentOrShadowRoot->SheetCount() : 0;
}
StyleSheet* IndexedGetter(uint32_t aIndex, bool& aFound) const
{
- if (!mStyleScope) {
+ if (!mDocumentOrShadowRoot) {
aFound = false;
return nullptr;
}
- StyleSheet* sheet = mStyleScope->SheetAt(aIndex);
+ StyleSheet* sheet = mDocumentOrShadowRoot->SheetAt(aIndex);
aFound = !!sheet;
return sheet;
}
@@ -66,7 +66,7 @@ public:
protected:
virtual ~StyleSheetList();
- StyleScope* mStyleScope; // Weak, cleared on "NodeWillBeDestroyed".
+ DocumentOrShadowRoot* mDocumentOrShadowRoot; // Weak, cleared on "NodeWillBeDestroyed".
};
} // namespace dom
diff --git a/dom/base/moz.build b/dom/base/moz.build
index ab0f0e0abc..cedaa0e49d 100755
--- a/dom/base/moz.build
+++ b/dom/base/moz.build
@@ -164,6 +164,7 @@ EXPORTS.mozilla.dom += [
'DirectionalityUtils.h',
'DocGroup.h',
'DocumentFragment.h',
+ 'DocumentOrShadowRoot.h',
'DocumentType.h',
'DOMCursor.h',
'DOMError.h',
@@ -214,7 +215,6 @@ EXPORTS.mozilla.dom += [
'SimpleTreeIterator.h',
'StructuredCloneHolder.h',
'StructuredCloneTags.h',
- 'StyleScope.h',
'StyleSheetList.h',
'SubtleCrypto.h',
'TabGroup.h',
@@ -243,6 +243,7 @@ SOURCES += [
'DirectionalityUtils.cpp',
'DocGroup.cpp',
'DocumentFragment.cpp',
+ 'DocumentOrShadowRoot.cpp',
'DocumentType.cpp',
'DOMCursor.cpp',
'DOMError.cpp',
@@ -361,7 +362,6 @@ SOURCES += [
'ScriptSettings.cpp',
'ShadowRoot.cpp',
'StructuredCloneHolder.cpp',
- 'StyleScope.cpp',
'StyleSheetList.cpp',
'SubtleCrypto.cpp',
'TabGroup.cpp',
diff --git a/dom/base/nsContentUtils.cpp b/dom/base/nsContentUtils.cpp
index c0d81a41e0..61d10e0223 100644
--- a/dom/base/nsContentUtils.cpp
+++ b/dom/base/nsContentUtils.cpp
@@ -3658,6 +3658,14 @@ nsContentUtils::ReportToConsole(uint32_t aErrorFlags,
aLineNumber, aColumnNumber);
}
+/* static */ void
+nsContentUtils::ReportEmptyGetElementByIdArg(const nsIDocument* aDoc)
+{
+ ReportToConsole(nsIScriptError::warningFlag,
+ NS_LITERAL_CSTRING("DOM"), aDoc,
+ nsContentUtils::eDOM_PROPERTIES,
+ "EmptyGetElementByIdParam");
+}
/* static */ nsresult
nsContentUtils::ReportToConsoleNonLocalized(const nsAString& aErrorText,
diff --git a/dom/base/nsContentUtils.h b/dom/base/nsContentUtils.h
index b58b0e0e3c..8cf105bb3e 100644
--- a/dom/base/nsContentUtils.h
+++ b/dom/base/nsContentUtils.h
@@ -941,6 +941,8 @@ public:
uint32_t aLineNumber = 0,
uint32_t aColumnNumber = 0);
+ static void ReportEmptyGetElementByIdArg(const nsIDocument* aDoc);
+
static void LogMessageToConsole(const char* aMsg);
/**
diff --git a/dom/base/nsDocument.cpp b/dom/base/nsDocument.cpp
index 7b280a188d..40bfa97e2a 100644
--- a/dom/base/nsDocument.cpp
+++ b/dom/base/nsDocument.cpp
@@ -300,9 +300,24 @@ GetHttpChannelHelper(nsIChannel* aChannel, nsIHttpChannel** aHttpChannel)
#define NAME_NOT_VALID ((nsSimpleContentList*)1)
+nsIdentifierMapEntry::nsIdentifierMapEntry(const nsIdentifierMapEntry::AtomOrString& aKey)
+ : mKey(aKey)
+{}
+
+nsIdentifierMapEntry::nsIdentifierMapEntry(const nsIdentifierMapEntry::AtomOrString* aKey)
+ : mKey(aKey ? *aKey : nullptr)
+{}
+
nsIdentifierMapEntry::~nsIdentifierMapEntry()
-{
-}
+{}
+
+nsIdentifierMapEntry::nsIdentifierMapEntry(nsIdentifierMapEntry&& aOther)
+ : mKey(mozilla::Move(aOther.mKey))
+ , mIdContentList(mozilla::Move(aOther.mIdContentList))
+ , mNameContentList(mozilla::Move(aOther.mNameContentList))
+ , mChangeCallbacks(mozilla::Move(aOther.mChangeCallbacks))
+ , mImageElement(mozilla::Move(aOther.mImageElement))
+{}
void
nsIdentifierMapEntry::Traverse(nsCycleCollectionTraversalCallback* aCallback)
@@ -326,6 +341,12 @@ nsIdentifierMapEntry::IsEmpty()
!mChangeCallbacks && !mImageElement;
}
+bool
+nsIdentifierMapEntry::HasNameElement() const
+{
+ return mNameContentList && mNameContentList->Length() != 0;
+}
+
Element*
nsIdentifierMapEntry::GetIdElement()
{
@@ -537,7 +558,7 @@ nsIdentifierMapEntry::HasIdElementExposedAsHTMLDocumentProperty()
size_t
nsIdentifierMapEntry::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
{
- return nsStringHashKey::SizeOfExcludingThis(aMallocSizeOf);
+ return mKey.mString.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
}
// Helper structs for the content->subdoc map
@@ -1226,6 +1247,7 @@ static already_AddRefed<mozilla::dom::NodeInfo> nullNodeInfo;
// ==================================================================
nsIDocument::nsIDocument()
: nsINode(nullNodeInfo),
+ DocumentOrShadowRoot(this),
mReferrerPolicySet(false),
mReferrerPolicy(mozilla::net::RP_Default),
mBlockAllMixedContent(false),
@@ -2704,8 +2726,7 @@ nsDocument::AddToNameTable(Element *aElement, nsIAtom* aName)
"Only put elements that need to be exposed as document['name'] in "
"the named table.");
- nsIdentifierMapEntry *entry =
- mIdentifierMap.PutEntry(nsDependentAtomString(aName));
+ nsIdentifierMapEntry* entry = mIdentifierMap.PutEntry(aName);
// Null for out-of-memory
if (entry) {
@@ -2724,8 +2745,7 @@ nsDocument::RemoveFromNameTable(Element *aElement, nsIAtom* aName)
if (mIdentifierMap.Count() == 0)
return;
- nsIdentifierMapEntry *entry =
- mIdentifierMap.GetEntry(nsDependentAtomString(aName));
+ nsIdentifierMapEntry* entry = mIdentifierMap.GetEntry(aName);
if (!entry) // Could be false if the element was anonymous, hence never added
return;
@@ -2739,8 +2759,7 @@ nsDocument::RemoveFromNameTable(Element *aElement, nsIAtom* aName)
void
nsDocument::AddToIdTable(Element *aElement, nsIAtom* aId)
{
- nsIdentifierMapEntry *entry =
- mIdentifierMap.PutEntry(nsDependentAtomString(aId));
+ nsIdentifierMapEntry* entry = mIdentifierMap.PutEntry(aId);
if (entry) { /* True except on OOM */
if (nsGenericHTMLElement::ShouldExposeIdAsHTMLDocumentProperty(aElement) &&
@@ -2762,8 +2781,7 @@ nsDocument::RemoveFromIdTable(Element *aElement, nsIAtom* aId)
return;
}
- nsIdentifierMapEntry *entry =
- mIdentifierMap.GetEntry(nsDependentAtomString(aId));
+ nsIdentifierMapEntry* entry = mIdentifierMap.GetEntry(aId);
if (!entry) // Can be null for XML elements with changing ids.
return;
@@ -3013,20 +3031,9 @@ Element*
nsIDocument::GetActiveElement()
{
// Get the focused element.
- if (nsCOMPtr<nsPIDOMWindowOuter> window = GetWindow()) {
- nsCOMPtr<nsPIDOMWindowOuter> focusedWindow;
- nsIContent* focusedContent =
- nsFocusManager::GetFocusedDescendant(window, false,
- getter_AddRefs(focusedWindow));
- // be safe and make sure the element is from this document
- if (focusedContent && focusedContent->OwnerDoc() == this) {
- if (focusedContent->ChromeOnlyAccess()) {
- focusedContent = focusedContent->FindFirstNonChromeOnlyAccessContent();
- }
- if (focusedContent) {
- return focusedContent->AsElement();
- }
- }
+ Element* focusedElement = GetRetargetedFocusedElement();
+ if (focusedElement) {
+ return focusedElement;
}
// No focused element anywhere in this document. Try to get the BODY.
@@ -3238,12 +3245,6 @@ nsDocument::GetElementsByClassName(const nsAString& aClasses,
return NS_OK;
}
-already_AddRefed<nsContentList>
-nsIDocument::GetElementsByClassName(const nsAString& aClasses)
-{
- return nsContentUtils::GetElementsByClassName(this, aClasses);
-}
-
NS_IMETHODIMP
nsDocument::ReleaseCapture()
{
@@ -4733,32 +4734,7 @@ nsDocument::BeginLoad()
void
nsDocument::ReportEmptyGetElementByIdArg()
{
- nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
- NS_LITERAL_CSTRING("DOM"), this,
- nsContentUtils::eDOM_PROPERTIES,
- "EmptyGetElementByIdParam");
-}
-
-Element*
-nsDocument::GetElementById(const nsAString& aElementId)
-{
- if (!CheckGetElementByIdArg(aElementId)) {
- return nullptr;
- }
-
- nsIdentifierMapEntry *entry = mIdentifierMap.GetEntry(aElementId);
- return entry ? entry->GetIdElement() : nullptr;
-}
-
-const nsTArray<Element*>*
-nsDocument::GetAllElementsForId(const nsAString& aElementId) const
-{
- if (aElementId.IsEmpty()) {
- return nullptr;
- }
-
- nsIdentifierMapEntry *entry = mIdentifierMap.GetEntry(aElementId);
- return entry ? &entry->GetIdElements() : nullptr;
+ nsContentUtils::ReportEmptyGetElementByIdArg(this);
}
NS_IMETHODIMP
@@ -4783,7 +4759,7 @@ nsDocument::AddIDTargetObserver(nsIAtom* aID, IDTargetObserver aObserver,
if (!CheckGetElementByIdArg(id))
return nullptr;
- nsIdentifierMapEntry *entry = mIdentifierMap.PutEntry(id);
+ nsIdentifierMapEntry* entry = mIdentifierMap.PutEntry(aID);
NS_ENSURE_TRUE(entry, nullptr);
entry->AddContentChangeCallback(aObserver, aData, aForImage);
@@ -4799,7 +4775,7 @@ nsDocument::RemoveIDTargetObserver(nsIAtom* aID, IDTargetObserver aObserver,
if (!CheckGetElementByIdArg(id))
return;
- nsIdentifierMapEntry *entry = mIdentifierMap.GetEntry(id);
+ nsIdentifierMapEntry* entry = mIdentifierMap.GetEntry(aID);
if (!entry) {
return;
}
@@ -5650,27 +5626,6 @@ nsDocument::BlockedTrackingNodes() const
return list.forget();
}
-already_AddRefed<nsContentList>
-nsIDocument::GetElementsByTagNameNS(const nsAString& aNamespaceURI,
- const nsAString& aLocalName,
- ErrorResult& aResult)
-{
- int32_t nameSpaceId = kNameSpaceID_Wildcard;
-
- if (!aNamespaceURI.EqualsLiteral("*")) {
- aResult =
- nsContentUtils::NameSpaceManager()->RegisterNameSpace(aNamespaceURI,
- nameSpaceId);
- if (aResult.Failed()) {
- return nullptr;
- }
- }
-
- NS_ASSERTION(nameSpaceId != kNameSpaceID_Unknown, "Unexpected namespace ID!");
-
- return NS_GetContentList(this, nameSpaceId, aLocalName);
-}
-
NS_IMETHODIMP
nsDocument::GetElementsByTagNameNS(const nsAString& aNamespaceURI,
const nsAString& aLocalName,
@@ -5678,7 +5633,7 @@ nsDocument::GetElementsByTagNameNS(const nsAString& aNamespaceURI,
{
ErrorResult rv;
RefPtr<nsContentList> list =
- nsIDocument::GetElementsByTagNameNS(aNamespaceURI, aLocalName, rv);
+ GetElementsByTagNameNS(aNamespaceURI, aLocalName, rv);
if (rv.Failed()) {
return rv.StealNSResult();
}
diff --git a/dom/base/nsDocument.h b/dom/base/nsDocument.h
index 6baf40270a..9d7b0aafd1 100644
--- a/dom/base/nsDocument.h
+++ b/dom/base/nsDocument.h
@@ -333,7 +333,6 @@ class nsDocument : public nsIDocument,
public:
typedef mozilla::dom::Element Element;
- using nsIDocument::GetElementsByTagName;
typedef mozilla::net::ReferrerPolicy ReferrerPolicy;
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
@@ -631,6 +630,11 @@ public:
// nsIDOMDocumentXBL
NS_DECL_NSIDOMDOCUMENTXBL
+ using mozilla::dom::DocumentOrShadowRoot::GetElementById;
+ using mozilla::dom::DocumentOrShadowRoot::GetElementsByTagName;
+ using mozilla::dom::DocumentOrShadowRoot::GetElementsByTagNameNS;
+ using mozilla::dom::DocumentOrShadowRoot::GetElementsByClassName;
+
// nsIDOMEventTarget
virtual nsresult GetEventTargetParent(
mozilla::EventChainPreVisitor& aVisitor) override;
@@ -819,10 +823,7 @@ public:
virtual void ResetScrolledToRefAlready() override;
virtual void SetChangeScrollPosWhenScrollingToRef(bool aValue) override;
- virtual Element *GetElementById(const nsAString& aElementId) override;
- virtual const nsTArray<Element*>* GetAllElementsForId(const nsAString& aElementId) const override;
-
- virtual Element *LookupImageElement(const nsAString& aElementId) override;
+ virtual Element* LookupImageElement(const nsAString& aElementId) override;
virtual void MozSetImageElement(const nsAString& aImageElementId,
Element* aElement) override;
@@ -1206,14 +1207,6 @@ public:
RefPtr<nsDOMStyleSheetSetList> mStyleSheetSetList;
RefPtr<nsScriptLoader> mScriptLoader;
nsDocHeaderData* mHeaderData;
- /* mIdentifierMap works as follows for IDs:
- * 1) Attribute changes affect the table immediately (removing and adding
- * entries as needed).
- * 2) Removals from the DOM affect the table immediately
- * 3) Additions to the DOM always update existing entries for names, and add
- * new ones for IDs.
- */
- nsTHashtable<nsIdentifierMapEntry> mIdentifierMap;
nsClassHashtable<nsStringHashKey, nsRadioGroupStruct> mRadioGroups;
diff --git a/dom/base/nsFocusManager.cpp b/dom/base/nsFocusManager.cpp
index c14087c8a7..3fc9546e84 100644
--- a/dom/base/nsFocusManager.cpp
+++ b/dom/base/nsFocusManager.cpp
@@ -813,10 +813,11 @@ nsFocusManager::ContentRemoved(nsIDocument* aDocument, nsIContent* aContent)
if (!window)
return NS_OK;
- // if the content is currently focused in the window, or is an ancestor
- // of the currently focused element, reset the focus within that window.
+ // if the content is currently focused in the window, or is an
+ // shadow-including inclusive ancestor of the currently focused element,
+ // reset the focus within that window.
nsIContent* content = window->GetFocusedNode();
- if (content && nsContentUtils::ContentIsDescendantOf(content, aContent)) {
+ if (content && nsContentUtils::ContentIsHostIncludingDescendantOf(content, aContent)) {
bool shouldShowFocusRing = window->ShouldShowFocusRing();
window->SetFocusedNode(nullptr);
diff --git a/dom/base/nsIContent.h b/dom/base/nsIContent.h
index ce0a4e5960..101d5e931b 100644
--- a/dom/base/nsIContent.h
+++ b/dom/base/nsIContent.h
@@ -697,6 +697,14 @@ public:
virtual mozilla::dom::ShadowRoot *GetContainingShadow() const = 0;
/**
+ * Gets the shadow host if this content is in a shadow tree. That is, the host
+ * of |GetContainingShadow|, if its not null.
+ *
+ * @return The shadow host, if this is in shadow tree, or null.
+ */
+ nsIContent* GetContainingShadowHost() const;
+
+ /**
* Gets the assigned slot associated with this content.
*
* @return The assigned slot element or null.
diff --git a/dom/base/nsIDocument.h b/dom/base/nsIDocument.h
index b4fda21c1a..d88db84237 100644
--- a/dom/base/nsIDocument.h
+++ b/dom/base/nsIDocument.h
@@ -34,7 +34,7 @@
#include "prclist.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/CORSMode.h"
-#include "mozilla/dom/StyleScope.h"
+#include "mozilla/dom/DocumentOrShadowRoot.h"
#include "mozilla/LinkedList.h"
#include "mozilla/StyleBackendType.h"
#include "mozilla/StyleSheet.h"
@@ -198,7 +198,7 @@ class nsContentList;
// Document interface. This is implemented by all document objects in
// Gecko.
class nsIDocument : public nsINode,
- public mozilla::dom::StyleScope
+ public mozilla::dom::DocumentOrShadowRoot
{
typedef mozilla::dom::GlobalObject GlobalObject;
@@ -499,7 +499,7 @@ public:
* to remove it.
*/
typedef bool (* IDTargetObserver)(Element* aOldElement,
- Element* aNewelement, void* aData);
+ Element* aNewelement, void* aData);
/**
* Add an IDTargetObserver for a specific ID. The IDTargetObserver
@@ -1071,14 +1071,9 @@ public:
*/
virtual void EnsureOnDemandBuiltInUASheet(mozilla::StyleSheet* aSheet) = 0;
- nsINode& AsNode() final
- {
- return *this;
- }
-
mozilla::dom::StyleSheetList* StyleSheets()
{
- return &StyleScope::EnsureDOMStyleSheets();
+ return &DocumentOrShadowRoot::EnsureDOMStyleSheets();
}
/**
@@ -2362,19 +2357,10 @@ public:
virtual void ResetScrolledToRefAlready() = 0;
virtual void SetChangeScrollPosWhenScrollingToRef(bool aValue) = 0;
- /**
- * This method is similar to GetElementById() from nsIDOMDocument but it
- * returns a mozilla::dom::Element instead of a nsIDOMElement.
- * It prevents converting nsIDOMElement to mozilla::dom::Element which is
- * already converted from mozilla::dom::Element.
- */
- virtual Element* GetElementById(const nsAString& aElementId) = 0;
-
- /**
- * This method returns _all_ the elements in this document which
- * have id aElementId, if there are any. Otherwise it returns null.
- */
- virtual const nsTArray<Element*>* GetAllElementsForId(const nsAString& aElementId) const = 0;
+ using mozilla::dom::DocumentOrShadowRoot::GetElementById;
+ using mozilla::dom::DocumentOrShadowRoot::GetElementsByTagName;
+ using mozilla::dom::DocumentOrShadowRoot::GetElementsByTagNameNS;
+ using mozilla::dom::DocumentOrShadowRoot::GetElementsByClassName;
/**
* Lookup an image element using its associated ID, which is usually provided
@@ -2574,18 +2560,6 @@ public:
nsIDocument* GetTopLevelContentDocument();
- already_AddRefed<nsContentList>
- GetElementsByTagName(const nsAString& aTagName)
- {
- return NS_GetContentList(this, kNameSpaceID_Unknown, aTagName);
- }
- already_AddRefed<nsContentList>
- GetElementsByTagNameNS(const nsAString& aNamespaceURI,
- const nsAString& aLocalName,
- mozilla::ErrorResult& aResult);
- already_AddRefed<nsContentList>
- GetElementsByClassName(const nsAString& aClasses);
- // GetElementById defined above
virtual already_AddRefed<Element>
CreateElement(const nsAString& aTagName,
const mozilla::dom::ElementCreationOptionsOrString& aOptions,
diff --git a/dom/base/nsIdentifierMapEntry.h b/dom/base/nsIdentifierMapEntry.h
index fce506cef3..119a7e4534 100644
--- a/dom/base/nsIdentifierMapEntry.h
+++ b/dom/base/nsIdentifierMapEntry.h
@@ -15,18 +15,24 @@
#include "mozilla/MemoryReporting.h"
#include "mozilla/Move.h"
-#include "mozilla/dom/Element.h"
#include "mozilla/net/ReferrerPolicy.h"
#include "nsCOMArray.h"
#include "nsCOMPtr.h"
-#include "nsContentList.h"
#include "nsIAtom.h"
-#include "nsIDocument.h"
#include "nsTArray.h"
#include "nsTHashtable.h"
+#include "nsHashKeys.h"
class nsIContent;
+class nsContentList;
+class nsBaseContentList;
+
+namespace mozilla {
+namespace dom {
+ class Element;
+} // namespace dom
+} // namespace mozilla
/**
* Right now our identifier map entries contain information for 'name'
@@ -40,26 +46,84 @@ class nsIContent;
* Perhaps the document.all results should have their own hashtable
* in nsHTMLDocument.
*/
-class nsIdentifierMapEntry : public nsStringHashKey
+class nsIdentifierMapEntry : public PLDHashEntryHdr
{
-public:
typedef mozilla::dom::Element Element;
typedef mozilla::net::ReferrerPolicy ReferrerPolicy;
- explicit nsIdentifierMapEntry(const nsAString& aKey) :
- nsStringHashKey(&aKey), mNameContentList(nullptr)
+ /**
+ * @see nsIDocument::IDTargetObserver, this is just here to avoid include
+ * hell.
+ */
+ typedef bool (* IDTargetObserver)(Element* aOldElement,
+ Element* aNewelement, void* aData);
+
+public:
+ struct AtomOrString
{
+ MOZ_IMPLICIT AtomOrString(nsIAtom* aAtom) : mAtom(aAtom) {}
+ MOZ_IMPLICIT AtomOrString(const nsAString& aString) : mString(aString) {}
+ AtomOrString(const AtomOrString& aOther)
+ : mAtom(aOther.mAtom)
+ , mString(aOther.mString)
+ {
+ }
+
+ AtomOrString(AtomOrString&& aOther)
+ : mAtom(aOther.mAtom.forget())
+ , mString(aOther.mString)
+ {
+ }
+
+ nsCOMPtr<nsIAtom> mAtom;
+ const nsString mString;
+ };
+
+ typedef const AtomOrString& KeyType;
+ typedef const AtomOrString* KeyTypePointer;
+
+ explicit nsIdentifierMapEntry(const AtomOrString& aKey);
+ explicit nsIdentifierMapEntry(const AtomOrString* aKey);
+ nsIdentifierMapEntry(nsIdentifierMapEntry&& aOther);
+ ~nsIdentifierMapEntry();
+
+ KeyType GetKey() const { return mKey; }
+
+ nsString GetKeyAsString() const
+ {
+ if (mKey.mAtom) {
+ return nsAtomString(mKey.mAtom);
+ }
+
+ return mKey.mString;
}
- explicit nsIdentifierMapEntry(const nsAString* aKey) :
- nsStringHashKey(aKey), mNameContentList(nullptr)
+
+ bool KeyEquals(const KeyTypePointer aOtherKey) const
{
+ if (mKey.mAtom) {
+ if (aOtherKey->mAtom) {
+ return mKey.mAtom == aOtherKey->mAtom;
+ }
+
+ return mKey.mAtom->Equals(aOtherKey->mString);
+ }
+
+ if (aOtherKey->mAtom) {
+ return aOtherKey->mAtom->Equals(mKey.mString);
+ }
+
+ return mKey.mString.Equals(aOtherKey->mString);
}
- nsIdentifierMapEntry(const nsIdentifierMapEntry& aOther) :
- nsStringHashKey(&aOther.GetKey())
+
+ static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
+
+ static PLDHashNumber HashKey(const KeyTypePointer aKey)
{
- NS_ERROR("Should never be called");
+ return aKey->mAtom ?
+ aKey->mAtom->hash() : mozilla::HashString(aKey->mString);
}
- ~nsIdentifierMapEntry();
+
+ enum { ALLOW_MEMMOVE = false };
void AddNameElement(nsINode* aDocument, Element* aElement);
void RemoveNameElement(Element* aElement);
@@ -67,9 +131,7 @@ public:
nsBaseContentList* GetNameContentList() {
return mNameContentList;
}
- bool HasNameElement() const {
- return mNameContentList && mNameContentList->Length() != 0;
- }
+ bool HasNameElement() const;
/**
* Returns the element if we know the element associated with this
@@ -109,9 +171,9 @@ public:
bool HasIdElementExposedAsHTMLDocumentProperty();
bool HasContentChangeCallback() { return mChangeCallbacks != nullptr; }
- void AddContentChangeCallback(nsIDocument::IDTargetObserver aCallback,
+ void AddContentChangeCallback(IDTargetObserver aCallback,
void* aData, bool aForImage);
- void RemoveContentChangeCallback(nsIDocument::IDTargetObserver aCallback,
+ void RemoveContentChangeCallback(IDTargetObserver aCallback,
void* aData, bool aForImage);
/**
@@ -122,7 +184,7 @@ public:
void Traverse(nsCycleCollectionTraversalCallback* aCallback);
struct ChangeCallback {
- nsIDocument::IDTargetObserver mCallback;
+ IDTargetObserver mCallback;
void* mData;
bool mForImage;
};
@@ -156,12 +218,16 @@ public:
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
private:
+ nsIdentifierMapEntry(const nsIdentifierMapEntry& aOther) = delete;
+ nsIdentifierMapEntry& operator=(const nsIdentifierMapEntry& aOther) = delete;
+
void FireChangeCallbacks(Element* aOldElement, Element* aNewElement,
bool aImageOnly = false);
+ AtomOrString mKey;
// empty if there are no elements with this ID.
// The elements are stored as weak pointers.
- nsTArray<Element*> mIdContentList;
+ AutoTArray<Element*, 1> mIdContentList;
RefPtr<nsBaseContentList> mNameContentList;
nsAutoPtr<nsTHashtable<ChangeCallbackEntry> > mChangeCallbacks;
RefPtr<Element> mImageElement;
diff --git a/dom/base/nsNameSpaceManager.h b/dom/base/nsNameSpaceManager.h
index d5c3a25fe8..e6dc67a3bb 100644
--- a/dom/base/nsNameSpaceManager.h
+++ b/dom/base/nsNameSpaceManager.h
@@ -10,13 +10,13 @@
#include "nsDataHashtable.h"
#include "nsHashKeys.h"
#include "nsIAtom.h"
-#include "nsIDocument.h"
#include "nsIObserver.h"
#include "nsTArray.h"
#include "mozilla/StaticPtr.h"
class nsAString;
+class nsIDocument;
/**
* The Name Space Manager tracks the association between a NameSpace
diff --git a/dom/html/nsHTMLDocument.cpp b/dom/html/nsHTMLDocument.cpp
index bd9de39f03..f3cb096b9a 100644
--- a/dom/html/nsHTMLDocument.cpp
+++ b/dom/html/nsHTMLDocument.cpp
@@ -2094,7 +2094,7 @@ nsHTMLDocument::GetSupportedNames(nsTArray<nsString>& aNames)
nsIdentifierMapEntry* entry = iter.Get();
if (entry->HasNameElement() ||
entry->HasIdElementExposedAsHTMLDocumentProperty()) {
- aNames.AppendElement(entry->GetKey());
+ aNames.AppendElement(entry->GetKeyAsString());
}
}
}
diff --git a/dom/html/nsHTMLDocument.h b/dom/html/nsHTMLDocument.h
index c9e46b3faf..4f44befbba 100644
--- a/dom/html/nsHTMLDocument.h
+++ b/dom/html/nsHTMLDocument.h
@@ -155,10 +155,7 @@ public:
virtual void RemovedFromDocShell() override;
- virtual mozilla::dom::Element *GetElementById(const nsAString& aElementId) override
- {
- return nsDocument::GetElementById(aElementId);
- }
+ using mozilla::dom::DocumentOrShadowRoot::GetElementById;
virtual void DocAddSizeOfExcludingThis(nsWindowSizes* aWindowSizes) const override;
// DocAddSizeOfIncludingThis is inherited from nsIDocument.
diff --git a/dom/webidl/Document.webidl b/dom/webidl/Document.webidl
index ba2ec17ea7..26985202be 100644
--- a/dom/webidl/Document.webidl
+++ b/dom/webidl/Document.webidl
@@ -136,7 +136,6 @@ partial interface Document {
// user interaction
[Pure]
readonly attribute WindowProxy? defaultView;
- readonly attribute Element? activeElement;
[Throws]
boolean hasFocus();
//(HTML only) attribute DOMString designMode;
@@ -283,8 +282,6 @@ partial interface Document {
// http://dev.w3.org/csswg/cssom/#extensions-to-the-document-interface
partial interface Document {
- [Constant]
- readonly attribute StyleSheetList styleSheets;
attribute DOMString? selectedStyleSheetSet;
readonly attribute DOMString? lastStyleSheetSet;
readonly attribute DOMString? preferredStyleSheetSet;
@@ -456,3 +453,4 @@ Document implements ParentNode;
Document implements OnErrorEventHandlerForNodes;
Document implements GeometryUtils;
Document implements FontFaceSource;
+Document implements DocumentOrShadowRoot;
diff --git a/dom/webidl/DocumentOrShadowRoot.webidl b/dom/webidl/DocumentOrShadowRoot.webidl
new file mode 100644
index 0000000000..16388d1264
--- /dev/null
+++ b/dom/webidl/DocumentOrShadowRoot.webidl
@@ -0,0 +1,29 @@
+/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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/.
+ *
+ * The origin of this IDL file is
+ * https://dom.spec.whatwg.org/#documentorshadowroot
+ * http://w3c.github.io/webcomponents/spec/shadow/#extensions-to-the-documentorshadowroot-mixin
+ */
+
+[NoInterfaceObject]
+interface DocumentOrShadowRoot {
+ // Not implemented yet: bug 1430308.
+ // Selection? getSelection();
+ // Not implemented yet: bug 1430301.
+ // Element? elementFromPoint (float x, float y);
+ // Not implemented yet: bug 1430301.
+ // sequence<Element> elementsFromPoint (float x, float y);
+ // Not implemented yet: bug 1430307.
+ // CaretPosition? caretPositionFromPoint (float x, float y);
+
+ readonly attribute Element? activeElement;
+ readonly attribute StyleSheetList styleSheets;
+
+ // Not implemented yet: bug 1430303.
+ // readonly attribute Element? pointerLockElement;
+ // Not implemented yet: bug 1430305.
+ // readonly attribute Element? fullscreenElement;
+};
diff --git a/dom/webidl/ShadowRoot.webidl b/dom/webidl/ShadowRoot.webidl
index 47e6cf5ec5..83acd4161d 100644
--- a/dom/webidl/ShadowRoot.webidl
+++ b/dom/webidl/ShadowRoot.webidl
@@ -32,6 +32,5 @@ interface ShadowRoot : DocumentFragment
[CEReactions, SetterThrows, TreatNullAs=EmptyString]
attribute DOMString innerHTML;
attribute boolean applyAuthorStyles;
- readonly attribute StyleSheetList styleSheets;
};
diff --git a/dom/webidl/moz.build b/dom/webidl/moz.build
index 8a86da783f..acdc392be5 100644
--- a/dom/webidl/moz.build
+++ b/dom/webidl/moz.build
@@ -113,6 +113,7 @@ WEBIDL_FILES = [
'Directory.webidl',
'Document.webidl',
'DocumentFragment.webidl',
+ 'DocumentOrShadowRoot.webidl',
'DocumentTimeline.webidl',
'DocumentType.webidl',
'DOMCursor.webidl',
diff --git a/dom/xul/XULDocument.cpp b/dom/xul/XULDocument.cpp
index 929efc1afc..d3b94920a3 100644
--- a/dom/xul/XULDocument.cpp
+++ b/dom/xul/XULDocument.cpp
@@ -1578,24 +1578,13 @@ XULDocument::GetCommandDispatcher(nsIDOMXULCommandDispatcher** aTracker)
}
Element*
-XULDocument::GetElementById(const nsAString& aId)
+XULDocument::GetRefById(const nsAString& aID)
{
- if (!CheckGetElementByIdArg(aId))
- return nullptr;
-
- nsIdentifierMapEntry *entry = mIdentifierMap.GetEntry(aId);
- if (entry) {
- Element* element = entry->GetIdElement();
- if (element)
- return element;
- }
-
- nsRefMapEntry* refEntry = mRefMap.GetEntry(aId);
- if (refEntry) {
- NS_ASSERTION(refEntry->GetFirstElement(),
- "nsRefMapEntries should have nonempty content lists");
+ if (nsRefMapEntry* refEntry = mRefMap.GetEntry(aID)) {
+ MOZ_ASSERT(refEntry->GetFirstElement());
return refEntry->GetFirstElement();
}
+
return nullptr;
}
diff --git a/dom/xul/XULDocument.h b/dom/xul/XULDocument.h
index 06abb797f0..a5ed497049 100644
--- a/dom/xul/XULDocument.h
+++ b/dom/xul/XULDocument.h
@@ -148,6 +148,7 @@ public:
using nsDocument::CreateElementNS;
NS_FORWARD_NSIDOMDOCUMENT(XMLDocument::)
// And explicitly import the things from nsDocument that we just shadowed
+ using mozilla::dom::DocumentOrShadowRoot::GetElementById;
using nsDocument::GetImplementation;
using nsDocument::GetTitle;
using nsDocument::SetTitle;
@@ -156,8 +157,8 @@ public:
using nsDocument::GetMozFullScreenElement;
using nsIDocument::GetLocation;
- // nsDocument interface overrides
- virtual Element* GetElementById(const nsAString & elementId) override;
+ // Helper for StyleScope::GetElementById.
+ Element* GetRefById(const nsAString & elementId);
// nsIDOMXULDocument interface
NS_DECL_NSIDOMXULDOCUMENT
diff --git a/editor/libeditor/TextEditor.cpp b/editor/libeditor/TextEditor.cpp
index c3cfa4a721..07b06a96aa 100644
--- a/editor/libeditor/TextEditor.cpp
+++ b/editor/libeditor/TextEditor.cpp
@@ -24,6 +24,7 @@
#include "nsCharTraits.h"
#include "nsComponentManagerUtils.h"
#include "nsContentCID.h"
+#include "nsContentList.h"
#include "nsCopySupport.h"
#include "nsDebug.h"
#include "nsDependentSubstring.h"
diff --git a/testing/web-platform/meta/shadow-dom/ShadowRoot-interface.html.ini b/testing/web-platform/meta/shadow-dom/ShadowRoot-interface.html.ini
deleted file mode 100644
index 546e41144f..0000000000
--- a/testing/web-platform/meta/shadow-dom/ShadowRoot-interface.html.ini
+++ /dev/null
@@ -1,29 +0,0 @@
-[ShadowRoot-interface.html]
- type: testharness
- [ShadowRoot.activeElement must return the focused element of the context object when shadow root is open.]
- expected: FAIL
-
- [ShadowRoot.activeElement must return the focused element of the context object when shadow root is closed.]
- expected: FAIL
-
- [ShadowRoot.host must return the shadow host of the context object.]
- expected: FAIL
-
- [ShadowRoot.innerHTML must return the result of the HTML fragment serialization algorithm when shadow root is open.]
- expected: FAIL
-
- [ShadowRoot.innerHTML must return the result of the HTML fragment serialization algorithm when shadow root is closed.]
- expected: FAIL
-
- [ShadowRoot.innerHTML must replace all with the result of invoking the fragment parsing algorithm when shadow root is open.]
- expected: FAIL
-
- [ShadowRoot.innerHTML must replace all with the result of invoking the fragment parsing algorithm when shadow root is closed.]
- expected: FAIL
-
- [ShadowRoot.styleSheets must return a StyleSheetList sequence containing the shadow root style sheets when shadow root is open.]
- expected: FAIL
-
- [ShadowRoot.styleSheets must return a StyleSheetList sequence containing the shadow root style sheets when shadow root is closed.]
- expected: FAIL
-
diff --git a/testing/web-platform/meta/shadow-dom/untriaged/elements-and-dom-objects/shadowroot-object/shadowroot-attributes/activeElement-confirm-return-null.html.ini b/testing/web-platform/meta/shadow-dom/untriaged/elements-and-dom-objects/shadowroot-object/shadowroot-attributes/activeElement-confirm-return-null.html.ini
deleted file mode 100644
index d82555f733..0000000000
--- a/testing/web-platform/meta/shadow-dom/untriaged/elements-and-dom-objects/shadowroot-object/shadowroot-attributes/activeElement-confirm-return-null.html.ini
+++ /dev/null
@@ -1,11 +0,0 @@
-[activeElement-confirm-return-null.html]
- type: testharness
- [confirm activeElement return null]
- expected: FAIL
-
- [confirm activeElement return null when there is other element in body]
- expected: FAIL
-
- [confirm activeElement return null when focus on the element in the outer shadow tree]
- expected: FAIL
-
diff --git a/testing/web-platform/meta/shadow-dom/untriaged/elements-and-dom-objects/shadowroot-object/shadowroot-attributes/test-007.html.ini b/testing/web-platform/meta/shadow-dom/untriaged/elements-and-dom-objects/shadowroot-object/shadowroot-attributes/test-007.html.ini
deleted file mode 100644
index e621a9779d..0000000000
--- a/testing/web-platform/meta/shadow-dom/untriaged/elements-and-dom-objects/shadowroot-object/shadowroot-attributes/test-007.html.ini
+++ /dev/null
@@ -1,5 +0,0 @@
-[test-007.html]
- type: testharness
- [A_10_01_01_03_01_T01]
- expected: FAIL
-
diff --git a/testing/web-platform/meta/shadow-dom/untriaged/user-interaction/active-element/test-001.html.ini b/testing/web-platform/meta/shadow-dom/untriaged/user-interaction/active-element/test-001.html.ini
deleted file mode 100644
index fdcb443433..0000000000
--- a/testing/web-platform/meta/shadow-dom/untriaged/user-interaction/active-element/test-001.html.ini
+++ /dev/null
@@ -1,5 +0,0 @@
-[test-001.html]
- type: testharness
- [A_07_03_01_T01]
- expected: FAIL
-
diff --git a/testing/web-platform/meta/shadow-dom/untriaged/user-interaction/active-element/test-002.html.ini b/testing/web-platform/meta/shadow-dom/untriaged/user-interaction/active-element/test-002.html.ini
deleted file mode 100644
index e2a9f3f2f2..0000000000
--- a/testing/web-platform/meta/shadow-dom/untriaged/user-interaction/active-element/test-002.html.ini
+++ /dev/null
@@ -1,5 +0,0 @@
-[test-002.html]
- type: testharness
- [A_07_03_02_T01]
- expected: FAIL
-