File tree 3 files changed +26
-28
lines changed
3 files changed +26
-28
lines changed Original file line number Diff line number Diff line change 1
1
# No ` innerHTML `
2
2
3
- Using ` innerHTML ` poses a potential security risk. It should only be used when clearing content .
3
+ Using ` innerHTML ` poses a potential security risk. Prefer using ` textContent ` to set text to an element .
4
4
5
5
``` js
6
6
// bad
@@ -9,8 +9,8 @@ function setContent(element, content) {
9
9
}
10
10
11
11
// good
12
- function clearContent (element ) {
13
- element .innerHTML = ' '
12
+ function setContent (element , content ) {
13
+ element .textContent = content
14
14
}
15
15
```
16
16
Original file line number Diff line number Diff line change @@ -2,31 +2,19 @@ module.exports = {
2
2
meta : {
3
3
type : 'problem' ,
4
4
docs : {
5
- description : 'disallow `Element.prototype.innerHTML``' ,
5
+ description : 'disallow `Element.prototype.innerHTML` in favor of `Element.prototype.textContent `' ,
6
6
url : require ( '../url' ) ( module )
7
7
} ,
8
8
schema : [ ]
9
9
} ,
10
10
11
11
create ( context ) {
12
12
return {
13
- AssignmentExpression ( node ) {
14
- if ( node . operator === '=' ) {
15
- const leftNode = node . left
16
- const rightNode = node . right
17
-
18
- if ( leftNode . property && leftNode . property . name === 'innerHTML' ) {
19
- if ( rightNode . type === 'Literal' && rightNode . value === '' ) {
20
- return
21
- }
22
-
23
- context . report ( {
24
- node,
25
- message :
26
- 'Using innerHTML poses a potential security risk and should not be used other than clearing content.'
27
- } )
28
- }
29
- }
13
+ 'MemberExpression[property.name=innerHTML]' : function ( node ) {
14
+ context . report ( {
15
+ node : node . property ,
16
+ message : 'Using innerHTML poses a potential security risk and should not be used. Prefer using textContent.'
17
+ } )
30
18
}
31
19
}
32
20
}
Original file line number Diff line number Diff line change @@ -6,27 +6,37 @@ const ruleTester = new RuleTester()
6
6
ruleTester . run ( 'no-innter-html' , rule , {
7
7
valid : [
8
8
{
9
- code : 'document.createElement("js-flash-text").innerHTML = ""'
9
+ code : 'document.createElement("js-flash-text").textContent = ""'
10
+ } ,
11
+ {
12
+ code : 'document.createElement("js-flash-text").textContent = "foo"'
10
13
}
11
14
] ,
12
15
invalid : [
13
16
{
14
17
code : 'document.createElement("js-flash-text").innerHTML = "foo"' ,
15
18
errors : [
16
19
{
17
- message :
18
- 'Using innerHTML poses a potential security risk and should not be used other than clearing content.' ,
19
- type : 'AssignmentExpression'
20
+ message : 'Using innerHTML poses a potential security risk and should not be used. Prefer using textContent.' ,
21
+ type : 'Identifier'
20
22
}
21
23
]
22
24
} ,
23
25
{
24
26
code : 'document.querySelector("js-flash-text").innerHTML = "<div>code</div>"' ,
25
27
errors : [
26
28
{
27
- message :
28
- 'Using innerHTML poses a potential security risk and should not be used other than clearing content.' ,
29
- type : 'AssignmentExpression'
29
+ message : 'Using innerHTML poses a potential security risk and should not be used. Prefer using textContent.' ,
30
+ type : 'Identifier'
31
+ }
32
+ ]
33
+ } ,
34
+ {
35
+ code : 'document.querySelector("js-flash-text").innerHTML = ""' ,
36
+ errors : [
37
+ {
38
+ message : 'Using innerHTML poses a potential security risk and should not be used. Prefer using textContent.' ,
39
+ type : 'Identifier'
30
40
}
31
41
]
32
42
}
You can’t perform that action at this time.
0 commit comments