dankito / RichTextEditor

Rich text WYSIWYG editor for Android and JavaFX
Apache License 2.0
92 stars 36 forks source link

getHtml returns html code with forward slashes around attribute values #14

Closed cyberpunk922 closed 5 years ago

cyberpunk922 commented 5 years ago

output: "<p>​hi there.... <span style=\"text-decoration-line: underline; background-color: rgb(255, 255, 0);\">This is article upload</span> <span style=\"color: rgb(244, 67, 54); font-weight: bold;\">test 1</span>.</p>"

issue: having trouble running this html code in browser. after removing the forward slashes the code runs properly in a browser. please help.

dankito commented 5 years ago

Hi,

sorry, I cannot reproduce this issue.

How did you get this html string?

For me neither getCurrentHtmlAsync() (preferred) nor getCachedHtml() (will be removed soon) did return the double quotes escaped, that means with a forward slash.

Did you just copy that string from debugger view in Android Studio (IntelliJ) or Eclipse? Older versions of IntelliJ escaped double quotes in debugger view, but that's not the string's real value.

Could you please tell me which RichTextEditor version and which platform (Android or Java) you used?

Also it would be great if you could provide the steps to reproduce so I can figure out what the issue is.

cyberpunk922 commented 5 years ago

hi,

I am using RichTextEditor version 2.0.1 method used getCurrentHtmlAsync(). passed the html string value to a string variable and then stored it in fierbase db.

please take a look at the code below

`private void save() { editor.getCurrentHtmlAsync(new GetCurrentHtmlCallback() {

        @Override
        public void htmlRetrieved(@NotNull String html) {
            saveHtml(html);
        }
    });
}

private void saveHtml(String html) {
    // ...
    htmlText=html;
}

private void savePostToDatabase() {
    userRef.child(currentUserId).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists())
            {
                String userfullname = dataSnapshot.child("fullname").getValue().toString();
                HashMap postMap = new HashMap();
                postMap.put("uid", currentUserId);
                postMap.put("date", currentDate);
                postMap.put("time", currentTime);
                postMap.put("peronalityname", "TonyStark");
                postMap.put("articlehtml", htmlText);
                postMap.put("authorname", userfullname);

                postsRef.child(currentUserId + randPostName).updateChildren(postMap)
                        .addOnCompleteListener(new OnCompleteListener() {
                            @Override
                            public void onComplete(@NonNull Task task)
                            {

                                if(task.isSuccessful())
                                {
                                    sendUserToMainActivity();
                                    //editor.setHtml("<p>​hi Jarvis setup my day</p><p><br></p><p>Here&nbsp; is my program</p><p><br></p><h1><b>Get me <span style=\"background-color: rgb(255, 235, 59); color: rgb(255, 44, 147);\">all</span> i <span style=\"color: rgb(255, 44, 147); background-color: rgb(255, 235, 59);\">need</span> hey</b></h1><p><b><br></b></p>");
                                    Toast.makeText(PostActivity.this, "Article posted successfully.", Toast.LENGTH_LONG).show();
                                    loadingBar.dismiss();
                                }
                                else
                                {
                                    Toast.makeText(PostActivity.this, "Error occurred while posting your article.", Toast.LENGTH_LONG).show();
                                    loadingBar.dismiss();
                                }
                            }
                        });
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}`
dankito commented 5 years ago

Sorry, I still cannot reproduce your issue.

At which point in your code do you try to display retrieved html in a browser?

I tried to do this by modifying your saveHtml() (in code I did it in Kotlin, hopefully converted in correctly to Java by head):

private void saveHtml(String html) { // ... // writes html to file output.html in external storage folder Downloads:

    // requires to add this permission to AndroidManifest.xml
    //      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    // and (after installing app) to enable this permission in Android Settings App -> Apps -> RichTextEditor -> Permissions
    // otherwise application will crash when executing this code

    // sets html's encoding so that it gets correctly displayed in browser
    String encodedHtml = "<html>\n" +
            " <head>\n  <meta charset=\"utf-8\" /> \n </head>\n" +
            " <body>\n" + html + " </body>\n" +
            "</html>";

    File downloadFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); // or use any other public directory
    File destinationFile = new File(downloadFolder, "output.html");
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(destinationFile), "UTF-8");

    outputStreamWriter.write(encodedHtml);

    outputStreamWriter.close();

}

I then went to a file manager app (like ES File Explorer) and opened output.html from Download folder in Firefox, which displayed it absolutely correctly.

As said, don't copy html from debugger view in older Android Studio versions as it escapes the double quotes.

Current Android Studio version (3.2.1) removes the escaped double quotes.

If you add a debugger break point at this line

postMap.put("articlehtml", htmlText);

what does debugger view show you in latest Android Studio version (right click in debugger view at bottom of screen on variable 'htmlText' and select 'View/Edit Text')? Are the quotes escaped?

cyberpunk922 commented 5 years ago

hi, thanks for your time. the getCurrentHtmlAsync() method works correctly. the only problem was that the way it was stored in the firebase db

whatsapp image 2018-12-30 at 11 54 44 pm 1