Skip to content

Commit

Permalink
Merge pull request #8 from Infomaniak/secure-get-selection
Browse files Browse the repository at this point in the history
Protect against empty range in selection
  • Loading branch information
LunarX authored Jun 19, 2024
2 parents 25cc271 + f221413 commit 1098168
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions rich-html-editor/src/main/assets/define_listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ function onBodyResize(callback) {
resizeObserver.observe(document.documentElement)
}

function getSelectionRangeOrNull() {
const selection = document.getSelection()
return (selection.rangeCount > 0) ? selection.getRangeAt(0) : null
}

// Core logic

function exportHtml() {
Expand Down
4 changes: 3 additions & 1 deletion rich-html-editor/src/main/assets/link_detection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ const DOCUMENT_POSITION_SAME = 0 // TODO: Check usefulness

function getAllLinksPartiallyContainedInsideSelection() {
let elements = [...getEditor().querySelectorAll("a[href]")]
const range = document.getSelection().getRangeAt(0)
const range = getSelectionRangeOrNull()
if (range === null) return []

const { startContainer, endContainer } = range

// TODO: Investigate RoosterJs issues with startContainer and endContainer https://github.com/microsoft/roosterjs/blob/b1d4bab67dcae342cfdc043a8cbe2b96bb823a44/packages/roosterjs-editor-dom/lib/utils/queryElements.ts#L30
Expand Down
5 changes: 4 additions & 1 deletion rich-html-editor/src/main/assets/manage_links.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Create link

function createLink(displayText, url) {
let range = document.getSelection().getRangeAt(0)
let range = getSelectionRangeOrNull()
if (range === null) return

if (range.collapsed) {
// There's no selection, only a cursor. We can add the link manually
let anchor = getAnchorNodeAtCursor()
Expand Down Expand Up @@ -48,6 +50,7 @@ function unlink() {

function unlinkAnchorNode(anchor) {
let selection = document.getSelection()
if (selection.rangeCount === 0) return

let range = selection.getRangeAt(0)
let rangeBackup = range.cloneRange()
Expand Down

0 comments on commit 1098168

Please sign in to comment.