forked from jsx-eslint/eslint-plugin-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsx-no-script-url.js
161 lines (151 loc) · 4.24 KB
/
jsx-no-script-url.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* @fileoverview Prevent usage of `javascript:` URLs
* @author Sergei Startsev
*/
'use strict';
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
const RuleTester = require('../../helpers/ruleTester');
const rule = require('../../../lib/rules/jsx-no-script-url');
const parsers = require('../../helpers/parsers');
const parserOptions = {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
jsx: true,
},
};
// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------
const ruleTester = new RuleTester({ parserOptions });
ruleTester.run('jsx-no-script-url', rule, {
valid: parsers.all([
{ code: '<a href="https://reactjs.org"></a>' },
{ code: '<a href="mailto:[email protected]"></a>' },
{ code: '<a href="#"></a>' },
{ code: '<a href=""></a>' },
{ code: '<a name="foo"></a>' },
{ code: '<a href={"javascript:"}></a>' },
{ code: '<Foo href="javascript:"></Foo>' },
{ code: '<a href />' },
{
code: '<Foo other="javascript:"></Foo>',
options: [[{ name: 'Foo', props: ['to', 'href'] }]],
},
{
code: '<Foo href="javascript:"></Foo>',
settings: {
linkComponents: [{ name: 'Foo', linkAttribute: ['to', 'href'] }],
},
},
{
code: '<Foo href="javascript:"></Foo>',
options: [[], { includeFromSettings: false }],
settings: {
linkComponents: [{ name: 'Foo', linkAttribute: ['to', 'href'] }],
},
},
{
code: '<Foo other="javascript:"></Foo>',
options: [[], { includeFromSettings: true }],
settings: {
linkComponents: [{ name: 'Foo', linkAttribute: ['to', 'href'] }],
},
},
]),
invalid: parsers.all([
// defaults
{
code: '<a href="javascript:"></a>',
errors: [{ messageId: 'noScriptURL' }],
},
{
code: '<a href="javascript:void(0)"></a>',
errors: [{ messageId: 'noScriptURL' }],
},
{
code: '<a href="j\n\n\na\rv\tascript:"></a>',
errors: [{ messageId: 'noScriptURL' }],
},
// with component passed by options
{
code: '<Foo to="javascript:"></Foo>',
errors: [{ messageId: 'noScriptURL' }],
options: [
[{ name: 'Foo', props: ['to', 'href'] }],
],
},
{
code: '<Foo href="javascript:"></Foo>',
errors: [{ messageId: 'noScriptURL' }],
options: [
[{ name: 'Foo', props: ['to', 'href'] }],
],
},
{ // make sure it still uses defaults when passed options
code: '<a href="javascript:void(0)"></a>',
errors: [{ messageId: 'noScriptURL' }],
options: [
[{ name: 'Foo', props: ['to', 'href'] }],
],
},
// with components passed by settings
{
code: '<Foo to="javascript:"></Foo>',
errors: [{ messageId: 'noScriptURL' }],
options: [
[{ name: 'Bar', props: ['to', 'href'] }],
{ includeFromSettings: true },
],
settings: {
linkComponents: [{ name: 'Foo', linkAttribute: 'to' }],
},
},
{
code: '<Foo href="javascript:"></Foo>',
errors: [{ messageId: 'noScriptURL' }],
options: [{ includeFromSettings: true }],
settings: {
linkComponents: [{ name: 'Foo', linkAttribute: ['to', 'href'] }],
},
},
{
code: `
<div>
<Foo href="javascript:"></Foo>
<Bar link="javascript:"></Bar>
</div>
`,
errors: [
{ messageId: 'noScriptURL' },
{ messageId: 'noScriptURL' },
],
options: [
[{ name: 'Bar', props: ['link'] }],
{ includeFromSettings: true },
],
settings: {
linkComponents: [{ name: 'Foo', linkAttribute: ['to', 'href'] }],
},
},
{
code: `
<div>
<Foo href="javascript:"></Foo>
<Bar link="javascript:"></Bar>
</div>
`,
errors: [
{ messageId: 'noScriptURL' },
],
options: [
[{ name: 'Bar', props: ['link'] }],
],
settings: {
linkComponents: [{ name: 'Foo', linkAttribute: ['to', 'href'] }],
},
},
]),
});