thymeleaf text mode string how to keep indent for each line - html

i have a string stored in database which contains line break, it should looks like following when displaying on page:
abc
def
hkg
now i need to add this string to an email dialog as email content and its format should be:
my thymeleaf text template to generate email content looks like:
Test: [# th:each="g : ${gList}"]
Description:
[(${g.descr})]
However, after i added the template content to a textarea, it is:
that is there is no indent from line 2, this is not what i want.
how can i get the format like following for one string which contains multiple lines?
Test:
Description:
abc
def
hkg
The html code to show the email content in dialog is :
<div class="row">
<label class="col-2 text-right">Content:</label>
<textarea id="emailContent" class="col-9" v-model="email.content" >
</textarea>
</div>
another question is : what if this string is long but no line break, it only has spaces between words. in this case, it still display the rest of this string from line 2 without indent.

The string stored in the database is abc\ndef\nhkg therefore when you print it out you are getting:
Test:\n
....Description:\n
........abc\n
def\n
hkg
Since this is plain text and you don't have any of the formatting capabilities of html, you're going to have to modify the string itself. Basically, you need to replace your newlines \n with 8 spaces and a new line. You want the string to look like:
abc\n........def\n........hkg
As for what that looks like in Thymeleaf. Kind of ugly, but I think this should work for you:
Test: [# th:each="g : ${gList}" th:with="newline=${T(System).getProperty('line.separator')}"]
Description:
[(${#strings.replace(g.descr, newline, newline + ' ')})]

Related

Creating new line within a String in HTML

I have string like as follow, which is generated in mvc controller and display in a view within a table cell. S020050A,S020050B,S020050C,S020050D,S020050E,S020050F
,S020050G,S020050H,S020050I,S020050J,S020050K,S020050L
As I want to wrap the text on every 5 words, I have pushed
tag on every 5 words, so that string look like as follow
S020050A,S020050B,S020050C,S020050D,S020050E,S020050F
,S020050G,S020050H,S020050I,S020050J,S020050K,S020050L
I am intended to break the line from there, but in a view it just shows as it is, tag not interpreted as new line but simply as a part of string
I tried putting Environment.Newline and /n as well, when I did this in simple html page, it worked, so I believe it asp.net own mechanism which suppress tag, how I can make tag interpreted as new line?
I could solve it, from controller code (back end) add -> \n instead where you want line break and then in view use this style ->
style="white-space: pre-line"

How to extract multiline content from the <pre> tag in html using Selenium

Originally the element looks like this:
<pre>test_line1 test_line2 test_line3 test4</pre>
but when i double click it it looks like this:
<pre>test_line1
test_line2
test_line3
test4</pre>
linebreaks here are not recognized as \n or
what to do? i need to extract text and compare it with expected text
and expected text can be written only in single line
String elementText = element.getText();
Assert.assertTrue(elementText,"test_line1 test_line2 test_line3 test4");
Assert.assertTrue(elementText,"test_line1\ntest_line2\ntest_line3\ntest4");
You can try with something like this:
actualText.replaceAll("(\r\n|\n)", "");
This will remove all potential new line symbols from the text.

Insert multiple lines from QTextEdit into Jinja2 template

I have a QTextEdit that has several lines (Name, Age, Height).
self.textbox = QTextEdit(self)
self.textbox.setPlainText('Name:\n Age: \nHeight: \n')
self.enteredText = self.textbox.toPlainText()
I want to put this information into a Jinja2 HTML template so it shows in the following way:
Personal data:
Name: (whatever the user wrote after "name")
Age: (whatever the user wrote after "age")
Height: (whatever the user wrote after "height")
I'm having issues with linebreaks, I'm not sure how to process my QTextEdit so that I also get the linebreaks in HTML. I tried self.enteredText.splitlines(True) but HTML does not understand \n as linebreak and my text is all in one line.
I know almost nothing about jinja html templates, but you could try joining the lines together using br tags:
lines = '<br/>'.join(self.enteredText.splitlines()).

Why SafeHtml only shows plain text & does not show formmatted text (GWT)?

HTML myHtml=new HTML(SafeHtmlUtils.fromString("<i>Test</i>"));
HTML myHtml2=new HTML("<i>Test2</i>");
testHTMLPanel.add(myHtml);
testHTMLPanel.add(myHtml2);
OUTPUT:
<i>Test</i>
Test2
The right output should be the formmatted text like the second one. Other Gwt html widget also have the similar problem.
I am using Eclipse Juno.
SafeHtmlUtils.fromString(String s)
HTML-escapes its argument and returns the result wrapped as a SafeHtml.
That means that you get somthing like &#6.0;i&#.62;Test&#.60;&#.47;i&.#62;
Check
https://developers.google.com/web-toolkit/doc/latest/DevGuideSecuritySafeHtml
It's a security thing:
The reason why you have SafeHtmlUtils.fromString(userString) is that you can take a dynamic string, for example from a user input, and create a html text from it. It's more safe than just use Html.setText(userString) because with setText(userString) it would be feasible to inject vulnerable code.
more about input validation: http://www.testingsecurity.com/input-validation

preserving linebreaks in values in JSON key-value pairs

I am not sure if line breaks are allowed in JSON values. I certainly am unable to create the following in JSON
{"foo": "I am not sure if line breaks are
allowed in JSON values. I certainly
am unable to create the following in JSON"}
The following certainly does not work
{"foo": "I am not sure if line breaks are\nallowed in JSON values. I certainly\nam unable to create the following in JSON"}
Preamble: I want to send a long message like above either to the browser or to the console app and display it neatly formatted so it is legible to the user.
If you are displaying (or inserting) the json value directly in HTML, you can't use it as it is because in html new lines are ignored and replaced by a space.
For example if you have:
<p>Hello,
I'm in other line.</p>
It will be represented as:
Hello, I'm in other line.
You must convert the new lines to paragraph or <br>, for example:
<p>Hello,<br>
I'm in other line.</p>
That will be show as:
Hello,
I'm in other line
If this is your case, you can simply use String.replace to change \n into <br>\n.
If you're displaying it in HTML, will simple do.
Or in a text area or something, try "\r\n", wrapped in double quotes.
Double backslashes are to escape.