Yahoo YUI Compressor or Google Closure Compiler libraries.
static final String
setRemoveSurroundingSpaces
method to remove all surrounding spaces (not recommended).static final String
<div>
and <li>
tags.static final String
static final Pattern
<?php ...
static final Pattern
<% ...
static final Pattern
<--# ...
HtmlCompressorStatistics
object containing statistics of the last HTML compression, if enabled.int
int
boolean
true
if CSS compression is enabled.boolean
true
if JavaScript compression is enabled.boolean
true
if compression is enabled.boolean
true
if HTML compression statistics is generatedboolean
true
if line breaks will be preserved.boolean
true
if all HTML comments will be removed.boolean
true
if method="get"
attributes will be removed from <form>
tagsboolean
true
if HTTP
protocol will be removed from href
, src
, cite
, and action
tag attributes.boolean
true
if HTTPS
protocol will be removed from href
, src
, cite
, and action
tag attributes.boolean
true
if type="text"
attributes will be removed from <input>
tagsboolean
true
if all inter-tag whitespace characters will be removed.boolean
true
if javascript:
pseudo-protocol will be removed from inline event handlers.boolean
true
if unnecessary attributes will be removed from <link>
tagsboolean
true
if all multiple whitespace characters will be replaced with single spaces.boolean
true
if all unnecessary quotes will be removed from tag attributes.boolean
true
if unnecessary attributes will be removed from <script>
tagsboolean
true
if type="text/style"
attributes will be removed from <style>
tagsboolean
true
if boolean attributes will be simplifiedboolean
true
if existing DOCTYPE declaration will be replaced with simple <!DOCTYPE html>
declaration.boolean
true
if Yahoo YUI Compressor will disable all the built-in micro optimizations during JavaScript compression.boolean
true
if Yahoo YUI Compressor will only minify javascript without obfuscating local symbols.boolean
true
if Yahoo YUI Compressor will preserve unnecessary semicolons during JavaScript compression.void
setCompressCss(boolean compressCss)
true
.void
setCompressJavaScript(boolean compressJavaScript)
true
.void
setCssCompressor(Compressor cssCompressor)
void
setEnabled(boolean enabled)
false
all compression will be bypassed.void
setGenerateStatistics(boolean generateStatistics)
true
, HTML compression statistics will be generated.void
setPreserveLineBreaks(boolean preserveLineBreaks)
true
, line breaks will be preserved.void
setPreservePatterns(List<Pattern> preservePatterns)
void
setRemoveComments(boolean removeComments)
true
all HTML comments will be removed.void
setRemoveFormAttributes(boolean removeFormAttributes)
true
, method="get"
attributes will be removed from <form>
tags.void
setRemoveHttpProtocol(boolean removeHttpProtocol)
true
, HTTP
protocol will be removed from href
, src
, cite
, and action
tag attributes.void
setRemoveHttpsProtocol(boolean removeHttpsProtocol)
true
, HTTPS
protocol will be removed from href
, src
, cite
, and action
tag attributes.void
setRemoveInputAttributes(boolean removeInputAttributes)
true
, type="text"
attributes will be removed from <input>
tags.void
setRemoveIntertagSpaces(boolean removeIntertagSpaces)
true
all inter-tag whitespace characters will be removed.void
setRemoveJavaScriptProtocol(boolean removeJavaScriptProtocol)
true
, javascript:
pseudo-protocol will be removed from inline event handlers.void
setRemoveLinkAttributes(boolean removeLinkAttributes)
true
, following attributes will be removed from <link rel="stylesheet">
and <link rel="alternate stylesheet">
tags: type="text/css" type="text/plain"void
setRemoveMultiSpaces(boolean removeMultiSpaces)
true
all multiple whitespace characters will be replaced with single spaces.void
setRemoveQuotes(boolean removeQuotes)
true
all unnecessary quotes will be removed from tag attributes.void
setRemoveScriptAttributes(boolean removeScriptAttributes)
true
, following attributes will be removed from <script>
tags: type="text/javascript" type="application/javascript" language="javascript"void
setRemoveStyleAttributes(boolean removeStyleAttributes)
true
, type="text/style"
attributes will be removed from <style>
tags.void
setRemoveSurroundingSpaces(String tagList)
void
setSimpleBooleanAttributes(boolean simpleBooleanAttributes)
true
, any values of following boolean attributes will be removed: checked selected disabled readonlyvoid
setSimpleDoctype(boolean simpleDoctype)
true
, existing DOCTYPE declaration will be replaced with simple <!DOCTYPE html>
declaration.void
setYuiCssLineBreak(int yuiCssLineBreak)
void
setYuiJsDisableOptimizations(boolean yuiJsDisableOptimizations)
void
setYuiJsLineBreak(int yuiJsLineBreak)
void
setYuiJsNoMunge(boolean yuiJsNoMunge)
void
setYuiJsPreserveAllSemiColons(boolean yuiJsPreserveAllSemiColons)
<?php ... ?>
tags. Could be passed inside a list to setPreservePatterns
method.<% ... %>
tags. Could be passed inside a list to setPreservePatterns
method.<--# ... -->
tags. Could be passed inside a list to setPreservePatterns
method.setRemoveSurroundingSpaces
method.<div>
and <li>
tags. Table tags are also included. Could be passed to setRemoveSurroundingSpaces
method.setRemoveSurroundingSpaces
method to remove all surrounding spaces (not recommended).compress
in interface Compressor
html
- HTML content to compresstrue
if JavaScript compression is enabled.true
. Default is false
for performance reasons.Note: Compressing JavaScript is not recommended if pages are compressed dynamically on-the-fly because of performance impact. You should consider putting JavaScript into a separate file and compressing it using standalone YUICompressor for example.
compressJavaScript
- set true
to enable JavaScript compression. Default is false
true
if CSS compression is enabled.true
. Default is false
for performance reasons.Note: Compressing CSS is not recommended if pages are compressed dynamically on-the-fly because of performance impact. You should consider putting CSS into a separate file and compressing it using standalone YUICompressor for example.
compressCss
- set true
to enable CSS compression. Default is false
true
if Yahoo YUI Compressor will only minify javascript without obfuscating local symbols. This corresponds to --nomunge
command line option.nomunge
parameter value used for JavaScript compression.--nomunge
command line option. This option has effect only if JavaScript compression is enabled. Default is false
.yuiJsNoMunge
- set true
to enable nomunge
modetrue
if Yahoo YUI Compressor will preserve unnecessary semicolons during JavaScript compression. This corresponds to --preserve-semi
command line option.preserve-semi
parameter value used for JavaScript compression.--preserve-semi
command line option. This option has effect only if JavaScript compression is enabled. Default is false
.yuiJsPreserveAllSemiColons
- set true
to enable preserve-semi
modetrue
if Yahoo YUI Compressor will disable all the built-in micro optimizations during JavaScript compression. This corresponds to --disable-optimizations
command line option.disable-optimizations
parameter value used for JavaScript compression.--disable-optimizations
command line option. This option has effect only if JavaScript compression is enabled. Default is false
.yuiJsDisableOptimizations
- set true
to enable disable-optimizations
mode--line-break
command line option.line-break
parameter value used for JavaScript compression.--line-break
command line option. This option has effect only if JavaScript compression is enabled. Default is -1
to disable line breaks.yuiJsLineBreak
- set number of symbols per line--line-break
command line option.line-break
parameter value used for CSS compression.--line-break
command line option. This option has effect only if CSS compression is enabled. Default is -1
to disable line breaks.yuiCssLineBreak
- set number of symbols per linetrue
if all unnecessary quotes will be removed from tag attributes.true
all unnecessary quotes will be removed from tag attributes. Default is false
.Note: Even though quotes are removed only when it is safe to do so, it still might break strict HTML validation. Turn this option on only if a page validation is not very important or to squeeze the most out of the compression. This option has no performance impact.
removeQuotes
- set true
to remove unnecessary quotes from tag attributestrue
if compression is enabled.true
if compression is enabled.false
all compression will be bypassed. Might be useful for testing purposes. Default is true
.enabled
- set false
to bypass all compressiontrue
if all HTML comments will be removed.true
if all HTML comments will be removedtrue
all HTML comments will be removed. Default is true
.removeComments
- set true
to remove all HTML commentstrue
if all multiple whitespace characters will be replaced with single spaces.true
if all multiple whitespace characters will be replaced with single spaces.true
all multiple whitespace characters will be replaced with single spaces. Default is true
.removeMultiSpaces
- set true
to replace all multiple whitespace characters will single spaces.true
if all inter-tag whitespace characters will be removed.true
if all inter-tag whitespace characters will be removed.true
all inter-tag whitespace characters will be removed. Default is false
.Note: It is fairly safe to turn this option on unless you rely on spaces for page formatting. Even if you do, you can always preserve required spaces with
. This option has no performance impact.
removeIntertagSpaces
- set true
to remove all inter-tag whitespace charactersPattern
objects defining rules for preserving block rulesCustom preservation rules have higher priority than default rules. Priority between custom rules are defined by their position in a list (beginning of a list has higher priority).
Besides custom patterns, you can use 3 predefined patterns: PHP_TAG_PATTERN
, SERVER_SCRIPT_TAG_PATTERN
, SERVER_SIDE_INCLUDE_PATTERN
.
preservePatterns
- List of Pattern
objects that will be used to skip matched blocks during compressionCompressor
implementation that will be used to compress inline CSS in HTML.HtmlCompressor currently comes with basic implementation for Yahoo YUI Compressor (called YuiCssCompressor
), but users can also create their own CSS compressors for custom needs.
If no compressor is set YuiCssCompressor
will be used by default.
cssCompressor
- Compressor
implementation that will be used for inline CSS compressiontrue
if existing DOCTYPE declaration will be replaced with simple <!DOCTYPE html>
declaration.true
if existing DOCTYPE declaration will be replaced with simple <!DOCTYPE html>
declaration.true
, existing DOCTYPE declaration will be replaced with simple <!DOCTYPE html>
declaration. Default is false
.simpleDoctype
- set true
to replace existing DOCTYPE declaration with <!DOCTYPE html>
true
if unnecessary attributes will be removed from <script>
tagstrue
if unnecessary attributes will be removed from <script>
tagstrue
, following attributes will be removed from <script>
tags:Default is false
.
removeScriptAttributes
- set true
to remove unnecessary attributes from <script>
tagstrue
if type="text/style"
attributes will be removed from <style>
tagstrue
if type="text/style"
attributes will be removed from <style>
tagstrue
, type="text/style"
attributes will be removed from <style>
tags. Default is false
.removeStyleAttributes
- set true
to remove type="text/style"
attributes from <style>
tagstrue
if unnecessary attributes will be removed from <link>
tagstrue
if unnecessary attributes will be removed from <link>
tagstrue
, following attributes will be removed from <link rel="stylesheet">
and <link rel="alternate stylesheet">
tags:Default is false
.
removeLinkAttributes
- set true
to remove unnecessary attributes from <link>
tagstrue
if method="get"
attributes will be removed from <form>
tagstrue
if method="get"
attributes will be removed from <form>
tagstrue
, method="get"
attributes will be removed from <form>
tags. Default is false
.removeFormAttributes
- set true
to remove method="get"
attributes from <form>
tagstrue
if type="text"
attributes will be removed from <input>
tagstrue
if type="text"
attributes will be removed from <input>
tagstrue
, type="text"
attributes will be removed from <input>
tags. Default is false
.removeInputAttributes
- set true
to remove type="text"
attributes from <input>
tagstrue
if boolean attributes will be simplifiedtrue
if boolean attributes will be simplifiedtrue
, any values of following boolean attributes will be removed:For example, <input readonly="readonly">
would become <input readonly>
Default is false
.
simpleBooleanAttributes
- set true
to simplify boolean attributestrue
if javascript:
pseudo-protocol will be removed from inline event handlers.true
if javascript:
pseudo-protocol will be removed from inline event handlers.true
, javascript:
pseudo-protocol will be removed from inline event handlers.For example, <a onclick="javascript:alert()">
would become <a onclick="alert()">
Default is false
.
removeJavaScriptProtocol
- set true
to remove javascript:
pseudo-protocol from inline event handlers.true
if HTTP
protocol will be removed from href
, src
, cite
, and action
tag attributes.true
if HTTP
protocol will be removed from href
, src
, cite
, and action
tag attributes.true
, HTTP
protocol will be removed from href
, src
, cite
, and action
tag attributes. URL without a protocol would make a browser use document's current protocol instead.Tags marked with rel="external"
will be skipped.
For example:
<a href="http://example.com"> <script src="http://google.com/js.js" rel="external">
would become:
<a href="//example.com"> <script src="http://google.com/js.js" rel="external">
Default is false
.
removeHttpProtocol
- set true
to remove HTTP
protocol from tag attributestrue
if HTTPS
protocol will be removed from href
, src
, cite
, and action
tag attributes.true
if HTTPS
protocol will be removed from href
, src
, cite
, and action
tag attributes.true
, HTTPS
protocol will be removed from href
, src
, cite
, and action
tag attributes. URL without a protocol would make a browser use document's current protocol instead.Tags marked with rel="external"
will be skipped.
For example:
<a href="https://example.com"> <script src="https://google.com/js.js" rel="external">
would become:
<a href="//example.com"> <script src="https://google.com/js.js" rel="external">
Default is false
.
removeHttpsProtocol
- set true
to remove HTTP
protocol from tag attributestrue
if HTML compression statistics is generatedtrue
if HTML compression statistics is generatedtrue
, HTML compression statistics will be generated.Important: Enabling statistics makes HTML compressor not thread safe.
Default is false
.
generateStatistics
- set true
to generate HTML compression statisticsHtmlCompressorStatistics
object containing statistics of the last HTML compression, if enabled. Should be called after compress(String)
HtmlCompressorStatistics
object containing last HTML compression statisticstrue
if line breaks will be preserved.true
if line breaks will be preserved.true
, line breaks will be preserved.Default is false
.
preserveLineBreaks
- set true
to preserve line breaksBesides custom defined lists, you can pass one of 3 predefined lists of tags: BLOCK_TAGS_MIN
, BLOCK_TAGS_MAX
, ALL_TAGS
.
tagList
- a comma separated list of tags around which spaces will be removed