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
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ static Rect getBoundingRect(const ShadowNodeFamily::AncestorList& ancestors) {
return layoutMetrics == EmptyLayoutMetrics ? Rect{} : layoutMetrics.frame;
}

static Rect getClippedTargetBoundingRect(
// Returns std::nullopt if the target is entirely clipped by an ancestor.
static std::optional<Rect> getClippedTargetBoundingRect(
const ShadowNodeFamily::AncestorList& targetAncestors) {
auto layoutMetrics = LayoutableShadowNode::computeRelativeLayoutMetrics(
targetAncestors,
Expand All @@ -159,7 +160,10 @@ static Rect getClippedTargetBoundingRect(
/* .applyParentClipping = */ .enableOverflowClipping = true,
/* .clipSpecifiedAncestor = */ .clipSpecifiedAncestor = false});

return layoutMetrics == EmptyLayoutMetrics ? Rect{} : layoutMetrics.frame;
if (layoutMetrics == EmptyLayoutMetrics) {
return std::nullopt;
}
return layoutMetrics.frame;
}

// Distinguishes between edge-adjacent vs. no intersection
Expand Down Expand Up @@ -212,13 +216,18 @@ static std::optional<Rect> computeIntersection(
// final clip against the root intersection rectangle.
auto clippedTargetFromRoot =
getClippedTargetBoundingRect(targetToRootAncestors);
if (!clippedTargetFromRoot.has_value()) {
// Don't fall through with an empty rect: at the origin, it would count as
// edge-adjacent to any root rect containing the origin.
return std::nullopt;
}

// Use root origin (without rootMargins) to translate coordinates of
// clippedTarget from relative to root, to top-level coordinate system
auto clippedTargetBoundingRect = hasExplicitRoot ? Rect{
.origin= rootBoundingRect.origin + clippedTargetFromRoot.origin,
.size=clippedTargetFromRoot.size}
: clippedTargetFromRoot;
.origin= rootBoundingRect.origin + clippedTargetFromRoot->origin,
.size=clippedTargetFromRoot->size}
: *clippedTargetFromRoot;

return intersectOrNull(rootMarginBoundingRect, clippedTargetBoundingRect);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3976,6 +3976,69 @@ describe('IntersectionObserver', () => {
expect(entries[0].isIntersecting).toBe(false);
expect(entries[0].intersectionRatio).toBe(0);
});

describe('targets fully clipped by an intermediate ScrollView', () => {
// The target (150..200) is inside the 1000x1000 viewport but entirely
// clipped by the 100x100 ScrollView. With a threshold of 0, even an
// edge-adjacent intersection would count, so this checks that a fully
// clipped target is not mistaken for one.
function renderFullyClippedTarget(): HostInstance {
const nodeRef = React.createRef<HostInstance>();

const root = Fantom.createRoot({
viewportWidth: 1000,
viewportHeight: 1000,
});
Fantom.runTask(() => {
root.render(
<ScrollView style={{width: 100, height: 100, flexGrow: 0}}>
<View
style={{width: 50, height: 50, marginTop: 150}}
ref={nodeRef}
/>
</ScrollView>,
);
});

return nullthrows(nodeRef.current);
}

it('should not report them as intersecting with the default threshold', () => {
const node = renderFullyClippedTarget();

const intersectionObserverCallback = jest.fn();

Fantom.runTask(() => {
observer = new IntersectionObserver(intersectionObserverCallback);
observer.observe(node);
});

expect(intersectionObserverCallback).toHaveBeenCalledTimes(1);
const [entries] = intersectionObserverCallback.mock.lastCall;
expect(entries.length).toBe(1);
expect(entries[0].isIntersecting).toBe(false);
expect(entries[0].intersectionRatio).toBe(0);
});

it('should not report them as intersecting with rootMargin', () => {
const node = renderFullyClippedTarget();

const intersectionObserverCallback = jest.fn();

Fantom.runTask(() => {
observer = new IntersectionObserver(intersectionObserverCallback, {
rootMargin: '500px 0px',
});
observer.observe(node);
});

expect(intersectionObserverCallback).toHaveBeenCalledTimes(1);
const [entries] = intersectionObserverCallback.mock.lastCall;
expect(entries.length).toBe(1);
expect(entries[0].isIntersecting).toBe(false);
expect(entries[0].intersectionRatio).toBe(0);
});
});
});

describe('unobserve(target)', () => {
Expand Down
Loading