templateClickEventsHaveKeyEventsRule.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. var aria_query_1 = require("aria-query");
  17. var lib_1 = require("tslint/lib");
  18. var ngWalker_1 = require("./angular/ngWalker");
  19. var basicTemplateAstVisitor_1 = require("./angular/templates/basicTemplateAstVisitor");
  20. var isHiddenFromScreenReader_1 = require("./util/isHiddenFromScreenReader");
  21. var isInteractiveElement_1 = require("./util/isInteractiveElement");
  22. var isPresentationRole_1 = require("./util/isPresentationRole");
  23. var domElements = new Set(aria_query_1.dom.keys());
  24. var Rule = (function (_super) {
  25. __extends(Rule, _super);
  26. function Rule() {
  27. return _super !== null && _super.apply(this, arguments) || this;
  28. }
  29. Rule.prototype.apply = function (sourceFile) {
  30. var walkerConfig = { templateVisitorCtrl: TemplateVisitorCtrl };
  31. var walker = new ngWalker_1.NgWalker(sourceFile, this.getOptions(), walkerConfig);
  32. return this.applyWithWalker(walker);
  33. };
  34. Rule.metadata = {
  35. description: 'Ensures that the click event is accompanied with at least one key event keyup, keydown or keypress',
  36. options: null,
  37. optionsDescription: 'Not configurable.',
  38. rationale: 'Keyboard is important for users with physical disabilities who cannot use mouse.',
  39. ruleName: 'template-click-events-have-key-events',
  40. type: 'functionality',
  41. typescriptOnly: true
  42. };
  43. Rule.FAILURE_STRING = 'click must be accompanied by either keyup, keydown or keypress event for accessibility';
  44. return Rule;
  45. }(lib_1.Rules.AbstractRule));
  46. exports.Rule = Rule;
  47. var TemplateVisitorCtrl = (function (_super) {
  48. __extends(TemplateVisitorCtrl, _super);
  49. function TemplateVisitorCtrl() {
  50. return _super !== null && _super.apply(this, arguments) || this;
  51. }
  52. TemplateVisitorCtrl.prototype.visitElement = function (el, context) {
  53. this.validateElement(el);
  54. _super.prototype.visitElement.call(this, el, context);
  55. };
  56. TemplateVisitorCtrl.prototype.validateElement = function (el) {
  57. var hasClick = el.outputs.some(function (output) { return output.name === 'click'; });
  58. if (!hasClick) {
  59. return;
  60. }
  61. if (!domElements.has(el.name)) {
  62. return;
  63. }
  64. if (isPresentationRole_1.isPresentationRole(el) || isHiddenFromScreenReader_1.isHiddenFromScreenReader(el)) {
  65. return;
  66. }
  67. if (isInteractiveElement_1.isInteractiveElement(el)) {
  68. return;
  69. }
  70. var hasKeyEvent = el.outputs.some(function (output) { return output.name.startsWith('keyup') || output.name.startsWith('keydown') || output.name.startsWith('keypress'); });
  71. if (hasKeyEvent) {
  72. return;
  73. }
  74. var _a = el.sourceSpan, endOffset = _a.end.offset, startOffset = _a.start.offset;
  75. this.addFailureFromStartToEnd(startOffset, endOffset, Rule.FAILURE_STRING);
  76. };
  77. return TemplateVisitorCtrl;
  78. }(basicTemplateAstVisitor_1.BasicTemplateAstVisitor));