-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathno-parse-html-literal.js
More file actions
155 lines (148 loc) · 3.21 KB
/
no-parse-html-literal.js
File metadata and controls
155 lines (148 loc) · 3.21 KB
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
'use strict';
const rule = require( '../../src/rules/no-parse-html-literal' );
const RuleTester = require( '../../tools/rule-tester' );
const error = 'Prefer DOM building to parsing HTML literals';
const errorMinimal = 'Single tag must use the format: <div>';
const errorSelfClosing = 'Single tag must use the format: <div/>';
const ruleTester = new RuleTester();
ruleTester.run( 'no-parse-html-literal', rule, {
valid: [
// $( html )
'$("")',
'$("#id > .class[attr]")',
'$(variable)',
'$(function(){})',
'$("<div>")',
'$("<div>", {width:100})',
'$("<" + "div" + ">")',
{
code: '$()',
docgen: false
},
{
code: '$([])',
docgen: false
},
{
code: '$(variable1 = variable2)',
docgen: false
},
{
code: '$("<div>")',
options: [ { singleTagStyle: 'minimal' } ]
},
{
code: '$("<div/>")',
options: [ { singleTagStyle: 'self-closing' } ]
},
{
code: '$("<div />")',
options: [ { singleTagStyle: 'any' } ]
},
{
code: '$("<div></div>")',
options: [ { singleTagStyle: 'any' } ]
},
{
code: '$("<div>" + "</div>")',
options: [ { singleTagStyle: 'any' } ]
},
// $.html
'$div.html()',
'$div.html(variable)',
'$.html("<div>contents</div>")',
// $.append
'$div.append(variable)',
// $.add
'$div.add(variable)',
// $.parseHTML
'$.parseHTML(variable)',
'$.parseHTML(variable1 + variable2)',
// TODO: This passes, but maybe it shouldn't?
'$.parseHTML("string" + variable)',
{
code: '(function(){$()})()',
docgen: false
}
],
// Build test cases by joining methods with strings
invalid: [
// Methods
'$',
'$div.html',
'$div.append',
'$div.add',
'$.parseHTML'
].reduce( ( acc, method ) => acc.concat(
[
// Strings
'"<div>contents</div>"',
'"<div attr=val>"',
'"<div attr=val />"',
'"<div>" + "content" + "</div>"'
].map( ( string, i ) => ( {
code: method + '(' + string + ')',
errors: [ error ],
docgen: i === 0 || method === '$'
} ) )
), [] ).concat( [
// In addition, don't even use $.parseHTML for single tags
{
code: '$.parseHTML("<div>")',
errors: [ error ]
},
// singleTagStyle
{
code: '$("<div/>")',
errors: [ errorMinimal ],
output: '$("<div>")'
},
{
code: '$("<div></div>")',
errors: [ errorMinimal ],
output: '$("<div>")'
},
{
code: '$("<div/>")',
options: [ { singleTagStyle: 'minimal' } ],
errors: [ errorMinimal ],
output: '$("<div>")',
docgen: false
},
{
code: '$("<div></div>")',
options: [ { singleTagStyle: 'minimal' } ],
errors: [ errorMinimal ],
output: '$("<div>")',
docgen: false
},
{
code: '$("<div>")',
options: [ { singleTagStyle: 'self-closing' } ],
errors: [ errorSelfClosing ],
output: '$("<div/>")'
},
{
code: '$("<div />")',
options: [ { singleTagStyle: 'self-closing' } ],
errors: [ errorSelfClosing ],
output: '$("<div/>")'
},
{
code: '$("<div></div>")',
options: [ { singleTagStyle: 'self-closing' } ],
errors: [ errorSelfClosing ],
output: '$("<div/>")'
},
{
code: '$("<div attr=val>")',
options: [ { singleTagStyle: 'any' } ],
errors: [ error ]
},
{
code: '$("<div><div>")',
options: [ { singleTagStyle: 'any' } ],
errors: [ error ]
}
] )
} );