chinalwb / Android-Rich-text-Editor

Android Rich Text Editor With customized spans - 富文本编辑器 - Don't miss this one :)
Apache License 2.0
844 stars 167 forks source link

StringIndexOutOfBoundsException #98

Open frankyxcs opened 4 years ago

frankyxcs commented 4 years ago

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)

chinalwb commented 4 years ago

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

frankyxcs commented 4 years ago

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

chinalwb commented 4 years ago

ok I'll try it out.

frankyxcs commented 4 years ago

hi still no clue how to fix this ?

chinalwb commented 4 years ago

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.

frankyxcs commented 4 years ago

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

chinalwb commented 4 years ago

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.

frankyxcs commented 4 years ago

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 commented 4 years ago

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

chinalwb commented 4 years ago

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?

frankyxcs commented 4 years ago

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 ?

chinalwb commented 4 years ago

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;
        }
    }
frankyxcs commented 4 years ago

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

chinalwb commented 4 years ago

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.