0

Right now, I'm using a CMS that displays the content of a textarea in a "Settings" page, on another page, but the content is copied inside some jQuery code:

$('textarea#envoi_contrat_conf').val('<cms:pages masterpage="gabarits.php" page_name="envoi-contrat-conferencier"><cms:show gabarit_content/></cms:pages>');

The content between the <cms:pages></cms:pages> tags is what is being saved in that textarea on the "Settings" page.

But right now, if I enter this in the textarea of the Settings page :

Hello
This is
A test

It will be displayed the same way, keeping the line breaks in the jQuery code. So it will break it. My code would look like this:

    $('textarea#envoi_contrat_conf').val('Hello
This is
A test');

What I need, is to make sure everything that is going to be inserted in that "val" section, replaces line breaks with something that will still keep the line breaks once being copied in the textarea.

If something like this would work, I would be fine with is:

    $('textarea#envoi_contrat_conf').val('Hello<br>This is<br>A test');

But <br> doesn't work inside a textarea.

Any ideas? Thanks a lot!


Someone linked another topic that was not related to my question.

Another user pointed out that I was looking for "\n" instead of actual line breaks. Thanks for his help.

But now I need to know how to replace actual line breaks in the code by "\n" instead. Since it's CMS generated content, I cannot hardcode a sentence. So I need to make sure even if the user makes line breaks in the textarea content, that it will appear with "\n" in the jQuery code instead of real line breaks in the code.

I tried this:

$('textarea#envoi_contrat_conf').val().replace(/\r\n|\r|\n/g,'\n');

Which successfully allow me to replace line breaks with something else, but the \n to be printed as is, not rendered. Thanks

larin555
  • 1,669
  • 4
  • 28
  • 43
  • Possible duplicate of [Replace all
    tag with space in javascript](https://stackoverflow.com/questions/37815103/replace-all-br-tag-with-space-in-javascript)
    – dippas Jun 04 '18 at 00:26
  • So ... `$('textarea#envoi_contrat_conf').val("Hello\nThis is\nA test")`? Got to point out that allowing people to enter text that will be interpreted by your code is a Bad Idea™ – Tibrogargan Jun 04 '18 at 00:31
  • @dippas, thanks but that's not what I need sorry – larin555 Jun 04 '18 at 02:19
  • @Tibrogargan Yes exactly! That worked for a hard coded phrase, but how can I make sure every line breaks from the CMS generated text would replace these linebreaks with "\n" instead? – larin555 Jun 04 '18 at 02:21
  • Why are you interpreting the value as code at all? I think you need to show how you obtaining this string from cms in the first place, and maybe what you're doing with it – Tibrogargan Jun 04 '18 at 02:24
  • It's a long story. Kind of stuck with a CMS that I'm not particularly used to work with, and time is a big issue. Since my client will be using this backend CMS on a local server, and that he needs this fix asap, I figured I would do it that way, and from what I have tested with hard coded values, it's working perfectly. Might not be the best way, but for now that would fix my clients issue, and I'll be able to work on a real and more secure solution. Any ideas how to do it? Thanks a lot! – larin555 Jun 04 '18 at 02:31
  • I have edited my main post by the way, I think I'm almost there! Thanks – larin555 Jun 04 '18 at 02:38

2 Answers2

1

Solution


var str = `${$('textarea#envoi_contrat_conf').val()}`

Template Literals

Not sure where or how you are getting a string with multiple line breaks but there is a way you can use such a string in the code without modifying it anywhere along the process.

  1. However you got this string it looks like this:

    "first segment
    second segment
    third segment"
    
  2. Store the string in a variable and interpolate it as a Template Literal by wrapping the variable in `${...}`

    ` $ {

    multiline string in variable or as a return from a function

    } `

    Note: The grave accent key (a.k.a. backtick): ` is located at the top left corner of every QWERTY keyboard (usually below the esc key.)


That's the how, now the why. Literal Strings are representation of a string wrapped in single or double quotes and are a syncytial nightmare. Template Literals are representation of a string wrapped in grave accents and have syntactic sugar that rectifies the problems the syntax of Literal strings cause. One important improvement is that a break in a string does not break it's value.

`text line 1 
 text line 2`;

"text line 1\n"+
"text line 2";

So wrapping a string value in backticks makes it a Template Literal. The ${... } combo interpolates a value or expression into the TL. Compare:

Template Literal Interpolation

`Template Literal interpolation: ${X} is easier to read and write, and " and ' are just normal characters`;

Literal String Concatenation

"Literal String concatenation: '+X+' is harder to read and write, and \" and \' have equal meaning so they must be escaped or used in nested combinations."

Demo


var str = `${$('.text0').val()}`;

$('.text1').val(str);
<textarea class='text0' rows='5'>
Hello
This is
A test
</textarea>

<textarea class='text1' rows='5'></textarea>




<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
zer00ne
  • 41,936
  • 6
  • 41
  • 68
0

Got it!

Here's the code I used:

$('textarea#envoi_contrat_conf').val().replace(/\r\n|\r|\n/g,"\\n");

The extra backslash espaces the first one, which needs to be printed out. Thanks for Tibrogargan for the help

larin555
  • 1,669
  • 4
  • 28
  • 43