-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from NielsdeBlaauw/aria-roles
Aria roles
- Loading branch information
Showing
8 changed files
with
153 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
namespace NdB\TwigCSA11Y\Rules; | ||
|
||
use Allocine\Twigcs\Rule\AbstractRule; | ||
use Allocine\Twigcs\Rule\RuleInterface; | ||
use Twig\Token as TwigToken; | ||
use Twig\TokenStream; | ||
|
||
class AriaRoles extends AbstractRule implements RuleInterface | ||
{ | ||
const VALID_ROLES = [ | ||
"article", "banner", "complementary", "main", "navigation", "region", | ||
"search", "contentinfo", "alert", "alertdialog", "application", | ||
"dialog", "group", "log", "marquee", "menu", "menubar", "menuitem", | ||
"menuitemcheckbox", "menuitemradio", "progressbar", "separator", | ||
"slider", "spinbutton", "status", "tab", "tablist", "tabpanel", | ||
"timer", "toolbar", "tooltip", "tree", "treegrid", "treeitem", | ||
"button", "button", "checkbox", "columnheader", "combobox", | ||
"contentinfo", "form", "grid", "gridcell", "heading", "img", "link", | ||
"listbox", "listitem", "option", "radio", "radiogroup", "row", | ||
"rowgroup", "rowheader", "scrollbar", "textbox", "document", | ||
"application", "presentation", "math", "definition", "note", "directory" | ||
]; | ||
|
||
const ABSTRACT_ROLES = [ | ||
"command", "composite", "input", "landmark", "range", "section", | ||
"sectionhead", "select", "structure", "widget" | ||
]; | ||
|
||
/** | ||
* @var \Allocine\Twigcs\Validator\Violation[] | ||
*/ | ||
protected $violations = []; | ||
|
||
/** | ||
* @param int $severity | ||
*/ | ||
public function __construct($severity) | ||
{ | ||
parent::__construct($severity); | ||
$this->violations = []; | ||
} | ||
|
||
public function check(TokenStream $tokens) | ||
{ | ||
while (!$tokens->isEOF()) { | ||
$token = $tokens->getCurrent(); | ||
|
||
if ($token->getType() === TwigToken::TEXT_TYPE) { | ||
$matches = []; | ||
$textToAnalyse = (string) $token->getValue(); | ||
$terminated = false; | ||
$tokenIndex = 1; | ||
while (!$terminated) { | ||
$nextToken = $tokens->look($tokenIndex); | ||
if ($nextToken->getType() !== TwigToken::ARROW_TYPE) { | ||
$textToAnalyse .= (string) $nextToken->getValue(); | ||
} | ||
if ($nextToken->getType() === TwigToken::TEXT_TYPE | ||
|| $nextToken->getType() === TwigToken::EOF_TYPE | ||
) { | ||
$terminated = true; | ||
} | ||
$tokenIndex++; | ||
} | ||
if (preg_match( | ||
"/role=((.)+)([>'\" ]+)/U", | ||
$textToAnalyse, | ||
$matches | ||
) | ||
) { | ||
$value = preg_replace('/[^\da-z]/i', '', $matches[1]); | ||
if (! in_array($value, self::VALID_ROLES, true)) { | ||
if (in_array($value, self::ABSTRACT_ROLES, true)) { | ||
/** | ||
* @psalm-suppress InternalMethod | ||
* @psalm-suppress UndefinedPropertyFetch | ||
*/ | ||
$this->addViolation( | ||
(string) $tokens->getSourceContext()->getPath(), | ||
$token->getLine(), | ||
$token->columnno, | ||
sprintf( | ||
'[A11Y.AriaRoles] Invalid abstract \'role\' value. Found `%1$s`.', | ||
trim($matches[0]) | ||
) | ||
); | ||
} else { | ||
/** | ||
* @psalm-suppress InternalMethod | ||
* @psalm-suppress UndefinedPropertyFetch | ||
*/ | ||
$this->addViolation( | ||
(string) $tokens->getSourceContext()->getPath(), | ||
$token->getLine(), | ||
$token->columnno, | ||
sprintf( | ||
'[A11Y.AriaRoles] Invalid \'role\'. Role must have a valid value. Found `%1$s`.', | ||
trim($matches[0]) | ||
) | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
$tokens->next(); | ||
} | ||
return $this->violations; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<?xml version="1.0"?> | ||
<checkstyle version="1.0.0"><file name="tests/test.twig"><error column="0" line="2" severity="error" message="[A11Y.TabIndex] Invalid 'tabindex'. Tabindex must be 0 or -1. Found `tabindex=1>.`" source="NdB\TwigCSA11Y\Rules\TabIndex"/><error column="20" line="12" severity="error" message="[A11Y.TabIndex] Invalid 'tabindex'. Tabindex must be 0 or -1. Found `tabindex=test.`" source="NdB\TwigCSA11Y\Rules\TabIndex"/><error column="53" line="12" severity="error" message="[A11Y.TabIndex] Invalid 'tabindex'. Tabindex must be 0 or -1. Found `tabindex='test'.`" source="NdB\TwigCSA11Y\Rules\TabIndex"/><error column="0" line="16" severity="error" message="[A11Y.BannedHTMLTags] Tag 'marquee' is dissallowed. Found `<marquee>`." source="NdB\TwigCSA11Y\Rules\BannedHTMLTags"/><error column="0" line="18" severity="error" message="[A11Y.BannedHTMLTags] Tag 'blink' is dissallowed. Found `<blink>`." source="NdB\TwigCSA11Y\Rules\BannedHTMLTags"/></file></checkstyle> | ||
<checkstyle version="1.0.0"><file name="tests/test.twig"><error column="0" line="2" severity="error" message="[A11Y.TabIndex] Invalid 'tabindex'. Tabindex must be 0 or -1. Found `tabindex=1>`." source="NdB\TwigCSA11Y\Rules\TabIndex"/><error column="0" line="3" severity="error" message="[A11Y.TabIndex] Invalid 'tabindex'. Tabindex must be 0 or -1. Found `tabindex="1"`." source="NdB\TwigCSA11Y\Rules\TabIndex"/><error column="0" line="4" severity="error" message="[A11Y.TabIndex] Invalid 'tabindex'. Tabindex must be 0 or -1. Found `tabindex='1'`." source="NdB\TwigCSA11Y\Rules\TabIndex"/><error column="22" line="10" severity="error" message="[A11Y.TabIndex] Invalid 'tabindex'. Tabindex must be 0 or -1. Found `tabindex=test`." source="NdB\TwigCSA11Y\Rules\TabIndex"/><error column="0" line="11" severity="error" message="[A11Y.TabIndex] Invalid 'tabindex'. Tabindex must be 0 or -1. Found `tabindex='test'`." source="NdB\TwigCSA11Y\Rules\TabIndex"/><error column="0" line="12" severity="error" message="[A11Y.TabIndex] Invalid 'tabindex'. Tabindex must be 0 or -1. Found `tabindex='test'`." source="NdB\TwigCSA11Y\Rules\TabIndex"/><error column="0" line="15" severity="error" message="[A11Y.BannedHTMLTags] Invalid tag 'marquee'. Found `<marquee>`." source="NdB\TwigCSA11Y\Rules\BannedHTMLTags"/><error column="0" line="16" severity="error" message="[A11Y.BannedHTMLTags] Invalid tag 'blink'. Found `<blink>`." source="NdB\TwigCSA11Y\Rules\BannedHTMLTags"/><error column="0" line="20" severity="error" message="[A11Y.AriaRoles] Invalid 'role'. Role must have a valid value. Found `role="invalid_role"`." source="NdB\TwigCSA11Y\Rules\AriaRoles"/><error column="0" line="21" severity="error" message="[A11Y.AriaRoles] Invalid 'role'. Role must have a valid value. Found `role='invalid_role'`." source="NdB\TwigCSA11Y\Rules\AriaRoles"/><error column="0" line="22" severity="error" message="[A11Y.AriaRoles] Invalid 'role'. Role must have a valid value. Found `role=invalid>`." source="NdB\TwigCSA11Y\Rules\AriaRoles"/><error column="0" line="23" severity="error" message="[A11Y.AriaRoles] Invalid abstract 'role' value. Found `role=command>`." source="NdB\TwigCSA11Y\Rules\AriaRoles"/><error column="0" line="24" severity="error" message="[A11Y.AriaRoles] Invalid 'role'. Role must have a valid value. Found `role=invalid>`." source="NdB\TwigCSA11Y\Rules\AriaRoles"/><error column="26" line="24" severity="error" message="[A11Y.AriaRoles] Invalid 'role'. Role must have a valid value. Found `role=invalid>`." source="NdB\TwigCSA11Y\Rules\AriaRoles"/><error column="0" line="25" severity="error" message="[A11Y.AriaRoles] Invalid 'role'. Role must have a valid value. Found `role='variable'`." source="NdB\TwigCSA11Y\Rules\AriaRoles"/></file></checkstyle> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,25 @@ | ||
{# TabIndex #} | ||
<div tabindex=1></div> | ||
<div tabindex="1"></div> | ||
<div tabindex='1'></div> | ||
<div tabindex="0"></div> | ||
<div tabindex='0'></div> | ||
<div tabindex='-1'></div> | ||
<div tabindex=-1></div> | ||
{{test}} | ||
<div tabindex='-1'></div> | ||
{{test}} | ||
<div class="{{test}}" tabindex={{test}} id="{{blala}}"></div> | ||
<div tabindex='{{test}}'></div> | ||
<div tabindex=1></div> {# BAD #} | ||
<div tabindex="1"></div> {# BAD #} | ||
<div tabindex='1'></div> {# BAD #} | ||
<div tabindex="0"></div> {# GOOD #} | ||
<div tabindex='0'></div> {# GOOD #} | ||
<div tabindex='-1'></div> {# GOOD #} | ||
<div tabindex=-1></div> {# GOOD #} | ||
<div tabindex='-1'></div> {# GOOD #} | ||
<div class="{{ test }}" tabindex={{ test }} id="{{ blala }}"></div> {# BAD #} | ||
<div tabindex='{{ test }}'></div> {# BAD #} | ||
<div tabindex='{{test}}'></div> {# BAD #} | ||
|
||
{# BannedHTMLTags #} | ||
<marquee>This is a marquee</marquee> | ||
{# - #} | ||
<p><blink>This blinks</blink></p> | ||
<marquee>This is a marquee</marquee> {# BAD #} | ||
<p><blink>This blinks</blink></p> {# BAD #} | ||
|
||
{# AriaRoles #} | ||
<div role="article"></div> {# GOOD #} | ||
<div role="invalid_role"></div> {# BAD #} | ||
<div role='invalid_role'></div> {# BAD #} | ||
<div role=invalid></div> {# BAD #} | ||
<div role=command></div> {# BAD - abstract role #} | ||
<div class='{{ variable }}' role=invalid></div> {# BAD #} | ||
<div role='{{ variable }}' /> {# BAD #} |