Closed Bug 1617669 Opened 5 years ago Closed 5 years ago

Shadow DOM handled by document.l10n on mutation, but not on creation

Categories

(Core :: XUL, enhancement)

enhancement
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla76
Tracking Status
firefox76 --- fixed

People

(Reporter: Pike, Assigned: zbraniecki)

References

Details

Attachments

(1 file, 1 obsolete file)

When using Fluent in shadow DOM, we add shadowRoot as explicit root to a DOMLocalization, but when manipulating the shadow DOM, the mutation observers fire on document.l10n.

This is sadly inconsistent.

We're looking at using shadow DOM to isolate remote settings l10n from the regular document.l10n in bug 1616280, and that doesn't work out alright.

smaug, it seems that https://searchfox.org/mozilla-central/source/dom/l10n/L10nMutations.cpp is rather generous at listening to mutations, and doesn't care about the roots in the corresponding DOMLocalization?

Would it be feasible to make that content check if the content is part of its roots?

Flags: needinfo?(bugs)

What content check?
I feel like I'm missing some context here.

Flags: needinfo?(bugs)
Priority: -- → P3

Andreio - can you provide a minimized testcase analogous to https://searchfox.org/mozilla-central/source/dom/l10n/tests/mochitest/dom_localization/test_connectRoot_webcomponent.html of the issue you're facing?

It'll make it easier for us to reproduce it and fix.

Component: Internationalization → XUL
Flags: needinfo?(andrei.br92)

The component has been changed since the backlog priority was decided, so we're resetting it.
For more information, please visit auto_nag documentation.

Priority: P3 → --
Attached file Test for Bug 1617669 (obsolete) —

In this test, I don't connectRoot the shadow dom, but the
localization from document.l10n is applied via a mutation observer
on setAttributes.
Also note that it's not the localization that we call setAttributes
on.

The observer methods check for elem->IsInComposedDoc(), which is OK if there's only one document.l10n involved, and everything with data-l10n-id is hooked up.

These checks would need to be extended to walk up their parents and see if the hit a root in mDOMLocalization?

smaug, does this testcase help to see what we were trying to do?

Flags: needinfo?(andrei.br92) → needinfo?(bugs)

But there is // document.domLoc.connectRoot(this.shadowRoot);
Why is that?

Flags: needinfo?(bugs)

I left the commented-out connectRoot in just to hightlight where the original test connected the root to it's non-default DOMLocalization object.

Just to clarify, I don't think this one test is an exhaustive test coverage, it's just one example. Which I understood to be what Zibi asked for in comment 2.

I'm still puzzled what this bug is about.
https://searchfox.org/mozilla-central/rev/96f1457323cc598a36f5701f8e67aedaf97acfcf/dom/l10n/DOMLocalization.cpp#85,105
observes mutations in the subtree under aNode. But I guess that works as expected? And initial translation doesn't happen? So do you mean that when ConnectRoot is called, some localization should happen? But isn't translateFragment for that case. or translateRoots.

I don't know what "Would it be feasible to make that content check if the content is part of its roots?" means.

The expected behavior is for this element to be not localized, w/out explicitly hooking it up to a DOMLocalization.

Once explicitly hooked up, the expectation is that it's handled by that DOMLocalization, and to not have different DOMLocalizations fight over what wins.

ok, still a bit unclear to me.
Do you perhaps mean that L10nMutations shouldn't do anything with nodes which aren't in its subtree?
(That happens because of https://searchfox.org/mozilla-central/rev/96f1457323cc598a36f5701f8e67aedaf97acfcf/dom/base/MutationObservers.cpp#84-88)

If so, the methods implementing nsIMutationObserver methods could check if modified node is contained within some root.
(that could be rather slow if there are lots of roots). One could of course be on L10nMutations per root, but that might be slow too if there are tons of them - but if we don't expect too many roots, that might be doable. Then L10nMutations could have a pointer to the relevant root.

Right.

Also, the root checker could drop if it crosses from shadow to non-shadow DOM?

As for the number of roots, I can see DOMLocalizations for things like panel objects to be quite extensive. OTH, shadow DOMs are probably pretty shallow, and you if you stop at the shadow-non-shadow bridge, you'd stop rather early.

I'll take a look at this. I'm concerned it'll lead to more code being increasingly broken when trying to fix it, so better now than later.

Assignee: nobody → gandalf
Status: NEW → ASSIGNED

Olli - can you check if the patch looks reasonable? I'm going to work on improving the tests (need to add coverage of adding elements) and will check how the performance looks like, but wanted to run it through you first.

Overall, I feel like it should be mostly ok for smaller cases, but I'm a bit concerned that for big documents like browser.xhtml or preferences.xhtml, traversing nodes up for every element to check if it's in the document will cost.

In reality, I'd probably want to just check if the element is in <template/> and only then verify if it is in roots, otherwise it must be since it got mutation observed called on it. Is that right?

Flags: needinfo?(bugs)

Alternatively, maybe we could extend the internal MutationObserver to not report mutations on nodes that aren't in its subtree?

That might be useful for some other cases, but requires quite some changes, and might slow down DOM mutations in general, since the whole
nsIMutationObserver system would need to track which node is possible slotted in shadow DOM etc.

Flags: needinfo?(bugs)

Does it mean you recommend it over the patch that I wrote? Or do you think the patch is a reasonable short term approach and we can investigate the feature for MutationObserver separately?

Flags: needinfo?(bugs)

Yes, go with the approach the patch has taken.

Flags: needinfo?(bugs)

Filed a follow up as bug 1624029.

See Also: → 1624029
Pushed by zbraniecki@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/f76a5f5c354b Check if an element is in any of the roots before adding to pending mutations. r=smaug
Status: ASSIGNED → RESOLVED
Closed: 5 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla76
Regressions: 1626937
Attachment #9128731 - Attachment is obsolete: true
Regressions: 1725890
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: