Loading ...
Sorry, an error occurred while loading the content.

2575Suggestion for whitespace exception when building an HTML string

Expand Messages
  • paulcrowder1979
    Aug 23, 2011
    • 0 Attachment
      I'm generally a fan of the whitespace rules that JSLint enforces, but there's on exception I'd like to see implemented. When I build an HTML string in JavaScript, I like to format it using multiple strings that are indented as they would be in an actual HTML document. For instance, instead of doing something like this:

      var html = '<div><div><img src="http://example.com/image.gif" /></div</div>';

      I like to do this (in case the indention isn't preserved with this post, imagine each line being indented as if to show a hierarchy like in an HTML document):

      var html =
      '<div>' +
      '<div>' +
      '<img src="http://example.com/image.gif" />' +
      '</div>' +

      This makes maintenance much easier since I can see the HTML in a familiar structure which lets me ensure each tag is properly closed, etc. Since I use YUI Compressor which uses constant folding when minifying the HTML, I still end up with just one string being created at runtime. Right now JSLint gives a warning when my code is formatted this way, forcing me to specify the sloppy whitespace option which means that all the other whitespace in the document no longer gets validated. It seems like JSLint could easily detect string concatenation and not enforce its whitespace rules on those lines.
    • Show all 7 messages in this topic