From 35754dd1af72abcae49edd2eeb76855d999a4e6d Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Sat, 13 Jun 2020 08:18:46 -0400 Subject: Bug 1355787 - nsIdentifierMapEntry should let one to use either strings or atoms as keys to avoid slow string assignments when possible. Tag #1375 --- dom/base/nsDocument.cpp | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'dom/base/nsDocument.cpp') diff --git a/dom/base/nsDocument.cpp b/dom/base/nsDocument.cpp index 7b280a188..203c44cbf 100644 --- a/dom/base/nsDocument.cpp +++ b/dom/base/nsDocument.cpp @@ -537,7 +537,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 @@ -2704,8 +2704,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 +2723,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 +2737,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 +2759,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; @@ -4783,7 +4779,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 +4795,7 @@ nsDocument::RemoveIDTargetObserver(nsIAtom* aID, IDTargetObserver aObserver, if (!CheckGetElementByIdArg(id)) return; - nsIdentifierMapEntry *entry = mIdentifierMap.GetEntry(id); + nsIdentifierMapEntry* entry = mIdentifierMap.GetEntry(aID); if (!entry) { return; } -- cgit v1.2.3 From 43725c7264ca3f63de348d1d1596ce1fe9e64d2d Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Sat, 13 Jun 2020 08:21:48 -0400 Subject: Bug 1426494 - Share more code between nsIDocument and ShadowRoot Tag #1375 --- dom/base/nsDocument.cpp | 82 ++++++++++++++++--------------------------------- 1 file changed, 26 insertions(+), 56 deletions(-) (limited to 'dom/base/nsDocument.cpp') diff --git a/dom/base/nsDocument.cpp b/dom/base/nsDocument.cpp index 203c44cbf..a1b1408a5 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() { @@ -1226,6 +1247,7 @@ static already_AddRefed nullNodeInfo; // ================================================================== nsIDocument::nsIDocument() : nsINode(nullNodeInfo), + DocumentOrShadowRoot(this), mReferrerPolicySet(false), mReferrerPolicy(mozilla::net::RP_Default), mBlockAllMixedContent(false), @@ -3234,12 +3256,6 @@ nsDocument::GetElementsByClassName(const nsAString& aClasses, return NS_OK; } -already_AddRefed -nsIDocument::GetElementsByClassName(const nsAString& aClasses) -{ - return nsContentUtils::GetElementsByClassName(this, aClasses); -} - NS_IMETHODIMP nsDocument::ReleaseCapture() { @@ -4729,32 +4745,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* -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 @@ -5646,27 +5637,6 @@ nsDocument::BlockedTrackingNodes() const return list.forget(); } -already_AddRefed -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, @@ -5674,7 +5644,7 @@ nsDocument::GetElementsByTagNameNS(const nsAString& aNamespaceURI, { ErrorResult rv; RefPtr list = - nsIDocument::GetElementsByTagNameNS(aNamespaceURI, aLocalName, rv); + GetElementsByTagNameNS(aNamespaceURI, aLocalName, rv); if (rv.Failed()) { return rv.StealNSResult(); } -- cgit v1.2.3 From 5e7917e2c8bd50754535652014c1de0054841912 Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Sat, 13 Jun 2020 08:23:21 -0400 Subject: Bug 1429656 - Implement ShadowRoot.activeElement Tag #1375 --- dom/base/nsDocument.cpp | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) (limited to 'dom/base/nsDocument.cpp') diff --git a/dom/base/nsDocument.cpp b/dom/base/nsDocument.cpp index a1b1408a5..40bfa97e2 100644 --- a/dom/base/nsDocument.cpp +++ b/dom/base/nsDocument.cpp @@ -3031,20 +3031,9 @@ Element* nsIDocument::GetActiveElement() { // Get the focused element. - if (nsCOMPtr window = GetWindow()) { - nsCOMPtr 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. -- cgit v1.2.3