Skip to content

Latest commit

 

History

History
96 lines (80 loc) · 2.74 KB

no-parse-html-literal.md

File metadata and controls

96 lines (80 loc) · 2.74 KB

no-parse-html-literal

Disallows parsing of HTML literal strings using either the jQuery method $() or $.parseHTML. Single tags are still allowed for creating new nodes as these don't tirgger the HTML parser. DOM build and manipulation methods should be used instead.

The format of single tags can be specified using the singleTagStyle option:

  • "minimal" (default) no whitespace or self-closing i.e. <div>
  • "self-closing" no whitespace and self-closing i.e. <div/>
  • "any" no style enforced

🔧 The --fix option on the command line can automatically fix some of the problems reported by this rule.

Rule details

❌ Examples of incorrect code:

$( '<div>contents</div>' );
$( '<div attr=val>' );
$( '<div attr=val />' );
$( '<div>' + 'content' + '</div>' );
$div.html( '<div>contents</div>' );
$div.append( '<div>contents</div>' );
$div.add( '<div>contents</div>' );
$.parseHTML( '<div>contents</div>' );
$.parseHTML( '<div>' );
$( '<div/>' );
$( '<div></div>' );

✔️ Examples of correct code:

$( '' );
$( '#id > .class[attr]' );
$( variable );
$( function () {} );
$( '<div>' );
$( '<div>', { width: 100 } );
$( '<' + 'div' + '>' );
$div.html();
$div.html( variable );
$.html( '<div>contents</div>' );
$div.append( variable );
$div.add( variable );
$.parseHTML( variable );
$.parseHTML( variable1 + variable2 );
$.parseHTML( 'string' + variable );

❌ Examples of incorrect code with [{"singleTagStyle":"any"}] options:

$( '<div attr=val>' );
$( '<div><div>' );

✔️ Examples of correct code with [{"singleTagStyle":"any"}] options:

$( '<div />' );
$( '<div></div>' );
$( '<div>' + '</div>' );

✔️ Examples of correct code with [{"singleTagStyle":"minimal"}] options:

$( '<div>' );

❌ Examples of incorrect code with [{"singleTagStyle":"self-closing"}] options:

$( '<div>' );
$( '<div />' );
$( '<div></div>' );

✔️ Examples of correct code with [{"singleTagStyle":"self-closing"}] options:

$( '<div/>' );

🔧 Examples of code fixed by this rule:

$( '<div/>' );      /* → */ $( '<div>' );
$( '<div></div>' ); /* → */ $( '<div>' );

🔧 Examples of code fixed by this rule with [{"singleTagStyle":"self-closing"}] options:

$( '<div>' );       /* → */ $( '<div/>' );
$( '<div />' );     /* → */ $( '<div/>' );
$( '<div></div>' ); /* → */ $( '<div/>' );

Resources