templateNoDistractingElementsRule.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 sprintf_js_1 = require("sprintf-js");
  17. var lib_1 = require("tslint/lib");
  18. var ngWalker_1 = require("./angular/ngWalker");
  19. var basicTemplateAstVisitor_1 = require("./angular/templates/basicTemplateAstVisitor");
  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: 'Enforces that no distracting elements are used',
  32. options: null,
  33. optionsDescription: 'Not configurable.',
  34. rationale: 'Elements that can be visually distracting can cause accessibility issues with visually impaired users.',
  35. ruleName: 'template-no-distracting-elements',
  36. type: 'functionality',
  37. typescriptOnly: true
  38. };
  39. Rule.FAILURE_STRING = 'Avoid using <%s/> elements as they create visual accessibility issues.';
  40. return Rule;
  41. }(lib_1.Rules.AbstractRule));
  42. exports.Rule = Rule;
  43. function getFailureMessage(element) {
  44. return sprintf_js_1.sprintf(Rule.FAILURE_STRING, element);
  45. }
  46. exports.getFailureMessage = getFailureMessage;
  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 (prop, context) {
  53. this.validateElement(prop);
  54. _super.prototype.visitElement.call(this, prop, context);
  55. };
  56. TemplateVisitorCtrl.prototype.validateElement = function (el) {
  57. if (el.name === 'marquee' || el.name === 'blink') {
  58. var _a = el.sourceSpan, endOffset = _a.end.offset, startOffset = _a.start.offset;
  59. this.addFailureFromStartToEnd(startOffset, endOffset, getFailureMessage(el.name));
  60. }
  61. };
  62. return TemplateVisitorCtrl;
  63. }(basicTemplateAstVisitor_1.BasicTemplateAstVisitor));