Class HtmlCompressor

  • All Implemented Interfaces:
    Compressor

    public class HtmlCompressor
    extends java.lang.Object
    implements Compressor
    Class that compresses given HTML source by removing comments, extra spaces and line breaks while preserving content within <pre>, <textarea>, <script> and <style> tags.

    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
    • Constructor Summary

      Constructors 
      Constructor Description
      HtmlCompressor()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String compress​(java.lang.String html)
      The main method that compresses given HTML source and returns compressed result.
      protected java.lang.String compressCssStyles​(java.lang.String source)  
      protected java.lang.String compressJavaScript​(java.lang.String source)  
      protected HtmlCompressor createCompressorClone()  
      protected void endStatistics​(java.lang.String html)  
      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.
      java.util.List<java.util.regex.Pattern> getPreservePatterns()
      Returns a list of Patterns defining custom preserving block rules
      java.lang.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​(java.lang.String html)  
      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 java.lang.String preserveBlocks​(java.lang.String html, java.util.List<java.lang.String> preBlocks, java.util.List<java.lang.String> taBlocks, java.util.List<java.lang.String> scriptBlocks, java.util.List<java.lang.String> styleBlocks, java.util.List<java.lang.String> eventBlocks, java.util.List<java.lang.String> condCommentBlocks, java.util.List<java.lang.String> skipBlocks, java.util.List<java.lang.String> lineBreakBlocks, java.util.List<java.util.List<java.lang.String>> userBlocks)  
      protected void processCondCommentBlocks​(java.util.List<java.lang.String> condCommentBlocks)  
      protected void processEventBlocks​(java.util.List<java.lang.String> eventBlocks)  
      protected java.lang.String processHtml​(java.lang.String html)  
      protected void processLineBreakBlocks​(java.util.List<java.lang.String> lineBreakBlocks)  
      protected void processPreBlocks​(java.util.List<java.lang.String> preBlocks)  
      protected void processPreservedBlocks​(java.util.List<java.lang.String> preBlocks, java.util.List<java.lang.String> taBlocks, java.util.List<java.lang.String> scriptBlocks, java.util.List<java.lang.String> styleBlocks, java.util.List<java.lang.String> eventBlocks, java.util.List<java.lang.String> condCommentBlocks, java.util.List<java.lang.String> skipBlocks, java.util.List<java.lang.String> lineBreakBlocks, java.util.List<java.util.List<java.lang.String>> userBlocks)  
      protected void processScriptBlocks​(java.util.List<java.lang.String> scriptBlocks)  
      protected void processSkipBlocks​(java.util.List<java.lang.String> skipBlocks)  
      protected void processStyleBlocks​(java.util.List<java.lang.String> styleBlocks)  
      protected void processTextAreaBlocks​(java.util.List<java.lang.String> taBlocks)  
      protected void processUserBlocks​(java.util.List<java.util.List<java.lang.String>> userBlocks)  
      protected java.lang.String removeComments​(java.lang.String html)  
      protected java.lang.String removeFormAttributes​(java.lang.String html)  
      protected java.lang.String removeHttpProtocol​(java.lang.String html)  
      protected java.lang.String removeHttpsProtocol​(java.lang.String html)  
      protected java.lang.String removeInputAttributes​(java.lang.String html)  
      protected java.lang.String removeIntertagSpaces​(java.lang.String html)  
      protected java.lang.String removeJavaScriptProtocol​(java.lang.String source)  
      protected java.lang.String removeLinkAttributes​(java.lang.String html)  
      protected java.lang.String removeMultiSpaces​(java.lang.String html)  
      protected java.lang.String removeQuotesInsideTags​(java.lang.String html)  
      protected java.lang.String removeScriptAttributes​(java.lang.String html)  
      protected java.lang.String removeSpacesInsideTags​(java.lang.String html)  
      protected java.lang.String removeStyleAttributes​(java.lang.String html)  
      protected java.lang.String removeSurroundingSpaces​(java.lang.String html)  
      protected java.lang.String returnBlocks​(java.lang.String html, java.util.List<java.lang.String> preBlocks, java.util.List<java.lang.String> taBlocks, java.util.List<java.lang.String> scriptBlocks, java.util.List<java.lang.String> styleBlocks, java.util.List<java.lang.String> eventBlocks, java.util.List<java.lang.String> condCommentBlocks, java.util.List<java.lang.String> skipBlocks, java.util.List<java.lang.String> lineBreakBlocks, java.util.List<java.util.List<java.lang.String>> userBlocks)  
      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​(java.util.List<java.util.regex.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​(java.lang.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 java.lang.String simpleBooleanAttributes​(java.lang.String html)  
      protected java.lang.String simpleDoctype​(java.lang.String html)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • JS_COMPRESSOR_YUI

        public static final java.lang.String JS_COMPRESSOR_YUI
        See Also:
        Constant Field Values
      • JS_COMPRESSOR_CLOSURE

        public static final java.lang.String JS_COMPRESSOR_CLOSURE
        See Also:
        Constant Field Values
      • PHP_TAG_PATTERN

        public static final java.util.regex.Pattern PHP_TAG_PATTERN
        Predefined pattern that matches <?php ... ?> tags. Could be passed inside a list to setPreservePatterns method.
      • SERVER_SCRIPT_TAG_PATTERN

        public static final java.util.regex.Pattern SERVER_SCRIPT_TAG_PATTERN
        Predefined pattern that matches <% ... %> tags. Could be passed inside a list to setPreservePatterns method.
      • SERVER_SIDE_INCLUDE_PATTERN

        public static final java.util.regex.Pattern SERVER_SIDE_INCLUDE_PATTERN
        Predefined pattern that matches <--# ... --> tags. Could be passed inside a list to setPreservePatterns method.
      • BLOCK_TAGS_MAX

        public static final java.lang.String 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 to setRemoveSurroundingSpaces method.
        See Also:
        Constant Field Values
      • tempCondCommentBlock

        protected static final java.lang.String tempCondCommentBlock
        See Also:
        Constant Field Values
      • tempTextAreaBlock

        protected static final java.lang.String tempTextAreaBlock
        See Also:
        Constant Field Values
      • tempScriptBlock

        protected static final java.lang.String tempScriptBlock
        See Also:
        Constant Field Values
      • tempLineBreakBlock

        protected static final java.lang.String tempLineBreakBlock
        See Also:
        Constant Field Values
      • emptyPattern

        protected static final java.util.regex.Pattern emptyPattern
      • skipPattern

        protected static final java.util.regex.Pattern skipPattern
      • condCommentPattern

        protected static final java.util.regex.Pattern condCommentPattern
      • commentPattern

        protected static final java.util.regex.Pattern commentPattern
      • intertagPattern_TagTag

        protected static final java.util.regex.Pattern intertagPattern_TagTag
      • intertagPattern_TagCustom

        protected static final java.util.regex.Pattern intertagPattern_TagCustom
      • intertagPattern_CustomTag

        protected static final java.util.regex.Pattern intertagPattern_CustomTag
      • intertagPattern_CustomCustom

        protected static final java.util.regex.Pattern intertagPattern_CustomCustom
      • multispacePattern

        protected static final java.util.regex.Pattern multispacePattern
      • tagEndSpacePattern

        protected static final java.util.regex.Pattern tagEndSpacePattern
      • tagLastUnquotedValuePattern

        protected static final java.util.regex.Pattern tagLastUnquotedValuePattern
      • tagQuotePattern

        protected static final java.util.regex.Pattern tagQuotePattern
      • prePattern

        protected static final java.util.regex.Pattern prePattern
      • taPattern

        protected static final java.util.regex.Pattern taPattern
      • scriptPattern

        protected static final java.util.regex.Pattern scriptPattern
      • stylePattern

        protected static final java.util.regex.Pattern stylePattern
      • tagPropertyPattern

        protected static final java.util.regex.Pattern tagPropertyPattern
      • cdataPattern

        protected static final java.util.regex.Pattern cdataPattern
      • doctypePattern

        protected static final java.util.regex.Pattern doctypePattern
      • typeAttrPattern

        protected static final java.util.regex.Pattern typeAttrPattern
      • jsTypeAttrPattern

        protected static final java.util.regex.Pattern jsTypeAttrPattern
      • jsLangAttrPattern

        protected static final java.util.regex.Pattern jsLangAttrPattern
      • styleTypeAttrPattern

        protected static final java.util.regex.Pattern styleTypeAttrPattern
      • linkTypeAttrPattern

        protected static final java.util.regex.Pattern linkTypeAttrPattern
      • linkRelAttrPattern

        protected static final java.util.regex.Pattern linkRelAttrPattern
      • formMethodAttrPattern

        protected static final java.util.regex.Pattern formMethodAttrPattern
      • inputTypeAttrPattern

        protected static final java.util.regex.Pattern inputTypeAttrPattern
      • booleanAttrPattern

        protected static final java.util.regex.Pattern booleanAttrPattern
      • eventJsProtocolPattern

        protected static final java.util.regex.Pattern eventJsProtocolPattern
      • httpProtocolPattern

        protected static final java.util.regex.Pattern httpProtocolPattern
      • httpsProtocolPattern

        protected static final java.util.regex.Pattern httpsProtocolPattern
      • relExternalPattern

        protected static final java.util.regex.Pattern relExternalPattern
      • eventPattern1

        protected static final java.util.regex.Pattern eventPattern1
      • eventPattern2

        protected static final java.util.regex.Pattern eventPattern2
      • lineBreakPattern

        protected static final java.util.regex.Pattern lineBreakPattern
      • surroundingSpacesMinPattern

        protected static final java.util.regex.Pattern surroundingSpacesMinPattern
      • surroundingSpacesMaxPattern

        protected static final java.util.regex.Pattern surroundingSpacesMaxPattern
      • surroundingSpacesAllPattern

        protected static final java.util.regex.Pattern surroundingSpacesAllPattern
      • tempCondCommentPattern

        protected static final java.util.regex.Pattern tempCondCommentPattern
      • tempPrePattern

        protected static final java.util.regex.Pattern tempPrePattern
      • tempTextAreaPattern

        protected static final java.util.regex.Pattern tempTextAreaPattern
      • tempScriptPattern

        protected static final java.util.regex.Pattern tempScriptPattern
      • tempStylePattern

        protected static final java.util.regex.Pattern tempStylePattern
      • tempEventPattern

        protected static final java.util.regex.Pattern tempEventPattern
      • tempSkipPattern

        protected static final java.util.regex.Pattern tempSkipPattern
      • tempLineBreakPattern

        protected static final java.util.regex.Pattern tempLineBreakPattern
    • Constructor Detail

      • HtmlCompressor

        public HtmlCompressor()
    • Method Detail

      • compress

        public java.lang.String compress​(java.lang.String html)
        The main method that compresses given HTML source and returns compressed result.
        Specified by:
        compress in interface Compressor
        Parameters:
        html - HTML content to compress
        Returns:
        compressed content.
      • initStatistics

        protected void initStatistics​(java.lang.String html)
      • endStatistics

        protected void endStatistics​(java.lang.String html)
      • preserveBlocks

        protected java.lang.String preserveBlocks​(java.lang.String html,
                                                  java.util.List<java.lang.String> preBlocks,
                                                  java.util.List<java.lang.String> taBlocks,
                                                  java.util.List<java.lang.String> scriptBlocks,
                                                  java.util.List<java.lang.String> styleBlocks,
                                                  java.util.List<java.lang.String> eventBlocks,
                                                  java.util.List<java.lang.String> condCommentBlocks,
                                                  java.util.List<java.lang.String> skipBlocks,
                                                  java.util.List<java.lang.String> lineBreakBlocks,
                                                  java.util.List<java.util.List<java.lang.String>> userBlocks)
      • returnBlocks

        protected java.lang.String returnBlocks​(java.lang.String html,
                                                java.util.List<java.lang.String> preBlocks,
                                                java.util.List<java.lang.String> taBlocks,
                                                java.util.List<java.lang.String> scriptBlocks,
                                                java.util.List<java.lang.String> styleBlocks,
                                                java.util.List<java.lang.String> eventBlocks,
                                                java.util.List<java.lang.String> condCommentBlocks,
                                                java.util.List<java.lang.String> skipBlocks,
                                                java.util.List<java.lang.String> lineBreakBlocks,
                                                java.util.List<java.util.List<java.lang.String>> userBlocks)
      • processHtml

        protected java.lang.String processHtml​(java.lang.String html)
      • removeSurroundingSpaces

        protected java.lang.String removeSurroundingSpaces​(java.lang.String html)
      • removeQuotesInsideTags

        protected java.lang.String removeQuotesInsideTags​(java.lang.String html)
      • removeSpacesInsideTags

        protected java.lang.String removeSpacesInsideTags​(java.lang.String html)
      • removeMultiSpaces

        protected java.lang.String removeMultiSpaces​(java.lang.String html)
      • removeIntertagSpaces

        protected java.lang.String removeIntertagSpaces​(java.lang.String html)
      • removeComments

        protected java.lang.String removeComments​(java.lang.String html)
      • simpleDoctype

        protected java.lang.String simpleDoctype​(java.lang.String html)
      • removeScriptAttributes

        protected java.lang.String removeScriptAttributes​(java.lang.String html)
      • removeStyleAttributes

        protected java.lang.String removeStyleAttributes​(java.lang.String html)
      • removeLinkAttributes

        protected java.lang.String removeLinkAttributes​(java.lang.String html)
      • removeFormAttributes

        protected java.lang.String removeFormAttributes​(java.lang.String html)
      • removeInputAttributes

        protected java.lang.String removeInputAttributes​(java.lang.String html)
      • simpleBooleanAttributes

        protected java.lang.String simpleBooleanAttributes​(java.lang.String html)
      • removeHttpProtocol

        protected java.lang.String removeHttpProtocol​(java.lang.String html)
      • removeHttpsProtocol

        protected java.lang.String removeHttpsProtocol​(java.lang.String html)
      • processPreservedBlocks

        protected void processPreservedBlocks​(java.util.List<java.lang.String> preBlocks,
                                              java.util.List<java.lang.String> taBlocks,
                                              java.util.List<java.lang.String> scriptBlocks,
                                              java.util.List<java.lang.String> styleBlocks,
                                              java.util.List<java.lang.String> eventBlocks,
                                              java.util.List<java.lang.String> condCommentBlocks,
                                              java.util.List<java.lang.String> skipBlocks,
                                              java.util.List<java.lang.String> lineBreakBlocks,
                                              java.util.List<java.util.List<java.lang.String>> userBlocks)
      • processPreBlocks

        protected void processPreBlocks​(java.util.List<java.lang.String> preBlocks)
      • processTextAreaBlocks

        protected void processTextAreaBlocks​(java.util.List<java.lang.String> taBlocks)
      • processCondCommentBlocks

        protected void processCondCommentBlocks​(java.util.List<java.lang.String> condCommentBlocks)
      • processSkipBlocks

        protected void processSkipBlocks​(java.util.List<java.lang.String> skipBlocks)
      • processLineBreakBlocks

        protected void processLineBreakBlocks​(java.util.List<java.lang.String> lineBreakBlocks)
      • processUserBlocks

        protected void processUserBlocks​(java.util.List<java.util.List<java.lang.String>> userBlocks)
      • processEventBlocks

        protected void processEventBlocks​(java.util.List<java.lang.String> eventBlocks)
      • removeJavaScriptProtocol

        protected java.lang.String removeJavaScriptProtocol​(java.lang.String source)
      • processScriptBlocks

        protected void processScriptBlocks​(java.util.List<java.lang.String> scriptBlocks)
      • processStyleBlocks

        protected void processStyleBlocks​(java.util.List<java.lang.String> styleBlocks)
      • compressJavaScript

        protected java.lang.String compressJavaScript​(java.lang.String source)
      • compressCssStyles

        protected java.lang.String compressCssStyles​(java.lang.String source)
      • createCompressorClone

        protected HtmlCompressor createCompressorClone()
      • isCompressJavaScript

        public boolean isCompressJavaScript()
        Returns true 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 to 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.

        Parameters:
        compressJavaScript - set true to enable JavaScript compression. Default is false
        See Also:
        Yahoo YUI Compressor
      • isCompressCss

        public boolean isCompressCss()
        Returns true 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 to 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.

        Parameters:
        compressCss - set true to enable CSS compression. Default is false
        See Also:
        Yahoo YUI Compressor
      • isYuiJsNoMunge

        public boolean isYuiJsNoMunge()
        Returns true 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:
        Yahoo YUI Compressor
      • 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 is false.
        Parameters:
        yuiJsNoMunge - set true to enable nomunge mode
        See Also:
        Yahoo YUI Compressor
      • isYuiJsPreserveAllSemiColons

        public boolean isYuiJsPreserveAllSemiColons()
        Returns true 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:
        Yahoo YUI Compressor
      • 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 is false.
        Parameters:
        yuiJsPreserveAllSemiColons - set true to enable preserve-semi mode
        See Also:
        Yahoo YUI Compressor
      • isYuiJsDisableOptimizations

        public boolean isYuiJsDisableOptimizations()
        Returns true 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:
        Yahoo YUI Compressor
      • 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 is false.
        Parameters:
        yuiJsDisableOptimizations - set true to enable disable-optimizations mode
        See Also:
        Yahoo YUI Compressor
      • 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:
        Yahoo YUI Compressor
      • 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:
        Yahoo YUI Compressor
      • 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:
        Yahoo YUI Compressor
      • 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:
        Yahoo YUI Compressor
      • isRemoveQuotes

        public boolean isRemoveQuotes()
        Returns true if all unnecessary quotes will be removed from tag attributes.
      • setRemoveQuotes

        public void setRemoveQuotes​(boolean removeQuotes)
        If set to 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.

        Parameters:
        removeQuotes - set true to remove unnecessary quotes from tag attributes
      • isEnabled

        public boolean isEnabled()
        Returns true if compression is enabled.
        Returns:
        true if compression is enabled.
      • setEnabled

        public void setEnabled​(boolean enabled)
        If set to false all compression will be bypassed. Might be useful for testing purposes. Default is true.
        Parameters:
        enabled - set false to bypass all compression
      • isRemoveComments

        public boolean isRemoveComments()
        Returns true if all HTML comments will be removed.
        Returns:
        true if all HTML comments will be removed
      • setRemoveComments

        public void setRemoveComments​(boolean removeComments)
        If set to true all HTML comments will be removed. Default is true.
        Parameters:
        removeComments - set true to remove all HTML comments
      • isRemoveMultiSpaces

        public boolean isRemoveMultiSpaces()
        Returns true 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 to true all multiple whitespace characters will be replaced with single spaces. Default is true.
        Parameters:
        removeMultiSpaces - set true to replace all multiple whitespace characters will single spaces.
      • isRemoveIntertagSpaces

        public boolean isRemoveIntertagSpaces()
        Returns true 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 to 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 &nbsp;. This option has no performance impact.

        Parameters:
        removeIntertagSpaces - set true to remove all inter-tag whitespace characters
      • getPreservePatterns

        public java.util.List<java.util.regex.Pattern> getPreservePatterns()
        Returns a list of Patterns defining custom preserving block rules
        Returns:
        list of Pattern objects defining rules for preserving block rules
      • setPreservePatterns

        public void setPreservePatterns​(java.util.List<java.util.regex.Pattern> preservePatterns)
        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 of Pattern objects that will be used to skip matched blocks during compression
      • getYuiErrorReporter

        public org.mozilla.javascript.ErrorReporter getYuiErrorReporter()
        Returns ErrorReporter 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:
        Yahoo YUI Compressor, Error Reporter Interface
      • getJavaScriptCompressor

        public Compressor 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:
        Yahoo YUI Compressor, Google Closure Compiler
      • getCssCompressor

        public Compressor 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:
        YuiCssCompressor, Yahoo YUI Compressor
      • setCssCompressor

        public void setCssCompressor​(Compressor cssCompressor)
        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:
        YuiCssCompressor, Yahoo YUI Compressor
      • isSimpleDoctype

        public boolean isSimpleDoctype()
        Returns true 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 to true, existing DOCTYPE declaration will be replaced with simple <!DOCTYPE html> declaration. Default is false.
        Parameters:
        simpleDoctype - set true to replace existing DOCTYPE declaration with <!DOCTYPE html>
      • isRemoveScriptAttributes

        public boolean isRemoveScriptAttributes()
        Returns true 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 to true, following attributes will be removed from <script> tags:
        • type="text/javascript"
        • type="application/javascript"
        • language="javascript"

        Default is false.

        Parameters:
        removeScriptAttributes - set true to remove unnecessary attributes from <script> tags
      • isRemoveStyleAttributes

        public boolean isRemoveStyleAttributes()
        Returns true if type="text/style" attributes will be removed from <style> tags
        Returns:
        true if type="text/style" attributes will be removed from <style> tags
      • setRemoveStyleAttributes

        public void setRemoveStyleAttributes​(boolean removeStyleAttributes)
        If set to true, type="text/style" attributes will be removed from <style> tags. Default is false.
        Parameters:
        removeStyleAttributes - set true to remove type="text/style" attributes from <style> tags
      • isRemoveLinkAttributes

        public boolean isRemoveLinkAttributes()
        Returns true 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 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 true to remove unnecessary attributes from <link> tags
      • isRemoveFormAttributes

        public boolean isRemoveFormAttributes()
        Returns true if method="get" attributes will be removed from <form> tags
        Returns:
        true if method="get" attributes will be removed from <form> tags
      • setRemoveFormAttributes

        public void setRemoveFormAttributes​(boolean removeFormAttributes)
        If set to true, method="get" attributes will be removed from <form> tags. Default is false.
        Parameters:
        removeFormAttributes - set true to remove method="get" attributes from <form> tags
      • isRemoveInputAttributes

        public boolean isRemoveInputAttributes()
        Returns true if type="text" attributes will be removed from <input> tags
        Returns:
        true if type="text" attributes will be removed from <input> tags
      • setRemoveInputAttributes

        public void setRemoveInputAttributes​(boolean removeInputAttributes)
        If set to true, type="text" attributes will be removed from <input> tags. Default is false.
        Parameters:
        removeInputAttributes - set true to remove type="text" attributes from <input> tags
      • isSimpleBooleanAttributes

        public boolean isSimpleBooleanAttributes()
        Returns true if boolean attributes will be simplified
        Returns:
        true if boolean attributes will be simplified
      • setSimpleBooleanAttributes

        public 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 true to simplify boolean attributes
      • isRemoveJavaScriptProtocol

        public boolean isRemoveJavaScriptProtocol()
        Returns true if javascript: pseudo-protocol will be removed from inline event handlers.
        Returns:
        true if javascript: pseudo-protocol will be removed from inline event handlers.
      • setRemoveJavaScriptProtocol

        public 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 true to remove javascript: pseudo-protocol from inline event handlers.
      • isRemoveHttpProtocol

        public boolean isRemoveHttpProtocol()
        Returns true if HTTP protocol will be removed from href, src, cite, and action tag attributes.
        Returns:
        true if HTTP protocol will be removed from href, src, cite, and action tag attributes.
      • setRemoveHttpProtocol

        public void setRemoveHttpProtocol​(boolean removeHttpProtocol)
        If set to 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.

        Parameters:
        removeHttpProtocol - set true to remove HTTP protocol from tag attributes
      • isRemoveHttpsProtocol

        public boolean isRemoveHttpsProtocol()
        Returns true if HTTPS protocol will be removed from href, src, cite, and action tag attributes.
        Returns:
        true if HTTPS protocol will be removed from href, src, cite, and action tag attributes.
      • setRemoveHttpsProtocol

        public void setRemoveHttpsProtocol​(boolean removeHttpsProtocol)
        If set to 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.

        Parameters:
        removeHttpsProtocol - set true to remove HTTP protocol from tag attributes
      • isGenerateStatistics

        public boolean isGenerateStatistics()
        Returns true if HTML compression statistics is generated
        Returns:
        true if HTML compression statistics is generated
      • setGenerateStatistics

        public 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 true to generate HTML compression statistics
        See Also:
        getStatistics()
      • isPreserveLineBreaks

        public boolean isPreserveLineBreaks()
        Returns true if line breaks will be preserved.
        Returns:
        true if line breaks will be preserved.
      • setPreserveLineBreaks

        public void setPreserveLineBreaks​(boolean preserveLineBreaks)
        If set to true, line breaks will be preserved.

        Default is false.

        Parameters:
        preserveLineBreaks - set true to preserve line breaks
      • getRemoveSurroundingSpaces

        public java.lang.String 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

        public void setRemoveSurroundingSpaces​(java.lang.String tagList)
        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