Make lint report repeating <br> elements and "&nbsp;" sequences. Rephrase report...
authorJuriy Zaytsev <kangax@gmail.com>
Mon, 22 Mar 2010 03:16:44 +0000 (23:16 -0400)
committerJuriy Zaytsev <kangax@gmail.com>
Mon, 22 Mar 2010 03:16:44 +0000 (23:16 -0400)
index.html
src/htmllint.js
src/htmlminifier.js

index 5668fdb..a933add 100644 (file)
@@ -10,7 +10,7 @@
     <div>
       <div id="outer-wrapper">
         <div id="wrapper">
-          <h1>HTML Minifier <span style="font-size:0.6em">(ver. 0.41)</span></h1>
+          <h1>HTML Minifier <span style="font-size:0.6em">(ver. 0.42)</span></h1>
           <p id="warning">
             Minifier is <strong>very draft</strong> and is <strong>not yet thoroughly tested</strong>. Use at your own risk. 
           </p>
         </ul>
         <ul>
           <li>HTMLLint: warn about repeating attributes (e.g. multiple styles, classes, etc.)</li>
-          <li>HTMLLint: warn about repeating &amp;nbsp; or &lt;br> sequences</li>
           <li>HTMLLint: warn about missing doctype</li>
         </ul>
       </div>
index 53b6dea..c97dfb7 100644 (file)
   
   function Lint() {
     this.log = [ ];
+    this._lastElement = null;
+    this._isElementRepeated = false;
   }
   
   Lint.prototype.testElement = function(tag) {
     if (isDeprecatedElement(tag)) {
       this.log.push(
-        '<li>Warning: found <span class="deprecated-element">deprecated element</span> (<strong>', 
-        tag, '</strong>)</li>');
+        '<li>Found <span class="deprecated-element">deprecated</span> <strong><code>&lt;' + 
+          tag + '&gt;</code></strong> element</li>');
     }
     else if (isPresentationalElement(tag)) {
       this.log.push(
-        '<li>Warning: found <span class="presentational-element">presentational element</span> (<strong>', 
-        tag, '</strong>)</li>');
+        '<li>Found <span class="presentational-element">presentational</span> <strong><code>&lt;' + 
+          tag + '&gt;</code></strong> element</li>');
     }
+    else {
+      this.checkRepeatingElement(tag);
+    }
+  };
+  
+  Lint.prototype.checkRepeatingElement = function(tag) {
+    if (tag === 'br' && this._lastElement === 'br') {
+      this._isElementRepeated = true;
+    }
+    else if (this._isElementRepeated) {
+      this._reportRepeatingElement();
+      this._isElementRepeated = false;
+    }
+    this._lastElement = tag;
+  };
+  
+  Lint.prototype._reportRepeatingElement = function() {
+    this.log.push('<li>Found <code>&lt;br></code> sequence. Try replacing it with styling.</li>');
   };
   
   Lint.prototype.testAttribute = function(tag, attrName, attrValue) {
     if (isEventAttribute(attrName)) {
       this.log.push(
-        '<li>Warning: found <span class="event-attribute">event attribute</span> (<strong>', 
-        attrName, '</strong>)</li>');
+        '<li>Found <span class="event-attribute">event attribute</span> (<strong>', 
+        attrName, '</strong>) on <strong><code>&lt;' + tag + '&gt;</code></strong> element</li>');
     }
     else if (isDeprecatedAttribute(tag, attrName)) {
       this.log.push(
-        '<li>Warning: found <span class="deprecated-attribute">deprecated attribute</span> (<strong>', 
-        attrName, '</strong> on <code>&lt;', tag, '&gt;</code> element)</li>');
+        '<li>Found <span class="deprecated-attribute">deprecated</span> <strong>' + 
+          attrName + '</strong> attribute on <strong><code>&lt;', tag, '&gt;</code></strong> element</li>');
     }
     else if (isStyleAttribute(attrName)) {
       this.log.push(
-        '<li>Warning: found <span class="style-attribute">style attribute</span> (on <code>&lt;', tag, '&gt;</code> element)</li>');
+        '<li>Found <span class="style-attribute">style attribute</span> on <strong><code>&lt;', tag, '&gt;</code></strong> element</li>');
     }
     else if (isInaccessibleAttribute(attrName, attrValue)) {
       this.log.push(
-        '<li>Warning: found <span class="inaccessible-attribute">inaccessible attribute</span> '+
-          '(on <code>&lt;', tag, '&gt;</code> element)</li>');
+        '<li>Found <span class="inaccessible-attribute">inaccessible attribute</span> '+
+          '(on <strong><code>&lt;', tag, '&gt;</code></strong> element)</li>');
+    }
+  };
+  
+  Lint.prototype.testChars = function(chars) {
+    this._lastElement = '';
+    if (/(&nbsp;\s*){2,}/.test(chars)) {
+      this.log.push('<li>Found repeating <strong><code>&amp;nbsp;</code></strong> sequence. Try replacing it with styling.</li>');
     }
   };
   
   };
   
   Lint.prototype.populate = function(writeToElement) {
+    if (this._isElementRepeated) {
+      this._reportRepeatingElement();
+    }
     var report;
     if (this.log.length && writeToElement) {
-      report = '<ul>' + this.log.join('') + '</ul>';
+      report = '<ol>' + this.log.join('') + '</ol>';
       writeToElement.innerHTML = report;
     }
   };
index 867457e..22308dc 100644 (file)
@@ -1,5 +1,5 @@
 /*!
- * HTMLMinifier v0.41
+ * HTMLMinifier v0.42
  * http://kangax.github.com/html-minifier/
  *
  * Copyright (c) 2010 Juriy "kangax" Zaytsev
           }
         }
         currentChars = text;
+        lint && lint.testChars(text);
         buffer.push(text);
       },
       comment: function( text ) {