* Find a native DOM range from a Editor `range`. * * Notice: the returned range will always be ordinal regardless of the direction of Editor `range` due to DOM API limit. * * there is no way to create a reverse DOM Range using Range.setStart/setEnd * according to https://dom.spec.what
(editor: Editor, range: Range)
| 470 | * according to https://dom.spec.whatwg.org/#concept-range-bp-set. |
| 471 | */ |
| 472 | toDOMRange(editor: Editor, range: Range): DOMRange { |
| 473 | const { anchor, focus } = range |
| 474 | const isBackward = Range.isBackward(range) |
| 475 | const domAnchor = Editable.toDOMPoint(editor, anchor) |
| 476 | const domFocus = Range.isCollapsed(range) ? domAnchor : Editable.toDOMPoint(editor, focus) |
| 477 | |
| 478 | const window = Editable.getWindow(editor) |
| 479 | const domRange = window.document.createRange() |
| 480 | const [startNode, startOffset] = isBackward ? domFocus : domAnchor |
| 481 | const [endNode, endOffset] = isBackward ? domAnchor : domFocus |
| 482 | |
| 483 | // A editor Point at zero-width Leaf always has an offset of 0 but a native DOM selection at |
| 484 | // zero-width node has an offset of 1 so we have to check if we are in a zero-width node and |
| 485 | // adjust the offset accordingly. |
| 486 | const startEl = (isDOMElement(startNode) ? startNode : startNode.parentElement) as HTMLElement |
| 487 | const isStartAtZeroWidth = !!startEl.getAttribute(DATA_EDITABLE_ZERO_WIDTH) |
| 488 | const endEl = (isDOMElement(endNode) ? endNode : endNode.parentElement) as HTMLElement |
| 489 | const isEndAtZeroWidth = !!endEl.getAttribute(DATA_EDITABLE_ZERO_WIDTH) |
| 490 | |
| 491 | domRange.setStart(startNode, isStartAtZeroWidth ? 1 : startOffset) |
| 492 | domRange.setEnd(endNode, isEndAtZeroWidth ? 1 : endOffset) |
| 493 | return domRange |
| 494 | }, |
| 495 | |
| 496 | /** |
| 497 | * Find a Editor node from a native DOM `element`. |
nothing calls this directly
no test coverage detected
searching dependent graphs…