Bug 1429656 - Implement ShadowRoot.activeElement

Tag #1375
This commit is contained in:
Matt A. Tobin 2020-06-13 08:23:21 -04:00 committed by Roy Tam
commit 693a79ca08
14 changed files with 88 additions and 72 deletions

View file

@ -6,6 +6,8 @@
#include "DocumentOrShadowRoot.h"
#include "mozilla/dom/StyleSheetList.h"
#include "nsDocument.h"
#include "nsFocusManager.h"
#include "ShadowRoot.h"
#include "XULDocument.h"
@ -100,5 +102,48 @@ 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;
}
}
}