1
0
mirror of https://github.com/twitter/twemoji.git synced 2024-07-01 02:19:09 +00:00

do not change nodes if not needed

This commit is contained in:
Andrea Giammarchi 2015-03-12 20:41:20 +00:00
parent 39a091da4b
commit 92d5bea1ab
5 changed files with 21 additions and 12 deletions

View File

@ -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',

View File

@ -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(

View File

@ -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(

View File

@ -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(

View File

@ -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(