Class HtmlCompressor
- All Implemented Interfaces:
- Compressor
Blocks that should be additionally preserved could be marked with:<!-- {{{ --
    ...
<!-- }}} -->
or any number of user defined patterns.
Content inside <script> or <style> tags could be optionally compressed using Yahoo YUI Compressor or Google Closure Compiler libraries.
- Author:
- Sergiy Kovalchuk
- Field SummaryFieldsModifier and TypeFieldDescription- static final StringCould be passed to- setRemoveSurroundingSpacesmethod to remove all surrounding spaces (not recommended).- static final StringPredefined list of tags that are block-level by default, excluding- <div>and- <li>tags.- static final StringPredefined list of tags that are very likely to be block-level.- static final PatternPredefined pattern that matches- <?php ...- static final PatternPredefined pattern that matches- <% ...- static final PatternPredefined pattern that matches- <--# ...
- Constructor SummaryConstructors
- Method SummaryModifier and TypeMethodDescriptionThe main method that compresses given HTML source and returns compressed result.Returns CSS compressor implementation that will be used to compress inline CSS in HTML.Returns a list of Patterns defining custom preserving block rulesReturns a comma separated list of tags around which spaces will be removed.Returns- HtmlCompressorStatisticsobject containing statistics of the last HTML compression, if enabled.- intReturns number of symbols per line Yahoo YUI Compressor will use during CSS compression.- intReturns number of symbols per line Yahoo YUI Compressor will use during JavaScript compression.- booleanReturns- trueif CSS compression is enabled.- booleanReturns- trueif JavaScript compression is enabled.- booleanReturns- trueif compression is enabled.- booleanReturns- trueif HTML compression statistics is generated- booleanReturns- trueif line breaks will be preserved.- booleanReturns- trueif all HTML comments will be removed.- booleanReturns- trueif- method="get"attributes will be removed from- <form>tags- booleanReturns- trueif- HTTPprotocol will be removed from- href,- src,- cite, and- actiontag attributes.- booleanReturns- trueif- HTTPSprotocol will be removed from- href,- src,- cite, and- actiontag attributes.- booleanReturns- trueif- type="text"attributes will be removed from- <input>tags- booleanReturns- trueif all inter-tag whitespace characters will be removed.- booleanReturns- trueif- javascript:pseudo-protocol will be removed from inline event handlers.- booleanReturns- trueif unnecessary attributes will be removed from- <link>tags- booleanReturns- trueif all multiple whitespace characters will be replaced with single spaces.- booleanReturns- trueif all unnecessary quotes will be removed from tag attributes.- booleanReturns- trueif unnecessary attributes will be removed from- <script>tags- booleanReturns- trueif- type="text/style"attributes will be removed from- <style>tags- booleanReturns- trueif boolean attributes will be simplified- booleanReturns- trueif existing DOCTYPE declaration will be replaced with simple- <!DOCTYPE html>declaration.- booleanReturns- trueif Yahoo YUI Compressor will disable all the built-in micro optimizations during JavaScript compression.- booleanReturns- trueif Yahoo YUI Compressor will only minify javascript without obfuscating local symbols.- booleanReturns- trueif Yahoo YUI Compressor will preserve unnecessary semicolons during JavaScript compression.- void- setCompressCss- (boolean compressCss) Enables CSS compression within <style> tags using Yahoo YUI Compressor if set to- true.- void- setCompressJavaScript- (boolean compressJavaScript) Enables JavaScript compression within <script> tags using Yahoo YUI Compressor if set to- true.- void- setCssCompressor- (Compressor cssCompressor) Sets CSS compressor implementation that will be used to compress inline CSS in HTML.- void- setEnabled- (boolean enabled) If set to- falseall compression will be bypassed.- void- setGenerateStatistics- (boolean generateStatistics) If set to- true, HTML compression statistics will be generated.- void- setPreserveLineBreaks- (boolean preserveLineBreaks) If set to- true, line breaks will be preserved.- void- setPreservePatterns- (List<Pattern> preservePatterns) This method allows setting custom block preservation rules defined by regular expression patterns.- void- setRemoveComments- (boolean removeComments) If set to- trueall HTML comments will be removed.- void- setRemoveFormAttributes- (boolean removeFormAttributes) If set to- true,- method="get"attributes will be removed from- <form>tags.- void- setRemoveHttpProtocol- (boolean removeHttpProtocol) If set to- true,- HTTPprotocol will be removed from- href,- src,- cite, and- actiontag attributes.- void- setRemoveHttpsProtocol- (boolean removeHttpsProtocol) If set to- true,- HTTPSprotocol will be removed from- href,- src,- cite, and- actiontag attributes.- void- setRemoveInputAttributes- (boolean removeInputAttributes) If set to- true,- type="text"attributes will be removed from- <input>tags.- void- setRemoveIntertagSpaces- (boolean removeIntertagSpaces) If set to- trueall inter-tag whitespace characters will be removed.- void- setRemoveJavaScriptProtocol- (boolean removeJavaScriptProtocol) If set to- true,- javascript:pseudo-protocol will be removed from inline event handlers.- void- setRemoveLinkAttributes- (boolean removeLinkAttributes) If set to- 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) If set to- trueall multiple whitespace characters will be replaced with single spaces.- void- setRemoveQuotes- (boolean removeQuotes) If set to- trueall unnecessary quotes will be removed from tag attributes.- void- setRemoveScriptAttributes- (boolean removeScriptAttributes) If set to- true, following attributes will be removed from- <script>tags: type="text/javascript" type="application/javascript" language="javascript"- void- setRemoveStyleAttributes- (boolean removeStyleAttributes) If set to- true,- type="text/style"attributes will be removed from- <style>tags.- void- setRemoveSurroundingSpaces- (String tagList) Enables surrounding spaces removal around provided comma separated list of tags.- void- setSimpleBooleanAttributes- (boolean simpleBooleanAttributes) If set to- true, any values of following boolean attributes will be removed: checked selected disabled readonly- void- setSimpleDoctype- (boolean simpleDoctype) If set to- true, existing DOCTYPE declaration will be replaced with simple- <!DOCTYPE html>declaration.- void- setYuiCssLineBreak- (int yuiCssLineBreak) Tells Yahoo YUI Compressor to break lines after the specified number of symbols during CSS compression.- void- setYuiJsDisableOptimizations- (boolean yuiJsDisableOptimizations) Tells Yahoo YUI Compressor to disable all the built-in micro optimizations during JavaScript compression.- void- setYuiJsLineBreak- (int yuiJsLineBreak) Tells Yahoo YUI Compressor to break lines after the specified number of symbols during JavaScript compression.- void- setYuiJsNoMunge- (boolean yuiJsNoMunge) Tells Yahoo YUI Compressor to only minify javascript without obfuscating local symbols.- void- setYuiJsPreserveAllSemiColons- (boolean yuiJsPreserveAllSemiColons) Tells Yahoo YUI Compressor to preserve unnecessary semicolons during JavaScript compression.
- Field Details- PHP_TAG_PATTERNPredefined pattern that matches- <?php ... ?>tags. Could be passed inside a list to- setPreservePatternsmethod.
- SERVER_SCRIPT_TAG_PATTERNPredefined pattern that matches- <% ... %>tags. Could be passed inside a list to- setPreservePatternsmethod.
- SERVER_SIDE_INCLUDE_PATTERNPredefined pattern that matches- <--# ... -->tags. Could be passed inside a list to- setPreservePatternsmethod.
- BLOCK_TAGS_MINPredefined list of tags that are very likely to be block-level. Could be passed to- setRemoveSurroundingSpacesmethod.- See Also:
 
- BLOCK_TAGS_MAXPredefined list of tags that are block-level by default, excluding- <div>and- <li>tags. Table tags are also included. Could be passed to- setRemoveSurroundingSpacesmethod.- See Also:
 
- ALL_TAGSCould be passed to- setRemoveSurroundingSpacesmethod to remove all surrounding spaces (not recommended).- See Also:
 
 
- Constructor Details- HtmlCompressorpublic HtmlCompressor()
 
- Method Details- compressThe main method that compresses given HTML source and returns compressed result.- Specified by:
- compressin interface- Compressor
- Parameters:
- html- HTML content to compress
- Returns:
- compressed content.
 
- isCompressJavaScriptpublic boolean isCompressJavaScript()Returns- trueif JavaScript compression is enabled.- Returns:
- current state of JavaScript compression.
 
- setCompressJavaScriptpublic void setCompressJavaScript- (boolean compressJavaScript) Enables JavaScript compression within <script> tags using Yahoo YUI Compressor if set to- true. Default is- falsefor 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. - Parameters:
- compressJavaScript- set- trueto enable JavaScript compression. Default is- false
- See Also:
 
- isCompressCsspublic boolean isCompressCss()Returns- trueif CSS compression is enabled.- Returns:
- current state of CSS compression.
 
- setCompressCsspublic void setCompressCss- (boolean compressCss) Enables CSS compression within <style> tags using Yahoo YUI Compressor if set to- true. Default is- falsefor 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. - Parameters:
- compressCss- set- trueto enable CSS compression. Default is- false
- See Also:
 
- isYuiJsNoMungepublic boolean isYuiJsNoMunge()Returns- trueif Yahoo YUI Compressor will only minify javascript without obfuscating local symbols. This corresponds to- --nomungecommand line option.- Returns:
- nomungeparameter value used for JavaScript compression.
- See Also:
 
- setYuiJsNoMungepublic void setYuiJsNoMunge- (boolean yuiJsNoMunge) Tells Yahoo YUI Compressor to only minify javascript without obfuscating local symbols. This corresponds to- --nomungecommand line option. This option has effect only if JavaScript compression is enabled. Default is- false.- Parameters:
- yuiJsNoMunge- set- trueto enable- nomungemode
- See Also:
 
- isYuiJsPreserveAllSemiColonspublic boolean isYuiJsPreserveAllSemiColons()Returns- trueif Yahoo YUI Compressor will preserve unnecessary semicolons during JavaScript compression. This corresponds to- --preserve-semicommand line option.- Returns:
- preserve-semiparameter value used for JavaScript compression.
- See Also:
 
- setYuiJsPreserveAllSemiColonspublic void setYuiJsPreserveAllSemiColons- (boolean yuiJsPreserveAllSemiColons) Tells Yahoo YUI Compressor to preserve unnecessary semicolons during JavaScript compression. This corresponds to- --preserve-semicommand line option. This option has effect only if JavaScript compression is enabled. Default is- false.- Parameters:
- yuiJsPreserveAllSemiColons- set- trueto enable- preserve-semimode
- See Also:
 
- isYuiJsDisableOptimizationspublic boolean isYuiJsDisableOptimizations()Returns- trueif Yahoo YUI Compressor will disable all the built-in micro optimizations during JavaScript compression. This corresponds to- --disable-optimizationscommand line option.- Returns:
- disable-optimizationsparameter value used for JavaScript compression.
- See Also:
 
- setYuiJsDisableOptimizationspublic void setYuiJsDisableOptimizations- (boolean yuiJsDisableOptimizations) Tells Yahoo YUI Compressor to disable all the built-in micro optimizations during JavaScript compression. This corresponds to- --disable-optimizationscommand line option. This option has effect only if JavaScript compression is enabled. Default is- false.- Parameters:
- yuiJsDisableOptimizations- set- trueto enable- disable-optimizationsmode
- See Also:
 
- getYuiJsLineBreakpublic int getYuiJsLineBreak()Returns number of symbols per line Yahoo YUI Compressor will use during JavaScript compression. This corresponds to- --line-breakcommand line option.- Returns:
- line-breakparameter value used for JavaScript compression.
- See Also:
 
- setYuiJsLineBreakpublic void setYuiJsLineBreak- (int yuiJsLineBreak) Tells Yahoo YUI Compressor to break lines after the specified number of symbols during JavaScript compression. This corresponds to- --line-breakcommand line option. This option has effect only if JavaScript compression is enabled. Default is- -1to disable line breaks.- Parameters:
- yuiJsLineBreak- set number of symbols per line
- See Also:
 
- getYuiCssLineBreakpublic int getYuiCssLineBreak()Returns number of symbols per line Yahoo YUI Compressor will use during CSS compression. This corresponds to- --line-breakcommand line option.- Returns:
- line-breakparameter value used for CSS compression.
- See Also:
 
- setYuiCssLineBreakpublic void setYuiCssLineBreak- (int yuiCssLineBreak) Tells Yahoo YUI Compressor to break lines after the specified number of symbols during CSS compression. This corresponds to- --line-breakcommand line option. This option has effect only if CSS compression is enabled. Default is- -1to disable line breaks.- Parameters:
- yuiCssLineBreak- set number of symbols per line
- See Also:
 
- isRemoveQuotespublic boolean isRemoveQuotes()Returns- trueif all unnecessary quotes will be removed from tag attributes.
- setRemoveQuotespublic void setRemoveQuotes- (boolean removeQuotes) If set to- trueall 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. - Parameters:
- removeQuotes- set- trueto remove unnecessary quotes from tag attributes
 
- isEnabledpublic boolean isEnabled()Returns- trueif compression is enabled.- Returns:
- trueif compression is enabled.
 
- setEnabledpublic void setEnabled- (boolean enabled) If set to- falseall compression will be bypassed. Might be useful for testing purposes. Default is- true.- Parameters:
- enabled- set- falseto bypass all compression
 
- isRemoveCommentspublic boolean isRemoveComments()Returns- trueif all HTML comments will be removed.- Returns:
- trueif all HTML comments will be removed
 
- setRemoveCommentspublic void setRemoveComments- (boolean removeComments) If set to- trueall HTML comments will be removed. Default is- true.- Parameters:
- removeComments- set- trueto remove all HTML comments
 
- isRemoveMultiSpacespublic boolean isRemoveMultiSpaces()Returns- trueif all multiple whitespace characters will be replaced with single spaces.- Returns:
- trueif all multiple whitespace characters will be replaced with single spaces.
 
- setRemoveMultiSpacespublic void setRemoveMultiSpaces- (boolean removeMultiSpaces) If set to- trueall multiple whitespace characters will be replaced with single spaces. Default is- true.- Parameters:
- removeMultiSpaces- set- trueto replace all multiple whitespace characters will single spaces.
 
- isRemoveIntertagSpacespublic boolean isRemoveIntertagSpaces()Returns- trueif all inter-tag whitespace characters will be removed.- Returns:
- trueif all inter-tag whitespace characters will be removed.
 
- setRemoveIntertagSpacespublic void setRemoveIntertagSpaces- (boolean removeIntertagSpaces) If set to- trueall 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.- Parameters:
- removeIntertagSpaces- set- trueto remove all inter-tag whitespace characters
 
- getPreservePatternsReturns a list of Patterns defining custom preserving block rules- Returns:
- list of Patternobjects defining rules for preserving block rules
 
- setPreservePatternsThis method allows setting custom block preservation rules defined by regular expression patterns. Blocks that match provided patterns will be skipped during HTML compression.- Custom 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.- Parameters:
- preservePatterns- List of- Patternobjects that will be used to skip matched blocks during compression
 
- getCssCompressorReturns CSS compressor implementation that will be used to compress inline CSS in HTML.- Returns:
- Compressorimplementation that will be used to compress inline CSS in HTML.
- See Also:
 
- setCssCompressorSets CSS compressor 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 - YuiCssCompressorwill be used by default.- Parameters:
- cssCompressor-- Compressorimplementation that will be used for inline CSS compression
- See Also:
 
- isSimpleDoctypepublic boolean isSimpleDoctype()Returns- trueif existing DOCTYPE declaration will be replaced with simple- <!DOCTYPE html>declaration.- Returns:
- trueif existing DOCTYPE declaration will be replaced with simple- <!DOCTYPE html>declaration.
 
- setSimpleDoctypepublic void setSimpleDoctype- (boolean simpleDoctype) If set to- true, existing DOCTYPE declaration will be replaced with simple- <!DOCTYPE html>declaration. Default is- false.- Parameters:
- simpleDoctype- set- trueto replace existing DOCTYPE declaration with- <!DOCTYPE html>
 
- isRemoveScriptAttributespublic boolean isRemoveScriptAttributes()Returns- trueif unnecessary attributes will be removed from- <script>tags- Returns:
- trueif unnecessary attributes will be removed from- <script>tags
 
- setRemoveScriptAttributespublic void setRemoveScriptAttributes- (boolean removeScriptAttributes) If set to- true, following attributes will be removed from- <script>tags:- type="text/javascript"
- type="application/javascript"
- language="javascript"
 - Default is - false.- Parameters:
- removeScriptAttributes- set- trueto remove unnecessary attributes from- <script>tags
 
- isRemoveStyleAttributespublic boolean isRemoveStyleAttributes()Returns- trueif- type="text/style"attributes will be removed from- <style>tags- Returns:
- trueif- type="text/style"attributes will be removed from- <style>tags
 
- setRemoveStyleAttributespublic void setRemoveStyleAttributes- (boolean removeStyleAttributes) If set to- true,- type="text/style"attributes will be removed from- <style>tags. Default is- false.- Parameters:
- removeStyleAttributes- set- trueto remove- type="text/style"attributes from- <style>tags
 
- isRemoveLinkAttributespublic boolean isRemoveLinkAttributes()Returns- trueif unnecessary attributes will be removed from- <link>tags- Returns:
- trueif unnecessary attributes will be removed from- <link>tags
 
- setRemoveLinkAttributespublic void setRemoveLinkAttributes- (boolean removeLinkAttributes) If set to- true, following attributes will be removed from- <link rel="stylesheet">and- <link rel="alternate stylesheet">tags:- type="text/css"
- type="text/plain"
 - Default is - false.- Parameters:
- removeLinkAttributes- set- trueto remove unnecessary attributes from- <link>tags
 
- isRemoveFormAttributespublic boolean isRemoveFormAttributes()Returns- trueif- method="get"attributes will be removed from- <form>tags- Returns:
- trueif- method="get"attributes will be removed from- <form>tags
 
- setRemoveFormAttributespublic void setRemoveFormAttributes- (boolean removeFormAttributes) If set to- true,- method="get"attributes will be removed from- <form>tags. Default is- false.- Parameters:
- removeFormAttributes- set- trueto remove- method="get"attributes from- <form>tags
 
- isRemoveInputAttributespublic boolean isRemoveInputAttributes()Returns- trueif- type="text"attributes will be removed from- <input>tags- Returns:
- trueif- type="text"attributes will be removed from- <input>tags
 
- setRemoveInputAttributespublic void setRemoveInputAttributes- (boolean removeInputAttributes) If set to- true,- type="text"attributes will be removed from- <input>tags. Default is- false.- Parameters:
- removeInputAttributes- set- trueto remove- type="text"attributes from- <input>tags
 
- isSimpleBooleanAttributespublic boolean isSimpleBooleanAttributes()Returns- trueif boolean attributes will be simplified- Returns:
- trueif boolean attributes will be simplified
 
- setSimpleBooleanAttributespublic void setSimpleBooleanAttributes- (boolean simpleBooleanAttributes) If set to- true, any values of following boolean attributes will be removed:- checked
- selected
- disabled
- readonly
 - For example, - <input readonly="readonly">would become- <input readonly>- Default is - false.- Parameters:
- simpleBooleanAttributes- set- trueto simplify boolean attributes
 
- isRemoveJavaScriptProtocolpublic boolean isRemoveJavaScriptProtocol()Returns- trueif- javascript:pseudo-protocol will be removed from inline event handlers.- Returns:
- trueif- javascript:pseudo-protocol will be removed from inline event handlers.
 
- setRemoveJavaScriptProtocolpublic void setRemoveJavaScriptProtocol- (boolean removeJavaScriptProtocol) If set to- 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.- Parameters:
- removeJavaScriptProtocol- set- trueto remove- javascript:pseudo-protocol from inline event handlers.
 
- isRemoveHttpProtocolpublic boolean isRemoveHttpProtocol()Returns- trueif- HTTPprotocol will be removed from- href,- src,- cite, and- actiontag attributes.- Returns:
- trueif- HTTPprotocol will be removed from- href,- src,- cite, and- actiontag attributes.
 
- setRemoveHttpProtocolpublic void setRemoveHttpProtocol- (boolean removeHttpProtocol) If set to- true,- HTTPprotocol will be removed from- href,- src,- cite, and- actiontag 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.- Parameters:
- removeHttpProtocol- set- trueto remove- HTTPprotocol from tag attributes
 
- isRemoveHttpsProtocolpublic boolean isRemoveHttpsProtocol()Returns- trueif- HTTPSprotocol will be removed from- href,- src,- cite, and- actiontag attributes.- Returns:
- trueif- HTTPSprotocol will be removed from- href,- src,- cite, and- actiontag attributes.
 
- setRemoveHttpsProtocolpublic void setRemoveHttpsProtocol- (boolean removeHttpsProtocol) If set to- true,- HTTPSprotocol will be removed from- href,- src,- cite, and- actiontag 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.- Parameters:
- removeHttpsProtocol- set- trueto remove- HTTPprotocol from tag attributes
 
- isGenerateStatisticspublic boolean isGenerateStatistics()Returns- trueif HTML compression statistics is generated- Returns:
- trueif HTML compression statistics is generated
 
- setGenerateStatisticspublic void setGenerateStatistics- (boolean generateStatistics) If set to- true, HTML compression statistics will be generated.- Important: Enabling statistics makes HTML compressor not thread safe. - Default is - false.- Parameters:
- generateStatistics- set- trueto generate HTML compression statistics
- See Also:
 
- getStatisticsReturns- HtmlCompressorStatisticsobject containing statistics of the last HTML compression, if enabled. Should be called after- compress(String)- Returns:
- HtmlCompressorStatisticsobject containing last HTML compression statistics
- See Also:
 
- isPreserveLineBreakspublic boolean isPreserveLineBreaks()Returns- trueif line breaks will be preserved.- Returns:
- trueif line breaks will be preserved.
 
- setPreserveLineBreakspublic void setPreserveLineBreaks- (boolean preserveLineBreaks) If set to- true, line breaks will be preserved.- Default is - false.- Parameters:
- preserveLineBreaks- set- trueto preserve line breaks
 
- getRemoveSurroundingSpacesReturns a comma separated list of tags around which spaces will be removed.- Returns:
- a comma separated list of tags around which spaces will be removed.
 
- setRemoveSurroundingSpacesEnables surrounding spaces removal around provided comma separated list of tags.- Besides custom defined lists, you can pass one of 3 predefined lists of tags: - BLOCK_TAGS_MIN,- BLOCK_TAGS_MAX,- ALL_TAGS.- Parameters:
- tagList- a comma separated list of tags around which spaces will be removed