question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Interpret newline escape codes in dialog messages?

See original GitHub issue

See rails/rails#16784

Currently, to get a newline in a data-confirm dialog, you need to either:

  1. Let it be interpreted as a newline at the Ruby string level (when using Rails helpers) or
  2. Insert an explicit newline in your HTML (when writing standard HTML) or
  3. Use an HTML escape code like 
 or 
, and make sure the string is marked as html_safe on the Rails end so those codes aren’t escaped.

The issue with 1 is that any Ruby string newlines are reflected in the HTML source, so the output of both methods 1 and 2 might look like:

<body>
  <div id="main-container">
    <div class="links">
      <ul>
        <li><a href="path/to/whatever" data-confirm="I want a gap between this line...

...and this line">My link</a></li>
        <li><a href="#">Another link</a></li>
      </ul>
    </div>
  </div>
</body>

You can imagine if you had a lot of these how messy it might look, though it is apparently valid.

3 is simply a bit of an obscure/hacky workaround.

Id prefer to be able to specify something like:

<a href="path/to/whatever" data-confirm="I want a gap between this line...\n\n...and this line">My link</a>

and have jquery-ujs interpret those newlines just as calling

confirm("I want a gap between this line...\n\n...and this line");

in Javascript would. Currently it just gives literal "\n"s in the message instead.

Issue Analytics

  • State:open
  • Created 9 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Ruxtoncommented, Jul 22, 2016

As per https://github.com/rails/rails/issues/16784#issuecomment-234437275

<%= link_to 'Click me', '#', data: { confirm: 'Line\nNewLine' } %>
$.rails.confirm = function(message) {
 return confirm(message.replace(/(\\n|\\r|\\r\\n)/gm,"\n"))
}
1reaction
noinklingcommented, Sep 9, 2014

Doing a .replace(/\\n/g, "\n") on what gets returned from .data('confirm') seems to work. The only issue I can think of if this was implemented, is that if you wanted a literal “\n” in your message, you’d need to double-escape it (single quotes) or triple-escape it (double quotes) in Ruby. Same for any other escape codes if you chose to support them.

Read more comments on GitHub >

github_iconTop Results From Across the Web

New line in JavaScript alert box - Stack Overflow
If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters. escape sequences \n is ...
Read more >
Special and Escaped Characters
A special character begins with a backslash, followed by the character representing the code, such as \t for tab and \n for newline....
Read more >
Guide to Escape Characters in VBA - Software Solutions Online
Dec Hex Binary HTML Char Description 0 0 0 &#0 NUL Null 1 1 1 &#1 SOH Start of Header 2 2 10 &#2 STX Start...
Read more >
display dialog boxes from shell scripts - manpages.ubuntu!
--cr-wrap Interpret embedded newlines in the dialog text as a newline on the ... This is the curses key code rather than a...
Read more >
Escape Sequences | Microsoft Learn
Escape Sequences ; \n, New line ; \r, Carriage return ; \t, Horizontal tab ; \v, Vertical tab.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found