I'm getting a multiline plain text from my backend.
For example:
Lorem ipsum dolor sit amet, <strong>consectetur</strong> adipiscing elit.
Nunc porta velit ut nunc pretium, quis auctor nunc placerat.
In quis porta neque.
Fusce dapibus faucibus mi ut egestas.
<p>Phasellus egestas magna posuere, bibendum dui quis, lacinia nisl.</p>
I want to render it with AngularJS as an HTML paragraphs.
So this example will become:
<p>Lorem ipsum dolor sit amet, <strong>consectetur</strong> adipiscing elit.</p>
<p>Nunc porta velit ut nunc pretium, quis auctor nunc placerat.
In quis porta neque.
Fusce dapibus faucibus mi ut egestas.</p>
<p><p>Phasellus egestas magna posuere, bibendum dui quis, lacinia nisl.</p></p>
Please notice, that all HTML code that existed in the initial plain text must be securely escaped (including existing <p>
's) so I can't just apply my filter and do escaping afterwards.
I see it as a 3 steps process:
But I'm not sure what is the best method of implementing each step.
Could you guide me with the right approach vector? Thank you!
To add a new paragraph in HTML, place your insertion mark cursor into the HTML document where you want to add the new paragraph. Then type the paragraph start tag: <p>. Then type the paragraph's text. At the end of the new paragraph's text, type the end tag: </p>.
As part of a form, the <textarea> tag creates a multiline text-entry area in the user's browser display. In it, the user may type a nearly unlimited number of lines of text.
You can use the <br> element whenever you need to add an HTML new line but not a new paragraph. As it is an empty element, it does not have a closing tag.
Add space below a line or paragraph of text To add extra space below a line or paragraph of text, or push text down lower on the page once, you can use the <br> tag.
I think I've finally solved this issue. I hope this answer will help someone.
As I mentioned earlier this is a 3-steps process.
For the first step I were trying to find some existing function to escape initial text, but to no avail. Seems like AngularJS is not exposing any methods of doing this (please correct me if I'm wrong). Finally I've created my own filter to escape HTML:
myModule.filter('htmlEscape', function() {
return function(input) {
if (!input) {
return '';
}
return input.
replace(/&/g, '&').
replace(/</g, '<').
replace(/>/g, '>').
replace(/'/g, ''').
replace(/"/g, '"')
;
};
});
Then I've created a filter to break plain text into paragraphs:
myModule.filter('textToHtml', ['$sce', 'htmlEscapeFilter', function($sce, htmlEscapeFilter) {
return function(input) {
if (!input) {
return '';
}
input = htmlEscapeFilter(input);
var output = '';
$.each(input.split("\n\n"), function(key, paragraph) {
output += '<p>' + paragraph + '</p>';
});
return $sce.trustAsHtml(output);
};
}])
I'm injecting early created htmlEscape
filter to it and using $sce.trustAsHtml
method to return value suitable for use with ng-bind-html
directive.
And in the end I'm invoking this filter in the partial template like this:
<div ng-bind-html="someText|textToHtml"></div>
Here's the result:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With