HtmlCompressorHTML 压缩库
HtmlCompressor 是一个 Java 类库,用来对 HTML 内容进行压缩。HtmlCompressor 会删除 HTML 中无用的空格、空行、注释等内容。
该项目还提供了 Maven 插件,详情请看 https://github.com/alextunyk/htmlcompressor-maven-plugin
示例代码:
HtmlCompressor compressor = new HtmlCompressor(); compressor.setEnabled(true); //if false all compression is off (default is true) compressor.setRemoveComments(true); //if false keeps HTML comments (default is true) compressor.setRemoveMultiSpaces(true); //if false keeps multiple whitespace characters (default is true) compressor.setRemoveIntertagSpaces(true); //removes iter-tag whitespace characters compressor.setRemoveQuotes(true); //removes unnecessary tag attribute quotes compressor.setSimpleDoctype(true); //simplify existing doctype compressor.setRemoveScriptAttributes(true); //remove optional attributes from script tags compressor.setRemoveStyleAttributes(true); //remove optional attributes from style tags compressor.setRemoveLinkAttributes(true); //remove optional attributes from link tags compressor.setRemoveFormAttributes(true); //remove optional attributes from form tags compressor.setRemoveInputAttributes(true); //remove optional attributes from input tags compressor.setSimpleBooleanAttributes(true); //remove values from boolean tag attributes compressor.setRemoveJavaScriptProtocol(true); //remove "javascript:" from inline event handlers compressor.setRemoveHttpProtocol(true); //replace "http://" with "//" inside tag attributes compressor.setRemoveHttpsProtocol(true); //replace "https://" with "//" inside tag attributes compressor.setPreserveLineBreaks(true); //preserves original line breaks compressor.setRemoveSurroundingSpaces("br,p"); //remove spaces around provided tags compressor.setCompressCss(true); //compress inline css compressor.setCompressJavaScript(true); //compress inline javascript compressor.setYuiCssLineBreak(80); //--line-break param for Yahoo YUI Compressor compressor.setYuiJsDisableOptimizations(true); //--disable-optimizations param for Yahoo YUI Compressor compressor.setYuiJsLineBreak(-1); //--line-break param for Yahoo YUI Compressor compressor.setYuiJsNoMunge(true); //--nomunge param for Yahoo YUI Compressor compressor.setYuiJsPreserveAllSemiColons(true);//--preserve-semi param for Yahoo YUI Compressor //use Google Closure Compiler for javascript compression compressor.setJavaScriptCompressor(new ClosureJavaScriptCompressor(CompilationLevel.SIMPLE_OPTIMIZATIONS)); //use your own implementation of css comressor compressor.setCssCompressor(new MyOwnCssCompressor()); String compressedHtml = compressor.compress(html);
评论