Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newlines handled incorrectly by innerText in IE8 #1864

Merged
merged 1 commit into from
Feb 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 3 additions & 42 deletions src/browser/ui/dom/DOMChildrenOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,9 @@
var Danger = require('Danger');
var ReactMultiChildUpdateTypes = require('ReactMultiChildUpdateTypes');

var getTextContentAccessor = require('getTextContentAccessor');
var setTextContent = require('setTextContent');
var invariant = require('invariant');

/**
* The DOM property to use when setting text content.
*
* @type {string}
* @private
*/
var textContentAccessor = getTextContentAccessor();

/**
* Inserts `childNode` as a child of `parentNode` at the `index`.
*
Expand All @@ -45,45 +37,14 @@ function insertChildAt(parentNode, childNode, index) {
);
}

var updateTextContent;
if (textContentAccessor === 'textContent') {
/**
* Sets the text content of `node` to `text`.
*
* @param {DOMElement} node Node to change
* @param {string} text New text content
*/
updateTextContent = function(node, text) {
node.textContent = text;
};
} else {
/**
* Sets the text content of `node` to `text`.
*
* @param {DOMElement} node Node to change
* @param {string} text New text content
*/
updateTextContent = function(node, text) {
// In order to preserve newlines correctly, we can't use .innerText to set
// the contents (see #1080), so we empty the element then append a text node
while (node.firstChild) {
node.removeChild(node.firstChild);
}
if (text) {
var doc = node.ownerDocument || document;
node.appendChild(doc.createTextNode(text));
}
};
}

/**
* Operations for updating with DOM children.
*/
var DOMChildrenOperations = {

dangerouslyReplaceNodeWithMarkup: Danger.dangerouslyReplaceNodeWithMarkup,

updateTextContent: updateTextContent,
updateTextContent: setTextContent,

/**
* Updates a component's children by processing a series of updates. The
Expand Down Expand Up @@ -156,7 +117,7 @@ var DOMChildrenOperations = {
);
break;
case ReactMultiChildUpdateTypes.TEXT_CONTENT:
updateTextContent(
setTextContent(
update.parentNode,
update.textContent
);
Expand Down
47 changes: 47 additions & 0 deletions src/browser/ui/dom/setTextContent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright 2013-2014 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @providesModule setTextContent
*/

"use strict";

var ExecutionEnvironment = require('ExecutionEnvironment');
var escapeTextForBrowser = require('escapeTextForBrowser');
var setInnerHTML = require('setInnerHTML');

/**
* Set the textContent property of a node, ensuring that whitespace is preserved
* even in IE8. innerText is a poor substitute for textContent and, among many
* issues, inserts <br> instead of the literal newline chars. innerHTML behaves
* as it should.
*
* @param {DOMElement} node
* @param {string} text
* @internal
*/
var setTextContent = function(node, text) {
node.textContent = text;
};

if (ExecutionEnvironment.canUseDOM) {
if (!('textContent' in document.documentElement)) {
setTextContent = function(node, text) {
setInnerHTML(node, escapeTextForBrowser(text));
};
}
}

module.exports = setTextContent;