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 IE11 not setting initial select value #1838

Merged
merged 1 commit into from
Aug 5, 2019
Merged

Conversation

marvinhagemeister
Copy link
Member

@marvinhagemeister marvinhagemeister commented Aug 5, 2019

Oh boy, it took me all day to figure this one out. Basically all browsers will infer the option value from textContent when it isn't present. It's probably done for backwards compatibility.

const option = document.createElement("option");
option.textContent = "A";
console.log(option.value); // Logs: "A"

At this point option.value is set and that will skip our own check later:

if (('value' in newProps) && newProps.value!==undefined && newProps.value !== dom.value) dom.value = newProps.value==null ? '' : newProps.value;

This works fine in all browsers except IE11. When the option value isn't explicitly set, it leads to subtle bugs like select.value not working anymore. In this case it will always reset it to an empty string 🤷‍♂️

@andrewiggins this is basically a continuation from #1708 and it turns out that my fix wasn't correct. This PR gets those bytes back 😉

Here is a simple test case to verify the behavior in IE11:

// IE11 Bug: <select> value won't be set correctly
var select = document.createElement("select");
var data = ["A", "B", "C"];
for (var i = 0; i < data.length; i++) {
	var option = document.createElement("option");
	option.textContent = data[i];
	// Uncomment this line to make `select.value` work
	// option.value = data[i];
	select.appendChild(option);
}

document.body.appendChild(select);
select.value = "C";
console.log("actual:", select.value, "expected: C");
// Logs: actual: "" expected: C

Removes -24 B 🎉

@coveralls
Copy link

coveralls commented Aug 5, 2019

Coverage Status

Coverage increased (+0.4%) to 99.875% when pulling 39d2a1f on IE11-select into c2909d0 on master.

Copy link
Member

@cristianbote cristianbote left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WOW! Absolutely mental work! 🎉🎊

Copy link
Member

@JoviDeCroock JoviDeCroock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, nice one! 💯

@cristianbote cristianbote merged commit 5c1376a into master Aug 5, 2019
@cristianbote cristianbote deleted the IE11-select branch August 5, 2019 13:20
@@ -88,16 +88,7 @@ function setProperty(dom, name, value, oldValue, isSvg) {
}
}
else if (name!=='list' && name!=='tagName' && !isSvg && (name in dom)) {
// Setting `select.value` doesn't work in IE11.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the best PR I have ever seen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants