Skip to content

Add sanitizer to ensure 'role' and 'tabindex' attributes are present for elements with 'on' attribute #4528

@westonruter

Description

@westonruter

Feature description

Something that comes up over and over again in the support forums is people adding on handler attributes for tap events but then not also adding role=button and tabindex=0 (actually, the value doesn't matter, just role and tabindex). Examples:

In #4447 I proposed to add validator support to remove the elements that lack these attributes. However, this would be somewhat jarring, especially in Reader mode where we don't yet have validation error reporting (#4480).

Therefore, I suggest a better behavior would be to introduce a new AMP_Accessibility_Sanitizer that looks for //*[ @on and contains( @on, 'tap:' ) and ( not( @tabindex ) or not( @role ) )] and then automatically supply the role=button if no role attribute is present, or tabindex=0 if no tabindex is present. Note that this should only be done for elements that are not intrinsically focusable, for example button and a would not be included. The specific elements that don't require role and tabindex attributes are indicated in the spec like for the button element:

  attrs: {
    name: "role"
    implicit: true
  }
  attrs: {
    name: "tabindex"
    implicit: true
  }

See the on attribute spec:

  attrs: {
    name: "on"
    trigger: {
      if_value_regex: "tap:.*"
      also_requires_attr: "role"
      also_requires_attr: "tabindex"
    }
  }

Ideally these would be extracted from the spec to add to AMP_Allowed_Tags_Generated so that we don't have to hard-code them. This would be an ultimate prerequisite for adding support for the trigger construct in #4447, but it can/should be done up first for a minor release.


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

Implementation brief

QA testing instructions

Demo

Changelog entry

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions