Before marking this question as duplicate, let me say that I have searched for hours but could not find any proper solution.
I have an web app built with AngularJS, using REST API. The JSON data received have encoded (utf-8) HTML special characters like <
for <
, '
for '
etc.
A few problems I am facing are:
1) Even though my HTML pages are saved in 'UTF-8' encoding and also has the ContentType
mentioned as:
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
All the encoded special characters that are hard-coded in the body of the HTML file are shown correctly, but they are not resolved properly when I use AngularJS to view them on the page. Meaning, if the JSON data contains <
, AngularJS would show it as <
instead of <
.
2) I searched SO to find that I could use ngSanitize
along with ng-bind-html
to solve this problem. From Rendering special characters in placeholder of input text box I found that AngularJS does not need ng-bind-html
if the values are encoded in UTF-16 using the \u
character sequence. And this actually works, except that my data is encoded in a different manner.
3) Finally, to show the special characters properly inside <input />
tags using ng-model
, I could not find a proper solution, and ended up processing each and every input tag value using Unescape HTML entities in Javascript?.
While both the solutions does work the way I want it to, but it is a very cumbersome process to use these methods in every input field of all the forms in my web app and almost everywhere, where AngularJS gives an output. Plus, both the solutions are a matter of great debate everywhere on SO since they are somehow or the other vulnerable to XSS attacks.
My question is, is there a "best practice" that can be followed such that AngularJS behaves properly with utf-8 encoded HTML special characters, without having to use ng-bind-html
in almost every other line of my html files and processing every value of every input field before binding them to ng-model
?
It kind of becomes spaghetti code to maintain and feels like an improper hack once the web app is considerably big, which mine is.
If it is of any use, I use the OWASP java Encode.forHtml(rawUserInput)
method to encode my user inputs.
Thank you, in advance.