-
Notifications
You must be signed in to change notification settings - Fork 783
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(commons): Add text.isValidAutocomplete method
- Loading branch information
1 parent
e6189ce
commit 8d44fe4
Showing
4 changed files
with
244 additions
and
175 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,2 @@ | ||
let { | ||
standaloneTerms = [], | ||
qualifiedTerms = [], | ||
qualifiers = [], | ||
locations = [], | ||
looseTyped = false | ||
} = | ||
options || {}; | ||
|
||
qualifiers = qualifiers.concat(['home', 'work', 'mobile', 'fax', 'pager']); | ||
locations = locations.concat(['billing', 'shipping']); | ||
standaloneTerms = standaloneTerms.concat([ | ||
'name', | ||
'honorific-prefix', | ||
'given-name', | ||
'additional-name', | ||
'family-name', | ||
'honorific-suffix', | ||
'nickname', | ||
'username', | ||
'new-password', | ||
'current-password', | ||
'organization-title', | ||
'organization', | ||
'street-address', | ||
'address-line1', | ||
'address-line2', | ||
'address-line3', | ||
'address-level4', | ||
'address-level3', | ||
'address-level2', | ||
'address-level1', | ||
'country', | ||
'country-name', | ||
'postal-code', | ||
'cc-name', | ||
'cc-given-name', | ||
'cc-additional-name', | ||
'cc-family-name', | ||
'cc-number', | ||
'cc-exp', | ||
'cc-exp-month', | ||
'cc-exp-year', | ||
'cc-csc', | ||
'cc-type', | ||
'transaction-currency', | ||
'transaction-amount', | ||
'language', | ||
'bday', | ||
'bday-day', | ||
'bday-month', | ||
'bday-year', | ||
'sex', | ||
'url', | ||
'photo' | ||
]); | ||
|
||
qualifiedTerms = qualifiedTerms.concat([ | ||
'tel', | ||
'tel-country-code', | ||
'tel-national', | ||
'tel-area-code', | ||
'tel-local', | ||
'tel-local-prefix', | ||
'tel-local-suffix', | ||
'tel-extension', | ||
'email', | ||
'impp' | ||
]); | ||
|
||
const autocomplete = node.getAttribute('autocomplete'); | ||
const autocompleteTerms = autocomplete.split(/\s+/g).map(term => { | ||
return term.toLowerCase(); | ||
}); | ||
|
||
if (!looseTyped) { | ||
if ( | ||
autocompleteTerms[0].length > 8 && | ||
autocompleteTerms[0].substr(0, 8) === 'section-' | ||
) { | ||
autocompleteTerms.shift(); | ||
} | ||
|
||
if (locations.includes(autocompleteTerms[0])) { | ||
autocompleteTerms.shift(); | ||
} | ||
|
||
if (qualifiers.includes(autocompleteTerms[0])) { | ||
autocompleteTerms.shift(); | ||
// only quantifiers allowed at this point | ||
standaloneTerms = []; | ||
} | ||
|
||
if (autocompleteTerms.length !== 1) { | ||
return false; | ||
} | ||
} | ||
|
||
const purposeTerm = autocompleteTerms[autocompleteTerms.length - 1]; | ||
return ( | ||
standaloneTerms.includes(purposeTerm) || qualifiedTerms.includes(purposeTerm) | ||
); | ||
const autocomplete = node.getAttribute('autocomplete') || ''; | ||
return axe.commons.text.isValidAutocomplete(autocomplete, options); |
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,118 @@ | ||
/* global text */ | ||
const autocomplete = { | ||
stateTerms: ['on', 'off'], | ||
standaloneTerms: [ | ||
'name', | ||
'honorific-prefix', | ||
'given-name', | ||
'additional-name', | ||
'family-name', | ||
'honorific-suffix', | ||
'nickname', | ||
'username', | ||
'new-password', | ||
'current-password', | ||
'organization-title', | ||
'organization', | ||
'street-address', | ||
'address-line1', | ||
'address-line2', | ||
'address-line3', | ||
'address-level4', | ||
'address-level3', | ||
'address-level2', | ||
'address-level1', | ||
'country', | ||
'country-name', | ||
'postal-code', | ||
'cc-name', | ||
'cc-given-name', | ||
'cc-additional-name', | ||
'cc-family-name', | ||
'cc-number', | ||
'cc-exp', | ||
'cc-exp-month', | ||
'cc-exp-year', | ||
'cc-csc', | ||
'cc-type', | ||
'transaction-currency', | ||
'transaction-amount', | ||
'language', | ||
'bday', | ||
'bday-day', | ||
'bday-month', | ||
'bday-year', | ||
'sex', | ||
'url', | ||
'photo' | ||
], | ||
qualifiers: ['home', 'work', 'mobile', 'fax', 'pager'], | ||
qualifiedTerms: [ | ||
'tel', | ||
'tel-country-code', | ||
'tel-national', | ||
'tel-area-code', | ||
'tel-local', | ||
'tel-local-prefix', | ||
'tel-local-suffix', | ||
'tel-extension', | ||
'email', | ||
'impp' | ||
], | ||
locations: ['billing', 'shipping'] | ||
}; | ||
text.autocomplete = autocomplete; | ||
|
||
text.isValidAutocomplete = function isValidAutocomplete( | ||
autocomplete, | ||
{ | ||
looseTyped = false, | ||
stateTerms = [], | ||
locations = [], | ||
qualifiers = [], | ||
standaloneTerms = [], | ||
qualifiedTerms = [] | ||
} = {} | ||
) { | ||
/*eslint max-statements: ["error", 21] */ | ||
autocomplete = autocomplete.toLowerCase(); | ||
stateTerms = stateTerms.concat(text.autocomplete.stateTerms); | ||
if (stateTerms.includes(autocomplete)) { | ||
return true; | ||
} | ||
|
||
qualifiers = qualifiers.concat(text.autocomplete.qualifiers); | ||
locations = locations.concat(text.autocomplete.locations); | ||
standaloneTerms = standaloneTerms.concat(text.autocomplete.standaloneTerms); | ||
qualifiedTerms = qualifiedTerms.concat(text.autocomplete.qualifiedTerms); | ||
|
||
const autocompleteTerms = autocomplete.split(/\s+/g); | ||
if (!looseTyped) { | ||
if ( | ||
autocompleteTerms[0].length > 8 && | ||
autocompleteTerms[0].substr(0, 8) === 'section-' | ||
) { | ||
autocompleteTerms.shift(); | ||
} | ||
|
||
if (locations.includes(autocompleteTerms[0])) { | ||
autocompleteTerms.shift(); | ||
} | ||
|
||
if (qualifiers.includes(autocompleteTerms[0])) { | ||
autocompleteTerms.shift(); | ||
// only quantifiers allowed at this point | ||
standaloneTerms = []; | ||
} | ||
|
||
if (autocompleteTerms.length !== 1) { | ||
return false; | ||
} | ||
} | ||
|
||
const purposeTerm = autocompleteTerms[autocompleteTerms.length - 1]; | ||
return ( | ||
standaloneTerms.includes(purposeTerm) || | ||
qualifiedTerms.includes(purposeTerm) | ||
); | ||
}; |
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.