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 ts = require("typescript");
|
17 | var Lint = require("tslint");
|
18 | var tsutils = require("tsutils");
|
19 | var JsxAttribute_1 = require("./utils/JsxAttribute");
|
20 | var IFRAME_ELEMENT_NAME = 'iframe';
|
21 | var TITLE_ATTRIBUTE_NAME = 'title';
|
22 | var SRC_ATTRIBUTE_NAME = 'src';
|
23 | var HIDDEN_ATTRIBUTE_NAME = 'hidden';
|
24 | var IFRAME_EMPTY_TITLE_ERROR_STRING = 'An iframe element must have a non-empty title.';
|
25 | var IFRAME_EMPTY_OR_HIDDEN_ERROR_STRING = 'An iframe element should not be hidden or empty.';
|
26 | var IFRAME_UNIQUE_TITLE_ERROR_STRING = 'An iframe element must have a unique title.';
|
27 | var Rule = (function (_super) {
|
28 | __extends(Rule, _super);
|
29 | function Rule() {
|
30 | return _super !== null && _super.apply(this, arguments) || this;
|
31 | }
|
32 | Rule.prototype.apply = function (sourceFile) {
|
33 | return sourceFile.languageVariant === ts.LanguageVariant.JSX ? this.applyWithFunction(sourceFile, walk) : [];
|
34 | };
|
35 | Rule.metadata = {
|
36 | ruleName: 'react-a11y-iframes',
|
37 | type: 'functionality',
|
38 | description: 'Enforce that iframe elements are not empty, have title, and are unique.',
|
39 | options: null,
|
40 | optionsDescription: '',
|
41 | typescriptOnly: false,
|
42 | issueClass: 'Non-SDL',
|
43 | issueType: 'Error',
|
44 | severity: 'Important',
|
45 | level: 'Opportunity for Excellence',
|
46 | group: 'Accessibility'
|
47 | };
|
48 | return Rule;
|
49 | }(Lint.Rules.AbstractRule));
|
50 | exports.Rule = Rule;
|
51 | function walk(ctx) {
|
52 | var previousTitles = new Set();
|
53 | function cb(node) {
|
54 | if (tsutils.isVariableDeclaration(node) || tsutils.isMethodDeclaration(node) || tsutils.isFunctionDeclaration(node)) {
|
55 | previousTitles.clear();
|
56 | }
|
57 | if (tsutils.isJsxOpeningElement(node) || tsutils.isJsxSelfClosingElement(node)) {
|
58 | if (node.tagName.getText() === IFRAME_ELEMENT_NAME) {
|
59 | var attributes = JsxAttribute_1.getJsxAttributesFromJsxElement(node);
|
60 | var titleAttribute = attributes[TITLE_ATTRIBUTE_NAME];
|
61 | var titleAttributeText = getAttributeText(titleAttribute);
|
62 | if (!titleAttribute || !titleAttributeText) {
|
63 | ctx.addFailureAtNode(node.tagName, IFRAME_EMPTY_TITLE_ERROR_STRING);
|
64 | }
|
65 | if (titleAttributeText && previousTitles.has(titleAttributeText)) {
|
66 | ctx.addFailureAtNode(node.tagName, IFRAME_UNIQUE_TITLE_ERROR_STRING);
|
67 | }
|
68 | else if (titleAttributeText) {
|
69 | previousTitles.add(titleAttributeText);
|
70 | }
|
71 | var hiddenAttribute = attributes[HIDDEN_ATTRIBUTE_NAME];
|
72 | var srcAttribute = attributes[SRC_ATTRIBUTE_NAME];
|
73 | if (hiddenAttribute || !srcAttribute || !getAttributeText(srcAttribute)) {
|
74 | ctx.addFailureAtNode(node.tagName, IFRAME_EMPTY_OR_HIDDEN_ERROR_STRING);
|
75 | }
|
76 | }
|
77 | }
|
78 | return ts.forEachChild(node, cb);
|
79 | }
|
80 | return ts.forEachChild(ctx.sourceFile, cb);
|
81 | }
|
82 | function getAttributeText(attribute) {
|
83 | if (attribute && attribute.initializer) {
|
84 | if (tsutils.isJsxExpression(attribute.initializer)) {
|
85 | return attribute.initializer.expression ? attribute.initializer.expression.getText() : undefined;
|
86 | }
|
87 | if (tsutils.isStringLiteral(attribute.initializer)) {
|
88 | return attribute.initializer.text;
|
89 | }
|
90 | }
|
91 | return undefined;
|
92 | }
|
93 |
|
\ | No newline at end of file |