|
| 1 | +// Copyright 2025 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import * as i18n from '../../core/i18n/i18n.js'; |
| 6 | +import * as Protocol from '../../generated/protocol.js'; |
| 7 | +import * as IssuesManager from '../../models/issues_manager/issues_manager.js'; |
| 8 | + |
| 9 | +const UIStrings = { |
| 10 | + /** |
| 11 | + * @description Tooltip text shown in the Elements panel when an element has an error. |
| 12 | + */ |
| 13 | + formLabelForNameError: 'Incorrect use of <label for=FORM_ELEMENT>', |
| 14 | + /** |
| 15 | + * @description Tooltip text shown in the Elements panel when an element has an error. |
| 16 | + */ |
| 17 | + formDuplicateIdForInputError: 'Duplicate form field id in the same form', |
| 18 | + /** |
| 19 | + * @description Tooltip text shown in the Elements panel when an element has an error. |
| 20 | + */ |
| 21 | + formInputWithNoLabelError: 'Form field without valid aria-labelledby attribute or associated label', |
| 22 | + /** |
| 23 | + * @description Tooltip text shown in the Elements panel when an element has an error. |
| 24 | + */ |
| 25 | + formAutocompleteAttributeEmptyError: 'Incorrect use of autocomplete attribute', |
| 26 | + /** |
| 27 | + * @description Tooltip text shown in the Elements panel when an element has an error. |
| 28 | + */ |
| 29 | + formEmptyIdAndNameAttributesForInputError: 'A form field element should have an id or name attribute', |
| 30 | + /** |
| 31 | + * @description Tooltip text shown in the Elements panel when an element has an error. |
| 32 | + */ |
| 33 | + formAriaLabelledByToNonExistingId: 'An aria-labelledby attribute doesn\'t match any element id', |
| 34 | + /** |
| 35 | + * @description Tooltip text shown in the Elements panel when an element has an error. |
| 36 | + */ |
| 37 | + formInputAssignedAutocompleteValueToIdOrNameAttributeError: 'An element doesn\'t have an autocomplete attribute', |
| 38 | + /** |
| 39 | + * @description Tooltip text shown in the Elements panel when an element has an error. |
| 40 | + */ |
| 41 | + formLabelHasNeitherForNorNestedInput: 'No label associated with a form field', |
| 42 | + /** |
| 43 | + * @description Tooltip text shown in the Elements panel when an element has an error. |
| 44 | + */ |
| 45 | + formLabelForMatchesNonExistingIdError: 'Incorrect use of <label for=FORM_ELEMENT>', |
| 46 | + /** |
| 47 | + * @description Tooltip text shown in the Elements panel when an element has an error. |
| 48 | + */ |
| 49 | + formInputHasWrongButWellIntendedAutocompleteValueError: 'Non-standard autocomplete attribute value', |
| 50 | + /** |
| 51 | + * @description Tooltip text shown in the Elements panel when an element has an error. |
| 52 | + */ |
| 53 | + disallowedSelectChild: 'Invalid element or text node within <select>', |
| 54 | + /** |
| 55 | + * @description Tooltip text shown in the Elements panel when an element has an error. |
| 56 | + */ |
| 57 | + disallowedOptGroupChild: 'Invalid element or text node within <optgroup>', |
| 58 | + /** |
| 59 | + * @description Tooltip text shown in the Elements panel when an element has an error. |
| 60 | + */ |
| 61 | + nonPhrasingContentOptionChild: 'Non-phrasing content used within an <option> element', |
| 62 | + /** |
| 63 | + * @description Tooltip text shown in the Elements panel when an element has an error. |
| 64 | + */ |
| 65 | + interactiveContentOptionChild: 'Interactive element inside of an <option> element', |
| 66 | + /** |
| 67 | + * @description Tooltip text shown in the Elements panel when an element has an error. |
| 68 | + */ |
| 69 | + interactiveContentLegendChild: 'Interactive element inside of a <legend> element', |
| 70 | +} as const; |
| 71 | + |
| 72 | +const str_ = i18n.i18n.registerUIStrings('panels/elements/ElementIssueUtils.ts', UIStrings); |
| 73 | +const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_); |
| 74 | + |
| 75 | +export interface ElementIssueDetails { |
| 76 | + tooltip: string; |
| 77 | + nodeId?: Protocol.DOM.BackendNodeId; |
| 78 | + attribute?: string; |
| 79 | +} |
| 80 | + |
| 81 | +export function getElementIssueDetails(issue: IssuesManager.Issue.Issue): ElementIssueDetails|undefined { |
| 82 | + if (issue instanceof IssuesManager.GenericIssue.GenericIssue) { |
| 83 | + const issueDetails = issue.details(); |
| 84 | + return { |
| 85 | + tooltip: getTooltipFromGenericIssue(issueDetails.errorType), |
| 86 | + nodeId: issueDetails.violatingNodeId, |
| 87 | + attribute: issueDetails.violatingNodeAttribute, |
| 88 | + }; |
| 89 | + } |
| 90 | + if (issue instanceof IssuesManager.SelectElementAccessibilityIssue.SelectElementAccessibilityIssue) { |
| 91 | + const issueDetails = issue.details(); |
| 92 | + return { |
| 93 | + tooltip: getTooltipFromSelectElementAccessibilityIssue(issueDetails.selectElementAccessibilityIssueReason), |
| 94 | + nodeId: issueDetails.nodeId, |
| 95 | + }; |
| 96 | + } |
| 97 | + |
| 98 | + return undefined; |
| 99 | +} |
| 100 | + |
| 101 | +function getTooltipFromGenericIssue(errorType: Protocol.Audits.GenericIssueErrorType): string { |
| 102 | + switch (errorType) { |
| 103 | + case Protocol.Audits.GenericIssueErrorType.FormLabelForNameError: |
| 104 | + return i18nString(UIStrings.formLabelForNameError); |
| 105 | + case Protocol.Audits.GenericIssueErrorType.FormDuplicateIdForInputError: |
| 106 | + return i18nString(UIStrings.formDuplicateIdForInputError); |
| 107 | + case Protocol.Audits.GenericIssueErrorType.FormInputWithNoLabelError: |
| 108 | + return i18nString(UIStrings.formInputWithNoLabelError); |
| 109 | + case Protocol.Audits.GenericIssueErrorType.FormAutocompleteAttributeEmptyError: |
| 110 | + return i18nString(UIStrings.formAutocompleteAttributeEmptyError); |
| 111 | + case Protocol.Audits.GenericIssueErrorType.FormEmptyIdAndNameAttributesForInputError: |
| 112 | + return i18nString(UIStrings.formEmptyIdAndNameAttributesForInputError); |
| 113 | + case Protocol.Audits.GenericIssueErrorType.FormAriaLabelledByToNonExistingId: |
| 114 | + return i18nString(UIStrings.formAriaLabelledByToNonExistingId); |
| 115 | + case Protocol.Audits.GenericIssueErrorType.FormInputAssignedAutocompleteValueToIdOrNameAttributeError: |
| 116 | + return i18nString(UIStrings.formInputAssignedAutocompleteValueToIdOrNameAttributeError); |
| 117 | + case Protocol.Audits.GenericIssueErrorType.FormLabelHasNeitherForNorNestedInput: |
| 118 | + return i18nString(UIStrings.formLabelHasNeitherForNorNestedInput); |
| 119 | + case Protocol.Audits.GenericIssueErrorType.FormLabelForMatchesNonExistingIdError: |
| 120 | + return i18nString(UIStrings.formLabelForMatchesNonExistingIdError); |
| 121 | + case Protocol.Audits.GenericIssueErrorType.FormInputHasWrongButWellIntendedAutocompleteValueError: |
| 122 | + return i18nString(UIStrings.formInputHasWrongButWellIntendedAutocompleteValueError); |
| 123 | + default: |
| 124 | + return ''; |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +function getTooltipFromSelectElementAccessibilityIssue(reason: Protocol.Audits.SelectElementAccessibilityIssueReason): |
| 129 | + string { |
| 130 | + switch (reason) { |
| 131 | + case Protocol.Audits.SelectElementAccessibilityIssueReason.DisallowedSelectChild: |
| 132 | + return i18nString(UIStrings.disallowedSelectChild); |
| 133 | + case Protocol.Audits.SelectElementAccessibilityIssueReason.DisallowedOptGroupChild: |
| 134 | + return i18nString(UIStrings.disallowedOptGroupChild); |
| 135 | + case Protocol.Audits.SelectElementAccessibilityIssueReason.NonPhrasingContentOptionChild: |
| 136 | + return i18nString(UIStrings.nonPhrasingContentOptionChild); |
| 137 | + case Protocol.Audits.SelectElementAccessibilityIssueReason.InteractiveContentOptionChild: |
| 138 | + return i18nString(UIStrings.interactiveContentOptionChild); |
| 139 | + case Protocol.Audits.SelectElementAccessibilityIssueReason.InteractiveContentLegendChild: |
| 140 | + return i18nString(UIStrings.interactiveContentLegendChild); |
| 141 | + default: |
| 142 | + return ''; |
| 143 | + } |
| 144 | +} |
0 commit comments