templateAccessibilityTabindexNoPositiveRule.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 lib_1 = require("tslint/lib");
  17. var ngWalker_1 = require("./angular/ngWalker");
  18. var basicTemplateAstVisitor_1 = require("./angular/templates/basicTemplateAstVisitor");
  19. var getAttributeValue_1 = require("./util/getAttributeValue");
  20. var Rule = (function (_super) {
  21. __extends(Rule, _super);
  22. function Rule() {
  23. return _super !== null && _super.apply(this, arguments) || this;
  24. }
  25. Rule.prototype.apply = function (sourceFile) {
  26. var walkerConfig = { templateVisitorCtrl: TemplateVisitorCtrl };
  27. var walker = new ngWalker_1.NgWalker(sourceFile, this.getOptions(), walkerConfig);
  28. return this.applyWithWalker(walker);
  29. };
  30. Rule.metadata = {
  31. description: 'Ensures that the tabindex attribute is not positive',
  32. options: null,
  33. optionsDescription: 'Not configurable.',
  34. rationale: 'positive values for tabindex attribute should be avoided because they mess up with the order of focus (AX_FOCUS_03)',
  35. ruleName: 'template-accessibility-tabindex-no-positive',
  36. type: 'functionality',
  37. typescriptOnly: true
  38. };
  39. Rule.FAILURE_MESSAGE = 'tabindex attribute cannot be positive';
  40. return Rule;
  41. }(lib_1.Rules.AbstractRule));
  42. exports.Rule = Rule;
  43. var TemplateVisitorCtrl = (function (_super) {
  44. __extends(TemplateVisitorCtrl, _super);
  45. function TemplateVisitorCtrl() {
  46. return _super !== null && _super.apply(this, arguments) || this;
  47. }
  48. TemplateVisitorCtrl.prototype.visitElement = function (ast, context) {
  49. this.validateElement(ast);
  50. _super.prototype.visitElement.call(this, ast, context);
  51. };
  52. TemplateVisitorCtrl.prototype.validateElement = function (element) {
  53. var tabIndexValue = getAttributeValue_1.getAttributeValue(element, 'tabindex');
  54. if (tabIndexValue) {
  55. tabIndexValue = parseInt(tabIndexValue, 10);
  56. if (tabIndexValue > 0) {
  57. var _a = element.sourceSpan, endOffset = _a.end.offset, startOffset = _a.start.offset;
  58. this.addFailureFromStartToEnd(startOffset, endOffset, Rule.FAILURE_MESSAGE);
  59. }
  60. }
  61. };
  62. return TemplateVisitorCtrl;
  63. }(basicTemplateAstVisitor_1.BasicTemplateAstVisitor));