I am retrieving some textual data using JSON, this data includes text formatted with linebreaks. I would very much like to render those linebreaks to the user.
Question: What is the "correct" / "recommended" approach to achieve this?
Options I've tried:
- Binding normally:
<p>${myText}</p>
: Renders no linebreaks - Using
<pre>
:<p><pre>${myText}></pre></p>
: Renders linebreaks, but have all the known and loved issues with long<pre>
text, like horizontal scrolling in some browsers and suboptimal word breaking. - Binding normally using a valueConverter that replaces linebreaks with
<br>
tags:<p>${myText | textFormat}</p>
export class TextFormatValueConverter {
toView(value) {
return value.replace(new RegExp('\r?\n','g'), '<br>');
}
}
This does render <br>
tags, but the Aurelia binder escapes the tags and shows them as literal text to the user.
* Binding using the above converter and innerHTML: <p innerHTML.bind="myText | textFormat"></p>
: Renders ok but I'm worried that it might be vulnerable to exploits, as the texts comes from a legacy system that does not do any input sanitazion with regards to usage for the web.