From 3dfffeaeca662cc5c7e9e62143c7c5902587547d Mon Sep 17 00:00:00 2001 From: FranklinDM Date: Fri, 3 Mar 2023 19:15:21 +0800 Subject: [PATCH] Issue #2135 - Bug 1430303: Implement ShadowRoot.pointerLockElement --- dom/base/DocumentOrShadowRoot.cpp | 16 +++ dom/base/DocumentOrShadowRoot.h | 2 + dom/base/nsDocument.cpp | 29 +---- dom/base/nsIDocument.h | 1 - .../file_pointerlock-api-with-shadow.html | 110 ++++++++++++++++++ dom/tests/mochitest/pointerlock/mochitest.ini | 1 + .../pointerlock/test_pointerlock-api.html | 4 +- dom/webidl/Document.webidl | 1 - dom/webidl/DocumentOrShadowRoot.webidl | 3 +- 9 files changed, 138 insertions(+), 29 deletions(-) create mode 100644 dom/tests/mochitest/pointerlock/file_pointerlock-api-with-shadow.html diff --git a/dom/base/DocumentOrShadowRoot.cpp b/dom/base/DocumentOrShadowRoot.cpp index 6213946075..8647825035 100644 --- a/dom/base/DocumentOrShadowRoot.cpp +++ b/dom/base/DocumentOrShadowRoot.cpp @@ -4,6 +4,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "DocumentOrShadowRoot.h" +#include "mozilla/EventStateManager.h" #include "mozilla/dom/StyleSheetList.h" #include "nsDocument.h" #include "nsFocusManager.h" @@ -144,5 +145,20 @@ DocumentOrShadowRoot::GetRetargetedFocusedElement() return nullptr; } +Element* +DocumentOrShadowRoot::GetPointerLockElement() +{ + nsCOMPtr pointerLockedElement = + do_QueryReferent(EventStateManager::sPointerLockedElement); + if (!pointerLockedElement) { + return nullptr; + } + + nsIContent* retargetedPointerLockedElement = Retarget(pointerLockedElement); + return + retargetedPointerLockedElement && retargetedPointerLockedElement->IsElement() ? + retargetedPointerLockedElement->AsElement() : nullptr; +} + } } diff --git a/dom/base/DocumentOrShadowRoot.h b/dom/base/DocumentOrShadowRoot.h index 89117f3317..e622107d03 100644 --- a/dom/base/DocumentOrShadowRoot.h +++ b/dom/base/DocumentOrShadowRoot.h @@ -114,6 +114,8 @@ public: ~DocumentOrShadowRoot() = default; + Element* GetPointerLockElement(); + protected: nsIContent* Retarget(nsIContent* aContent) const; diff --git a/dom/base/nsDocument.cpp b/dom/base/nsDocument.cpp index 2ad5d47b32..a7af75979f 100644 --- a/dom/base/nsDocument.cpp +++ b/dom/base/nsDocument.cpp @@ -11331,7 +11331,7 @@ GetPointerLockError(Element* aElement, Element* aCurrentLock, return "PointerLockDeniedInUse"; } - if (!aElement->IsInUncomposedDoc()) { + if (!aElement->IsInComposedDoc()) { return "PointerLockDeniedNotInDocument"; } @@ -11382,11 +11382,11 @@ ChangePointerLockedElement(Element* aElement, nsIDocument* aDocument, MOZ_ASSERT(aDocument); MOZ_ASSERT(aElement != aPointerLockedElement); if (aPointerLockedElement) { - MOZ_ASSERT(aPointerLockedElement->GetUncomposedDoc() == aDocument); + MOZ_ASSERT(aPointerLockedElement->GetComposedDoc() == aDocument); aPointerLockedElement->ClearPointerLock(); } if (aElement) { - MOZ_ASSERT(aElement->GetUncomposedDoc() == aDocument); + MOZ_ASSERT(aElement->GetComposedDoc() == aDocument); aElement->SetPointerLock(); EventStateManager::sPointerLockedElement = do_GetWeakReference(aElement); EventStateManager::sPointerLockedDoc = do_GetWeakReference(aDocument); @@ -11410,9 +11410,9 @@ PointerLockRequest::Run() nsCOMPtr doc = do_QueryReferent(mDocument); nsDocument* d = static_cast(doc.get()); const char* error = nullptr; - if (!e || !d || !e->GetUncomposedDoc()) { + if (!e || !d || !e->GetComposedDoc()) { error = "PointerLockDeniedNotInDocument"; - } else if (e->GetUncomposedDoc() != d) { + } else if (e->GetComposedDoc() != d) { error = "PointerLockDeniedMovedDocument"; } if (!error) { @@ -11578,25 +11578,6 @@ nsDocument::GetMozPointerLockElement(nsIDOMElement** aPointerLockedElement) return NS_OK; } -Element* -nsIDocument::GetPointerLockElement() -{ - nsCOMPtr pointerLockedElement = - do_QueryReferent(EventStateManager::sPointerLockedElement); - if (!pointerLockedElement) { - return nullptr; - } - - // Make sure pointer locked element is in the same document. - nsCOMPtr pointerLockedDoc = - do_QueryReferent(EventStateManager::sPointerLockedDoc); - if (pointerLockedDoc != this) { - return nullptr; - } - - return pointerLockedElement; -} - nsresult nsDocument::Observe(nsISupports *aSubject, const char *aTopic, diff --git a/dom/base/nsIDocument.h b/dom/base/nsIDocument.h index f6645aaefc..3bdbd0896c 100644 --- a/dom/base/nsIDocument.h +++ b/dom/base/nsIDocument.h @@ -2643,7 +2643,6 @@ public: return !!GetFullscreenElement(); } void ExitFullscreen(); - Element* GetPointerLockElement(); void ExitPointerLock() { UnlockPointer(this); diff --git a/dom/tests/mochitest/pointerlock/file_pointerlock-api-with-shadow.html b/dom/tests/mochitest/pointerlock/file_pointerlock-api-with-shadow.html new file mode 100644 index 0000000000..b80a61ed11 --- /dev/null +++ b/dom/tests/mochitest/pointerlock/file_pointerlock-api-with-shadow.html @@ -0,0 +1,110 @@ + + + + + Bug 633602 + + + + + + + + Mozilla Bug 633602 + +
+
+    
+  
+ + diff --git a/dom/tests/mochitest/pointerlock/mochitest.ini b/dom/tests/mochitest/pointerlock/mochitest.ini index 1d9e92cbcf..57f0101ea5 100644 --- a/dom/tests/mochitest/pointerlock/mochitest.ini +++ b/dom/tests/mochitest/pointerlock/mochitest.ini @@ -8,6 +8,7 @@ tags = fullscreen support-files = pointerlock_utils.js file_pointerlock-api.html + file_pointerlock-api-with-shadow.html file_pointerlockerror.html file_escapeKey.html file_withoutDOM.html diff --git a/dom/tests/mochitest/pointerlock/test_pointerlock-api.html b/dom/tests/mochitest/pointerlock/test_pointerlock-api.html index 3094c565e7..cfc48063ac 100644 --- a/dom/tests/mochitest/pointerlock/test_pointerlock-api.html +++ b/dom/tests/mochitest/pointerlock/test_pointerlock-api.html @@ -33,7 +33,8 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=633602 ["full-screen-api.unprefix.enabled", true], ["full-screen-api.allow-trusted-requests-only", false], ["full-screen-api.transition-duration.enter", "0 0"], - ["full-screen-api.transition-duration.leave", "0 0"] + ["full-screen-api.transition-duration.leave", "0 0"], + ["dom.webcomponents.enabled", true] ]}, nextTest); // Run the tests which go full-screen in new window, as Mochitests @@ -49,6 +50,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=633602 "file_movementXY.html", "file_nestedFullScreen.html", "file_pointerlock-api.html", + "file_pointerlock-api-with-shadow.html", "file_pointerlockerror.html", "file_pointerLockPref.html", "file_removedFromDOM.html", diff --git a/dom/webidl/Document.webidl b/dom/webidl/Document.webidl index 2ade9a2c9b..46cb629feb 100644 --- a/dom/webidl/Document.webidl +++ b/dom/webidl/Document.webidl @@ -258,7 +258,6 @@ partial interface Document { // https://w3c.github.io/pointerlock/#extensions-to-the-document-interface // https://w3c.github.io/pointerlock/#extensions-to-the-documentorshadowroot-mixin partial interface Document { - readonly attribute Element? pointerLockElement; [BinaryName="pointerLockElement", Pref="pointer-lock-api.prefixed.enabled"] readonly attribute Element? mozPointerLockElement; void exitPointerLock(); diff --git a/dom/webidl/DocumentOrShadowRoot.webidl b/dom/webidl/DocumentOrShadowRoot.webidl index 16388d1264..267f16f23b 100644 --- a/dom/webidl/DocumentOrShadowRoot.webidl +++ b/dom/webidl/DocumentOrShadowRoot.webidl @@ -22,8 +22,7 @@ interface DocumentOrShadowRoot { readonly attribute Element? activeElement; readonly attribute StyleSheetList styleSheets; - // Not implemented yet: bug 1430303. - // readonly attribute Element? pointerLockElement; + readonly attribute Element? pointerLockElement; // Not implemented yet: bug 1430305. // readonly attribute Element? fullscreenElement; };