Interpret newline escape codes in dialog messages?
See original GitHub issueSee rails/rails#16784
Currently, to get a newline in a data-confirm
dialog, you need to either:
- Let it be interpreted as a newline at the Ruby string level (when using Rails helpers) or
- Insert an explicit newline in your HTML (when writing standard HTML) or
- Use an HTML escape code like
or
, and make sure the string is marked ashtml_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:
- Created 9 years ago
- Comments:7 (3 by maintainers)
Top 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 � NUL Null
1 1 1  SOH Start of Header
2 2 10  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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
As per https://github.com/rails/rails/issues/16784#issuecomment-234437275
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.