java.lang.StringIndexOutOfBoundsException : String index out of range in yuicompressor

标签 java spring-boot yui-compressor

下面的代码会产生错误

java.lang.StringIndexOutOfBoundsException:字符串索引超出范围

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.setCompressCss(true); //compress css using Yahoo YUI Compressor 
compressor.setCompressJavaScript(true); //compress js using Yahoo YUI Compressor 
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 


String str = "<html><body><p>Hover over the checkbox to simulate a mouse-click.</p><form><input type=\"checkbox\" id=\"myCheck\" onmouseover=\"myFunction()\" onclick=\"alert('click event occured')\"></form><script>function myFunction(){document.getElementById(\"myCheck\").click();}</script></body></html>";

System.out.println(compressor.compress(str));

If remove the <script> tag and its contents, then its working fine. 
or
if comment compressor.setCompressJavaScript(true); this line, then its working fine.

pom.xml

<dependency>
    <groupId>com.googlecode.htmlcompressor</groupId>
    <artifactId>htmlcompressor</artifactId>
    <version>1.5.2</version>
</dependency>
<dependency>
    <groupId>com.yahoo.platform.yui</groupId>
    <artifactId>yuicompressor</artifactId>
    <version>2.4.8</version>
</dependency>

Spring Boot版本:2.1.4.RELEASE

谢谢

最佳答案

尝试添加结束标签</input>就在</form>之前:

<html>
    <body>
        <p>Hover over the checkbox to simulate a mouse-click.</p>
        <form>
            <input
                type = "checkbox"
                id = "myCheck"
                onmouseover = "myFunction()"
                onclick = "alert('click event occured')">
            </input>
        </form>
        <script>function myFunction(){document.getElementById("myCheck").click();}</script>
    </body>
</html>

或者,您也可以尝试从 Maven 依赖项中排除 rhino :

<dependency>
    <groupId>com.yahoo.platform.yui</groupId>
    <artifactId>yuicompressor</artifactId>
    <version>2.4.8</version>
    <exclusions>
        <exclusion>
            <artifactId>js</artifactId>
            <groupId>rhino</groupId>
        </exclusion>
    </exclusions>
</dependency>

关于java.lang.StringIndexOutOfBoundsException : String index out of range in yuicompressor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55917898/

相关文章:

java - 如何在类型查询中制定 JOIN?

java - Gradle java项目在构建期间替换文件中的单行

java - 具有多个主类的 Gradle 应用程序插件

java - setOnUtteranceProgressListener 根本不适用于 API > 21 的文本转语音

java - Spring Boot OAuth2 单点注销(注销)

java - 尾随部分不允许有 Gradle 内容

gradle - 将资源从子项目复制到胖jar根路径

java - maven yui 压缩 war :war

javascript - 从 JavaScript 中的 switch cases 中删除死代码

maven - 进行 war 时,文件在Maven项目中被覆盖