Skip to content

Commit b31d5e1

Browse files
committed
bug #1130 [LiveComponent] Workaround for bad behavior with Chrome's translation feature (weaverryan)
This PR was merged into the 2.x branch. Discussion ---------- [LiveComponent] Workaround for bad behavior with Chrome's translation feature | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Tickets | Fix #1120 | License | MIT A bit of a hack, but works nicely locally. As `@smnandre` mentioned, Firefox and Safari are already ok, as they modify the page without triggering the MutationObserver or adding any ugly `font` tags ;). Cheers! Commits ------- 5204f83 [LiveComponent] Workaround for bad behavior with Chrome's translation feature
2 parents 976b53f + 5204f83 commit b31d5e1

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

src/LiveComponent/assets/dist/Rendering/ExternalMutationTracker.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ export default class {
2222
private handleStyleAttributeMutation;
2323
private handleGenericAttributeMutation;
2424
private extractStyles;
25+
private isElementAddedByTranslation;
2526
}

src/LiveComponent/assets/dist/live_controller.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,9 @@ class ExternalMutationTracker {
15651565
if (!this.shouldTrackChangeCallback(element)) {
15661566
continue;
15671567
}
1568+
if (this.isElementAddedByTranslation(element)) {
1569+
continue;
1570+
}
15681571
let isChangeInAddedElement = false;
15691572
for (const addedElement of this.addedElements) {
15701573
if (addedElement.contains(element)) {
@@ -1603,6 +1606,9 @@ class ExternalMutationTracker {
16031606
this.removedElements.splice(this.removedElements.indexOf(node), 1);
16041607
return;
16051608
}
1609+
if (this.isElementAddedByTranslation(node)) {
1610+
return;
1611+
}
16061612
this.addedElements.push(node);
16071613
});
16081614
mutation.removedNodes.forEach((node) => {
@@ -1711,6 +1717,9 @@ class ExternalMutationTracker {
17111717
});
17121718
return styleObject;
17131719
}
1720+
isElementAddedByTranslation(element) {
1721+
return element.tagName === 'FONT' && element.getAttribute('style') === 'vertical-align: inherit;';
1722+
}
17141723
}
17151724

17161725
class ChildComponentWrapper {

src/LiveComponent/assets/src/Rendering/ExternalMutationTracker.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ export default class {
7575
continue;
7676
}
7777

78+
if (this.isElementAddedByTranslation(element)) {
79+
continue;
80+
}
81+
7882
// ignore changes in elements that were externally-added
7983
let isChangeInAddedElement = false;
8084
for (const addedElement of this.addedElements) {
@@ -133,6 +137,10 @@ export default class {
133137
return;
134138
}
135139

140+
if (this.isElementAddedByTranslation(node)) {
141+
return;
142+
}
143+
136144
this.addedElements.push(node);
137145
});
138146

@@ -293,4 +301,16 @@ export default class {
293301

294302
return styleObject;
295303
}
304+
305+
/**
306+
* Helps avoid tracking changes by Chrome's translation feature.
307+
*
308+
* When Chrome translates, it mutates the dom in a way that triggers MutationObserver.
309+
* This includes adding new elements wrapped in a <font> tag. This causes live
310+
* components to incorrectly think that these new elements should persist through
311+
* re-renders, causing duplicate text.
312+
*/
313+
private isElementAddedByTranslation(element: Element): boolean {
314+
return element.tagName === 'FONT' && element.getAttribute('style') === 'vertical-align: inherit;'
315+
}
296316
}

0 commit comments

Comments
 (0)