From: Juriy Zaytsev Date: Fri, 12 Feb 2010 05:54:00 +0000 (-0500) Subject: Add ability to remove optional tags ( and for now); X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=9faa99da5d5837b13c7be0dbfa6ab120d863cf3f;p=html-minifier.git Add ability to remove optional tags ( and for now); Bump to version 0.3; Reorganize main page slightly. --- diff --git a/index.html b/index.html index 94f14f6..c25676b 100644 --- a/index.html +++ b/index.html @@ -11,90 +11,103 @@
-

HTML Minifier (ver. 0.2)

+

HTML Minifier (ver. 0.3)

-
-
    -
  • - - - - - - Conditional comments are left intact. +

    + +

    + +
+
+
    +
  • + + + + + + Conditional comments are left intact. + +
  • +
  • + + +
  • +
  • + + +
  • +
  • + + +
  • +
  • + + +
  • +
  • + + +
    + <script language="Javascript" ...>
    + <form method="get" ...>
    + <input type="text" ...>
    + <script src="..." charset="...">
    + <a id="..." name="...">
    + <... onclick="javascript:..." ...> +
    +
  • +
  • + + +
  • +
  • + +
  • -
  • - - -
  • -
  • - - -
  • -
  • - - -
  • -
  • - - -
  • -
  • - - -
    - <script language="Javascript" ...>
    - <form method="get" ...>
    - <input type="text" ...>
    - <script src="..." charset="...">
    - <a id="..." name="...">
    - <... onclick="javascript:..." ...> -
    -
  • -
  • - - -
  • -
  • - - -
  • -
  • - - -
  • -
-
+ + +
  • + + +
  • +
  • + + +
  • +
    -

    - +

    Minifier is very draft and is not yet thoroughly tested. Use at your own risk. diff --git a/master.css b/master.css index 32767f8..09f79f8 100644 --- a/master.css +++ b/master.css @@ -3,17 +3,17 @@ textarea { height: 30em; } h1 { margin-top: 0.5em; font-size: 1.25em; } button { font-weight: bold; width: 100px; } -#wrapper { overflow: hidden; min-width: 900px; } -#input { width: 65%; } -#output { width: 100%; height: 18em; margin-bottom: 2em; } -#options { float: right; width: 33%; padding-left: 1em; } +#wrapper { overflow: hidden; width: 65%; float: left; } +#input { width: 99%; height: 18em; } +#output { width: 99%; height: 18em; margin-bottom: 2em; } +#options { float: right; width: 33%; padding-left: 1em; margin-top: 2.5em; min-height: 50em; } #options ul { list-style: none; padding: 0.5em; overflow: hidden; background: #ffe; margin-top: 0; } #options ul li { float: left; clear: both; padding-bottom: 0.5em; } #options ul li div { margin-left: 1.75em; } #options label, #options input { float: left; } #options input { margin-right: 0.5em; } #stats { margin-bottom: 2em; } -#todo { font-family: monospace; } +#todo { font-family: monospace; margin-bottom: 2em; } #warning { background: #fcc; padding: 0.25em; display: inline-block; } .success { color: green; } diff --git a/master.js b/master.js index b4a2dbb..e020834 100644 --- a/master.js +++ b/master.js @@ -19,7 +19,8 @@ removeRedundantAttributes: byId('remove-redundant-attributes').checked, useShortDoctype: byId('use-short-doctype').checked, removeEmptyAttributes: byId('remove-empty-attributes').checked, - removeEmptyElements: byId('remove-empty-elements').checked + removeEmptyElements: byId('remove-empty-elements').checked, + removeOptionalTags: byId('remove-optional-tags').checked, }; } diff --git a/src/htmlminifier.js b/src/htmlminifier.js index 8817099..ce57761 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -1,5 +1,5 @@ /*! - * HTML Minifier v0.2 + * HTML Minifier v0.3 * http://kangax.github.com/html-minifier/ * * Copyright (c) 2010 Juriy "kangax" Zaytsev @@ -117,6 +117,10 @@ return tag !== 'textarea'; } + function isOptionalTag(tag) { + return (/^(?:body|html)$/).test(tag); + } + function canCollapseWhitespace(tag) { return !(/^(?:script|style|pre|textarea)$/.test(tag)); } @@ -186,7 +190,8 @@ }, end: function( tag ) { var isElementEmpty = currentChars === '' && tag === currentTag; - if (options.removeEmptyElements && isElementEmpty && canRemoveElement(tag)) { + if ((options.removeEmptyElements && isElementEmpty && canRemoveElement(tag)) || + (options.removeOptionalTags && isOptionalTag(tag))) { // noop } else { diff --git a/tests/index.html b/tests/index.html index 91e0b90..668ed14 100644 --- a/tests/index.html +++ b/tests/index.html @@ -324,6 +324,14 @@ input = ''; equals(minify(input, { collapseBooleanAttributes: true }), ''); }); + + test('removing optional tags', function(){ + input = 'hello

    foobar

    '; + output = 'hello

    foobar

    '; + equals(minify(input, { removeOptionalTags: true }), output); + equals(minify(input), input); + }); + })(this);