Skip to content

Commit

Permalink
Add no-meta-redirect-with-time-limit rule (ember-template-lint#868)
Browse files Browse the repository at this point in the history
  • Loading branch information
MelSumner authored and rwjblue committed Oct 31, 2019
1 parent 5fff1df commit e867616
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
30 changes: 30 additions & 0 deletions docs/rule/no-meta-redirect-with-time-limit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## (no-meta-redirect-with-time-limit)

Sometimes a page automatically redirects to a different page. When this happens after a timed delay, it is an unexpected change of context that may interrupt the user. Redirects without timed delays are okay, but pleasae consider a server-side method for redirecting instead (method will vary based on your server type).

This rule checks for the meta tag with a redirect; if it exists, it checks for a timed delay greater than 0.

### Examples

This rule **forbids** the following:

```hbs
<meta http-equiv="refresh" content="5; url=http://www.example.com" />
```

This rule **allows** the following:

```hbs
<meta http-equiv="refresh" content="0; url=http://www.example.com" />
```

### Migration

* To fix, reduce the timed delay to zero, or use the appropriate server-side redirect method for your server type.

### References

* [F40: Failure due to using meta redirect with a time limit](https://www.w3.org/WAI/WCAG21/Techniques/failures/F40)
* [Success Criterion 2.2.1: Timing Adjustable](https://www.w3.org/WAI/WCAG21/Understanding/timing-adjustable)
* [Success Criterion 2.2.4: Interruptions](https://www.w3.org/WAI/WCAG21/Understanding/interruptions)
* [Success Criterion 3.2.5: Change on Request](https://www.w3.org/WAI/WCAG21/Understanding/change-on-request)
1 change: 1 addition & 0 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* [no-input-tagname](rule/no-input-tagname.md)
* [no-invalid-interactive](rule/no-invalid-interactive.md)
* [no-log](rule/no-log.md)
* [no-meta-redirect-with-time-limit](rule/no-meta-redirect-with-time-limit.md)
* [no-negated-condition](rule/no-negated-condition.md)
* [no-nested-interactive](rule/no-nested-interactive.md)
* [no-obsolete-elements](rule/no-obsolete-elements.md)
Expand Down
1 change: 1 addition & 0 deletions lib/rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = {
'no-input-tagname': require('./lint-no-input-tagname'),
'no-invalid-interactive': require('./lint-no-invalid-interactive'),
'no-log': require('./lint-no-log'),
'no-meta-redirect-with-time-limit': require('./lint-no-meta-redirect-with-time-limit'),
'no-negated-condition': require('./lint-no-negated-condition'),
'no-nested-interactive': require('./lint-no-nested-interactive'),
'no-obsolete-elements': require('./lint-no-obsolete-elements'),
Expand Down
39 changes: 39 additions & 0 deletions lib/rules/lint-no-meta-redirect-with-time-limit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

const AstNodeInfo = require('../helpers/ast-node-info');
const Rule = require('./base');

module.exports = class NoMetaRedirectWithTimeLimit extends Rule {
logNode({ node, message }) {
return this.log({
message,
line: node.loc && node.loc.start.line,
column: node.loc && node.loc.start.column,
source: this.sourceForNode(node),
});
}
visitor() {
return {
ElementNode(node) {
const isMeta = node.tag === 'meta';
const hasMetaRedirect = AstNodeInfo.hasAttribute(node, 'http-equiv');

if (isMeta && hasMetaRedirect) {
const contentAttr = AstNodeInfo.hasAttribute(node, 'content');
const contentAttrValue = AstNodeInfo.elementAttributeValue(node, 'content');
if (contentAttr) {
// since the content attribute will have both the delay (in seconds) and the new URL, we only need to check the first character in the value string.
if (contentAttrValue.charAt(0) !== '0') {
this.log({
message: 'a meta redirect should not have a delay value greater than zero',
line: node.loc && node.loc.start.line,
column: node.loc && node.loc.start.column,
source: this.sourceForNode(node),
});
}
}
}
},
};
}
};
25 changes: 25 additions & 0 deletions test/unit/rules/lint-no-meta-redirect-with-time-limit-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const generateRuleTests = require('../../helpers/rule-test-harness');

generateRuleTests({
name: 'no-meta-redirect-with-time-limit',

config: true,

good: ['<meta http-equiv="refresh" content="0; url=http://www.example.com">'],

bad: [
{
template: '<meta http-equiv="refresh" content="1; url=http://www.example.com">',

result: {
moduleId: 'layout.hbs',
message: 'a meta redirect should not have a delay value greater than zero',
line: 1,
column: 0,
source: '<meta http-equiv="refresh" content="1; url=http://www.example.com">',
},
},
],
});

0 comments on commit e867616

Please sign in to comment.