Skip to content

Commit d6e9d33

Browse files
authored
Merge pull request #639 from wxiaoguang/patch-1
Stricter html string check
2 parents c61b342 + 131ed6d commit d6e9d33

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

docs/rules/unescaped-html-literal.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Constructing raw HTML with string literals is error prone and may lead to security issues.
1010

11-
Instead use [`lit-html`](https://github.com/Polymer/lit-html)'s `html` tagged template literal to safely construct HTML literal strings. Alternatively, you can use document builder APIs like `document.createElement`.
11+
Instead use [`lit-html`](https://github.com/Polymer/lit-html)'s `html` tagged template literal to safely construct HTML literal strings. Alternatively, you can implement your own `html` tagged template literal function, or use document builder APIs like `document.createElement`.
1212

1313
👎 Examples of **incorrect** code for this rule:
1414

lib/rules/unescaped-html-literal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default {
1515
},
1616

1717
create(context) {
18-
const htmlOpenTag = /^<[a-zA-Z]/
18+
const htmlOpenTag = /^\s*<[a-zA-Z]/
1919

2020
return {
2121
Literal(node) {

tests/unescaped-html-literal.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ ruleTester.run('unescaped-html-literal', rule, {
5757
},
5858
],
5959
},
60+
{
61+
code: "const helloHTML = ` \n\t<div>Hello ${name}!</div>`",
62+
parserOptions: {ecmaVersion: 2017},
63+
errors: [
64+
{
65+
message: 'Unescaped HTML literal. Use html`` tag template literal for secure escaping.',
66+
type: 'TemplateLiteral',
67+
},
68+
],
69+
},
6070
{
6171
code: 'const helloHTML = foo`<div>Hello ${name}!</div>`',
6272
parserOptions: {ecmaVersion: 2017},

0 commit comments

Comments
 (0)