Re: [w3c/selection-api] Need spec changes to Range and StaticRange to support nodes in different tree? (Issue #169)

There are multiple spec changes that we should consider to support a Range with nodes in different trees. Here is a summary of the above conversations:

## 1. Output of getComposedRanges
Currently, the function returns a list of StaticRange. However, this violates the spec rule for what is a [valid StaticRange](https://dom.spec.whatwg.org/#staticrange-valid):
> A [StaticRange](https://dom.spec.whatwg.org/#staticrange) is valid if all of the following are true:
> 1. Its [start](https://dom.spec.whatwg.org/#concept-range-start) and [end](https://dom.spec.whatwg.org/#concept-range-end) are in the same [node tree](https://dom.spec.whatwg.org/#concept-node-tree). […]

Potential changes so we don't violate the spec:
Option 1: Change definition of StaticRange to allow start/end nodes in different trees.
Option 2: Add new StaticComposedRange, to allow start/end nodes in different trees.
Option 3: Add new “shadow-including valid” definition.
Option 4: Change getComposedRanges to return as many ranges as there are trees.
More?

## 2. When to use Flat tree traversal to compare positions
1. [Range setStart/setEnd](https://dom.spec.whatwg.org/#concept-range-bp-set):
> Step 4.1: If range’s root is not equal to node’s root, or if bp is after the range’s end, set range’s end to bp.

2. [Selection SetBaseAndExtent](https://w3c.github.io/selection-api/#dom-selection-setbaseandextent):
> Step 2: If nodeA is nodeB, then return equal if offsetA is offsetB, before if offsetA is less than offsetB, and after if offsetA is greater than offsetB.

In the above steps, the before/after/equal [position](https://dom.spec.whatwg.org/#concept-range-bp-position) definitions are for within the same tree.
> The position of a [boundary point](https://dom.spec.whatwg.org/#concept-range-bp) (nodeA, offsetA) relative to a [boundary point](https://dom.spec.whatwg.org/#concept-range-bp) (nodeB, offsetB) is before, equal, or after, as returned by these steps:
> 1. Assert: nodeA and nodeB have the same [root](https://dom.spec.whatwg.org/#concept-tree-root).

How do we update the specs to allow nodes in different trees? Maybe define new concepts, maybe `composed position`, `composed before`, etc?

Should we always use FlatTreeTraversal when storing this composed range? Or, should it only be stored when we have nodes in different trees and are crossing shadow boundaries? First option makes the most sense because of slotted contents. Those nodes should be compared using Flat tree, even if in the same tree.
Example above: https://github.com/w3c/selection-api/issues/169#issuecomment-2023179494

## 3. Selection::containsNode() mismatch
Currently, this is returning weird results for slotted cases since all comparisons are using DOM tree traversal. We should probably keep that behavior as is.
Proposal: Add new function `isPointInRange`to StaticRange (output of getComposedRanges) so it can return the right result. This should use a flat tree traversal to compare positions.

Bonus: Should we consider adding other functions such as toString()?

-- 
Reply to this email directly or view it on GitHub:
https://github.com/w3c/selection-api/issues/169#issuecomment-2375456402
You are receiving this because you are subscribed to this thread.

Message ID: <w3c/selection-api/issues/169/2375456402@github.com>

Received on Wednesday, 25 September 2024 23:44:11 UTC