diff options
author | Matt A. Tobin <email@mattatobin.com> | 2020-06-13 08:21:48 -0400 |
---|---|---|
committer | Matt A. Tobin <email@mattatobin.com> | 2020-06-13 08:21:48 -0400 |
commit | 43725c7264ca3f63de348d1d1596ce1fe9e64d2d (patch) | |
tree | e1ff1b11cc54e63b665aab1aa3776a62ee56250d /dom/base/DocumentOrShadowRoot.cpp | |
parent | 35754dd1af72abcae49edd2eeb76855d999a4e6d (diff) | |
download | UXP-43725c7264ca3f63de348d1d1596ce1fe9e64d2d.tar UXP-43725c7264ca3f63de348d1d1596ce1fe9e64d2d.tar.gz UXP-43725c7264ca3f63de348d1d1596ce1fe9e64d2d.tar.lz UXP-43725c7264ca3f63de348d1d1596ce1fe9e64d2d.tar.xz UXP-43725c7264ca3f63de348d1d1596ce1fe9e64d2d.zip |
Bug 1426494 - Share more code between nsIDocument and ShadowRoot
Tag #1375
Diffstat (limited to 'dom/base/DocumentOrShadowRoot.cpp')
-rw-r--r-- | dom/base/DocumentOrShadowRoot.cpp | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/dom/base/DocumentOrShadowRoot.cpp b/dom/base/DocumentOrShadowRoot.cpp new file mode 100644 index 000000000..8376ab97e --- /dev/null +++ b/dom/base/DocumentOrShadowRoot.cpp @@ -0,0 +1,104 @@ +/* -*- 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 "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); +} + +} +} |