|
6 | 6 | } from 'aria-query';
|
7 | 7 | import { AXObjects, AXObjectRoles, elementAXObjects } from 'axobject-query';
|
8 | 8 | import Attribute from '../nodes/Attribute';
|
| 9 | +import { regex_whitespaces } from '../../utils/patterns'; |
9 | 10 |
|
10 | 11 | const aria_roles = roles_map.keys();
|
11 | 12 | const abstract_roles = new Set(aria_roles.filter(role => roles_map.get(role).abstract));
|
@@ -223,3 +224,194 @@ export function is_semantic_role_element(role: ARIARoleDefinitionKey, tag_name:
|
223 | 224 | }
|
224 | 225 | return false;
|
225 | 226 | }
|
| 227 | + |
| 228 | +// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofilling-form-controls:-the-autocomplete-attribute |
| 229 | +const address_type_tokens = new Set(['shipping', 'billing']); |
| 230 | +const autofill_field_name_tokens = new Set([ |
| 231 | + 'name', |
| 232 | + 'honorific-prefix', |
| 233 | + 'given-name', |
| 234 | + 'additional-name', |
| 235 | + 'family-name', |
| 236 | + 'honorific-suffix', |
| 237 | + 'nickname', |
| 238 | + 'username', |
| 239 | + 'new-password', |
| 240 | + 'current-password', |
| 241 | + 'one-time-code', |
| 242 | + 'organization-title', |
| 243 | + 'organization', |
| 244 | + 'street-address', |
| 245 | + 'address-line1', |
| 246 | + 'address-line2', |
| 247 | + 'address-line3', |
| 248 | + 'address-level4', |
| 249 | + 'address-level3', |
| 250 | + 'address-level2', |
| 251 | + 'address-level1', |
| 252 | + 'country', |
| 253 | + 'country-name', |
| 254 | + 'postal-code', |
| 255 | + 'cc-name', |
| 256 | + 'cc-given-name', |
| 257 | + 'cc-additional-name', |
| 258 | + 'cc-family-name', |
| 259 | + 'cc-number', |
| 260 | + 'cc-exp', |
| 261 | + 'cc-exp-month', |
| 262 | + 'cc-exp-year', |
| 263 | + 'cc-csc', |
| 264 | + 'cc-type', |
| 265 | + 'transaction-currency', |
| 266 | + 'transaction-amount', |
| 267 | + 'language', |
| 268 | + 'bday', |
| 269 | + 'bday-day', |
| 270 | + 'bday-month', |
| 271 | + 'bday-year', |
| 272 | + 'sex', |
| 273 | + 'url', |
| 274 | + 'photo' |
| 275 | +]); |
| 276 | +const contact_type_tokens = new Set(['home', 'work', 'mobile', 'fax', 'pager']); |
| 277 | +const autofill_contact_field_name_tokens = new Set([ |
| 278 | + 'tel', |
| 279 | + 'tel-country-code', |
| 280 | + 'tel-national', |
| 281 | + 'tel-area-code', |
| 282 | + 'tel-local', |
| 283 | + 'tel-local-prefix', |
| 284 | + 'tel-local-suffix', |
| 285 | + 'tel-extension', |
| 286 | + 'email', |
| 287 | + 'impp' |
| 288 | +]); |
| 289 | + |
| 290 | +const control_group_text_types = new Set(['hidden', 'text', 'search']); |
| 291 | +const control_group_multiline_types = new Set(['hidden']); |
| 292 | +const control_group_password_types = new Set(['hidden', 'text', 'search', 'password']); |
| 293 | +const control_group_url_types = new Set(['hidden', 'text', 'search', 'url']); |
| 294 | +const control_group_username_types = new Set(['hidden', 'text', 'search', 'email']); |
| 295 | +const control_group_telephone_types = new Set(['hidden', 'text', 'search', 'tel']); |
| 296 | +const control_group_numeric_types = new Set(['hidden', 'text', 'search', 'number']); |
| 297 | +const control_group_month_types = new Set(['hidden', 'text', 'search', 'month']); |
| 298 | +const control_group_date_types = new Set(['hidden', 'text', 'search', 'date']); |
| 299 | + |
| 300 | +const appropriate_types_for_field_names = new Map([ |
| 301 | + ['name', control_group_text_types], |
| 302 | + ['honorific-prefix', control_group_text_types], |
| 303 | + ['given-name', control_group_text_types], |
| 304 | + ['additional-name', control_group_text_types], |
| 305 | + ['family-name', control_group_text_types], |
| 306 | + ['honorific-suffix', control_group_text_types], |
| 307 | + ['nickname', control_group_text_types], |
| 308 | + ['organization-title', control_group_text_types], |
| 309 | + ['username', control_group_username_types], |
| 310 | + ['new-password', control_group_password_types], |
| 311 | + ['current-password', control_group_password_types], |
| 312 | + ['one-time-code', control_group_password_types], |
| 313 | + ['organization', control_group_text_types], |
| 314 | + ['street-address', control_group_multiline_types], |
| 315 | + ['address-line1', control_group_text_types], |
| 316 | + ['address-line2', control_group_text_types], |
| 317 | + ['address-line3', control_group_text_types], |
| 318 | + ['address-level4', control_group_text_types], |
| 319 | + ['address-level3', control_group_text_types], |
| 320 | + ['address-level2', control_group_text_types], |
| 321 | + ['address-level1', control_group_text_types], |
| 322 | + ['country', control_group_text_types], |
| 323 | + ['country-name', control_group_text_types], |
| 324 | + ['postal-code', control_group_text_types], |
| 325 | + ['cc-name', control_group_text_types], |
| 326 | + ['cc-given-name', control_group_text_types], |
| 327 | + ['cc-additional-name', control_group_text_types], |
| 328 | + ['cc-family-name', control_group_text_types], |
| 329 | + ['cc-number', control_group_text_types], |
| 330 | + ['cc-exp', control_group_month_types], |
| 331 | + ['cc-exp-month', control_group_numeric_types], |
| 332 | + ['cc-exp-year', control_group_numeric_types], |
| 333 | + ['cc-csc', control_group_text_types], |
| 334 | + ['cc-type', control_group_text_types], |
| 335 | + ['transaction-currency', control_group_text_types], |
| 336 | + ['transaction-amount', control_group_numeric_types], |
| 337 | + ['language', control_group_text_types], |
| 338 | + ['bday', control_group_date_types], |
| 339 | + ['bday-day', control_group_numeric_types], |
| 340 | + ['bday-month', control_group_numeric_types], |
| 341 | + ['bday-year', control_group_numeric_types], |
| 342 | + ['sex', control_group_text_types], |
| 343 | + ['url', control_group_url_types], |
| 344 | + ['photo', control_group_url_types], |
| 345 | + ['tel', control_group_telephone_types], |
| 346 | + ['tel-country-code', control_group_text_types], |
| 347 | + ['tel-national', control_group_text_types], |
| 348 | + ['tel-area-code', control_group_text_types], |
| 349 | + ['tel-local', control_group_text_types], |
| 350 | + ['tel-local-prefix', control_group_text_types], |
| 351 | + ['tel-local-suffix', control_group_text_types], |
| 352 | + ['tel-extension', control_group_text_types], |
| 353 | + ['email', control_group_username_types], |
| 354 | + ['impp', control_group_url_types] |
| 355 | +]); |
| 356 | + |
| 357 | +function is_appropriate_type_for_field_name(type: string, field_name: string) { |
| 358 | + if (autofill_field_name_tokens.has(field_name)) { |
| 359 | + return appropriate_types_for_field_names.get(field_name)?.has(type); |
| 360 | + } |
| 361 | + |
| 362 | + return false; |
| 363 | +} |
| 364 | + |
| 365 | +function is_appropriate_type_for_contact_field_name(type: string, field_name: string) { |
| 366 | + if (autofill_contact_field_name_tokens.has(field_name)) { |
| 367 | + return appropriate_types_for_field_names.get(field_name)?.has(type); |
| 368 | + } |
| 369 | + |
| 370 | + return false; |
| 371 | +} |
| 372 | + |
| 373 | +export function is_valid_autocomplete(type: null | true | string, autocomplete: null | true | string) { |
| 374 | + if (typeof autocomplete !== 'string' || typeof type !== 'string') { |
| 375 | + return false; |
| 376 | + } |
| 377 | + |
| 378 | + const tokens = autocomplete.trim().toLowerCase().split(regex_whitespaces); |
| 379 | + const normalized_type = type.toLowerCase(); |
| 380 | + |
| 381 | + const input_wears_autofill_anchor_mantle = normalized_type === 'hidden'; |
| 382 | + const input_wears_autofill_expectation_mantle = !input_wears_autofill_anchor_mantle; |
| 383 | + |
| 384 | + if (input_wears_autofill_expectation_mantle) { |
| 385 | + if (tokens[0] === 'on' || tokens[0] === 'off') { |
| 386 | + return tokens.length === 1; |
| 387 | + } |
| 388 | + } |
| 389 | + |
| 390 | + if (typeof tokens[0] === 'string' && tokens[0].startsWith('section-')) { |
| 391 | + tokens.shift(); |
| 392 | + } |
| 393 | + |
| 394 | + if (address_type_tokens.has(tokens[0])) { |
| 395 | + tokens.shift(); |
| 396 | + } |
| 397 | + |
| 398 | + if (is_appropriate_type_for_field_name(normalized_type, tokens[0])) { |
| 399 | + tokens.shift(); |
| 400 | + } else { |
| 401 | + if (contact_type_tokens.has(tokens[0])) { |
| 402 | + tokens.shift(); |
| 403 | + } |
| 404 | + |
| 405 | + if (is_appropriate_type_for_contact_field_name(normalized_type, tokens[0])) { |
| 406 | + tokens.shift(); |
| 407 | + } else { |
| 408 | + return false; |
| 409 | + } |
| 410 | + } |
| 411 | + |
| 412 | + if (tokens[0] === 'webauthn') { |
| 413 | + tokens.shift(); |
| 414 | + } |
| 415 | + |
| 416 | + return tokens.length === 0; |
| 417 | +} |
0 commit comments