@samitouri / QOS-React-2 / commits / 18bf0b80bc

Fix ReactDOMSelection for IE 11

IE 11 no longer supports the legacy document.selection API. Their implementation of window.getSelection() doesn't support the extend() method, which we were relying on. If the selection is RTL and selection extend is missing, then just flip the selection.

Josh Duck committed Nov 4, 2013 at 11:24 UTC 18bf0b80bc701fe0b9fb9216ce907baf4fb035ec
1 file changed +18 -3
src/core/ReactDOMSelection.js
+18 -3
@@ -137,7 +137,15 @@ function setModernOffsets(node, offsets) {
137 var length = node[getTextContentAccessor()].length;
138 var start = Math.min(offsets.start, length);
139 var end = typeof offsets.end === 'undefined' ?
140 - start : Math.min(offsets.end, length);
140 + start : Math.min(offsets.end, length);
141 +
142 + // IE 11 uses modern selection, but doesn't support the extend method.
143 + // Flip backward selections, so we can set with a single range.
144 + if (!selection.extend && start > end) {
145 + var temp = end;
146 + end = start;
147 + start = temp;
148 + }
149
150 var startMarker = getNodeForCharacterOffset(node, start);
151 var endMarker = getNodeForCharacterOffset(node, end);
@@ -146,8 +154,15 @@ function setModernOffsets(node, offsets) {
154 var range = document.createRange();
155 range.setStart(startMarker.node, startMarker.offset);
156 selection.removeAllRanges();
149 - selection.addRange(range);
150 - selection.extend(endMarker.node, endMarker.offset);
157 +
158 + if (start > end) {
159 + selection.addRange(range);
160 + selection.extend(endMarker.node, endMarker.offset);
161 + } else {
162 + range.setEnd(endMarker.node, endMarker.offset);
163 + selection.addRange(range);
164 + }
165 +
166 range.detach();
167 }
168 }