Skip to content

Commit bce0498

Browse files
awesomeklingpbrw
authored andcommitted
LibWeb: Avoid invalidation on .textContent setter no-op
When setting the textContent of an element with no children to null or the empty string, nothing happens. Even so, we were still invalidating style, layout and collections, causing pointless churn. Skipping invalidation in this case also revealed that we were missing invalidation when changing the selected state of HTMLOptionElement. This was all caught by existing tests already in-tree. :^)
1 parent a3d2055 commit bce0498

File tree

3 files changed

+8
-0
lines changed

3 files changed

+8
-0
lines changed

Libraries/LibWeb/DOM/Node.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ void Node::set_text_content(Optional<String> const& maybe_content)
202202

203203
// If DocumentFragment or Element, string replace all with the given value within this.
204204
if (is<DocumentFragment>(this) || is<Element>(this)) {
205+
// OPTIMIZATION: Replacing nothing with nothing is a no-op. Avoid all invalidation in this case.
206+
if (!first_child() && content.is_empty()) {
207+
return;
208+
}
205209
string_replace_all(content);
206210
}
207211

Libraries/LibWeb/DOM/Node.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ enum class IsDescendant {
7474
X(HTMLIFrameElementGeometryChange) \
7575
X(HTMLInputElementSetChecked) \
7676
X(HTMLObjectElementUpdateLayoutAndChildObjects) \
77+
X(HTMLOptionElementSelectedChange) \
7778
X(HTMLSelectElementSetIsOpen) \
7879
X(Hover) \
7980
X(MediaQueryChangedMatchState) \

Libraries/LibWeb/HTML/HTMLOptionElement.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ void HTMLOptionElement::set_selected(bool selected)
6868

6969
void HTMLOptionElement::set_selected_internal(bool selected)
7070
{
71+
if (m_selected != selected)
72+
invalidate_style(DOM::StyleInvalidationReason::HTMLOptionElementSelectedChange);
73+
7174
m_selected = selected;
7275
if (selected)
7376
m_selectedness_update_index = m_next_selectedness_update_index++;

0 commit comments

Comments
 (0)