Skip to content

Commit

Permalink
Merge pull request #70 from botandrose/fix-early-return
Browse files Browse the repository at this point in the history
Fix early return
  • Loading branch information
1cg authored Dec 17, 2024
2 parents 31617cd + 2997fe8 commit 4d3ef0c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/idiomorph.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ var Idiomorph = (function () {

// if we are at the end of the exiting parent's children, just append
if (insertionPoint == null) {
if (ctx.callbacks.beforeNodeAdded(newChild) === false) return;
if (ctx.callbacks.beforeNodeAdded(newChild) === false) continue;

oldParent.appendChild(newChild);
ctx.callbacks.afterNodeAdded(newChild);
Expand Down Expand Up @@ -376,7 +376,7 @@ var Idiomorph = (function () {

// abandon all hope of morphing, just insert the new child before the insertion point
// and move on
if (ctx.callbacks.beforeNodeAdded(newChild) === false) return;
if (ctx.callbacks.beforeNodeAdded(newChild) === false) continue;

oldParent.insertBefore(newChild, insertionPoint);
ctx.callbacks.afterNodeAdded(newChild);
Expand Down
17 changes: 17 additions & 0 deletions test/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,23 @@ describe("Core morphing tests", function(){
document.body.removeChild(parent);
});

it.only('can prevent element addition w/ the beforeNodeAdded callback', function() {
let parent = make("<div><p>1</p><p>2</p></div>");
document.body.append(parent);

// morph
let finalSrc = "<p>1</p><p>2</p><p>3</p><p>4</p>";

Idiomorph.morph(parent, finalSrc, {morphStyle:'innerHTML', callbacks : {
beforeNodeAdded(node) {
if(node.outerHTML === "<p>3</p>") return false
}
}});
parent.innerHTML.should.equal("<p>1</p><p>2</p><p>4</p>");

document.body.removeChild(parent);
});

it('ignores active textarea value when ignoreActiveValue is true', function()
{
let parent = make("<div><textarea>foo</textarea></div>");
Expand Down

0 comments on commit 4d3ef0c

Please sign in to comment.