XUL:Focus Behaviour

From MozillaWiki
Jump to: navigation, search

The following is how Mozilla determines what elements can be focused and which can not:

  • elements that are hidden (display: none) cannot be focused
  • elements that have the CSS visibility property set to collapse or hidden are not focusable.
  • an HTML element in an editor is not focusable
  • an HTML element cannot be focused if it has a disabled attribute.
  • an XML element cannot be focused unless it has a link/xlink.
  • a XUL element that implements nsIDOMXULControlElement cannot be focused if its disabled property is true.
  • otherwise the focusability of an element is determined by the -moz-user-focus property. If it is set to 'ignore' or 'none, the element cannot be focused. Other values allow focus.
  • the value of -moz-user-focus is overriden for HTML elements if they have a tabindex attribute
  • the value of -moz-user-focus is overriden for an enabled XUL element that implements nsIDOMXULControlElement if its tabIndex property returns a value greater or equal to zero.
  • you can focus a scrollable HTML element -- one with overflow set to 'scroll' or 'auto' -- if you change the focus with the tab key, but not by using the mouse.

Some HTML elements such as form elements have additional focus rules.

On the Mac, there's a system setting "Full Keyboard Access", which can be used to toggle between this model and a model where only listboxes, trees and textboxes can be focused.

The following XUL elements implement nsIDOMXULControlElement:

button, caption, checkbox, colorpicker, description, label, listbox, listitem, menu, menuitem, menulist, radio, radiogroup, richlistbox, richlistitem, tab, tabs, textbox, tree

The following XUL elements have -moz-user-focus set to normal:

browser, editor, iframe

How focus should behave in 2.0

Two attributes are used to specify how focus behaves on an element, focusable and tabindex. A focusable property is added to nsIDOMXULElement. It is a string with three possible values: 'false', 'true' or 'auto'.

On setting the focusable property, it should change the value of the focusable attribute on that element to the same value. On retrieving, it should retrieve the value of the focusable attribute on that element.

If the value of the focusable property is not the string 'true' or the string 'false', then the value 'auto' is assumed. Thus, the 'auto' value is the default if the focusable attribute is not specified. The focusable attribute should not be specified when the value is 'auto'.

When the focusable property is 'false', the element cannot be focused by any means. The tab index of the element is ignored and the element is skipped in the tab order.

When the focusable property is 'true', the element may be focused by clicking it with the mouse. The used tab index is determined as follows:

  • if the element implements the nsIDOMXULControlElement interface, the value of the tabIndex property of that interface is the used tab index.
  • otherwise, if the element has a tabindex attribute set to a non-negative integer, that value is the used tab index.
  • otherwise, the used tab index is -1.

When the focusable property is 'auto', the focusability is determined from the tab index. The used tab index is determined as follows:

  • if the element implements the nsIDOMXULControlElement interface, the value of the tabIndex property of that interface is the used tab index. The element is focusable, and the used tab index affects the tabbing order.
  • otherwise, if the element has a tabindex attribute set to a non-negative integer, that value is the used tab index. The element is focusable, and the used tab index affects the tabbing order.
  • otherwise, the used tab index is -1 and the element is not focusable.

When the used tab index is -1, the element is not part of the tab order, but may still be focusable as defined above. When the used tab index is a non negative integer, it becomes part of the tab order.

The default implementation of the getter for the tabIndex property of the nsIDOMXULControlElement interface is:

  • if the tabindex attribute is set to a positive integer or -1, return that value.
  • otherwise, return 0.

The -moz-user-focus CSS property is obsolete and does nothing (or for compatibility, the value 'ignore' is treated as focusable='false' and the value 'normal' is treated as focusable='auto').