Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 107 additions & 2 deletions packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -3399,6 +3399,7 @@ FragmentInstance.prototype.unobserveUsing = function (
unobserveChild,
observer,
);
unobservePendingChildren(this, observer);
}
};
function unobserveChild(
Expand All @@ -3415,6 +3416,92 @@ function unobserveChild(
observer.unobserve(instance);
return false;
}

type PendingIntersectionUnobserve = {
fragmentInstance: FragmentInstanceType,
observer: IntersectionObserver,
instance: Instance,
};

let pendingIntersectionUnobserves: Array<PendingIntersectionUnobserve> = [];
let intersectionUnobserveScheduled: boolean = false;

function isIntersectionObserver(
observer: IntersectionObserver | ResizeObserver,
): boolean {
// IntersectionObserver has rootMargin; ResizeObserver does not. Avoid
// instanceof so jsdom mocks and cross-realm observers still match.
return typeof (observer as any).rootMargin === 'string';
}

// A later commit can reinsert the same node before the post-paint unobserve
// runs (Activity hidden → visible). Cancel so the flush does not drop it.
function cancelPendingIntersectionUnobserve(
fragmentInstance: FragmentInstanceType,
observer: IntersectionObserver | ResizeObserver,
instance: Instance,
): void {
let writeIdx = 0;
for (let i = 0; i < pendingIntersectionUnobserves.length; i++) {
const pending = pendingIntersectionUnobserves[i];
if (
pending.fragmentInstance !== fragmentInstance ||
pending.observer !== observer ||
pending.instance !== instance
) {
pendingIntersectionUnobserves[writeIdx++] = pending;
}
}
pendingIntersectionUnobserves.length = writeIdx;
}

// unobserveUsing() only walks the children that are still mounted, so release
// the deleted ones that are waiting for their exit record.
function unobservePendingChildren(
fragmentInstance: FragmentInstanceType,
observer: IntersectionObserver | ResizeObserver,
): void {
let writeIdx = 0;
for (let i = 0; i < pendingIntersectionUnobserves.length; i++) {
const pending = pendingIntersectionUnobserves[i];
if (
pending.fragmentInstance === fragmentInstance &&
pending.observer === observer
) {
observer.unobserve(pending.instance);
} else {
pendingIntersectionUnobserves[writeIdx++] = pending;
}
}
pendingIntersectionUnobserves.length = writeIdx;
}

function schedulePendingIntersectionUnobserve(
fragmentInstance: FragmentInstanceType,
observer: IntersectionObserver,
instance: Instance,
): void {
pendingIntersectionUnobserves.push({
fragmentInstance,
observer,
instance,
});
if (!intersectionUnobserveScheduled) {
intersectionUnobserveScheduled = true;
// Unobserving in this commit would cancel the record IntersectionObserver
// delivers for the now disconnected target. Wait until after paint so the
// exit fires first, then drop the observer's strong ref to the node.
requestPostPaintCallback(() => {
intersectionUnobserveScheduled = false;
const pending = pendingIntersectionUnobserves;
pendingIntersectionUnobserves = [];
for (let i = 0; i < pending.length; i++) {
const item = pending[i];
item.observer.unobserve(item.instance);
}
});
}
}
// $FlowFixMe[prop-missing]
FragmentInstance.prototype.getClientRects = function (
this: FragmentInstanceType,
Expand Down Expand Up @@ -3797,8 +3884,10 @@ export function commitNewChildToFragmentInstance(
return;
}
const instance: InstanceWithFragmentHandles = childInstance as any;
if (fragmentInstance._observers !== null) {
fragmentInstance._observers.forEach(observer => {
const observers = fragmentInstance._observers;
if (observers !== null) {
observers.forEach(observer => {
cancelPendingIntersectionUnobserve(fragmentInstance, observer, instance);
observer.observe(instance);
});
}
Expand Down Expand Up @@ -3826,6 +3915,22 @@ export function deleteChildFromFragmentInstance(
return;
}
const instance: InstanceWithFragmentHandles = childInstance as any;
const observers = fragmentInstance._observers;
if (observers !== null) {
observers.forEach(observer => {
if (isIntersectionObserver(observer)) {
// Stay observed until the next IntersectionObserver delivery so a
// disconnected target still gets an isIntersecting: false record.
schedulePendingIntersectionUnobserve(
fragmentInstance,
observer as any as IntersectionObserver,
instance,
);
} else {
observer.unobserve(instance);
}
});
}
if (enableFragmentRefsInstanceHandles) {
if (instance.reactFragments != null) {
instance.reactFragments.delete(fragmentInstance);
Expand Down
20 changes: 4 additions & 16 deletions packages/react-dom-bindings/src/client/validateDOMNesting.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ const inScopeTags = [
'th',
'marquee',
'object',
'select',
'template',

// https://html.spec.whatwg.org/multipage/syntax.html#html-integration-point
Expand Down Expand Up @@ -306,22 +307,6 @@ function isTagValidWithParent(
): boolean {
// First, let's check if we're in an unusual parsing mode...
switch (parentTag) {
// https://html.spec.whatwg.org/multipage/syntax.html#parsing-main-inselect
case 'select':
return (
tag === 'hr' ||
tag === 'option' ||
tag === 'optgroup' ||
tag === 'script' ||
tag === 'template' ||
tag === '#text'
);
case 'optgroup':
return tag === 'option' || tag === '#text';
// Strictly speaking, seeing an <option> doesn't mean we're in a <select>
// but
case 'option':
return tag === '#text';
// https://html.spec.whatwg.org/multipage/syntax.html#parsing-main-intd
// https://html.spec.whatwg.org/multipage/syntax.html#parsing-main-incaption
// No special behavior since these rules fall back to "in body" mode for
Expand Down Expand Up @@ -424,6 +409,9 @@ function isTagValidWithParent(
case 'caption':
case 'col':
case 'colgroup':
case 'input':
// <input> causes open <select> tags to close.
return parentTag !== 'select';
case 'frameset':
case 'frame':
case 'tbody':
Expand Down
Loading
Loading