Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: should not clear selectedIndex when morphing option directly #263

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 6 additions & 21 deletions src/specialElHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,13 @@ function syncBooleanAttrProp(fromEl, toEl, name) {

export default {
OPTION: function(fromEl, toEl) {
var parentNode = fromEl.parentNode;
if (parentNode) {
var parentName = parentNode.nodeName.toUpperCase();
if (parentName === 'OPTGROUP') {
parentNode = parentNode.parentNode;
parentName = parentNode && parentNode.nodeName.toUpperCase();
}
if (parentName === 'SELECT' && !parentNode.hasAttribute('multiple')) {
if (fromEl.hasAttribute('selected') && !toEl.selected) {
// Workaround for MS Edge bug where the 'selected' attribute can only be
// removed if set to a non-empty value:
// https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/12087679/
fromEl.setAttribute('selected', 'selected');
fromEl.removeAttribute('selected');
}
// We have to reset select element's selectedIndex to -1, otherwise setting
// fromEl.selected using the syncBooleanAttrProp below has no effect.
// The correct selectedIndex will be set in the SELECT special handler below.
parentNode.selectedIndex = -1;
}
if (fromEl.hasAttribute('selected') && !toEl.selected) {
// Workaround for MS Edge bug where the 'selected' attribute can only be
// removed if set to a non-empty value:
// https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/12087679/
fromEl.setAttribute('selected', 'selected');
fromEl.removeAttribute('selected');
}
syncBooleanAttrProp(fromEl, toEl, 'selected');
},
/**
* The "value" attribute is special for the <input> element since it sets
Expand Down
12 changes: 12 additions & 0 deletions test/browser/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,18 @@ describe('morphdom' , function() {
expect(el1.selectedIndex).to.equal(-1);
});

it('should not clear selectedIndex when morphing option directly', function () {
var sel = node('select');
var el1 = node('option', {}, 'Option 1');
sel.appendChild(el1);
document.body.appendChild(sel);

var el2 = node('option', {}, 'Option 1');
var index = sel.selectedIndex;
morphdom(el1, el2);
expect(sel.selectedIndex).to.equal(index);
});

it('can handle shadow DOM removal via shadowRoot', function () {
var html = '<form><label>This should be removed</label><input type="checkbox" id="element-to-be-removed"><p>Element to keep in dom</p></form>';
var container = document.createElement('div');
Expand Down