Closed Bug 1584202 Opened 5 years ago Closed 5 years ago

Can't programatically focus a contenteditable label element

Categories

(Core :: DOM: UI Events & Focus Handling, defect, P3)

defect

Tracking

()

RESOLVED DUPLICATE of bug 1240285

People

(Reporter: emilio, Unassigned)

Details

data:text/html,<label contenteditable style="display: block; user-select: text">Foo</label><button onclick='document.querySelector("label").focus()'>Focus label</button>

Click and tab navigation do focus the element, so this is pretty weird behavior IMO.

WDYT Masayuki? I think we should allow to programatically focus the element, but wanted to double-check with you.

Flags: needinfo?(masayuki)

I think that <label> element should do nothing about focus handling if it's editable. For example, even if it's an element in contenteditable="true", clicking it should be treated as moving caret to the clicked position.

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

This is not any element, but labels specifically. If I replace label with div or span, then the test works as expected.

HTMLLabelElement::focus has been overridden, to delegate the focus to the labeled element as identified by the for attribute, but when the for attribute is not set, nothing happens.

I suppose that there should be a fallback to focusing the label element if it is either a contentEditable host or has a tabIndex, after:
https://searchfox.org/mozilla-central/rev/5a10be606f2d76ef22f1f44565749490de991d35/dom/html/HTMLLabelElement.cpp#60-64

Summary: Can't programatically focus a contenteditable element → Can't programatically focus a contenteditable label element
Status: NEW → RESOLVED
Closed: 5 years ago
Component: DOM: Core & HTML → DOM: UI Events & Focus Handling
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.