From fb18db7f19cbfa77e7ce3063e96829d4bbf68e87 Mon Sep 17 00:00:00 2001 From: Matt King Date: Wed, 10 Jul 2019 21:16:39 -0700 Subject: [PATCH] Naming and Describing Section: Revisions to address feedback in issue 1050 (pull #1062) * Fallback naming guidance section: reference cardinal rule 4 to avoid it. * Naming Rule 3: Remove mention of alt attribute: Per feedback from @LJWatson in issue #1050, revises rule 3 to remove mention of alt. * Adds sentence refering readers to the naming techniques for specific advantages of using HTML features instead of ARIA. * Fix formatting and bugs in code snippets reported in #1060. * Intro: most fundamental and important -> most important * Minor editorial change to section: How Are Name and Description Strings Derived? * Intro paragraph: additional editorial simplification based on feedback from @LJWatson in issue #1050. * Clarify language describing when captions become descriptions: to resolve feedback from @jessebeach in issue #1050. Modified language in sections 5.3.2.6 Naming Tables and Figures with Captions and section 5.4.1.3 Describing Tables and Figures with Captions. * Provide richer description of screen reader behavior in section 5.1 per feedback from @jongund in issue #1050. * Remove aria-details guidance, which will be addressed in the future by issue #70. --- aria-practices.html | 260 +++++++++++++++++++------------------------- 1 file changed, 112 insertions(+), 148 deletions(-) diff --git a/aria-practices.html b/aria-practices.html index c826b0c267..48c1357acd 100644 --- a/aria-practices.html +++ b/aria-practices.html @@ -3697,15 +3697,10 @@
Examples

Providing Accessible Names and Descriptions

- Note: This is a draft of a new section. - Please provide feedback in - issue 1050. -

-

- Providing elements with accessible names, and where appropriate, accessible descriptions is one of the most fundamental and important responsibilities authors have when developing accessible web experiences. - While doing so is straightforward for most elements, technical mistakes that can have devastating impacts on users of assistive technologies are easy to make and unfortunately common. - To help authors provide effective and reliable accessible names and descriptions, this section explains their purpose, when authors need to provide them, how browsers assemble them, and rules for coding and composing them. - It also provides guidance for effectively employing the following naming and describing techniques and WAI-ARIA properties: + Providing elements with accessible names, and where appropriate, accessible descriptions is one of the most important responsibilities authors have when developing accessible web experiences. + While doing so is straightforward for most elements, technical mistakes that can completely block users of assistive technologies are easy to make and unfortunately common. + To help authors effectively provide accessible names and descriptions, this section explains their purpose, when authors need to provide them, how browsers assemble them, and rules for coding and composing them. + It also guides authors in the use of the following naming and describing techniques and WAI-ARIA properties:

@@ -3759,8 +3760,8 @@

What ARE Accessible Names and Descriptions?

How Are Name and Description Strings Derived?

Because there are several elements and attributes for specifying text to include in an accessible name or description string, and because authors can combine them in a practically endless number of ways, browsers implement fairly complex algorithms for assembling the strings. -The sections on accessible name calculation and accessible description calculation provide detailed explanations of the algorithms. -However, most authors do not need detailed understanding of the algorithms since nearly all circumstances where a name or description is useful are supported by the coding patterns described in the naming techniques and describing techniques sections. +The sections on accessible name calculation and accessible description calculation explain the algorithms and how they implement precedence. +However, most authors do not need such detailed understanding of the algorithms since nearly all circumstances where a name or description is useful are supported by the coding patterns described in the naming techniques and describing techniques sections.

@@ -3797,8 +3798,9 @@
Rule 2: Prefer Visible Text
Rule 3: Prefer Native Techniques

- In HTML documents, whenever possible, rely on HTML naming techniques, such as the HTML label element, alt attribute for images, caption element for tables, etc. + In HTML documents, whenever possible, rely on HTML naming techniques, such as the HTML label element for form elements and caption element for tables. While less flexible, their simplicity and reliance on visible text help ensure robust accessible experiences. + Several of the naming techniques highlight specific accessibility advantages of using HTML features instead of ARIA attributes.

@@ -3864,8 +3866,7 @@
Naming with Child Content
In two special cases, certain descendants are ignored: group descendants of treeitem elements and menu descendants of menuitem elements are omitted from the calculation. For example, in the following tree, the name of the first tree item is Fruits; Apples, Bananas, and Oranges are omitted.

-

-<ul role="tree">
+        
<ul role="tree">
   <li role="treeitem">Fruits
     <ul role="group">
       <li role="treeitem">Apples</li>
@@ -3873,8 +3874,7 @@ 
Naming with Child Content
<li role="treeitem">Oranges</li> </ul> </li> -</ul> -
+</ul>

If an element with one of the above roles that supports naming from child content is named by using aria-label or aria-labelledby, content contained in the element and its descendants is hidden from assistive technology users unless the descendant content is referenced by aria-labelledby. It is strongly recommended to avoid using either of these attributes to override content of one of the above elements except in rare circumstances where hiding content from assistive technology users is beneficial. @@ -3898,11 +3898,9 @@

Naming with a String Attribute Via aria-label
However, when applied to nearly any other type of element, assistive technologies will render both the value of aria-label and the content of the element. For example, the name of the following navigation region is "Product".

-

-  <nav aria-label="Product">
-    <!-- list of navigation links to product pages -->
-  </nav>
-          
+
<nav aria-label="Product">
+  <!-- list of navigation links to product pages -->
+</nav>

When encountering this navigation region, a screen reader user will hear the name and role of the element, e.g., "Product navigation region", and then be able to read through the links contained in the region.

@@ -3923,19 +3921,15 @@
Naming with Referenced Content Via aria-labelledby
The aria-labelledby property enables authors to reference other elements on the page to define an accessible name. For example, the following switch is named by the text content of a previous sibling element.

-

-  <span id="night-mode-label">Night mode</span>
-  <span role="switch" aria-checked="false" tabindex="0" aria-labelledby="night-mode-label"></span>
-          
+
<span id="night-mode-label">Night mode</span>
+<span role="switch" aria-checked="false" tabindex="0" aria-labelledby="night-mode-label"></span>

Note that while using aria-labelledby is similar in this situation to using an HTML label element with the for attribute, one significant difference is that browsers do not automatically make clicking on the labeling element activate the labeled element; that is an author responsibility. However, HTML label cannot be used to label a span element. Fortunately, an HTML input with type="checkbox" allows the ARIA switch role, so when feasible, using the following approach creates a more robust solution.

-

-  <label for="night-mode">Night mode</label>
-  <input type="checkbox" role="switch" id="night-mode">
-          
+
<label for="night-mode">Night mode</label>
+<input type="checkbox" role="switch" id="night-mode">

The aria-labelledby property is useful in a wide variety of situations because:

An example of referencing a hidden element with aria-labelledby could be a label for a night switch control:

<span id="night-mode-label" hidden>Night mode</span>
-  <input type="checkbox" role="switch" aria-labelledby="night-mode-label">
+<input type="checkbox" role="switch" aria-labelledby="night-mode-label">

In some cases, the most effective name for an element is its own content combined with the content of another element. Because aria-labelledby has highest precedence in name calculation, in those situations, it is possible to use aria-labelledby to reference both the element itself and the other element. In the following example, the "Read more..." link is named by the element itself and the article’s heading, resulting in a name for the link of "Read more... 7 ways you can help save the bees".

-

-  <h2 id="bees-heading">7 ways you can help save the bees</h2>
-  <p>Bees are disappearing rapidly. Here are seven things you can do to help.</p>
-  <p><a id="bees-read-more" aria-labelledby="bees-read-more bees-heading">Read more...</a></p>
-          
+
<h2 id="bees-heading">7 ways you can help save the bees</h2>
+<p>Bees are disappearing rapidly. Here are seven things you can do to help.</p>
+<p><a id="bees-read-more" aria-labelledby="bees-read-more bees-heading">Read more...</a></p>

When multiple elements are referenced by aria-labelledby, text content from each referenced element is concatenated in the order specified in the aria-labelledby value. If an element is referenced more than one time, only the first reference is processed. @@ -3990,21 +3982,17 @@

Naming Form Controls with the Label Element
However, unless the text is programmatically associated with the checkbox, assistive technology users will experience a checkbox without a label. Wrapping the checkbox and the labeling text in a label element as follows gives the checkbox an accessible name.

-

-  <label>
-    <input type="checkbox" name="subscribe">
-    subscribe to our newsletter
-  </label>
-          
+
<label>
+  <input type="checkbox" name="subscribe">
+  subscribe to our newsletter
+</label>

A form control can also be associated with a label by using the for attribute on the label element. This allows the label and the form control to be siblings or have different parents in the DOM, but requires adding an id attribute to the form control, which can be error-prone. When possible, use the above encapsulation technique for association instead of the following for attribute technique.

-

-  <input type="checkbox" name="subscribe" id="subscribe_checkbox">
-  <label for="subscribe_checkbox">subscribe to our newsletter</label>
-          
+
<input type="checkbox" name="subscribe" id="subscribe_checkbox">
+<label for="subscribe_checkbox">subscribe to our newsletter</label>

Using the label element is an effective technique for satisfying Rule 2: Prefer Visible Text. It also satisfies Rule 3: Prefer Native Techniques. @@ -4019,14 +4007,12 @@

Naming Fieldsets with the Legend Element
The HTML fieldset element can be used to group form controls, and the legend element can be used to give the group a name. For example, a group of radio buttons can be grouped together in a fieldset, where the legend element labels the group for the radio buttons.

-

-  <fieldset>
-    <legend>Select your starter class</legend>
-    <label><input name="starter-class" value="green"> Green</label>
-    <label><input name="starter-class" value="red"> Red</label>
-    <label><input name="starter-class" value="blue"> Blue</label>
-  </fieldset>
-          
+
<fieldset>
+  <legend>Select your starter class</legend>
+  <label><input type="radio" name="starter-class" value="green"> Green</label>
+  <label><input type="radio" name="starter-class" value="red"> Red</label>
+  <label><input type="radio" name="starter-class" value="blue"> Blue</label>
+</fieldset>

This grouping technique is particularly useful for presenting multiple choice questions. It enables authors to associate a question with a group of answers. @@ -4035,27 +4021,32 @@

Naming Fieldsets with the Legend Element

Similar benefits can be gained from grouping and naming other types of related form fields using fieldset and legend.

-

-  <fieldset>
-    <legend>Shipping address</legend>
-    <p><label>Full name <input name="name" required></label></p>
-    <p><label>Address line 1 <input name="address-1" required></label></p>
-    <p><label>Address line 2 <input name="address-2"></label></p>
-    ...
-  </fieldset>
-  <fieldset>
-    <legend>Billing address</legend>
-    ...
-  </fieldset>
-          
+
<fieldset>
+  <legend>Shipping address</legend>
+  <p><label>Full name <input name="name" required></label></p>
+  <p><label>Address line 1 <input name="address-1" required></label></p>
+  <p><label>Address line 2 <input name="address-2"></label></p>
+  ...
+</fieldset>
+<fieldset>
+  <legend>Billing address</legend>
+  ...
+</fieldset>

Using the legend element to name a fieldset element satisfies Rule 2: Prefer Visible Text and Rule 3: Prefer Native Techniques.

Naming Tables and Figures with Captions
-

The accessible name for the HTML table and figure elements can be derived from a child caption or figcaption element, respectively.

-

Tables and figures often have a caption to explain what the table of figure is about, how to read them, and sometimes giving them numbers to be able to refer to them in surrounding prose. This can help users navigate and understand the content better. This is generally useful for all users, but especially for users of assistive technologies.

-

In HTML, the table element marks up a data table, and can be provided with a caption using the caption element.

+

+ The accessible name for HTML table and figure elements can be derived from a child caption or figcaption element, respectively. + Tables and figures often have a caption to explain what they are about, how to read them, and sometimes giving them numbers used to refer to them in surrounding prose. + Captions can help all users better understand content, but are especially helpful to users of assistive technologies. +

+

+ In HTML, the table element marks up a data table, and can be provided with a caption using the caption element. + If the table element does not have aria-label or aria-labelledby, then the caption will be used as the accessible name. + For example, the accessible name of the following table is Special opening hours. +

<table>
  <caption>Special opening hours</caption>
  <tr><td>30 May <td>Closed
@@ -4079,12 +4070,23 @@ 
Naming Tables and Figures with Captions
</table>

Note: Above table content is from Caloric restriction, the traditional Okinawan diet, and healthy aging: the diet of the world's longest-lived people and its potential impact on morbidity and life span.

-

Similarly, the HTML figure element can be given a caption using the figcaption element. The caption can appear before or after the figure, but it is more common for figures to have the caption after.

+

+ If a table is named using aria-label or aria-labelledby, then a caption element, if present, will become an accessible description. + For an example, see Describing Tables and Figures with Captions. +

+

+ Similarly, an HTML figure element can be given a caption using the figcaption element. + The caption can appear before or after the figure, but it is more common for figures to have the caption after. +

<figure>
  <img alt="Painting of a person walking in a desert." src="Hole_JesusalDesierto.jpg">
  <figcaption>Jesus entering the desert as imagined by William Hole, 1908</figcaption>
 </figure>
- +

+ Like with table elements, if a figure is not named using aria-label or aria-labelledby, the content of the figcaption element will be used as the accessible name. + However unlike table elements, if the figcaption element is not used for the name, it does not become an accessible description unless it is referenced by aria-describedby. + Nevertheless, assistive technologies will render the content of a figcaption regardless of whether it is used as a name, description, or neither. +

Using the caption element to name a table element, or a figcaption element to name a figure element, satisfies Rule 2: Prefer Visible Text and Rule 3: Prefer Native Techniques.

@@ -4092,7 +4094,8 @@
Naming Tables and Figures with Captions
Fallback Names Derived from Titles and Placeholders

When an accessible name is not provided using one of the primary techniques (e.g., the aria-label or aria-labelledby attributes), or native markup techniques (e.g., the HTML label element, or the alt attribute of the HTML img element), browsers calculate an accessible name from other attributes as a fallback mechanism. - It is recommended authors prefer the explicit labeling techniques described above over fallback techniques described in this section. + Because the attributes used in fallback name calculation are not intended for naming, they typically yield low quality accessible names that are not effective. +So, As advised by Rule 4: Avoid Browser Fallback, prefer the explicit labeling techniques described above over fallback techniques described in this section.

@@ -4104,10 +4107,10 @@

Fallback Names Derived from Titles and Placeholders

For example, a fieldset element without a legend element child, but with a title attribute, gets its accessible name from the title attribute.

<fieldset title="Select your starter class">
-    <label><input name="starter-class" value="green"> Green</label>
-    <label><input name="starter-class" value="red"> Red</label>
-    <label><input name="starter-class" value="blue"> Blue</label>
-   </fieldset>
+ <label><input type="radio" name="starter-class" value="green"> Green</label> + <label><input type="radio" name="starter-class" value="red"> Red</label> + <label><input type="radio" name="starter-class" value="blue"> Blue</label> +</fieldset>

For the HTML input and textarea elements, the placeholder attribute is used as a fallback labeling mechanism if nothing else (including the title attribute) results in a label. @@ -4951,8 +4954,7 @@

Accessible name calculation

When calculating a name from content for the treeitem role, descendant content of child group elements are not included. For example, in the following tree, the name of the first tree item is Fruits; Apples, Bananas, and Oranges are automatically omitted.

-

-<ul role="tree">
+        
<ul role="tree">
   <li role="treeitem">Fruits
     <ul role="group">
       <li role="treeitem">Apples</li>
@@ -4960,14 +4962,12 @@ 

Accessible name calculation

<li role="treeitem">Oranges</li> </ul> </li> -</ul> -
+</ul>

Similarly, when calculating a name from content for the menuitem role, descendant content of child menu elements are not included. So, the name of the first parent menuitem in the following menu is Fruits.

-

-<ul role="menu">
+        
<ul role="menu">
   <li role="menuitem">Fruits
     <ul role="menu">
       <li role="menuitem">Apples</li>
@@ -4975,8 +4975,7 @@ 

Accessible name calculation

<li role="menuitem">Oranges</li> </ul> </li> -</ul> -
+</ul>
Examples of non-recursive accessible name calculation
@@ -5036,12 +5035,10 @@
Examples of recursive accessible name calculation

In this example, the label for the button is computed by first following the aria-labelledby reference to the parent element, and then computing the label for that element from the child nodes, first visiting the button element again but ignoring the aria-labelledby reference and instead using the aria-label, and then visiting the next child (the text node). The resulting label is Remove meeting: Daily status report.

-

-  <div id="meeting-1">
-    <button aria-labelledby="meeting-1" aria-label="Remove meeting:">X</button>
-    Daily status report
-  </div>
-  
+
<div id="meeting-1">
+  <button aria-labelledby="meeting-1" aria-label="Remove meeting:">X</button>
+  Daily status report
+</div>
@@ -5057,19 +5054,15 @@
Describing by referencing content with aria-describedby
For example, a button could be described by a sibling paragraph.

-

-  <button aria-describedby="trash-desc">Move to trash</button>
-  ...
-  <p id="trash-desc">Items in the trash will be permanently removed after 30 days.</p>
-          
+
<button aria-describedby="trash-desc">Move to trash</button>
+...
+<p id="trash-desc">Items in the trash will be permanently removed after 30 days.</p>

Descriptions are reduced to text strings. For example, if the description contains an HTML img element, a text equivalent of the image is computed.

-

-  <button aria-describedby="trash-desc"> Move to <img src="bin.svg" alt="trash"></button>
-  ...
-  <p id="trash-desc">Items in <img src="bin.svg" alt="the trash"> will be permanently removed after 30 days.</p>
-          
+
<button aria-describedby="trash-desc"> Move to <img src="bin.svg" alt="trash"></button>
+...
+<p id="trash-desc">Items in <img src="bin.svg" alt="the trash"> will be permanently removed after 30 days.</p>

As with aria-labelledby, it is possible to reference an element using aria-describedby even if that element is hidden. @@ -5078,56 +5071,28 @@

Describing by referencing content with aria-describedby
In the following example, the accessible description for the input element is Your username is the name that you use to log in to this service.

-

-  <label for="username">Username</label>
-  <input id="username" name="username" aria-describedby="username-desc">
-  <button aria-expanded="false" aria-controls="username-desc" aria-label="Help about username">?</button>
-  <p id="username-desc" hidden>
-    Your username is the name that you use to log in to this service.
-  </p>
-          
- - -
-
Describing by referencing content with aria-details
-

- In some cases, a plain text description is insufficient. - The aria-details property can be used in such situations. - In the following example, a text field for a passenger’s name (when booking a flight) has a description that is a list of three items, and contains a link to an external document with further details. -

- -

-  <ul id="full-name-desc">
-    <li>The passenger's name must match the name in their passport.</li>
-    <li>The name must consist of only characters in the A-Z range.</li>
-    <li><a href="faq.html#name">What if the name in the passport contains other characters?</a></li>
-  </ul>
-  <fieldset>
-    <legend>Passenger 1 (adult)</legend>
-    <p>
-      <label>Full name <input name="full-name" aria-details="full-name-desc"></label>
-    </p>
-    ...
-  </fieldset>
-          
- -

- If both aria-details and aria-describedby are specified on an element, browsers ignore the aria-describedby value when calculating the accessible description. - Specifying both is potentially useful if there is a need to provide a fallback description for browsers and assistive technologies that do not yet support aria-details. -

+
<label for="username">Username</label>
+<input id="username" name="username" aria-describedby="username-desc">
+<button aria-expanded="false" aria-controls="username-desc" aria-label="Help about username">?</button>
+<p id="username-desc" hidden>
+  Your username is the name that you use to log in to this service.
+</p>
Describing Tables and Figures with Captions
-

In HTML, a table element can get its accessible description from the child caption element, if that wasn't used as the accessible name.

-

For example, a preceding heading might be appropriate as an accessible name for a table, and the caption element can contain a longer description of the table and how the table is laid out. In such a situation, the aria-labelledby attribute can be used on the table to set the accessible name to that of the heading.

+

+ In HTML, if the table is named using aria-label or aria-labelledby, a child caption element becomes an accessible description. + For example, a preceding heading might serve as an appropriate accessible name, and the caption element might contain a longer description. + In such a situation, aria-labelledby could be used on the table to set the accessible name to the heading content and the caption would become the accessible description. +

<h2 id="events-heading">Upcoming events</h2>
 <table aria-labelledby="events-heading">
  <caption>
   Calendar of upcoming events, weeks 27 through 31, with each week starting with
   Monday. The first column is the week number.
  </caption>
- <tr><th>Week<th>Monday<th>Tuesday<th>Wednesday<th>Thursday<th>Friday<Saturday<Sunday
+ <tr><th>Week<th>Monday<th>Tuesday<th>Wednesday<th>Thursday<th>Friday<th>Saturday<th>Sunday
  <tr><td>27<td><td><td><td><td><td><td>
  <tr><td>28<td><td><td><td><td><td><td><a href="/events/9856">Crown Princess's birthday</a>
  <tr><td>29<td><td><td><td><td><td><td>
@@ -5149,8 +5114,8 @@ 
Describing Tables and Figures with Captions
Descriptions Derived from Titles
-

If the aria-details attribute is not used, and an accessible description was not provided using the aria-describedby attribute or one of the primary host-language-specific attributes or elements (e.g., the caption element for table), then, for HTML, if the element has a title attribute, that is used as the accessible description.

-

A visible description together with aria-describedby or aria-details is generally recommended. If a description that is not visible is desired, then the title attribute can be used, for any HTML element that can have an accessible description.

+

If an accessible description was not provided using the aria-describedby attribute or one of the primary host-language-specific attributes or elements (e.g., the caption element for table), then, for HTML, if the element has a title attribute, that is used as the accessible description.

+

A visible description together with aria-describedby is generally recommended. If a description that is not visible is desired, then the title attribute can be used, for any HTML element that can have an accessible description.

Note that the title attribute might not be accessible to some users, in particular sighted users not using a screen reader and not using a pointing device that supports hover (e.g., a mouse).

For example, an input element with input constrained using the pattern attribute can use the title attribute to describe what the expected input is.

<label> Part number:
@@ -5163,7 +5128,7 @@ 
Descriptions Derived from Titles
title="Follow W3C on Twitter"> <img src="/2008/site/images/Twitter_bird_logo_2012.svg" alt="Twitter" class="social-icon" height="40" /> - </a>
+</a>
@@ -5171,14 +5136,13 @@
Descriptions Derived from Titles

Accessible description calculation

Like the accessible name calculation, the accessible description calculation produces a text string. - However, there is one exception: when an element is described using the aria-details attribute the assistive technology is provided with a reference instead of constructing a flattened string.

The accessible description calculation algorithm is the same as the accessible name calculation algorithm except for a few branch points that depend on whether a name or description is being calculated. In particular, when accumulating text for an accessible description, the algorithm uses aria-describedby instead of aria-labelledby.

- When aria-details is not used, user agents construct an accessible description string for an element by walking through a list of potential description methods and using the first that generates a description. + User agents construct an accessible description string for an element by walking through a list of potential description methods and using the first that generates a description. The algorithm they follow is defined in the accessible name specification. It is roughly like the following: