Skip to content

Commit

Permalink
Bug 1797792 - Tweak HTMLOptionsCollection.length limit. r=smaug
Browse files Browse the repository at this point in the history
As per the discussion in whatwg/html#8347

Differential Revision: https://phabricator.services.mozilla.com/D160544
  • Loading branch information
emilio committed Oct 28, 2022
1 parent cd13faf commit 30558e6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
10 changes: 5 additions & 5 deletions dom/html/HTMLSelectElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,25 +566,25 @@ void HTMLSelectElement::GetType(nsAString& aType) {
}
}

#define MAX_DYNAMIC_SELECT_LENGTH 10000

void HTMLSelectElement::SetLength(uint32_t aLength, ErrorResult& aRv) {
constexpr uint32_t kMaxDynamicSelectLength = 100000;

uint32_t curlen = Length();

if (curlen > aLength) { // Remove extra options
for (uint32_t i = curlen; i > aLength; --i) {
Remove(i - 1);
}
} else if (aLength > curlen) {
if (aLength > MAX_DYNAMIC_SELECT_LENGTH) {
if (aLength > kMaxDynamicSelectLength) {
nsAutoString strOptionsLength;
strOptionsLength.AppendInt(aLength);

nsAutoString strLimit;
strLimit.AppendInt(MAX_DYNAMIC_SELECT_LENGTH);
strLimit.AppendInt(kMaxDynamicSelectLength);

nsContentUtils::ReportToConsole(
nsIScriptError::warningFlag, "DOM"_ns, GetOwnerDocument(),
nsIScriptError::warningFlag, "DOM"_ns, OwnerDoc(),
nsContentUtils::eDOM_PROPERTIES,
"SelectOptionsLengthAssignmentWarning", {strOptionsLength, strLimit});
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,21 @@
});

test(function() {
mySelect.options.length = 10001;
mySelect.options.length = 100001;
assert_equals(mySelect.options.length, 3, "Length of <select> should remain unchanged");
});

test(function() {
mySelect.options.length = 10000;
assert_equals(mySelect.options.length, 10000, "Length of <select> should be 10,000");
mySelect.options.length = 100000;
assert_equals(mySelect.options.length, 100000, "Length of <select> should be 10,0000");
});

test(function() {
mySelect.appendChild(new Option());
mySelect.appendChild(new Option());
assert_equals(mySelect.options.length, 100002, "Manual expansion still works");
mySelect.options.length = 100001;
assert_equals(mySelect.options.length, 100001, "Truncation works if over the limit");
});
</script>
</body>
Expand Down

0 comments on commit 30558e6

Please sign in to comment.