微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

HtmlCompressor HTML 压缩库

程序名称:HtmlCompressor

授权协议: Apache

操作系统: 跨平台

开发语言: Java

HtmlCompressor 介绍

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);

HtmlCompressor 官网

https://code.google.com/archive/p/htmlcompressor/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐