Skip to content

Commit

Permalink
[Fix] jsx-no-target-blank: Allow rel="noreferrer" when `allowReferr…
Browse files Browse the repository at this point in the history
…er` is true
  • Loading branch information
edemaine authored and ljharb committed Feb 16, 2021
1 parent 734dc53 commit e54118b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -17,13 +17,15 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
* [`no-typos`]: avoid a crash on bindingless `prop-types` import; add warning ([#2899][] @ljharb)
* [`jsx-curly-brace-presence`]: ignore containers with comments ([#2900][] @golopot)
* [`destructuring-assignment`]: fix a false positive for local prop named `context` in SFC ([#2929][] @SyMind)
* [`jsx-no-target-blank`]: Allow rel="noreferrer" when `allowReferrer` is true ([#2925][] @edemaine)

### Changed
* [Docs] [`jsx-no-constructed-context-values`][]: fix invalid example syntax ([#2910][] @kud)
* [readme] Replace lists of rules with tables in readme ([#2908][] @motato1)
* [Docs] added missing curly braces ([#2923][] @Muditxofficial)

[#2929]: https://github.com/yannickcr/eslint-plugin-react/pull/2929
[#2925]: https://github.com/yannickcr/eslint-plugin-react/pull/2925
[#2923]: https://github.com/yannickcr/eslint-plugin-react/pull/2923
[#2910]: https://github.com/yannickcr/eslint-plugin-react/pull/2910
[#2908]: https://github.com/yannickcr/eslint-plugin-react/pull/2908
Expand Down
6 changes: 5 additions & 1 deletion lib/rules/jsx-no-target-blank.js
Expand Up @@ -88,7 +88,11 @@ function hasSecureRel(node, allowReferrer, warnOnSpreadAttributes, spreadAttribu
const relAttribute = node.attributes[relIndex];
const value = getStringFromValue(relAttribute.value);
const tags = value && typeof value === 'string' && value.toLowerCase().split(' ');
return tags && (allowReferrer ? tags.indexOf('noopener') >= 0 : tags.indexOf('noreferrer') >= 0);
const noreferrer = tags && tags.indexOf('noreferrer') >= 0;
if (noreferrer) {
return true;
}
return allowReferrer && tags && tags.indexOf('noopener') >= 0;
}

module.exports = {
Expand Down
10 changes: 10 additions & 0 deletions tests/lib/rules/jsx-no-target-blank.js
Expand Up @@ -103,6 +103,10 @@ ruleTester.run('jsx-no-target-blank', rule, {
code: '<a href="foobar" target="_blank" rel="noopener"></a>',
options: [{allowReferrer: true}]
},
{
code: '<a href="foobar" target="_blank" rel="noreferrer"></a>',
options: [{allowReferrer: true}]
},
{
code: '<a target={3} />'
}
Expand Down Expand Up @@ -212,6 +216,12 @@ ruleTester.run('jsx-no-target-blank', rule, {
output: '<a target={"_blank"} href="//example.com/19" rel="noreferrer"></a>',
errors: defaultErrors
},
{
code: '<a href="http://example.com/20" target="_blank"></a>',
output: '<a href="http://example.com/20" target="_blank" rel="noreferrer"></a>',
options: [{allowReferrer: true}],
errors: defaultErrors
},
{
code: '<a target="_blank" href={ dynamicLink }></a>',
output: '<a target="_blank" href={ dynamicLink } rel="noreferrer"></a>',
Expand Down

0 comments on commit e54118b

Please sign in to comment.