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 Summary
FieldsModifier and TypeFieldDescriptionstatic final String
Could be passed tosetRemoveSurroundingSpaces
method to remove all surrounding spaces (not recommended).static final String
Predefined list of tags that are block-level by default, excluding<div>
and<li>
tags.static final String
Predefined list of tags that are very likely to be block-level.protected static final Pattern
The Constant booleanAttrPattern.protected static final Pattern
The Constant cdataPattern.protected static final Pattern
The Constant commentPattern.protected static final Pattern
The Constant condCommentPattern.protected static final Pattern
The Constant doctypePattern.protected static final Pattern
The Constant emptyPattern.protected static final Pattern
The Constant eventJsProtocolPattern.protected static final Pattern
The Constant eventPattern1 (unmasked).protected static final Pattern
The Constant eventPattern2.protected static final Pattern
The Constant formMethodAttrPattern.protected static final Pattern
The Constant httpProtocolPattern.protected static final Pattern
The Constant httpsProtocolPattern.protected static final Pattern
The Constant inputTypeAttrPattern.protected static final Pattern
The Constant intertagPattern_CustomCustom.protected static final Pattern
The Constant intertagPattern_CustomTag.protected static final Pattern
The Constant intertagPattern_TagCustom.protected static final Pattern
The Constant intertagPattern_TagTag.static final String
The Constant JS_COMPRESSOR_CLOSURE.static final String
The Constant JS_COMPRESSOR_YUI.protected static final Pattern
The Constant jsLangAttrPattern.protected static final Pattern
The Constant jsTypeAttrPattern.protected static final Pattern
The Constant lineBreakPattern.protected static final Pattern
The Constant linkRelAttrPattern.protected static final Pattern
The Constant linkTypeAttrPattern.protected static final Pattern
The Constant multispacePattern.static final Pattern
Predefined pattern that matches<?php ...
protected static final Pattern
The Constant prePattern.protected static final Pattern
The Constant relExternalPattern.protected static final Pattern
The Constant scriptPattern.static final Pattern
Predefined pattern that matches<% ...
static final Pattern
Predefined pattern that matches<--# ...
protected static final Pattern
The Constant skipPattern.protected static final Pattern
The Constant stylePattern.protected static final Pattern
The Constant styleTypeAttrPattern.protected static final Pattern
The Constant surroundingSpacesAllPattern.protected static final Pattern
The Constant surroundingSpacesMaxPattern.protected static final Pattern
The Constant surroundingSpacesMinPattern.protected static final Pattern
The Constant tagEndSpacePattern.protected static final Pattern
The Constant tagLastUnquotedValuePattern.protected static final Pattern
The Constant tagPropertyPattern.protected static final Pattern
The Constant tagQuotePattern.protected static final Pattern
The Constant taPattern.protected static final String
The Constant tempCondCommentBlock.protected static final String
The Constant tempEventBlock.protected static final String
The Constant tempLineBreakBlock.protected static final String
The Constant tempPreBlock.protected static final String
The Constant tempScriptBlock.protected static final String
The Constant tempSkipBlock.protected static final String
The Constant tempStyleBlock.protected static final String
The Constant tempTextAreaBlock.protected static final String
The Constant tempUserBlock.protected static final Pattern
The Constant tempCondCommentPattern.protected static final Pattern
The Constant tempEventPattern.protected static final Pattern
The Constant tempLineBreakPattern.protected static final Pattern
The Constant tempPrePattern.protected static final Pattern
The Constant tempScriptPattern.protected static final Pattern
The Constant tempSkipPattern.protected static final Pattern
The Constant tempStylePattern.protected static final Pattern
The Constant tempTextAreaPattern.protected static final Pattern
The Constant typeAttrPattern. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionThe main method that compresses given HTML source and returns compressed result.protected String
compressCssStyles
(String source) Compress css styles.protected String
compressJavaScript
(String source) Compress java script.protected HtmlCompressor
Creates the compressor clone.protected void
endStatistics
(String html) End statistics.Returns CSS compressor implementation that will be used to compress inline CSS in HTML.Returns JavaScript compressor implementation that will be used to compress inline JavaScript in HTML.Returns a list of Patterns defining custom preserving block rules.Returns a comma separated list of tags around which spaces will be removed.ReturnsHtmlCompressorStatistics
object containing statistics of the last HTML compression, if enabled.int
Returns number of symbols per line Yahoo YUI Compressor will use during CSS compression.org.mozilla.javascript.ErrorReporter
ReturnsErrorReporter
used by YUI Compressor to log error messages during JavasSript compression.int
Returns number of symbols per line Yahoo YUI Compressor will use during JavaScript compression.protected void
initStatistics
(String html) Inits the statistics.boolean
Returnstrue
if CSS compression is enabled.boolean
Returnstrue
if JavaScript compression is enabled.boolean
Returnstrue
if compression is enabled.boolean
Returnstrue
if HTML compression statistics is generated.boolean
Returnstrue
if line breaks will be preserved.boolean
Returnstrue
if all HTML comments will be removed.boolean
Returnstrue
ifmethod="get"
attributes will be removed from<form>
tags.boolean
Returnstrue
ifHTTP
protocol will be removed fromhref
,src
,cite
, andaction
tag attributes.boolean
Returnstrue
ifHTTPS
protocol will be removed fromhref
,src
,cite
, andaction
tag attributes.boolean
Returnstrue
iftype="text"
attributes will be removed from<input>
tags.boolean
Returnstrue
if all inter-tag whitespace characters will be removed.boolean
Returnstrue
ifjavascript:
pseudo-protocol will be removed from inline event handlers.boolean
Returnstrue
if unnecessary attributes will be removed from<link>
tags.boolean
Returnstrue
if all multiple whitespace characters will be replaced with single spaces.boolean
Returnstrue
if all unnecessary quotes will be removed from tag attributes.boolean
Returnstrue
if unnecessary attributes will be removed from<script>
tags.boolean
Returnstrue
iftype="text/style"
attributes will be removed from<style>
tags.boolean
Returnstrue
if boolean attributes will be simplified.boolean
Returnstrue
if existing DOCTYPE declaration will be replaced with simple<!DOCTYPE html>
declaration.boolean
Returnstrue
if Yahoo YUI Compressor will disable all the built-in micro optimizations during JavaScript compression.boolean
Returnstrue
if Yahoo YUI Compressor will only minify javascript without obfuscating local symbols.boolean
Returnstrue
if Yahoo YUI Compressor will preserve unnecessary semicolons during JavaScript compression.protected String
preserveBlocks
(String html, List<String> preBlocks, List<String> taBlocks, List<String> scriptBlocks, List<String> styleBlocks, List<String> eventBlocks, List<String> condCommentBlocks, List<String> skipBlocks, List<String> lineBreakBlocks, List<List<String>> userBlocks) Preserve blocks.protected void
processCondCommentBlocks
(List<String> condCommentBlocks) Process cond comment blocks.protected void
processEventBlocks
(List<String> eventBlocks) Process event blocks.protected String
processHtml
(String html) Process html.protected void
processLineBreakBlocks
(List<String> lineBreakBlocks) Process line break blocks.protected void
processPreBlocks
(List<String> preBlocks) Process pre blocks.protected void
processPreservedBlocks
(List<String> preBlocks, List<String> taBlocks, List<String> scriptBlocks, List<String> styleBlocks, List<String> eventBlocks, List<String> condCommentBlocks, List<String> skipBlocks, List<String> lineBreakBlocks, List<List<String>> userBlocks) Process preserved blocks.protected void
processScriptBlocks
(List<String> scriptBlocks) Process script blocks.protected void
processSkipBlocks
(List<String> skipBlocks) Process skip blocks.protected void
processStyleBlocks
(List<String> styleBlocks) Process style blocks.protected void
processTextAreaBlocks
(List<String> taBlocks) Process text area blocks.protected void
processUserBlocks
(List<List<String>> userBlocks) Process user blocks.protected String
removeComments
(String html) Removes the comments.protected String
removeFormAttributes
(String html) Removes the form attributes.protected String
removeHttpProtocol
(String html) Removes the http protocol.protected String
removeHttpsProtocol
(String html) Removes the https protocol.protected String
removeInputAttributes
(String html) Removes the input attributes.protected String
removeIntertagSpaces
(String html) Removes the intertag spaces.protected String
removeJavaScriptProtocol
(String source) Removes the java script protocol.protected String
removeLinkAttributes
(String html) Removes the link attributes.protected String
removeMultiSpaces
(String html) Removes the multi spaces.protected String
removeQuotesInsideTags
(String html) Removes the quotes inside tags.protected String
removeScriptAttributes
(String html) Removes the script attributes.protected String
removeSpacesInsideTags
(String html) Removes the spaces inside tags.protected String
removeStyleAttributes
(String html) Removes the style attributes.protected String
Removes the surrounding spaces.protected String
returnBlocks
(String html, List<String> preBlocks, List<String> taBlocks, List<String> scriptBlocks, List<String> styleBlocks, List<String> eventBlocks, List<String> condCommentBlocks, List<String> skipBlocks, List<String> lineBreakBlocks, List<List<String>> userBlocks) Return blocks.void
setCompressCss
(boolean compressCss) Enables CSS compression within <style> tags using Yahoo YUI Compressor if set totrue
.void
setCompressJavaScript
(boolean compressJavaScript) Enables JavaScript compression within <script> tags using Yahoo YUI Compressor if set totrue
.void
setCssCompressor
(Compressor cssCompressor) Sets CSS compressor implementation that will be used to compress inline CSS in HTML.void
setEnabled
(boolean enabled) If set tofalse
all compression will be bypassed.void
setGenerateStatistics
(boolean generateStatistics) If set totrue
, HTML compression statistics will be generated.void
setJavaScriptCompressor
(Compressor javaScriptCompressor) Sets JavaScript compressor implementation that will be used to compress inline JavaScript in HTML.void
setPreserveLineBreaks
(boolean preserveLineBreaks) If set totrue
, 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 totrue
all HTML comments will be removed.void
setRemoveFormAttributes
(boolean removeFormAttributes) If set totrue
,method="get"
attributes will be removed from<form>
tags.void
setRemoveHttpProtocol
(boolean removeHttpProtocol) If set totrue
,HTTP
protocol will be removed fromhref
,src
,cite
, andaction
tag attributes.void
setRemoveHttpsProtocol
(boolean removeHttpsProtocol) If set totrue
,HTTPS
protocol will be removed fromhref
,src
,cite
, andaction
tag attributes.void
setRemoveInputAttributes
(boolean removeInputAttributes) If set totrue
,type="text"
attributes will be removed from<input>
tags.void
setRemoveIntertagSpaces
(boolean removeIntertagSpaces) If set totrue
all inter-tag whitespace characters will be removed.void
setRemoveJavaScriptProtocol
(boolean removeJavaScriptProtocol) If set totrue
,javascript:
pseudo-protocol will be removed from inline event handlers.void
setRemoveLinkAttributes
(boolean removeLinkAttributes) If set totrue
, 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 totrue
all multiple whitespace characters will be replaced with single spaces.void
setRemoveQuotes
(boolean removeQuotes) If set totrue
all unnecessary quotes will be removed from tag attributes.void
setRemoveScriptAttributes
(boolean removeScriptAttributes) If set totrue
, following attributes will be removed from<script>
tags: type="text/javascript" type="application/javascript" language="javascript"void
setRemoveStyleAttributes
(boolean removeStyleAttributes) If set totrue
,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 totrue
, any values of following boolean attributes will be removed: checked selected disabled readonlyvoid
setSimpleDoctype
(boolean simpleDoctype) If set totrue
, 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
setYuiErrorReporter
(org.mozilla.javascript.ErrorReporter yuiErrorReporter) SetsErrorReporter
that YUI Compressor will use for reporting errors during JavaScript 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.protected String
Simple boolean attributes.protected String
simpleDoctype
(String html) Simple doctype.
-
Field Details
-
JS_COMPRESSOR_YUI
The Constant JS_COMPRESSOR_YUI.- See Also:
-
JS_COMPRESSOR_CLOSURE
The Constant JS_COMPRESSOR_CLOSURE.- See Also:
-
PHP_TAG_PATTERN
Predefined pattern that matches<?php ... ?>
tags. Could be passed inside a list tosetPreservePatterns
method. -
SERVER_SCRIPT_TAG_PATTERN
Predefined pattern that matches<% ... %>
tags. Could be passed inside a list tosetPreservePatterns
method. -
SERVER_SIDE_INCLUDE_PATTERN
Predefined pattern that matches<--# ... -->
tags. Could be passed inside a list tosetPreservePatterns
method. -
BLOCK_TAGS_MIN
Predefined list of tags that are very likely to be block-level. Could be passed tosetRemoveSurroundingSpaces
method.- See Also:
-
BLOCK_TAGS_MAX
Predefined list of tags that are block-level by default, excluding<div>
and<li>
tags. Table tags are also included. Could be passed tosetRemoveSurroundingSpaces
method.- See Also:
-
ALL_TAGS
Could be passed tosetRemoveSurroundingSpaces
method to remove all surrounding spaces (not recommended).- See Also:
-
TEMP_COND_COMMENT_BLOCK
The Constant tempCondCommentBlock.- See Also:
-
TEMP_PRE_BLOCK
The Constant tempPreBlock.- See Also:
-
TEMP_TEXT_AREA_BLOCK
The Constant tempTextAreaBlock.- See Also:
-
TEMP_SCRIPT_BLOCK
The Constant tempScriptBlock.- See Also:
-
TEMP_STYLE_BLOCK
The Constant tempStyleBlock.- See Also:
-
TEMP_EVENT_BLOCK
The Constant tempEventBlock.- See Also:
-
TEMP_LINE_BREAK_BLOCK
The Constant tempLineBreakBlock.- See Also:
-
TEMP_SKIP_BLOCK
The Constant tempSkipBlock.- See Also:
-
TEMP_USER_BLOCK
The Constant tempUserBlock.- See Also:
-
emptyPattern
The Constant emptyPattern. -
skipPattern
The Constant skipPattern. -
condCommentPattern
The Constant condCommentPattern. -
commentPattern
The Constant commentPattern. -
intertagPattern_TagTag
The Constant intertagPattern_TagTag. -
intertagPattern_TagCustom
The Constant intertagPattern_TagCustom. -
intertagPattern_CustomTag
The Constant intertagPattern_CustomTag. -
intertagPattern_CustomCustom
The Constant intertagPattern_CustomCustom. -
multispacePattern
The Constant multispacePattern. -
tagEndSpacePattern
The Constant tagEndSpacePattern. -
tagLastUnquotedValuePattern
The Constant tagLastUnquotedValuePattern. -
tagQuotePattern
The Constant tagQuotePattern. -
prePattern
The Constant prePattern. -
taPattern
The Constant taPattern. -
scriptPattern
The Constant scriptPattern. -
stylePattern
The Constant stylePattern. -
tagPropertyPattern
The Constant tagPropertyPattern. -
cdataPattern
The Constant cdataPattern. -
doctypePattern
The Constant doctypePattern. -
typeAttrPattern
The Constant typeAttrPattern. -
jsTypeAttrPattern
The Constant jsTypeAttrPattern. -
jsLangAttrPattern
The Constant jsLangAttrPattern. -
styleTypeAttrPattern
The Constant styleTypeAttrPattern. -
linkTypeAttrPattern
The Constant linkTypeAttrPattern. -
linkRelAttrPattern
The Constant linkRelAttrPattern. -
formMethodAttrPattern
The Constant formMethodAttrPattern. -
inputTypeAttrPattern
The Constant inputTypeAttrPattern. -
booleanAttrPattern
The Constant booleanAttrPattern. -
eventJsProtocolPattern
The Constant eventJsProtocolPattern. -
httpProtocolPattern
The Constant httpProtocolPattern. -
httpsProtocolPattern
The Constant httpsProtocolPattern. -
relExternalPattern
The Constant relExternalPattern. -
eventPattern1
The Constant eventPattern1 (unmasked). -
eventPattern2
The Constant eventPattern2. -
lineBreakPattern
The Constant lineBreakPattern. -
surroundingSpacesMinPattern
The Constant surroundingSpacesMinPattern. -
surroundingSpacesMaxPattern
The Constant surroundingSpacesMaxPattern. -
surroundingSpacesAllPattern
The Constant surroundingSpacesAllPattern. -
tempCondCommentPattern
The Constant tempCondCommentPattern. -
tempPrePattern
The Constant tempPrePattern. -
tempTextAreaPattern
The Constant tempTextAreaPattern. -
tempScriptPattern
The Constant tempScriptPattern. -
tempStylePattern
The Constant tempStylePattern. -
tempEventPattern
The Constant tempEventPattern. -
tempSkipPattern
The Constant tempSkipPattern. -
tempLineBreakPattern
The Constant tempLineBreakPattern.
-
-
Constructor Details
-
HtmlCompressor
public HtmlCompressor()
-
-
Method Details
-
compress
The main method that compresses given HTML source and returns compressed result.- Specified by:
compress
in interfaceCompressor
- Parameters:
html
- HTML content to compress- Returns:
- compressed content.
-
initStatistics
Inits the statistics.- Parameters:
html
- the html
-
endStatistics
End statistics.- Parameters:
html
- the html
-
preserveBlocks
protected String preserveBlocks(String html, List<String> preBlocks, List<String> taBlocks, List<String> scriptBlocks, List<String> styleBlocks, List<String> eventBlocks, List<String> condCommentBlocks, List<String> skipBlocks, List<String> lineBreakBlocks, List<List<String>> userBlocks) Preserve blocks.- Parameters:
html
- the htmlpreBlocks
- the pre blockstaBlocks
- the ta blocksscriptBlocks
- the script blocksstyleBlocks
- the style blockseventBlocks
- the event blockscondCommentBlocks
- the cond comment blocksskipBlocks
- the skip blockslineBreakBlocks
- the line break blocksuserBlocks
- the user blocks- Returns:
- the string
-
returnBlocks
protected String returnBlocks(String html, List<String> preBlocks, List<String> taBlocks, List<String> scriptBlocks, List<String> styleBlocks, List<String> eventBlocks, List<String> condCommentBlocks, List<String> skipBlocks, List<String> lineBreakBlocks, List<List<String>> userBlocks) Return blocks.- Parameters:
html
- the htmlpreBlocks
- the pre blockstaBlocks
- the ta blocksscriptBlocks
- the script blocksstyleBlocks
- the style blockseventBlocks
- the event blockscondCommentBlocks
- the cond comment blocksskipBlocks
- the skip blockslineBreakBlocks
- the line break blocksuserBlocks
- the user blocks- Returns:
- the string
-
processHtml
Process html.- Parameters:
html
- the html- Returns:
- the string
-
removeSurroundingSpaces
Removes the surrounding spaces.- Parameters:
html
- the html- Returns:
- the string
-
removeQuotesInsideTags
Removes the quotes inside tags.- Parameters:
html
- the html- Returns:
- the string
-
removeSpacesInsideTags
Removes the spaces inside tags.- Parameters:
html
- the html- Returns:
- the string
-
removeMultiSpaces
Removes the multi spaces.- Parameters:
html
- the html- Returns:
- the string
-
removeIntertagSpaces
Removes the intertag spaces.- Parameters:
html
- the html- Returns:
- the string
-
removeComments
Removes the comments.- Parameters:
html
- the html- Returns:
- the string
-
simpleDoctype
Simple doctype.- Parameters:
html
- the html- Returns:
- the string
-
removeScriptAttributes
Removes the script attributes.- Parameters:
html
- the html- Returns:
- the string
-
removeStyleAttributes
Removes the style attributes.- Parameters:
html
- the html- Returns:
- the string
-
removeLinkAttributes
Removes the link attributes.- Parameters:
html
- the html- Returns:
- the string
-
removeFormAttributes
Removes the form attributes.- Parameters:
html
- the html- Returns:
- the string
-
removeInputAttributes
Removes the input attributes.- Parameters:
html
- the html- Returns:
- the string
-
simpleBooleanAttributes
Simple boolean attributes.- Parameters:
html
- the html- Returns:
- the string
-
removeHttpProtocol
Removes the http protocol.- Parameters:
html
- the html- Returns:
- the string
-
removeHttpsProtocol
Removes the https protocol.- Parameters:
html
- the html- Returns:
- the string
-
processPreservedBlocks
protected void processPreservedBlocks(List<String> preBlocks, List<String> taBlocks, List<String> scriptBlocks, List<String> styleBlocks, List<String> eventBlocks, List<String> condCommentBlocks, List<String> skipBlocks, List<String> lineBreakBlocks, List<List<String>> userBlocks) Process preserved blocks.- Parameters:
preBlocks
- the pre blockstaBlocks
- the ta blocksscriptBlocks
- the script blocksstyleBlocks
- the style blockseventBlocks
- the event blockscondCommentBlocks
- the cond comment blocksskipBlocks
- the skip blockslineBreakBlocks
- the line break blocksuserBlocks
- the user blocks
-
processPreBlocks
Process pre blocks.- Parameters:
preBlocks
- the pre blocks
-
processTextAreaBlocks
Process text area blocks.- Parameters:
taBlocks
- the ta blocks
-
processCondCommentBlocks
Process cond comment blocks.- Parameters:
condCommentBlocks
- the cond comment blocks
-
processSkipBlocks
Process skip blocks.- Parameters:
skipBlocks
- the skip blocks
-
processLineBreakBlocks
Process line break blocks.- Parameters:
lineBreakBlocks
- the line break blocks
-
processUserBlocks
Process user blocks.- Parameters:
userBlocks
- the user blocks
-
processEventBlocks
Process event blocks.- Parameters:
eventBlocks
- the event blocks
-
removeJavaScriptProtocol
Removes the java script protocol.- Parameters:
source
- the source- Returns:
- the string
-
processScriptBlocks
Process script blocks.- Parameters:
scriptBlocks
- the script blocks
-
processStyleBlocks
Process style blocks.- Parameters:
styleBlocks
- the style blocks
-
compressJavaScript
Compress java script.- Parameters:
source
- the source- Returns:
- the string
-
compressCssStyles
Compress css styles.- Parameters:
source
- the source- Returns:
- the string
-
createCompressorClone
Creates the compressor clone.- Returns:
- the html compressor
-
isCompressJavaScript
public boolean isCompressJavaScript()Returnstrue
if JavaScript compression is enabled.- Returns:
- current state of JavaScript compression.
-
setCompressJavaScript
public void setCompressJavaScript(boolean compressJavaScript) Enables JavaScript compression within <script> tags using Yahoo YUI Compressor if set totrue
. Default isfalse
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.
- Parameters:
compressJavaScript
- settrue
to enable JavaScript compression. Default isfalse
- See Also:
-
isCompressCss
public boolean isCompressCss()Returnstrue
if CSS compression is enabled.- Returns:
- current state of CSS compression.
-
setCompressCss
public void setCompressCss(boolean compressCss) Enables CSS compression within <style> tags using Yahoo YUI Compressor if set totrue
. Default isfalse
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.
- Parameters:
compressCss
- settrue
to enable CSS compression. Default isfalse
- See Also:
-
isYuiJsNoMunge
public boolean isYuiJsNoMunge()Returnstrue
if Yahoo YUI Compressor will only minify javascript without obfuscating local symbols. This corresponds to--nomunge
command line option.- Returns:
nomunge
parameter value used for JavaScript compression.- See Also:
-
setYuiJsNoMunge
public void setYuiJsNoMunge(boolean yuiJsNoMunge) Tells Yahoo YUI Compressor to only minify javascript without obfuscating local symbols. This corresponds to--nomunge
command line option. This option has effect only if JavaScript compression is enabled. Default isfalse
.- Parameters:
yuiJsNoMunge
- settrue
to enablenomunge
mode- See Also:
-
isYuiJsPreserveAllSemiColons
public boolean isYuiJsPreserveAllSemiColons()Returnstrue
if Yahoo YUI Compressor will preserve unnecessary semicolons during JavaScript compression. This corresponds to--preserve-semi
command line option.- Returns:
preserve-semi
parameter value used for JavaScript compression.- See Also:
-
setYuiJsPreserveAllSemiColons
public void setYuiJsPreserveAllSemiColons(boolean yuiJsPreserveAllSemiColons) Tells Yahoo YUI Compressor to preserve unnecessary semicolons during JavaScript compression. This corresponds to--preserve-semi
command line option. This option has effect only if JavaScript compression is enabled. Default isfalse
.- Parameters:
yuiJsPreserveAllSemiColons
- settrue
to enablepreserve-semi
mode- See Also:
-
isYuiJsDisableOptimizations
public boolean isYuiJsDisableOptimizations()Returnstrue
if Yahoo YUI Compressor will disable all the built-in micro optimizations during JavaScript compression. This corresponds to--disable-optimizations
command line option.- Returns:
disable-optimizations
parameter value used for JavaScript compression.- See Also:
-
setYuiJsDisableOptimizations
public void setYuiJsDisableOptimizations(boolean yuiJsDisableOptimizations) Tells Yahoo YUI Compressor to disable all the built-in micro optimizations during JavaScript compression. This corresponds to--disable-optimizations
command line option. This option has effect only if JavaScript compression is enabled. Default isfalse
.- Parameters:
yuiJsDisableOptimizations
- settrue
to enabledisable-optimizations
mode- See Also:
-
getYuiJsLineBreak
public int getYuiJsLineBreak()Returns number of symbols per line Yahoo YUI Compressor will use during JavaScript compression. This corresponds to--line-break
command line option.- Returns:
line-break
parameter value used for JavaScript compression.- See Also:
-
setYuiJsLineBreak
public void setYuiJsLineBreak(int yuiJsLineBreak) Tells Yahoo YUI Compressor to break lines after the specified number of symbols during JavaScript compression. This corresponds to--line-break
command line option. This option has effect only if JavaScript compression is enabled. Default is-1
to disable line breaks.- Parameters:
yuiJsLineBreak
- set number of symbols per line- See Also:
-
getYuiCssLineBreak
public int getYuiCssLineBreak()Returns number of symbols per line Yahoo YUI Compressor will use during CSS compression. This corresponds to--line-break
command line option.- Returns:
line-break
parameter value used for CSS compression.- See Also:
-
setYuiCssLineBreak
public void setYuiCssLineBreak(int yuiCssLineBreak) Tells Yahoo YUI Compressor to break lines after the specified number of symbols during CSS compression. This corresponds to--line-break
command line option. This option has effect only if CSS compression is enabled. Default is-1
to disable line breaks.- Parameters:
yuiCssLineBreak
- set number of symbols per line- See Also:
-
isRemoveQuotes
public boolean isRemoveQuotes()Returnstrue
if all unnecessary quotes will be removed from tag attributes.- Returns:
- true, if is removes the quotes
-
setRemoveQuotes
public void setRemoveQuotes(boolean removeQuotes) If set totrue
all unnecessary quotes will be removed from tag attributes. Default isfalse
.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
- settrue
to remove unnecessary quotes from tag attributes
-
isEnabled
public boolean isEnabled()Returnstrue
if compression is enabled.- Returns:
true
if compression is enabled.
-
setEnabled
public void setEnabled(boolean enabled) If set tofalse
all compression will be bypassed. Might be useful for testing purposes. Default istrue
.- Parameters:
enabled
- setfalse
to bypass all compression
-
isRemoveComments
public boolean isRemoveComments()Returnstrue
if all HTML comments will be removed.- Returns:
true
if all HTML comments will be removed
-
setRemoveComments
public void setRemoveComments(boolean removeComments) If set totrue
all HTML comments will be removed. Default istrue
.- Parameters:
removeComments
- settrue
to remove all HTML comments
-
isRemoveMultiSpaces
public boolean isRemoveMultiSpaces()Returnstrue
if all multiple whitespace characters will be replaced with single spaces.- Returns:
true
if all multiple whitespace characters will be replaced with single spaces.
-
setRemoveMultiSpaces
public void setRemoveMultiSpaces(boolean removeMultiSpaces) If set totrue
all multiple whitespace characters will be replaced with single spaces. Default istrue
.- Parameters:
removeMultiSpaces
- settrue
to replace all multiple whitespace characters will single spaces.
-
isRemoveIntertagSpaces
public boolean isRemoveIntertagSpaces()Returnstrue
if all inter-tag whitespace characters will be removed.- Returns:
true
if all inter-tag whitespace characters will be removed.
-
setRemoveIntertagSpaces
public void setRemoveIntertagSpaces(boolean removeIntertagSpaces) If set totrue
all inter-tag whitespace characters will be removed. Default isfalse
.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
- settrue
to remove all inter-tag whitespace characters
-
getPreservePatterns
Returns a list of Patterns defining custom preserving block rules.- Returns:
- list of
Pattern
objects defining rules for preserving block rules
-
setPreservePatterns
This 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 ofPattern
objects that will be used to skip matched blocks during compression
-
getYuiErrorReporter
public org.mozilla.javascript.ErrorReporter getYuiErrorReporter()ReturnsErrorReporter
used by YUI Compressor to log error messages during JavasSript compression.- Returns:
ErrorReporter
used by YUI Compressor to log error messages during JavasSript compression- See Also:
-
setYuiErrorReporter
public void setYuiErrorReporter(org.mozilla.javascript.ErrorReporter yuiErrorReporter) SetsErrorReporter
that YUI Compressor will use for reporting errors during JavaScript compression. If noErrorReporter
was providedYuiJavaScriptCompressor.DefaultErrorReporter
will be used which reports errors toSystem.err
stream.- Parameters:
yuiErrorReporter
-ErrorReporter
that will be used by YUI Compressor- See Also:
-
getJavaScriptCompressor
Returns JavaScript compressor implementation that will be used to compress inline JavaScript in HTML.- Returns:
Compressor
implementation that will be used to compress inline JavaScript in HTML.- See Also:
-
setJavaScriptCompressor
Sets JavaScript compressor implementation that will be used to compress inline JavaScript in HTML.HtmlCompressor currently comes with basic implementations for Yahoo YUI Compressor (called
YuiJavaScriptCompressor
) and Google Closure Compiler (calledClosureJavaScriptCompressor
) that should be enough for most cases, but users can also create their own JavaScript compressors for custom needs.If no compressor is set
YuiJavaScriptCompressor
will be used by default.- Parameters:
javaScriptCompressor
-Compressor
implementation that will be used for inline JavaScript compression- See Also:
-
getCssCompressor
Returns CSS compressor implementation that will be used to compress inline CSS in HTML.- Returns:
Compressor
implementation that will be used to compress inline CSS in HTML.- See Also:
-
setCssCompressor
Sets 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
YuiCssCompressor
will be used by default.- Parameters:
cssCompressor
-Compressor
implementation that will be used for inline CSS compression- See Also:
-
isSimpleDoctype
public boolean isSimpleDoctype()Returnstrue
if existing DOCTYPE declaration will be replaced with simple<!DOCTYPE html>
declaration.- Returns:
true
if existing DOCTYPE declaration will be replaced with simple<!DOCTYPE html>
declaration.
-
setSimpleDoctype
public void setSimpleDoctype(boolean simpleDoctype) If set totrue
, existing DOCTYPE declaration will be replaced with simple<!DOCTYPE html>
declaration. Default isfalse
.- Parameters:
simpleDoctype
- settrue
to replace existing DOCTYPE declaration with<!DOCTYPE html>
-
isRemoveScriptAttributes
public boolean isRemoveScriptAttributes()Returnstrue
if unnecessary attributes will be removed from<script>
tags.- Returns:
true
if unnecessary attributes will be removed from<script>
tags
-
setRemoveScriptAttributes
public void setRemoveScriptAttributes(boolean removeScriptAttributes) If set totrue
, following attributes will be removed from<script>
tags:- type="text/javascript"
- type="application/javascript"
- language="javascript"
Default is
false
.- Parameters:
removeScriptAttributes
- settrue
to remove unnecessary attributes from<script>
tags
-
isRemoveStyleAttributes
public boolean isRemoveStyleAttributes()Returnstrue
iftype="text/style"
attributes will be removed from<style>
tags.- Returns:
true
iftype="text/style"
attributes will be removed from<style>
tags
-
setRemoveStyleAttributes
public void setRemoveStyleAttributes(boolean removeStyleAttributes) If set totrue
,type="text/style"
attributes will be removed from<style>
tags. Default isfalse
.- Parameters:
removeStyleAttributes
- settrue
to removetype="text/style"
attributes from<style>
tags
-
isRemoveLinkAttributes
public boolean isRemoveLinkAttributes()Returnstrue
if unnecessary attributes will be removed from<link>
tags.- Returns:
true
if unnecessary attributes will be removed from<link>
tags
-
setRemoveLinkAttributes
public void setRemoveLinkAttributes(boolean removeLinkAttributes) If set totrue
, 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
- settrue
to remove unnecessary attributes from<link>
tags
-
isRemoveFormAttributes
public boolean isRemoveFormAttributes()Returnstrue
ifmethod="get"
attributes will be removed from<form>
tags.- Returns:
true
ifmethod="get"
attributes will be removed from<form>
tags
-
setRemoveFormAttributes
public void setRemoveFormAttributes(boolean removeFormAttributes) If set totrue
,method="get"
attributes will be removed from<form>
tags. Default isfalse
.- Parameters:
removeFormAttributes
- settrue
to removemethod="get"
attributes from<form>
tags
-
isRemoveInputAttributes
public boolean isRemoveInputAttributes()Returnstrue
iftype="text"
attributes will be removed from<input>
tags.- Returns:
true
iftype="text"
attributes will be removed from<input>
tags
-
setRemoveInputAttributes
public void setRemoveInputAttributes(boolean removeInputAttributes) If set totrue
,type="text"
attributes will be removed from<input>
tags. Default isfalse
.- Parameters:
removeInputAttributes
- settrue
to removetype="text"
attributes from<input>
tags
-
isSimpleBooleanAttributes
public boolean isSimpleBooleanAttributes()Returnstrue
if boolean attributes will be simplified.- Returns:
true
if boolean attributes will be simplified
-
setSimpleBooleanAttributes
public void setSimpleBooleanAttributes(boolean simpleBooleanAttributes) If set totrue
, 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
- settrue
to simplify boolean attributes
-
isRemoveJavaScriptProtocol
public boolean isRemoveJavaScriptProtocol()Returnstrue
ifjavascript:
pseudo-protocol will be removed from inline event handlers.- Returns:
true
ifjavascript:
pseudo-protocol will be removed from inline event handlers.
-
setRemoveJavaScriptProtocol
public void setRemoveJavaScriptProtocol(boolean removeJavaScriptProtocol) If set totrue
,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
- settrue
to removejavascript:
pseudo-protocol from inline event handlers.
-
isRemoveHttpProtocol
public boolean isRemoveHttpProtocol()Returnstrue
ifHTTP
protocol will be removed fromhref
,src
,cite
, andaction
tag attributes.- Returns:
true
ifHTTP
protocol will be removed fromhref
,src
,cite
, andaction
tag attributes.
-
setRemoveHttpProtocol
public void setRemoveHttpProtocol(boolean removeHttpProtocol) If set totrue
,HTTP
protocol will be removed fromhref
,src
,cite
, andaction
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
.- Parameters:
removeHttpProtocol
- settrue
to removeHTTP
protocol from tag attributes
-
isRemoveHttpsProtocol
public boolean isRemoveHttpsProtocol()Returnstrue
ifHTTPS
protocol will be removed fromhref
,src
,cite
, andaction
tag attributes.- Returns:
true
ifHTTPS
protocol will be removed fromhref
,src
,cite
, andaction
tag attributes.
-
setRemoveHttpsProtocol
public void setRemoveHttpsProtocol(boolean removeHttpsProtocol) If set totrue
,HTTPS
protocol will be removed fromhref
,src
,cite
, andaction
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
.- Parameters:
removeHttpsProtocol
- settrue
to removeHTTP
protocol from tag attributes
-
isGenerateStatistics
public boolean isGenerateStatistics()Returnstrue
if HTML compression statistics is generated.- Returns:
true
if HTML compression statistics is generated
-
setGenerateStatistics
public void setGenerateStatistics(boolean generateStatistics) If set totrue
, HTML compression statistics will be generated.Important: Enabling statistics makes HTML compressor not thread safe.
Default is
false
.- Parameters:
generateStatistics
- settrue
to generate HTML compression statistics- See Also:
-
getStatistics
ReturnsHtmlCompressorStatistics
object containing statistics of the last HTML compression, if enabled. Should be called aftercompress(String)
- Returns:
HtmlCompressorStatistics
object containing last HTML compression statistics- See Also:
-
isPreserveLineBreaks
public boolean isPreserveLineBreaks()Returnstrue
if line breaks will be preserved.- Returns:
true
if line breaks will be preserved.
-
setPreserveLineBreaks
public void setPreserveLineBreaks(boolean preserveLineBreaks) If set totrue
, line breaks will be preserved.Default is
false
.- Parameters:
preserveLineBreaks
- settrue
to preserve line breaks
-
getRemoveSurroundingSpaces
Returns 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.
-
setRemoveSurroundingSpaces
Enables 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
-