From 92d5bea1abf0183fd00d306511c541b55b414959 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Thu, 12 Mar 2015 20:41:20 +0000 Subject: [PATCH] do not change nodes if not needed --- test.js | 5 +---- twemoji-generator.js | 7 +++++-- twemoji.amd.js | 7 +++++-- twemoji.js | 7 +++++-- twemoji.npm.js | 7 +++++-- 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/test.js b/test.js index c7eb5ccc..1fc4ae48 100644 --- a/test.js +++ b/test.js @@ -146,10 +146,7 @@ wru.test([{ div = document.createElement('div'); div.appendChild(document.createTextNode('I \u2764\uFE0E emoji!')); twemoji.parse(div); - wru.assert('default parsing created 3 nodes anyway', div.childNodes.length === 3); - wru.assert('first child is the expected one', div.removeChild(div.firstChild).nodeValue === 'I '); - wru.assert('emoji child is unchanged', div.removeChild(div.firstChild).nodeValue === '\u2764\uFE0E'); - wru.assert('last child is the expected one', div.removeChild(div.firstChild).nodeValue === ' emoji!'); + wru.assert('default parsing did NOT create 3 nodes anyway', div.childNodes.length === 1); } },{ name: 'DOM parsing + size', diff --git a/twemoji-generator.js b/twemoji-generator.js index a0954a22..3f224d2a 100755 --- a/twemoji-generator.js +++ b/twemoji-generator.js @@ -604,6 +604,7 @@ function createTwemoji(re) { var allText = grabAllTextNodes(node, []), length = allText.length, + modified = false, fragment, subnode, text, @@ -644,13 +645,15 @@ function createTwemoji(re) { img.setAttribute('draggable', 'false'); img.alt = alt; img.src = src; + modified = true; + fragment.appendChild(img); } } - fragment.appendChild(img || createText(alt)); + if (!img) fragment.appendChild(createText(alt)); img = null; } // is there actually anything to replace in here ? - if (0 < i) { + if (modified) { // any text left to be added ? if (i < text.length) { fragment.appendChild( diff --git a/twemoji.amd.js b/twemoji.amd.js index 9f347b2e..e3d67694 100644 --- a/twemoji.amd.js +++ b/twemoji.amd.js @@ -327,6 +327,7 @@ define(function () { var allText = grabAllTextNodes(node, []), length = allText.length, + modified = false, fragment, subnode, text, @@ -367,13 +368,15 @@ define(function () { img.setAttribute('draggable', 'false'); img.alt = alt; img.src = src; + modified = true; + fragment.appendChild(img); } } - fragment.appendChild(img || createText(alt)); + if (!img) fragment.appendChild(createText(alt)); img = null; } // is there actually anything to replace in here ? - if (0 < i) { + if (modified) { // any text left to be added ? if (i < text.length) { fragment.appendChild( diff --git a/twemoji.js b/twemoji.js index 59570cf8..62dd3555 100644 --- a/twemoji.js +++ b/twemoji.js @@ -326,6 +326,7 @@ var twemoji = (function ( var allText = grabAllTextNodes(node, []), length = allText.length, + modified = false, fragment, subnode, text, @@ -366,13 +367,15 @@ var twemoji = (function ( img.setAttribute('draggable', 'false'); img.alt = alt; img.src = src; + modified = true; + fragment.appendChild(img); } } - fragment.appendChild(img || createText(alt)); + if (!img) fragment.appendChild(createText(alt)); img = null; } // is there actually anything to replace in here ? - if (0 < i) { + if (modified) { // any text left to be added ? if (i < text.length) { fragment.appendChild( diff --git a/twemoji.npm.js b/twemoji.npm.js index d6489363..9ba34697 100644 --- a/twemoji.npm.js +++ b/twemoji.npm.js @@ -327,6 +327,7 @@ var twemoji = (function ( var allText = grabAllTextNodes(node, []), length = allText.length, + modified = false, fragment, subnode, text, @@ -367,13 +368,15 @@ var twemoji = (function ( img.setAttribute('draggable', 'false'); img.alt = alt; img.src = src; + modified = true; + fragment.appendChild(img); } } - fragment.appendChild(img || createText(alt)); + if (!img) fragment.appendChild(createText(alt)); img = null; } // is there actually anything to replace in here ? - if (0 < i) { + if (modified) { // any text left to be added ? if (i < text.length) { fragment.appendChild(