Skip to content

Commit

Permalink
chore(selectionchange): use feature detection
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielhase committed Aug 3, 2021
1 parent 20f2bb9 commit 1e2caed
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/feature-detection.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,30 @@ import browser from 'bowser'
export const contenteditable = typeof document.documentElement.contentEditable !== 'undefined'

const parser = browser.getParser(window.navigator.userAgent)
const browserName = parser.getBrowser()
const browserEngine = parser.getEngineName()
const webKit = browserEngine === 'WebKit'

/**
* Check selectionchange event (currently supported in IE, Chrome, Firefox and Safari)
* Firefox supports it since version 52 (2017) so pretty sure this is fine.
* Check selectionchange event (supported in IE, Chrome, Firefox and Safari)
* Firefox supports it since version 52 (2017).
* Opera has no support as of 2021.
*/
// not exactly feature detection... is it?
export const selectionchange = !(browserName === 'Opera')
const hasNativeSelectionchangeSupport = (document) => {
const doc = document
const osc = doc.onselectionchange
if (osc !== undefined) {
try {
doc.onselectionchange = 0
return doc.onselectionchange === null
} catch (e) {
} finally {
doc.onselectionchange = osc
}
}
return false
}

export const selectionchange = hasNativeSelectionchangeSupport(document)

// See Keyboard.prototype.preventContenteditableBug for more information.
export const contenteditableSpanBug = !!webKit

0 comments on commit 1e2caed

Please sign in to comment.