MicroSugarDeveloperOrg / Avalonia.WebView

MIT License
216 stars 39 forks source link

Android HtmlContent should use LoadDataWithBaseURL #36

Open stefanolson opened 10 months ago

stefanolson commented 10 months ago

I was having some html content not appearing when using the WebView.HtmlContent property. After doing a bit of digging I've discovered that it's due to the use of LoadData in the Android.Webkit.WebView. This code fails to have anything display:

var webView = new global::Android.Webkit.WebView(parentContext);

webView.LoadData( """
                                        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled</title><style type="text/css">
                                        p { margin-top: 0px;margin-bottom: 12px;line-height: 1.15; }
                                        body { font-family: 'Verdana';font-style: Normal;font-weight: normal;font-size: 16px; }
                                        .Normal { telerik-style-type: paragraph;telerik-style-name: Normal;margin-bottom: 0px;border-collapse: collapse; }
                                        .TableNormal { telerik-style-type: table;telerik-style-name: TableNormal;border-collapse: collapse; }
                                        .Normal_1 { telerik-style-type: paragraph;telerik-style-name: Normal_1;margin-top: 0px;margin-bottom: 0px;line-height: 1.15;border-collapse: collapse; }
                                        .TableNormal_1 { telerik-style-type: table;telerik-style-name: TableNormal_1;margin-top: 0px;line-height: 1.15;border-collapse: collapse; }
                                        .NormalWeb { telerik-style-type: paragraph;telerik-style-name: NormalWeb;margin-top: 6.66px;margin-bottom: 6.66px;line-height: 1.15;border-collapse: collapse; }
                                        .s_171A2DA3 { telerik-style-type: local;font-family: 'Arial';font-size: 14.6666666666667px; }
                                        .s_4E170F3A { telerik-style-type: local;font-family: 'Arial';font-weight: bold;font-size: 16px;color: #241E49; }
                                        .s_1A1C539C { telerik-style-type: local;font-family: 'Arial';font-weight: bold;font-size: 16px;color: #7F5CA3; } </style></head><body><p class="Normal ">forwarded test message</p><p class="Normal "><span>&nbsp;</span></p><p class="Normal "><span>&nbsp;</span></p><p class="NormalWeb "><span class="s_171A2DA3">Kind Regards, </span><br /><br /><span class="s_4E170F3A">my name here</span><span class="s_171A2DA3"> </span><br /><br /><span class="s_1A1C539C">Team Leader</span><span class="s_171A2DA3"> </span><br /><span class="s_1A1C539C">My Department</span><span class="s_171A2DA3"> </span></p><p class="Normal "><span>&nbsp;</span></p></body></html>
                                        """, default, default);

But this works fine:

webView.LoadDataWithBaseURL(null, """
                                        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled</title><style type="text/css">
                                        p { margin-top: 0px;margin-bottom: 12px;line-height: 1.15; }
                                        body { font-family: 'Verdana';font-style: Normal;font-weight: normal;font-size: 16px; }
                                        .Normal { telerik-style-type: paragraph;telerik-style-name: Normal;margin-bottom: 0px;border-collapse: collapse; }
                                        .TableNormal { telerik-style-type: table;telerik-style-name: TableNormal;border-collapse: collapse; }
                                        .Normal_1 { telerik-style-type: paragraph;telerik-style-name: Normal_1;margin-top: 0px;margin-bottom: 0px;line-height: 1.15;border-collapse: collapse; }
                                        .TableNormal_1 { telerik-style-type: table;telerik-style-name: TableNormal_1;margin-top: 0px;line-height: 1.15;border-collapse: collapse; }
                                        .NormalWeb { telerik-style-type: paragraph;telerik-style-name: NormalWeb;margin-top: 6.66px;margin-bottom: 6.66px;line-height: 1.15;border-collapse: collapse; }
                                        .s_171A2DA3 { telerik-style-type: local;font-family: 'Arial';font-size: 14.6666666666667px; }
                                        .s_4E170F3A { telerik-style-type: local;font-family: 'Arial';font-weight: bold;font-size: 16px;color: #241E49; }
                                        .s_1A1C539C { telerik-style-type: local;font-family: 'Arial';font-weight: bold;font-size: 16px;color: #7F5CA3; } </style></head><body><p class="Normal ">forwarded test message</p><p class="Normal "><span>&nbsp;</span></p><p class="Normal "><span>&nbsp;</span></p><p class="NormalWeb "><span class="s_171A2DA3">Kind Regards, </span><br /><br /><span class="s_4E170F3A">my name here</span><span class="s_171A2DA3"> </span><br /><br /><span class="s_1A1C539C">Team Leader</span><span class="s_171A2DA3"> </span><br /><span class="s_1A1C539C">My Department</span><span class="s_171A2DA3"> </span></p><p class="Normal "><span>&nbsp;</span></p></body></html>
                                        """,default, default, null);

Changed the source to test it there, but I can't get source code to build due to this: 14:36:27:737 1>C:\Program Files\dotnet\sdk\8.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets(117,5): error NETSDK1139: The target platform identifier ios was not recognized.

Installed ios workload again, but not working so far, sadly.

Anyway, only change is:
bool IWebViewControl.NavigateToString(string htmlContent)
[..]
webView.LoadData(htmlContent, default, default);
becomes:
webView.LoadDataWithBaseURL(null, htmlContent, default, default, null);

I'm not sure how to submit the source change so hopefully someone can to make the change and put it into the nuget packages.

Thanks, Stefan

rolf-szomor commented 8 months ago

@stefanolson added pull request: https://github.com/MicroSugarDeveloperOrg/Avalonia.WebView/pull/50