Skip to content

Commit

Permalink
Revert "Improve performance of appending <option> to <select>"
Browse files Browse the repository at this point in the history
This reverts commit 3a569a6.
  • Loading branch information
eps1lon authored and domenic committed Jan 7, 2023
1 parent bda8241 commit dc20dbc
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 72 deletions.
3 changes: 1 addition & 2 deletions benchmark/html/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";

module.exports = {
parsing: require("./parsing"),
select: require("./select")
parsing: require("./parsing")
};
9 changes: 0 additions & 9 deletions benchmark/html/select.js

This file was deleted.

62 changes: 1 addition & 61 deletions lib/jsdom/living/nodes/HTMLSelectElement-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ class HTMLSelectElementImpl extends HTMLElementImpl {
this._customValidityErrorMessage = "";

this._labels = null;

// Essentially a cache for the current selected element for use while building a select by appending options.
// See _selectednessSettingAlgorithmWithKnownNewAddition().
this._lastSelectedOptionForFastPath = null;
}

_formReset() {
Expand All @@ -57,10 +53,6 @@ class HTMLSelectElementImpl extends HTMLElementImpl {
}

_askedForAReset() {
this._selectednessSettingAlgorithm();
}

_selectednessSettingAlgorithm() {
if (this.hasAttributeNS(null, "multiple")) {
return;
}
Expand All @@ -82,7 +74,6 @@ class HTMLSelectElementImpl extends HTMLElementImpl {
if (!disabled) {
// (do not set dirty)
option._selectedness = true;
this._lastSelectedOptionForFastPath = option;
break;
}
}
Expand All @@ -91,56 +82,19 @@ class HTMLSelectElementImpl extends HTMLElementImpl {
selected.forEach((option, index) => {
option._selectedness = index === selected.length - 1;
});
this._lastSelectedOptionForFastPath = selected[selected.length - 1];
}
}

_selectednessSettingAlgorithmWithKnownNewAddition(child) {
if (this.hasAttributeNS(null, "multiple")) {
return;
}
if (child.hasAttributeNS(null, "disabled")) {
return;
}
const optArr = toOptionArray(child);
if (optArr.length === 0) {
return;
}
const selectedOptions = optArr.filter(opt => opt._selectedness);
// If any selected is last selected
if (selectedOptions.length > 0) {
// pushed at least one element with selected=true, reset
this._askedForAReset();
return;
}

// No selection so first option is selected
if ((!this._lastSelectedOptionForFastPath || !this._lastSelectedOptionForFastPath._selectedness) &&
this._displaySize === 1) {
this._lastSelectedOptionForFastPath = optArr[0];
this._lastSelectedOptionForFastPath._selectedness = true;
}
}

_descendantAdded(parent, child) {
if (child.nodeType === NODE_TYPE.ELEMENT_NODE) {
if (parent === this) {
this._selectednessSettingAlgorithmWithKnownNewAddition(child);
} else if (parent._localName === "optgroup") {
this._selectednessSettingAlgorithmWithKnownNewAddition(parent);
} else {
this._askedForAReset();
}
this._askedForAReset();
}

super._descendantAdded(parent, child);
}

_descendantRemoved(parent, child) {
if (child.nodeType === NODE_TYPE.ELEMENT_NODE) {
if (this._lastSelectedOptionForFastPath === child) {
this._lastSelectedOptionForFastPath = null;
}
this._askedForAReset();
}

Expand Down Expand Up @@ -324,20 +278,6 @@ class HTMLSelectElementImpl extends HTMLElementImpl {

mixin(HTMLSelectElementImpl.prototype, DefaultConstraintValidationImpl.prototype);

function toOptionArray(child) {
const array = [];
if (child._localName === "option" && !child.hasAttributeNS(null, "disabled")) {
array.push(child);
} else if (child._localName === "optgroup") {
for (const childOfGroup of domSymbolTree.childrenIterator(child)) {
if (childOfGroup._localName === "option" && !childOfGroup.hasAttributeNS(null, "disabled")) {
array.push(childOfGroup);
}
}
}
return array;
}

module.exports = {
implementation: HTMLSelectElementImpl
};

0 comments on commit dc20dbc

Please sign in to comment.