@@ -3,6 +3,7 @@ import clone from 'clone';
33import equal from 'deep-equal' ;
44import Emitter from './emitter' ;
55import logger from './logger' ;
6+ import * as shadow from 'shadow-selection-polyfill' ;
67
78let debug = logger ( 'quill:selection' ) ;
89
@@ -28,7 +29,7 @@ class Selection {
2829 this . lastRange = this . savedRange = new Range ( 0 , 0 ) ;
2930 this . handleComposition ( ) ;
3031 this . handleDragging ( ) ;
31- this . emitter . listenDOM ( 'selectionchange' , this . rootDocument , ( ) => {
32+ this . emitter . listenDOM ( '-shadow- selectionchange' , document , ( ) => {
3233 if ( ! this . mouseDown ) {
3334 setTimeout ( this . update . bind ( this , Emitter . sources . USER ) , 1 ) ;
3435 }
@@ -158,9 +159,21 @@ class Selection {
158159 }
159160
160161 getNativeRange ( ) {
161- let selection = this . rootDocument . getSelection ( ) ;
162- if ( selection == null || selection . rangeCount <= 0 ) return null ;
163- let nativeRange = selection . getRangeAt ( 0 ) ;
162+ let nativeRange ;
163+
164+ // in Safari and iOS we need to use shadow selection polyfill
165+ const ua = navigator . userAgent ;
166+ const isSafari = / ^ ( (? ! c h r o m e | a n d r o i d ) .) * s a f a r i / i. test ( ua ) || / i P a d | i P h o n e / . test ( ua ) ;
167+ const hasShadowWithNoSelection = this . rootDocument instanceof ShadowRoot && ! this . rootDocument . getSelection ;
168+
169+ if ( hasShadowWithNoSelection && isSafari ) {
170+ nativeRange = shadow . getRange ( ) ;
171+ } else {
172+ let selection = this . rootDocument . getSelection ( ) ;
173+ if ( selection == null || selection . rangeCount <= 0 ) return null ;
174+ nativeRange = selection . getRangeAt ( 0 ) ;
175+ }
176+
164177 if ( nativeRange == null ) return null ;
165178 let range = this . normalizeNative ( nativeRange ) ;
166179 debug . info ( 'getNativeRange' , range ) ;
@@ -269,7 +282,7 @@ class Selection {
269282 if ( startNode != null && ( this . root . parentNode == null || startNode . parentNode == null || endNode . parentNode == null ) ) {
270283 return ;
271284 }
272- let selection = this . rootDocument . getSelection ( ) ;
285+ let selection = typeof this . rootDocument . getSelection === 'function' ? this . rootDocument . getSelection ( ) : document . getSelection ( ) ;
273286 if ( selection == null ) return ;
274287 if ( startNode != null ) {
275288 if ( ! this . hasFocus ( ) ) this . root . focus ( ) ;
0 commit comments