forked from ember-template-lint/ember-template-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
no-meta-redirect-with-time-limit
rule (ember-template-lint#868)
- Loading branch information
Showing
5 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
test/unit/rules/lint-no-meta-redirect-with-time-limit-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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">', | ||
}, | ||
}, | ||
], | ||
}); |