Skip to content

Commit b776181

Browse files
committed
chore(selectionchange): use feature detection
1 parent efb4585 commit b776181

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/feature-detection.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,30 @@ import browser from 'bowser'
1111
export const contenteditable = typeof document.documentElement.contentEditable !== 'undefined'
1212

1313
const parser = browser.getParser(window.navigator.userAgent)
14-
const browserName = parser.getBrowser()
1514
const browserEngine = parser.getEngineName()
1615
const webKit = browserEngine === 'WebKit'
1716

1817
/**
19-
* Check selectionchange event (currently supported in IE, Chrome, Firefox and Safari)
20-
* Firefox supports it since version 52 (2017) so pretty sure this is fine.
18+
* Check selectionchange event (supported in IE, Chrome, Firefox and Safari)
19+
* Firefox supports it since version 52 (2017).
20+
* Opera has no support as of 2021.
2121
*/
22-
// not exactly feature detection... is it?
23-
export const selectionchange = !(browserName === 'Opera')
22+
const hasNativeSelectionchangeSupport = (document) => {
23+
const doc = document
24+
const osc = doc.onselectionchange
25+
if (osc !== undefined) {
26+
try {
27+
doc.onselectionchange = 0
28+
return doc.onselectionchange === null
29+
} catch (e) {
30+
} finally {
31+
doc.onselectionchange = osc
32+
}
33+
}
34+
return false
35+
}
36+
37+
export const selectionchange = hasNativeSelectionchangeSupport(document)
2438

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

0 commit comments

Comments
 (0)