Android-Rich-text-Editor icon indicating copy to clipboard operation
Android-Rich-text-Editor copied to clipboard

StringIndexOutOfBoundsException

Open frankyxcs opened this issue 6 years ago • 14 comments

Hi

i have a lot StringIndexOutOfBoundsException. Do you know how to fix them ?

Caused by: java.lang.StringIndexOutOfBoundsException:

at java.lang.String.startEndAndLength (String.java:298)

at java.lang.String.substring (String.java:1087)

at com.chinalwb.are.android.inner.HtmlToSpannedConverter.getFontSize (Html.java:1699)

at com.chinalwb.are.android.inner.HtmlToSpannedConverter.startCssStyle (Html.java:1281)

at com.chinalwb.are.android.inner.HtmlToSpannedConverter.handleStartTag (Html.java:916)

at com.chinalwb.are.android.inner.HtmlToSpannedConverter.startElement (Html.java:1483)

at org.ccil.cowan.tagsoup.Parser.push (Parser.java:795)

at org.ccil.cowan.tagsoup.Parser.rectify (Parser.java:1062)

at org.ccil.cowan.tagsoup.Parser.stagc (Parser.java:1017)

at org.ccil.cowan.tagsoup.HTMLScanner.scan (HTMLScanner.java:624)

at org.ccil.cowan.tagsoup.Parser.parse (Parser.java:450)

at com.chinalwb.are.android.inner.HtmlToSpannedConverter.convert (Html.java:855)

at com.chinalwb.are.android.inner.Html.fromHtml (Html.java:286)

at com.chinalwb.are.AREditText.fromHtml (AREditText.java:453)

frankyxcs avatar Sep 22 '19 23:09 frankyxcs

Let me know the html you're trying to parse. Thanks

chinalwb avatar Sep 23 '19 01:09 chinalwb

sorry thats not possible because these errors are comming from user input

frankyxcs avatar Sep 23 '19 01:09 frankyxcs

ok I'll try it out.

chinalwb avatar Sep 23 '19 02:09 chinalwb

hi still no clue how to fix this ?

frankyxcs avatar Jun 27 '20 16:06 frankyxcs

Hi @frankyxcs , sorry for my late response, I can fix it, but I want to know are you using are in your app now?

If yes, do you use it as a gradle dependency or wrap all the are code into your project?

Thanks.

chinalwb avatar Jun 28 '20 01:06 chinalwb

i use the library directly as an dependency in gradle

in my opinion the bug might be in html.java in this function ? private static int getFontSize(String fontSizePx) {

frankyxcs avatar Jun 28 '20 01:06 frankyxcs

Yes you're right. The code has only covered the fontSize for px but not for em and vw, or maybe also pt I think?

Do you have any suggestion about this? I know little about em and vw, so I am not sure what the fontSize should be calculated as.

Fixing will be pretty straightforward, but let me know the expected behavior before getting down to it.

Thanks.

chinalwb avatar Jun 28 '20 02:06 chinalwb

The problem is that i have this annoying error again and again from user input

But this is also a strange error in my opinion because the user can only format his text from the toolbar and then the text is saved in a database and when the user edits the text then sometimes on some phones this error occurs.

But i do not know in which language this error occurs.. but i cannot imagine that this bug depends on a language ?

Thats very strange because the text is save as html and also with 18px for example so never em, vw or pt will be used from the text or what do you think ? maybe i am wrong with my opinion ?

frankyxcs avatar Jun 28 '20 02:06 frankyxcs

But StringIndexOutOfBoundsException says that the index would not match so with substring 18px or 18em or 18pt would always result in 18 and everything should be fine ?! ...so i do not unterstand this error at the moment

frankyxcs avatar Jun 28 '20 03:06 frankyxcs

ok I got the problem scenario. It's your code saved the HTML (generated by ARE) and when reopen the HTML content, the ARE itself cannot parse and show correctly.

Have you ever checked whether the saved content is working as expected? I.e.: the fontSize end up with a px suffix?

chinalwb avatar Jun 28 '20 03:06 chinalwb

Thats the question if the bug depends on a language ?! Also the problem is that this error does not occur on my test phones :-) on my phones everything is fine

my solution now would be :

private static int getFontSize(String fontSizePx) {
   //  int pxIndex = fontSizePx.indexOf("px");
  //   String fontSizeStr = fontSizePx.substring(0, pxIndex);
 // String fontSizeStr = fontSizePx.substring(0, pxIndex);


 String fontSizeStr =  fontSizePx.replaceAll("[^0-9]", "");


    try {
        return Integer.parseInt(fontSizeStr);
    } catch (NumberFormatException e) {
        return 18;
    }
}

what do you think ?

frankyxcs avatar Jun 28 '20 03:06 frankyxcs

Sorry I tried to reproduce but it looked like basically fine to me..

I don't think it is language different.

Thanks for prompting the solution, seems like you are just trying to return 18 if running into errors when parsing the font-size.

How about this:

    private static int getFontSize(String fontSizePx) {
        try {
            int pxIndex = fontSizePx.indexOf("px");
            String fontSizeStr = fontSizePx.substring(0, pxIndex);
            return Integer.parseInt(fontSizeStr);
        } catch (Exception e) {
            e.printStackTrace();
            Log.w("ARE", "getFontSize: font-size seems like invalid: " + fontSizePx);
            return 18;
        }
    }

chinalwb avatar Jun 28 '20 04:06 chinalwb

I do not know :-) Still strange ..i really do not unterstand why StringIndexOutOfBoundsException occurs ??

If the px is "always" saved then this error should never occur ... so why does is occur ?!

Your solution is good too but it does not catch the StringIndexOutOfBoundsException

I think i will try my solution ...and tell you how it works maybe in 1 or 2 weeks

sorry i am wrong :-) Exception does catch all

frankyxcs avatar Jun 28 '20 04:06 frankyxcs

Do all of your HTML content from ARE output? Is it possible that user can copy and paste from somewhere else? And BTW what's your app looks like? Can I get it from Google Play Store or anywhere? I want to give it a try, thanks.

chinalwb avatar Jun 28 '20 04:06 chinalwb