Skip to content

Commit 240cb61

Browse files
authored
Minimize attribute churn when retranslating DOM Overlays (#354)
1 parent a6ae23f commit 240cb61

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

fluent-dom/src/overlay.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ function overlayChildNodes(fromFragment, toElement) {
128128
toElement.appendChild(fromFragment);
129129
}
130130

131+
function hasAttribute(attributes, name) {
132+
if (!attributes) {
133+
return false;
134+
}
135+
for (let attr of attributes) {
136+
if (attr.name === name) {
137+
return true;
138+
}
139+
}
140+
return false;
141+
}
142+
131143
/**
132144
* Transplant localizable attributes of an element to another element.
133145
*
@@ -144,9 +156,11 @@ function overlayAttributes(fromElement, toElement) {
144156
.split(",").map(i => i.trim())
145157
: null;
146158

147-
// Remove existing localizable attributes.
159+
// Remove existing localizable attributes if they
160+
// will not be used in the new translation.
148161
for (const attr of Array.from(toElement.attributes)) {
149-
if (isAttrNameLocalizable(attr.name, toElement, explicitlyAllowed)) {
162+
if (isAttrNameLocalizable(attr.name, toElement, explicitlyAllowed)
163+
&& !hasAttribute(fromElement.attributes, attr.name)) {
150164
toElement.removeAttribute(attr.name);
151165
}
152166
}
@@ -160,7 +174,8 @@ function overlayAttributes(fromElement, toElement) {
160174

161175
// Set localizable attributes.
162176
for (const attr of Array.from(fromElement.attributes)) {
163-
if (isAttrNameLocalizable(attr.name, toElement, explicitlyAllowed)) {
177+
if (isAttrNameLocalizable(attr.name, toElement, explicitlyAllowed)
178+
&& toElement.getAttribute(attr.name) !== attr.value) {
164179
toElement.setAttribute(attr.name, attr.value);
165180
}
166181
}

0 commit comments

Comments
 (0)