[jslint] Re: Suggestion for whitespace exception when building an HTML string
- I've avoided putting the spacing in the string itself since it wouldn't get minified out. As for the concatenation cost, remember that I use YUI Compressor to minify the script which uses constant folding to combine literal strings during the minification step. That means that code that's written like this:
var myHtml = '<div>' + '</div>';
Gets converted to this when minified:
Storing the HTML in the DOM of course has performance implications and is also not practical in my real-world code; while the sample code I provided in my original post consists of all literal strings, in reality it's a combination of literal strings and variables. I used literal strings in my example for simplicity's sake.
- Actually what you are describing is not minification. It is obfuscation, which is different. YUI can do both.
In markup languages, particularly HTML/XML, each and every white space character always has value. This is evident when you use the white-space css property with the "pre" value. Most of the time, however, the value of the white space is trivial as it is typically tokenized or saturated into the structure prior to being painted to the screen. In these cases white space can be minified to a single character without substantive harm, although the value of the document is irrevocably different. When white space is entirely removed not only is the value of the document different, but so is the structure of the document. This is evident in reflection to the removal of text nodes from the DOM. Changing the structure from removing text nodes that only contain white space is typically ok if there is understanding on which text nodes are removed and the impact to the tokenized result of the parsed content, however this is certainly less safe than merely tokenizing white space without removing any DOM nodes. This is less safe in that automated agents can no longer walk the DOM with predefined guidance and produce identical results. This warning is otherwise trivial as content oriented automated agents, such as meta-data bots, walk the DOM from the content out.
If you are not ready to consider these aspects of parsing markup then you should not be concerning yourself with dynamically altering the white space of markup code. You may likely be harming and altering the data value of markup documents without knowledge to the effect.
Austin Cheney, CISSP