Skip to content

Commit

Permalink
Merge pull request #34 from seanpdoyle/ignore-active-value-excludes-b…
Browse files Browse the repository at this point in the history
…ody-element

Exclude `document.body` when morphing with `ignoreActiveValue: true`
  • Loading branch information
1cg authored Jan 29, 2024
2 parents 193866b + 1bf50b4 commit d031215
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dist/idiomorph.amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ var Idiomorph = (function () {
* @returns {boolean}
*/
function ignoreValueOfActiveElement(possibleActiveElement, ctx) {
return ctx.ignoreActiveValue && possibleActiveElement === document.activeElement;
return ctx.ignoreActiveValue && possibleActiveElement === document.activeElement && possibleActiveElement !== document.body;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion dist/idiomorph.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ var Idiomorph = (function () {
* @returns {boolean}
*/
function ignoreValueOfActiveElement(possibleActiveElement, ctx) {
return ctx.ignoreActiveValue && possibleActiveElement === document.activeElement;
return ctx.ignoreActiveValue && possibleActiveElement === document.activeElement && possibleActiveElement !== document.body;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion dist/idiomorph.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ var Idiomorph = (function () {
* @returns {boolean}
*/
function ignoreValueOfActiveElement(possibleActiveElement, ctx) {
return ctx.ignoreActiveValue && possibleActiveElement === document.activeElement;
return ctx.ignoreActiveValue && possibleActiveElement === document.activeElement && possibleActiveElement !== document.body;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/idiomorph.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ var Idiomorph = (function () {
* @returns {boolean}
*/
function ignoreValueOfActiveElement(possibleActiveElement, ctx) {
return ctx.ignoreActiveValue && possibleActiveElement === document.activeElement;
return ctx.ignoreActiveValue && possibleActiveElement === document.activeElement && possibleActiveElement !== document.body;
}

/**
Expand Down
22 changes: 22 additions & 0 deletions test/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,28 @@ describe("Core morphing tests", function(){
document.body.removeChild(parent);
});

it('does not ignore body when ignoreActiveValue is true and no element has focus', function()
{
let parent = make("<div><input value='foo'></div>");
document.body.append(parent);

let initial = parent.querySelector("input");

// morph
let finalSrc = '<input value="bar">';
Idiomorph.morph(initial, finalSrc, {morphStyle:'outerHTML'});
initial.outerHTML.should.equal('<input value="bar">');

document.activeElement.should.equal(initial);

let finalSrc2 = '<input class="foo" value="doh">';
Idiomorph.morph(initial, finalSrc2, {morphStyle:'outerHTML', ignoreActiveValue: true});
initial.value.should.equal('doh');
initial.classList.value.should.equal('foo');

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

it('can ignore attributes w/ the beforeAttributeUpdated callback', function()
{
let parent = make("<div><input value='foo'></div>");
Expand Down

0 comments on commit d031215

Please sign in to comment.