Skip to content

Commit

Permalink
Stop ReactInputSelection breaking in IE8
Browse files Browse the repository at this point in the history
In the IE code path the method assumed that the input.value property
was non-null. A quick fix is to use either value or innerText; which means
the same code can be shared for textarea and contentEditable components.

The code is slightly buggy because the range.parentElement() !== input check
will fail for contentEditable components when the focus within a deep DOM tree.
  • Loading branch information
joshduck authored and zpao committed Sep 14, 2013
1 parent 3e4302e commit ed7fa0e
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/core/ReactInputSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

"use strict";

var getTextContentAccessor = require('getTextContentAccessor');

// It is not safe to read the document.activeElement property in IE if there's
// nothing focused.
function getActiveElement() {
Expand Down Expand Up @@ -80,8 +82,9 @@ var ReactInputSelection = {
},

/**
* @getSelection: Gets the selection bounds of a textarea or input.
* -@input: Look up selection bounds of this input or textarea
* @getSelection: Gets the selection bounds of a focused textarea, input or
* contentEditable node.
* -@input: Look up selection bounds of this input
* -@return {start: selectionStart, end: selectionEnd}
*/
getSelection: function(input) {
Expand Down Expand Up @@ -114,7 +117,8 @@ var ReactInputSelection = {
return {start: 0, end: 0};
}

var length = input.value.length;
var value = input.value || input[getTextContentAccessor()];
var length = value.length;

if (input.nodeName === 'INPUT') {
return {
Expand All @@ -129,7 +133,7 @@ var ReactInputSelection = {
range2.setEndPoint('StartToStart', range);
return {
start: length - range2.text.length,
end: end
end: end
};
}
},
Expand Down

0 comments on commit ed7fa0e

Please sign in to comment.