Fixes for html wrapping and encoding (#746)

This commit is contained in:
Matt Portune 2020-02-28 15:20:59 -05:00 committed by GitHub
parent 25aec80e4c
commit 033b2b9ba0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Web;
using Xamarin.Forms; using Xamarin.Forms;
namespace Bit.App.Utilities namespace Bit.App.Utilities
@ -35,6 +36,13 @@ namespace Bit.App.Utilities
var specialColor = $"<span style=\"color:#{((Color)Application.Current.Resources["PasswordSpecialColor"]).ToHex().Substring(3)}\">"; var specialColor = $"<span style=\"color:#{((Color)Application.Current.Resources["PasswordSpecialColor"]).ToHex().Substring(3)}\">";
var result = string.Empty; var result = string.Empty;
// iOS won't hide the zero-width space char without these div attrs, but Android won't respect
// display:inline-block and adds a newline after the password. Hence, only iOS gets the div.
if(Device.RuntimePlatform == Device.iOS)
{
result += "<div style=\"display:inline-block; align-items:center; justify-content:center; text-align:center; word-break:break-all; white-space:pre-wrap; min-width:0\">";
}
// Start with an otherwise uncovered case so we will definitely enter the "something changed" // Start with an otherwise uncovered case so we will definitely enter the "something changed"
// state. // state.
var currentType = CharType.None; var currentType = CharType.None;
@ -83,15 +91,32 @@ namespace Bit.App.Utilities
break; break;
} }
} }
if(currentType == CharType.Special)
{
result += HttpUtility.HtmlEncode(c);
}
else
{
result += c; result += c;
} }
// Add zero-width space after every char so per-char wrapping works consistently
result += "&#8203;";
}
// Close off last span. // Close off last span.
if (currentType != CharType.None) if (currentType != CharType.None)
{ {
result += "</span>"; result += "</span>";
} }
// Close off iOS div
if(Device.RuntimePlatform == Device.iOS)
{
result += "</div>";
}
return result; return result;
} }
} }