Re: [csswg-drafts] [cssom-1] A more direct method than `caretPositionFromPoint()` (#10346)

Hi Keith,

I think what you want is the selection bounds regardless of where is current selection. For contenteditable div, you can get current selection bounds using `document.getSelection().getRangeAt(0).getBoundingClientRect()`. For textarea/input element, there is no way to get selection bounds today.

Instead of adding `currentCaretPosition()` API on Element, I would like to propose the following:

```
partial interface HTMLTextAreaElement {
  [NewObject] DOMRect getSelectionBoundingClientRect();
}

partial interface HTMLInputElement {
  [NewObject] DOMRect getSelectionBoundingClientRect();
}
```

`getSelectionBoundingClientRect()` would return the bounding rect of current selection in textarea/input element. The bounding rect is the caret rect if the selection is collapsed. If there is no active selection in textarea/input, it would return empty rect.

The new proposed API aligns with existing selection APIs on textarea/input such as [`select()`](https://html.spec.whatwg.org/#the-textarea-element:dom-textarea/input-select), [`selectionStart`](https://html.spec.whatwg.org/#the-textarea-element:dom-textarea/input-selectionstart), [`selectionEnd`](https://html.spec.whatwg.org/#the-textarea-element:dom-textarea/input-selectionend), etc.


-- 
GitHub Notification of comment by siliu1
Please view or discuss this issue at https://github.com/w3c/csswg-drafts/issues/10346#issuecomment-2130103293 using your GitHub account


-- 
Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config

Received on Friday, 24 May 2024 18:05:23 UTC