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