-
Notifications
You must be signed in to change notification settings - Fork 45.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored selector API to support multiple types
Selectors can now be: React component, accessibility role, display text, or data-testname.
- Loading branch information
Brian Vaughn
committed
Apr 14, 2020
1 parent
774c7ec
commit 41d3047
Showing
9 changed files
with
746 additions
and
355 deletions.
There are no files selected for viewing
479 changes: 292 additions & 187 deletions
479
packages/react-dom/src/__tests__/ReactDOMTestSelectors-test.internal.js
Large diffs are not rendered by default.
Oops, something went wrong.
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,140 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
// Below code forked from dom-accessibility-api | ||
|
||
const tagToRoleMappings = { | ||
ARTICLE: 'article', | ||
ASIDE: 'complementary', | ||
BODY: 'document', | ||
BUTTON: 'button', | ||
DATALIST: 'listbox', | ||
DD: 'definition', | ||
DETAILS: 'group', | ||
DIALOG: 'dialog', | ||
DT: 'term', | ||
FIELDSET: 'group', | ||
FIGURE: 'figure', | ||
// WARNING: Only with an accessible name | ||
FORM: 'form', | ||
FOOTER: 'contentinfo', | ||
H1: 'heading', | ||
H2: 'heading', | ||
H3: 'heading', | ||
H4: 'heading', | ||
H5: 'heading', | ||
H6: 'heading', | ||
HEADER: 'banner', | ||
HR: 'separator', | ||
LEGEND: 'legend', | ||
LI: 'listitem', | ||
MATH: 'math', | ||
MAIN: 'main', | ||
MENU: 'list', | ||
NAV: 'navigation', | ||
OL: 'list', | ||
OPTGROUP: 'group', | ||
// WARNING: Only in certain context | ||
OPTION: 'option', | ||
OUTPUT: 'status', | ||
PROGRESS: 'progressbar', | ||
// WARNING: Only with an accessible name | ||
SECTION: 'region', | ||
SUMMARY: 'button', | ||
TABLE: 'table', | ||
TBODY: 'rowgroup', | ||
TEXTAREA: 'textbox', | ||
TFOOT: 'rowgroup', | ||
// WARNING: Only in certain context | ||
TD: 'cell', | ||
TH: 'columnheader', | ||
THEAD: 'rowgroup', | ||
TR: 'row', | ||
UL: 'list', | ||
}; | ||
|
||
function getImplicitRole(element: Element): string | null { | ||
const mappedByTag = tagToRoleMappings[element.tagName]; | ||
if (mappedByTag !== undefined) { | ||
return mappedByTag; | ||
} | ||
|
||
switch (element.tagName) { | ||
case 'A': | ||
case 'AREA': | ||
case 'LINK': | ||
if (element.hasAttribute('href')) { | ||
return 'link'; | ||
} | ||
break; | ||
case 'IMG': | ||
if ((element.getAttribute('alt') || '').length > 0) { | ||
return 'img'; | ||
} | ||
break; | ||
case 'INPUT': { | ||
const type = (element: any).type; | ||
switch (type) { | ||
case 'button': | ||
case 'image': | ||
case 'reset': | ||
case 'submit': | ||
return 'button'; | ||
case 'checkbox': | ||
case 'radio': | ||
return type; | ||
case 'range': | ||
return 'slider'; | ||
case 'email': | ||
case 'tel': | ||
case 'text': | ||
case 'url': | ||
if (element.hasAttribute('list')) { | ||
return 'combobox'; | ||
} | ||
return 'textbox'; | ||
case 'search': | ||
if (element.hasAttribute('list')) { | ||
return 'combobox'; | ||
} | ||
return 'searchbox'; | ||
default: | ||
return null; | ||
} | ||
} | ||
|
||
case 'SELECT': | ||
if (element.hasAttribute('multiple') || (element: any).size > 1) { | ||
return 'listbox'; | ||
} | ||
return 'combobox'; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
function getExplicitRole(element: Element): string | null { | ||
const role = element.getAttribute('role'); | ||
if (role) { | ||
return role.trim().split(' ')[0]; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
// https://w3c.github.io/html-aria/#document-conformance-requirements-for-use-of-aria-attributes-in-html | ||
export function getRole(element: Element): string | null { | ||
const explicitRole = getExplicitRole(element); | ||
|
||
if (explicitRole !== null) { | ||
return explicitRole; | ||
} | ||
|
||
return getImplicitRole(element); | ||
} |
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
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
Oops, something went wrong.