简体   繁体   中英

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

The below code produce an error

java.lang.StringIndexOutOfBoundsException: String index out of range

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 version: 2.1.4.RELEASE

Thanks

try adding a closing tag </input> right before </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>

Alternatively you can also try excluding rhino from maven dependencies :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM