From b8460c1854b4a22160b41f3770bd9c3b28920c94 Mon Sep 17 00:00:00 2001 From: Juriy Zaytsev Date: Wed, 10 Mar 2010 12:37:04 -0500 Subject: [PATCH] Start testing HTMLLint. Add detection of href="javascript:void(0)" during linting. --- index.html | 1 - master.css | 2 +- src/htmllint.js | 25 +++++++++++++----- src/htmlminifier.js | 4 ++- tests/lint-tests.html | 59 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 tests/lint-tests.html diff --git a/index.html b/index.html index 87558b8..76f8fe8 100644 --- a/index.html +++ b/index.html @@ -147,7 +147,6 @@
  • HTMLLint: warn about repeating attributes (e.g. multiple styles, classes, etc.)
  • HTMLLint: warn about repeating &nbsp; or <br> sequences
  • HTMLLint: warn about missing doctype
  • -
  • HTMLLint: warn about href="javascript:void 0"
  • diff --git a/master.css b/master.css index e7e6ee1..b8705c1 100644 --- a/master.css +++ b/master.css @@ -26,4 +26,4 @@ button { font-weight: bold; width: 100px; } .short { display: inline-block; width: 20em; margin-top: 0.25em; } .deprecated-element, .deprecated-attribute { color: red; } -.presentational-element, .presentational-attribute { color: #FF8C00; } \ No newline at end of file +.presentational-element, .presentational-attribute, .inaccessible-attribute { color: #FF8C00; } \ No newline at end of file diff --git a/src/htmllint.js b/src/htmllint.js index 3de3225..53b6dea 100644 --- a/src/htmllint.js +++ b/src/htmllint.js @@ -56,12 +56,18 @@ (attrName === 'width' && (/^(?:hr|td|th|applet|pre)$/).test(tag)) ); } + function isInaccessibleAttribute(attrName, attrValue) { + return ( + attrName === 'href' && + (/^\s*javascript\s*:\s*void\s*(\s+0|\(\s*0\s*\))\s*$/i).test(attrValue) + ); + } function Lint() { this.log = [ ]; } - Lint.prototype._testElement = function(tag, attrName) { + Lint.prototype.testElement = function(tag) { if (isDeprecatedElement(tag)) { this.log.push( '

  • Warning: found deprecated element (', @@ -74,7 +80,7 @@ } }; - Lint.prototype._testAttribute = function(tag, attrName) { + Lint.prototype.testAttribute = function(tag, attrName, attrValue) { if (isEventAttribute(attrName)) { this.log.push( '
  • Warning: found event attribute (', @@ -83,17 +89,22 @@ else if (isDeprecatedAttribute(tag, attrName)) { this.log.push( '
  • Warning: found deprecated attribute (', - attrName, ' on ', tag, ' element)
  • '); + attrName, ' on <', tag, '> element)'); } else if (isStyleAttribute(attrName)) { this.log.push( - '
  • Warning: found style attribute (on ', tag, ' element)
  • '); + '
  • Warning: found style attribute (on <', tag, '> element)
  • '); + } + else if (isInaccessibleAttribute(attrName, attrValue)) { + this.log.push( + '
  • Warning: found inaccessible attribute '+ + '(on <', tag, '> element)
  • '); } }; - Lint.prototype.test = function(tag, attrName) { - this._testElement(tag, attrName); - this._testAttribute(tag, attrName); + Lint.prototype.test = function(tag, attrName, attrValue) { + this.testElement(tag); + this.testAttribute(tag, attrName, attrValue); }; Lint.prototype.populate = function(writeToElement) { diff --git a/src/htmlminifier.js b/src/htmlminifier.js index dd7a9c9..2092add 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -200,8 +200,10 @@ buffer.push('<', tag); + lint && lint.testElement(tag); + for ( var i = 0, len = attrs.length; i < len; i++ ) { - lint && lint.test(tag, attrs[i].name.toLowerCase()); + lint && lint.testAttribute(tag, attrs[i].name.toLowerCase(), attrs[i].escaped); buffer.push(normalizeAttribute(attrs[i], attrs, tag, options)); } diff --git a/tests/lint-tests.html b/tests/lint-tests.html new file mode 100644 index 0000000..d121159 --- /dev/null +++ b/tests/lint-tests.html @@ -0,0 +1,59 @@ + + + + + + + + + + + + + +

    HTML Lint

    +

    +

    +
      + + + + \ No newline at end of file -- 2.34.1