From 888cc309e09ebc6bde7e13a30521ba5a47f217a2 Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Mon, 9 Sep 2013 14:59:36 -0700 Subject: [PATCH] Stop using comments as boundary markers in dangerouslyRenderMarkup. Jordan warned (and StackOverflow confirmed) that IE8 doesn't respect HTML comment nodes when setting `.innerHTML`: http://stackoverflow.com/questions/15006001/inserting-a-comment-in-innerhtml The virtue of the comment strategy was that the parser did all the work of maintaining the boundaries between markup chunks, using comments as the boundary markers. The new strategy (of tagging the first node with a special attribute) has a similar virtue, since the parser should preserve that attribute only for the nodes we care about, and any rendered nodes that do not have the attribute can be ignored (and complained about by `console.error`). --- src/dom/Danger.js | 102 +++++++++---------------------- src/dom/__tests__/Danger-test.js | 8 +-- 2 files changed, 33 insertions(+), 77 deletions(-) diff --git a/src/dom/Danger.js b/src/dom/Danger.js index 68f365c624..cf4b038a6d 100644 --- a/src/dom/Danger.js +++ b/src/dom/Danger.js @@ -29,11 +29,8 @@ var getMarkupWrap = require('getMarkupWrap'); var invariant = require('invariant'); var mutateHTMLNodeWithMarkup = require('mutateHTMLNodeWithMarkup'); -var COMMENT_NODE_TYPE = 8; - -// This buffer will be reused in dangerouslyRenderMarkup to avoid unnecessary -// array allocations. -var reusableBuffer = []; +var OPEN_TAG_NAME_EXP = /^(<[^ \/>]+)/; +var RESULT_INDEX_ATTR = 'data-danger-index'; /** * Extracts the `nodeName` from a string of markup. @@ -89,97 +86,58 @@ var Danger = { } var markupListByNodeName = markupByNodeName[nodeName]; - var commentedMarkupList = reusableBuffer; - commentedMarkupList.length = 0; - // This for-in loop skips the holes of the sparse array. The order of // iteration should follow the order of assignment, which happens to match // numerical index order, but we don't rely on that. for (var resultIndex in markupListByNodeName) { if (markupListByNodeName.hasOwnProperty(resultIndex)) { - // Push the requested markup followed by a sentinel HTML - // comment. Comments are a good choice for sentinels because they can - // appear as children of any HTML node. - commentedMarkupList.push( - markupListByNodeName[resultIndex], - // This resultIndex will be parsed back out below. - '' + var markup = markupListByNodeName[resultIndex]; + + // Push the requested markup with an additional RESULT_INDEX_ATTR + // attribute. If the markup does not start with a < character, it + // will be discarded below (with an appropriate console.error). + markupListByNodeName[resultIndex] = markup.replace( + OPEN_TAG_NAME_EXP, + // This index will be parsed back out below. + '$1 ' + RESULT_INDEX_ATTR + '="' + resultIndex + '" ' ); } } - // Render each group of markup with similar wrapping `nodeName`, with - // sentinel comments interspersed. + // Render each group of markup with similar wrapping `nodeName`. var renderNodes = createNodesFromMarkup( - commentedMarkupList.join(''), + markupListByNodeName.join(''), emptyFunction // Do nothing special with