public class HtmlCompressor extends Object implements 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.
Modifier and Type | Field and Description |
---|---|
static String |
ALL_TAGS
Could be passed to
setRemoveSurroundingSpaces method to remove all
surrounding spaces (not recommended). |
static String |
BLOCK_TAGS_MAX
Predefined list of tags that are block-level by default, excluding
<div> and
<li> tags. |
static String |
BLOCK_TAGS_MIN
Predefined list of tags that are very likely to be block-level.
|
protected static Pattern |
booleanAttrPattern
The Constant booleanAttrPattern.
|
protected static Pattern |
cdataPattern
The Constant cdataPattern.
|
protected static Pattern |
commentPattern
The Constant commentPattern.
|
protected static Pattern |
condCommentPattern
The Constant condCommentPattern.
|
protected static Pattern |
doctypePattern
The Constant doctypePattern.
|
protected static Pattern |
emptyPattern
The Constant emptyPattern.
|
protected static Pattern |
eventJsProtocolPattern
The Constant eventJsProtocolPattern.
|
protected static Pattern |
eventPattern1
The Constant eventPattern1 (unmasked).
|
protected static Pattern |
eventPattern2
The Constant eventPattern2.
|
protected static Pattern |
formMethodAttrPattern
The Constant formMethodAttrPattern.
|
protected static Pattern |
httpProtocolPattern
The Constant httpProtocolPattern.
|
protected static Pattern |
httpsProtocolPattern
The Constant httpsProtocolPattern.
|
protected static Pattern |
inputTypeAttrPattern
The Constant inputTypeAttrPattern.
|
protected static Pattern |
intertagPattern_CustomCustom
The Constant intertagPattern_CustomCustom.
|
protected static Pattern |
intertagPattern_CustomTag
The Constant intertagPattern_CustomTag.
|
protected static Pattern |
intertagPattern_TagCustom
The Constant intertagPattern_TagCustom.
|
protected static Pattern |
intertagPattern_TagTag
The Constant intertagPattern_TagTag.
|
static String |
JS_COMPRESSOR_CLOSURE
The Constant JS_COMPRESSOR_CLOSURE.
|
static String |
JS_COMPRESSOR_YUI
The Constant JS_COMPRESSOR_YUI.
|
protected static Pattern |
jsLangAttrPattern
The Constant jsLangAttrPattern.
|
protected static Pattern |
jsTypeAttrPattern
The Constant jsTypeAttrPattern.
|
protected static Pattern |
lineBreakPattern
The Constant lineBreakPattern.
|
protected static Pattern |
linkRelAttrPattern
The Constant linkRelAttrPattern.
|
protected static Pattern |
linkTypeAttrPattern
The Constant linkTypeAttrPattern.
|
protected static Pattern |
multispacePattern
The Constant multispacePattern.
|
static Pattern |
PHP_TAG_PATTERN
Predefined pattern that matches
<?php ... |
protected static Pattern |
prePattern
The Constant prePattern.
|
protected static Pattern |
relExternalPattern
The Constant relExternalPattern.
|
protected static Pattern |
scriptPattern
The Constant scriptPattern.
|
static Pattern |
SERVER_SCRIPT_TAG_PATTERN
Predefined pattern that matches
<% ... |
static Pattern |
SERVER_SIDE_INCLUDE_PATTERN
Predefined pattern that matches
<--# ... |
protected static Pattern |
skipPattern
The Constant skipPattern.
|
protected static Pattern |
stylePattern
The Constant stylePattern.
|
protected static Pattern |
styleTypeAttrPattern
The Constant styleTypeAttrPattern.
|
protected static Pattern |
surroundingSpacesAllPattern
The Constant surroundingSpacesAllPattern.
|
protected static Pattern |
surroundingSpacesMaxPattern
The Constant surroundingSpacesMaxPattern.
|
protected static Pattern |
surroundingSpacesMinPattern
The Constant surroundingSpacesMinPattern.
|
protected static Pattern |
tagEndSpacePattern
The Constant tagEndSpacePattern.
|
protected static Pattern |
tagLastUnquotedValuePattern
The Constant tagLastUnquotedValuePattern.
|
protected static Pattern |
tagPropertyPattern
The Constant tagPropertyPattern.
|
protected static Pattern |
tagQuotePattern
The Constant tagQuotePattern.
|
protected static Pattern |
taPattern
The Constant taPattern.
|
protected static String |
TEMP_COND_COMMENT_BLOCK
The Constant tempCondCommentBlock.
|
protected static String |
TEMP_EVENT_BLOCK
The Constant tempEventBlock.
|
protected static String |
TEMP_LINE_BREAK_BLOCK
The Constant tempLineBreakBlock.
|
protected static String |
TEMP_PRE_BLOCK
The Constant tempPreBlock.
|
protected static String |
TEMP_SCRIPT_BLOCK
The Constant tempScriptBlock.
|
protected static String |
TEMP_SKIP_BLOCK
The Constant tempSkipBlock.
|
protected static String |
TEMP_STYLE_BLOCK
The Constant tempStyleBlock.
|
protected static String |
TEMP_TEXT_AREA_BLOCK
The Constant tempTextAreaBlock.
|
protected static String |
TEMP_USER_BLOCK
The Constant tempUserBlock.
|
protected static Pattern |
tempCondCommentPattern
The Constant tempCondCommentPattern.
|
protected static Pattern |
tempEventPattern
The Constant tempEventPattern.
|
protected static Pattern |
tempLineBreakPattern
The Constant tempLineBreakPattern.
|
protected static Pattern |
tempPrePattern
The Constant tempPrePattern.
|
protected static Pattern |
tempScriptPattern
The Constant tempScriptPattern.
|
protected static Pattern |
tempSkipPattern
The Constant tempSkipPattern.
|
protected static Pattern |
tempStylePattern
The Constant tempStylePattern.
|
protected static Pattern |
tempTextAreaPattern
The Constant tempTextAreaPattern.
|
protected static Pattern |
typeAttrPattern
The Constant typeAttrPattern.
|
Constructor and Description |
---|
HtmlCompressor() |
Modifier and Type | Method and Description |
---|---|
String |
compress(String html)
The 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 |
createCompressorClone()
Creates the compressor clone.
|
protected void |
endStatistics(String html)
End statistics.
|
Compressor |
getCssCompressor()
Returns CSS compressor implementation that will be used to compress inline CSS in HTML.
|
Compressor |
getJavaScriptCompressor()
Returns JavaScript compressor implementation that will be used to compress inline JavaScript in HTML.
|
List<Pattern> |
getPreservePatterns()
Returns a list of Patterns defining custom preserving block rules.
|
String |
getRemoveSurroundingSpaces()
Returns a comma separated list of tags around which spaces will be removed.
|
HtmlCompressorStatistics |
getStatistics()
Returns
HtmlCompressorStatistics object containing statistics of the last HTML compression, if enabled. |
int |
getYuiCssLineBreak()
Returns number of symbols per line Yahoo YUI Compressor will use during CSS compression.
|
org.mozilla.javascript.ErrorReporter |
getYuiErrorReporter()
Returns
ErrorReporter used by YUI Compressor to log error messages during JavasSript compression. |
int |
getYuiJsLineBreak()
Returns number of symbols per line Yahoo YUI Compressor will use during JavaScript compression.
|
protected void |
initStatistics(String html)
Inits the statistics.
|
boolean |
isCompressCss()
Returns
true if CSS compression is enabled. |
boolean |
isCompressJavaScript()
Returns
true if JavaScript compression is enabled. |
boolean |
isEnabled()
Returns
true if compression is enabled. |
boolean |
isGenerateStatistics()
Returns
true if HTML compression statistics is generated. |
boolean |
isPreserveLineBreaks()
Returns
true if line breaks will be preserved. |
boolean |
isRemoveComments()
Returns
true if all HTML comments will be removed. |
boolean |
isRemoveFormAttributes()
Returns
true if method="get" attributes will be removed from <form>
tags. |
boolean |
isRemoveHttpProtocol()
Returns
true if HTTP protocol will be removed from href , src ,
cite , and action tag attributes. |
boolean |
isRemoveHttpsProtocol()
Returns
true if HTTPS protocol will be removed from href , src
, cite , and action tag attributes. |
boolean |
isRemoveInputAttributes()
Returns
true if type="text" attributes will be removed from <input>
tags. |
boolean |
isRemoveIntertagSpaces()
Returns
true if all inter-tag whitespace characters will be removed. |
boolean |
isRemoveJavaScriptProtocol()
Returns
true if javascript: pseudo-protocol will be removed from inline event handlers. |
boolean |
isRemoveLinkAttributes()
Returns
true if unnecessary attributes will be removed from <link> tags. |
boolean |
isRemoveMultiSpaces()
Returns
true if all multiple whitespace characters will be replaced with single spaces. |
boolean |
isRemoveQuotes()
Returns
true if all unnecessary quotes will be removed from tag attributes. |
boolean |
isRemoveScriptAttributes()
Returns
true if unnecessary attributes will be removed from <script> tags. |
boolean |
isRemoveStyleAttributes()
Returns
true if type="text/style" attributes will be removed from
<style> tags. |
boolean |
isSimpleBooleanAttributes()
Returns
true if boolean attributes will be simplified. |
boolean |
isSimpleDoctype()
Returns
true if existing DOCTYPE declaration will be replaced with simple
<!DOCTYPE html> declaration. |
boolean |
isYuiJsDisableOptimizations()
Returns
true if Yahoo YUI Compressor will disable all the built-in micro optimizations during
JavaScript compression. |
boolean |
isYuiJsNoMunge()
Returns
true if Yahoo YUI Compressor will only minify javascript without obfuscating local symbols. |
boolean |
isYuiJsPreserveAllSemiColons()
Returns
true 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 |
removeSurroundingSpaces(String html)
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 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
false all compression will be bypassed. |
void |
setGenerateStatistics(boolean generateStatistics)
If set to
true , 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 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
true all 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 , HTTP protocol will be removed from href , src ,
cite , and action tag attributes. |
void |
setRemoveHttpsProtocol(boolean removeHttpsProtocol)
If set to
true , HTTPS protocol will be removed from href , src
, cite , and action tag 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
true all 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
true all multiple whitespace characters will be replaced with single spaces. |
void |
setRemoveQuotes(boolean removeQuotes)
If set to
true all 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 |
setYuiErrorReporter(org.mozilla.javascript.ErrorReporter yuiErrorReporter)
Sets
ErrorReporter 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 |
simpleBooleanAttributes(String html)
Simple boolean attributes.
|
protected String |
simpleDoctype(String html)
Simple doctype.
|
public static final String JS_COMPRESSOR_YUI
public static final String JS_COMPRESSOR_CLOSURE
public static final Pattern PHP_TAG_PATTERN
<?php ... ?>
tags. Could be passed inside a list to
setPreservePatterns
method.public static final Pattern SERVER_SCRIPT_TAG_PATTERN
<% ... %>
tags. Could be passed inside a list to
setPreservePatterns
method.public static final Pattern SERVER_SIDE_INCLUDE_PATTERN
<--# ... -->
tags. Could be passed inside a list to
setPreservePatterns
method.public static final String BLOCK_TAGS_MIN
setRemoveSurroundingSpaces
method.public static final String BLOCK_TAGS_MAX
<div>
and
<li>
tags. Table tags are also included. Could be passed to
setRemoveSurroundingSpaces
method.public static final String ALL_TAGS
setRemoveSurroundingSpaces
method to remove all
surrounding spaces (not recommended).protected static final String TEMP_COND_COMMENT_BLOCK
protected static final String TEMP_PRE_BLOCK
protected static final String TEMP_TEXT_AREA_BLOCK
protected static final String TEMP_SCRIPT_BLOCK
protected static final String TEMP_STYLE_BLOCK
protected static final String TEMP_EVENT_BLOCK
protected static final String TEMP_LINE_BREAK_BLOCK
protected static final String TEMP_SKIP_BLOCK
protected static final String TEMP_USER_BLOCK
protected static final Pattern emptyPattern
protected static final Pattern skipPattern
protected static final Pattern condCommentPattern
protected static final Pattern commentPattern
protected static final Pattern intertagPattern_TagTag
protected static final Pattern intertagPattern_TagCustom
protected static final Pattern intertagPattern_CustomTag
protected static final Pattern intertagPattern_CustomCustom
protected static final Pattern multispacePattern
protected static final Pattern tagEndSpacePattern
protected static final Pattern tagLastUnquotedValuePattern
protected static final Pattern tagQuotePattern
protected static final Pattern prePattern
protected static final Pattern taPattern
protected static final Pattern scriptPattern
protected static final Pattern stylePattern
protected static final Pattern tagPropertyPattern
protected static final Pattern cdataPattern
protected static final Pattern doctypePattern
protected static final Pattern typeAttrPattern
protected static final Pattern jsTypeAttrPattern
protected static final Pattern jsLangAttrPattern
protected static final Pattern styleTypeAttrPattern
protected static final Pattern linkTypeAttrPattern
protected static final Pattern linkRelAttrPattern
protected static final Pattern formMethodAttrPattern
protected static final Pattern inputTypeAttrPattern
protected static final Pattern booleanAttrPattern
protected static final Pattern eventJsProtocolPattern
protected static final Pattern httpProtocolPattern
protected static final Pattern httpsProtocolPattern
protected static final Pattern relExternalPattern
protected static final Pattern eventPattern1
protected static final Pattern eventPattern2
protected static final Pattern lineBreakPattern
protected static final Pattern surroundingSpacesMinPattern
protected static final Pattern surroundingSpacesMaxPattern
protected static final Pattern surroundingSpacesAllPattern
protected static final Pattern tempCondCommentPattern
protected static final Pattern tempPrePattern
protected static final Pattern tempTextAreaPattern
protected static final Pattern tempScriptPattern
protected static final Pattern tempStylePattern
protected static final Pattern tempEventPattern
protected static final Pattern tempSkipPattern
protected static final Pattern tempLineBreakPattern
public String compress(String html)
compress
in interface Compressor
html
- HTML content to compressprotected void initStatistics(String html)
html
- the htmlprotected void endStatistics(String html)
html
- the htmlprotected 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)
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 blocksprotected 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)
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 blocksprotected String processHtml(String html)
html
- the htmlprotected String removeSurroundingSpaces(String html)
html
- the htmlprotected String removeQuotesInsideTags(String html)
html
- the htmlprotected String removeSpacesInsideTags(String html)
html
- the htmlprotected String removeMultiSpaces(String html)
html
- the htmlprotected String removeIntertagSpaces(String html)
html
- the htmlprotected String removeComments(String html)
html
- the htmlprotected String simpleDoctype(String html)
html
- the htmlprotected String removeScriptAttributes(String html)
html
- the htmlprotected String removeStyleAttributes(String html)
html
- the htmlprotected String removeLinkAttributes(String html)
html
- the htmlprotected String removeFormAttributes(String html)
html
- the htmlprotected String removeInputAttributes(String html)
html
- the htmlprotected String simpleBooleanAttributes(String html)
html
- the htmlprotected String removeHttpProtocol(String html)
html
- the htmlprotected String removeHttpsProtocol(String html)
html
- the htmlprotected 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)
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 blocksprotected void processPreBlocks(List<String> preBlocks)
preBlocks
- the pre blocksprotected void processTextAreaBlocks(List<String> taBlocks)
taBlocks
- the ta blocksprotected void processCondCommentBlocks(List<String> condCommentBlocks)
condCommentBlocks
- the cond comment blocksprotected void processSkipBlocks(List<String> skipBlocks)
skipBlocks
- the skip blocksprotected void processLineBreakBlocks(List<String> lineBreakBlocks)
lineBreakBlocks
- the line break blocksprotected void processUserBlocks(List<List<String>> userBlocks)
userBlocks
- the user blocksprotected void processEventBlocks(List<String> eventBlocks)
eventBlocks
- the event blocksprotected String removeJavaScriptProtocol(String source)
source
- the sourceprotected void processScriptBlocks(List<String> scriptBlocks)
scriptBlocks
- the script blocksprotected void processStyleBlocks(List<String> styleBlocks)
styleBlocks
- the style blocksprotected String compressJavaScript(String source)
source
- the sourceprotected String compressCssStyles(String source)
source
- the sourceprotected HtmlCompressor createCompressorClone()
public boolean isCompressJavaScript()
true
if JavaScript compression is enabled.public void setCompressJavaScript(boolean compressJavaScript)
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
public boolean isCompressCss()
true
if CSS compression is enabled.public void setCompressCss(boolean compressCss)
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
public boolean isYuiJsNoMunge()
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.public void setYuiJsNoMunge(boolean yuiJsNoMunge)
--nomunge
command line option. This option has effect only if JavaScript compression is enabled.
Default is false
.yuiJsNoMunge
- set true
to enable nomunge
modepublic boolean isYuiJsPreserveAllSemiColons()
true
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.public void setYuiJsPreserveAllSemiColons(boolean yuiJsPreserveAllSemiColons)
--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
modepublic boolean isYuiJsDisableOptimizations()
true
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.public void setYuiJsDisableOptimizations(boolean yuiJsDisableOptimizations)
--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
modepublic int getYuiJsLineBreak()
--line-break
command line option.line-break
parameter value used for JavaScript compression.public void setYuiJsLineBreak(int yuiJsLineBreak)
--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 linepublic int getYuiCssLineBreak()
--line-break
command line option.line-break
parameter value used for CSS compression.public void setYuiCssLineBreak(int yuiCssLineBreak)
--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 linepublic boolean isRemoveQuotes()
true
if all unnecessary quotes will be removed from tag attributes.public void setRemoveQuotes(boolean removeQuotes)
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 attributespublic boolean isEnabled()
true
if compression is enabled.true
if compression is enabled.public void setEnabled(boolean enabled)
false
all compression will be bypassed. Might be useful for testing purposes. Default is
true
.enabled
- set false
to bypass all compressionpublic boolean isRemoveComments()
true
if all HTML comments will be removed.true
if all HTML comments will be removedpublic void setRemoveComments(boolean removeComments)
true
all HTML comments will be removed. Default is true
.removeComments
- set true
to remove all HTML commentspublic boolean isRemoveMultiSpaces()
true
if all multiple whitespace characters will be replaced with single spaces.true
if all multiple whitespace characters will be replaced with single spaces.public void setRemoveMultiSpaces(boolean removeMultiSpaces)
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.public boolean isRemoveIntertagSpaces()
true
if all inter-tag whitespace characters will be removed.true
if all inter-tag whitespace characters will be removed.public void setRemoveIntertagSpaces(boolean removeIntertagSpaces)
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 characterspublic List<Pattern> getPreservePatterns()
Pattern
objects defining rules for preserving block rulespublic void setPreservePatterns(List<Pattern> preservePatterns)
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
.
preservePatterns
- List of Pattern
objects that will be used to skip matched blocks during compressionpublic org.mozilla.javascript.ErrorReporter getYuiErrorReporter()
ErrorReporter
used by YUI Compressor to log error messages during JavasSript compression.ErrorReporter
used by YUI Compressor to log error messages during JavasSript compressionpublic void setYuiErrorReporter(org.mozilla.javascript.ErrorReporter yuiErrorReporter)
ErrorReporter
that YUI Compressor will use for reporting errors during JavaScript compression.
If no ErrorReporter
was provided YuiJavaScriptCompressor.DefaultErrorReporter
will be used
which reports errors to System.err
stream.yuiErrorReporter
- ErrorReporter
that will be used by YUI CompressorYuiJavaScriptCompressor.DefaultErrorReporter
,
Yahoo YUI Compressor,
ErrorReporter
Interfacepublic Compressor getJavaScriptCompressor()
Compressor
implementation that will be used to compress inline JavaScript in HTML.YuiJavaScriptCompressor
,
ClosureJavaScriptCompressor
,
Yahoo YUI Compressor,
Google Closure Compilerpublic void setJavaScriptCompressor(Compressor javaScriptCompressor)
HtmlCompressor currently comes with basic implementations for
Yahoo YUI Compressor (called
YuiJavaScriptCompressor
) and Google Closure
Compiler (called ClosureJavaScriptCompressor
) 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.
javaScriptCompressor
- Compressor
implementation that will be used for inline JavaScript compressionYuiJavaScriptCompressor
,
ClosureJavaScriptCompressor
,
Yahoo YUI Compressor,
Google Closure Compilerpublic Compressor getCssCompressor()
Compressor
implementation that will be used to compress inline CSS in HTML.YuiCssCompressor
,
Yahoo YUI Compressorpublic void setCssCompressor(Compressor cssCompressor)
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 compressionYuiCssCompressor
,
Yahoo YUI Compressorpublic boolean isSimpleDoctype()
true
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.public void setSimpleDoctype(boolean simpleDoctype)
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>
public boolean isRemoveScriptAttributes()
true
if unnecessary attributes will be removed from <script>
tags.true
if unnecessary attributes will be removed from <script>
tagspublic void setRemoveScriptAttributes(boolean removeScriptAttributes)
true
, following attributes will be removed from <script>
tags:
Default is false
.
removeScriptAttributes
- set true
to remove unnecessary attributes from <script>
tagspublic boolean isRemoveStyleAttributes()
true
if type="text/style"
attributes will be removed from
<style>
tags.true
if type="text/style"
attributes will be removed from
<style>
tagspublic void setRemoveStyleAttributes(boolean removeStyleAttributes)
true
, type="text/style"
attributes will be removed from
<style>
tags. Default is false
.removeStyleAttributes
- set true
to remove type="text/style"
attributes from
<style>
tagspublic boolean isRemoveLinkAttributes()
true
if unnecessary attributes will be removed from <link>
tags.true
if unnecessary attributes will be removed from <link>
tagspublic void setRemoveLinkAttributes(boolean removeLinkAttributes)
true
, 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>
tagspublic boolean isRemoveFormAttributes()
true
if method="get"
attributes will be removed from <form>
tags.true
if method="get"
attributes will be removed from <form>
tagspublic void setRemoveFormAttributes(boolean removeFormAttributes)
true
, method="get"
attributes will be removed from <form>
tags. Default is false
.removeFormAttributes
- set true
to remove method="get"
attributes from <form>
tagspublic boolean isRemoveInputAttributes()
true
if type="text"
attributes will be removed from <input>
tags.true
if type="text"
attributes will be removed from <input>
tagspublic void setRemoveInputAttributes(boolean removeInputAttributes)
true
, type="text"
attributes will be removed from <input>
tags. Default is false
.removeInputAttributes
- set true
to remove type="text"
attributes from <input>
tagspublic boolean isSimpleBooleanAttributes()
true
if boolean attributes will be simplified.true
if boolean attributes will be simplifiedpublic void setSimpleBooleanAttributes(boolean simpleBooleanAttributes)
true
, 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 attributespublic boolean isRemoveJavaScriptProtocol()
true
if javascript:
pseudo-protocol will be removed from inline event handlers.true
if javascript:
pseudo-protocol will be removed from inline event handlers.public void setRemoveJavaScriptProtocol(boolean removeJavaScriptProtocol)
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.public boolean isRemoveHttpProtocol()
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.public void setRemoveHttpProtocol(boolean removeHttpProtocol)
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 attributespublic boolean isRemoveHttpsProtocol()
true
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.public void setRemoveHttpsProtocol(boolean removeHttpsProtocol)
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 attributespublic boolean isGenerateStatistics()
true
if HTML compression statistics is generated.true
if HTML compression statistics is generatedpublic void setGenerateStatistics(boolean generateStatistics)
true
, 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 statisticsgetStatistics()
public HtmlCompressorStatistics getStatistics()
HtmlCompressorStatistics
object containing statistics of the last HTML compression, if enabled.
Should be called after compress(String)
HtmlCompressorStatistics
object containing last HTML compression statisticsHtmlCompressorStatistics
,
setGenerateStatistics(boolean)
public boolean isPreserveLineBreaks()
true
if line breaks will be preserved.true
if line breaks will be preserved.public void setPreserveLineBreaks(boolean preserveLineBreaks)
true
, line breaks will be preserved.
Default is false
.
preserveLineBreaks
- set true
to preserve line breakspublic String getRemoveSurroundingSpaces()
public void setRemoveSurroundingSpaces(String tagList)
Besides 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 removedCopyright © 2009–2020 Jeremy Landis. All rights reserved.