Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LiveComponent] Workaround for bad behavior with Chrome's translation feature #1130

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ export default class {
private handleStyleAttributeMutation;
private handleGenericAttributeMutation;
private extractStyles;
private isElementAddedByTranslation;
}
9 changes: 9 additions & 0 deletions src/LiveComponent/assets/dist/live_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,9 @@ class ExternalMutationTracker {
if (!this.shouldTrackChangeCallback(element)) {
continue;
}
if (this.isElementAddedByTranslation(element)) {
continue;
}
let isChangeInAddedElement = false;
for (const addedElement of this.addedElements) {
if (addedElement.contains(element)) {
Expand Down Expand Up @@ -1603,6 +1606,9 @@ class ExternalMutationTracker {
this.removedElements.splice(this.removedElements.indexOf(node), 1);
return;
}
if (this.isElementAddedByTranslation(node)) {
return;
}
this.addedElements.push(node);
});
mutation.removedNodes.forEach((node) => {
Expand Down Expand Up @@ -1711,6 +1717,9 @@ class ExternalMutationTracker {
});
return styleObject;
}
isElementAddedByTranslation(element) {
return element.tagName === 'FONT' && element.getAttribute('style') === 'vertical-align: inherit;';
}
}

class ChildComponentWrapper {
Expand Down
20 changes: 20 additions & 0 deletions src/LiveComponent/assets/src/Rendering/ExternalMutationTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export default class {
continue;
}

if (this.isElementAddedByTranslation(element)) {
continue;
}

// ignore changes in elements that were externally-added
let isChangeInAddedElement = false;
for (const addedElement of this.addedElements) {
Expand Down Expand Up @@ -133,6 +137,10 @@ export default class {
return;
}

if (this.isElementAddedByTranslation(node)) {
return;
}

this.addedElements.push(node);
});

Expand Down Expand Up @@ -293,4 +301,16 @@ export default class {

return styleObject;
}

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