简体   繁体   English

在Rails中转义此HTML的正确方法

[英]Correct way to escape this HTML in Rails

I'm adding a very basic version of the Stackoverflow question formatting system to my app. 我在我的应用程序中添加了一个非常基本的Stackoverflow问题格式化系统版本。 I've got it to the point where this string: 我已经知道了这个字符串:

_italic_ and **bold** and `@inline_code` and: \n    </code snippet>

becomes: 变成:

<i>italic</i> and <b>bold</b> and <span class="block-code">@inline_code</span> and: <br/>
<span class="block-code"></code snippet></span>

What is the correct and safest way to display this so that the bold, italic and span tags all become HTML tags but other tags are displayed as text? 什么是正确且最安全的显示方式,以使粗体,斜体和跨度标签全部变为HTML标签,而其他标签显示为文本?

The solution was to swap the ampersands and less-than symbols for HTML entities... 解决方案是将&符号和小于号替换为HTML实体...

string.gsub("&", "&amp;").gsub("<", "&lt;")

...and then wrap the ouput in <pre> tags. ...然后将输出包装在<pre>标签中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM