Skip to content

Commit

Permalink
fix: restore selection should consider the window of the container
Browse files Browse the repository at this point in the history
  • Loading branch information
ling1726 committed Sep 12, 2024
1 parent d6cb4e7 commit f999e70
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ export function getPublicInstance(instance: Instance): Instance {

export function prepareForCommit(containerInfo: Container): Object | null {
eventsEnabled = ReactBrowserEventEmitterIsEnabled();
selectionInformation = getSelectionInformation();
selectionInformation = getSelectionInformation(containerInfo);
let activeInstance = null;
if (enableCreateEventHandleAPI) {
const focusedElem = selectionInformation.focusedElem;
Expand Down
15 changes: 9 additions & 6 deletions packages/react-dom-bindings/src/client/ReactInputSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,12 @@ function isSameOriginFrame(iframe) {
}
}

function getActiveElementDeep() {
function getActiveElementDeep(containerInfo) {
let win = window;
let element = getActiveElement();
if (containerInfo?.ownerDocument?.defaultView) {
win = containerInfo.ownerDocument.defaultView
}
let element = getActiveElement(win.document);
while (element instanceof win.HTMLIFrameElement) {
if (isSameOriginFrame(element)) {
win = element.contentWindow;
Expand Down Expand Up @@ -97,8 +100,8 @@ export function hasSelectionCapabilities(elem) {
);
}

export function getSelectionInformation() {
const focusedElem = getActiveElementDeep();
export function getSelectionInformation(containerInfo) {
const focusedElem = getActiveElementDeep(containerInfo);
return {
focusedElem: focusedElem,
selectionRange: hasSelectionCapabilities(focusedElem)
Expand All @@ -112,8 +115,8 @@ export function getSelectionInformation() {
* restore it. This is useful when performing operations that could remove dom
* nodes and place them back in, resulting in focus being lost.
*/
export function restoreSelection(priorSelectionInformation) {
const curFocusedElem = getActiveElementDeep();
export function restoreSelection(priorSelectionInformation, containerInfo) {
const curFocusedElem = getActiveElementDeep(containerInfo);
const priorFocusedElem = priorSelectionInformation.focusedElem;
const priorSelectionRange = priorSelectionInformation.selectionRange;
if (curFocusedElem !== priorFocusedElem && isInDocument(priorFocusedElem)) {
Expand Down
16 changes: 1 addition & 15 deletions packages/shared/ReactVersion.js
Original file line number Diff line number Diff line change
@@ -1,15 +1 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// TODO: this is special because it gets imported during build.
//
// It exists as a placeholder so that DevTools can support work tag changes between releases.
// When we next publish a release, update the matching TODO in backend/renderer.js
// TODO: This module is used both by the release scripts and to expose a version
// at runtime. We should instead inject the version number as part of the build
// process, and use the ReactVersions.js module as the single source of truth.
export default '19.0.0';
export default '19.0.0-rc-d6cb4e77-20240911';

0 comments on commit f999e70

Please sign in to comment.