Skip to content

Instantly share code, notes, and snippets.

@KonnorRogers
Created October 21, 2024 07:19
Show Gist options
  • Save KonnorRogers/a99da9119f2c205478e3181af50fa30c to your computer and use it in GitHub Desktop.
Save KonnorRogers/a99da9119f2c205478e3181af50fa30c to your computer and use it in GitHub Desktop.
Finding if your contenteditable is part of a range across shadow dom.
const selection = document.getSelection()
if (!selection) { return }
let hasNode = false
if (typeof selection.getComposedRanges === "function") {
const staticRange = selection.getComposedRanges(this.contentEditableElement.getRootNode())[0]
if (!staticRange) { return }
range = selection.setBaseAndExtent(
staticRange.startContainer,
staticRange.startOffset,
staticRange.endContainer,
staticRange.endOffset
)
hasNode = selection.containsNode(this.contentEditableElement)
} else {
range = selection.getRangeAt(0)
hasNode = range.intersectsNode(this.contentEditableElement)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment