Closed Bug 1476301 Opened 6 years ago Closed 6 years ago

[FIX] Focus trap into webcomponents with shadowDOM


(Core :: DOM: Core & HTML, defect, P3)

63 Branch



Tracking Status
firefox63 --- fixed


(Reporter: xaviermd, Assigned: smaug)



(Keywords: nightly-community)


(3 files, 1 obsolete file)

User Agent: Mozilla/5.0 (X11; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0
Build ID: 20180716100102

Steps to reproduce:

- Go on (NOTE : <import> polyfilled) or download
- Use →tab to move focus in the page

Actual results:

Once the focus is into a shadowDOM of the webcomponent, the focus cannot go outside forward or backward

Expected results:

The focus must continue to go forward
I find out a part of the answer : 

The shadowRoot of my WebComponent has “2 tabindex="0"” declarations in the <template> tag  :

<div class="interface" tabindex="0">
	<div class="principal">
		<img class="poster nosmall" src="" alt="" />
		<div class="pageerror">
		<div class="pagemain">
			<a class="control" tabindex="0">

If I put different positive numbers, I have less problems, excepted that the focus became really erratic.

Afterwards ( ), I added some code to support a <menu>, with tabindex="0" added via innerHTML :

line.innerHTML = `<a href="#${}&t=${cuepoint}" tabindex="0">

Funnily, the webcomponent is not reacting the same way, as you can focus backwards via [shift]+[tab], but you're still trapped in.

I can try to add programmatically element.tabindex, but it means a lot of undesired complexity.

Chrome works as desired. May be it is a bug due to their webcomponents.js polyfill ? It means my code really needs a native <link rel="import"> support.
Component: Untriaged → DOM
Product: Firefox → Core
Priority: -- → P3
Any chance for a minimal testcase attached to this bug?
Flags: needinfo?(xaviermd)
Yes, I will try to create it
Flags: needinfo?(xaviermd)
Here is the minimal testcase.

When you move your focus with the [tab] key (or [shift]+[tab] for backwards), once the focus enters into a webcomponent, it cannot evade it.
Adding style for a better visibility
Attachment #8998222 - Attachment is obsolete: true
Great, thanks. I'll try to look at this tomorrow
ok, hmm, I think I've fixed that issue already once before. And I thought I had added a testcase :/
Assignee: nobody → bugs
Ever confirmed: true
On could argue this is a bug in FrameTraversal, since it deals with inline elements differently.

The key point in the test is that host is not div, but span.

(there is another sequential bug in shadow DOM, so I may need to tweak this code still a bit.)
Attachment #8998675 - Flags: review?(mrbkap)
Summary: [WebComponents] Focus trap into webcomponents with shadowDOM → [FIX] Focus trap into webcomponents with shadowDOM
Comment on attachment 8998675 [details] [diff] [review]

Review of attachment 8998675 [details] [diff] [review]:

::: dom/base/nsFocusManager.cpp
@@ +3599,5 @@
>        nsIContent* currentContent = frame->GetContent();
>        nsIContent* oldTopLevelHost = currentTopLevelHost;
> +      if (oldTopLevelHost != currentContent) {
> +        nsIContent* topLevel = GetTopLevelHost(currentContent);
> +        currentTopLevelHost = topLevel;

Nit: no need for the temporary `topLevel`.
Attachment #8998675 - Flags: review?(mrbkap) → review+
Pushed by
try to ensure sequential focusing can leave shadow trees, r=mrbkap
Closed: 6 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla63
I just checked and.... keyboard focus is still trapped in a little more complex page.
What i have missed ?
Resolution: FIXED → ---
Could you open a new bug, please.
The patch in this bug fixed the issue shown by the testcase.
Closed: 6 years ago6 years ago
Flags: needinfo?(xaviermd)
Resolution: --- → FIXED
Yes, i will.

But i still don't understand where the problem lies, and so how to reproduce it.
Flags: needinfo?(xaviermd)
Flags: needinfo?(xaviermd)
I also encountered a complicated situations where shadow dom focusable elements cannot be tabbed to, and right now I also haven't found the virutal cause where the issue lies. Uploading the whole document and styles seems an overkill
Component: DOM → DOM: Core & HTML

Sorry. No more focus problems here.

Flags: needinfo?(xaviermd)
You need to log in before you can comment on or make changes to this bug.