如何在 Rails 中转义 javascript 生成的 html?
在我的页面的一侧,我有一个非常简单的电子邮件表单。另一方面,我预览了建议的电子邮件。例如:
当用户填写字段时,我想更新 keyup 上的预览。我写了一个小js函数来做到这一点:
var email_previewer = function() {
var fields = [
{ input: $('input#editor_invitation_to'), preview: $('dt.to') },
{ input: $('#editor_invitation_message'), preview: $('#message') }
];
var perserve_linebreaks = function(text) {
var html = text.replace(/\r\n\r\n/g, "</p><p>"),
html = html.replace(/\r\n/g, "<br />"),
html = html.replace(/\n\n/g, "</p><p>"),
html = html.replace(/\n/g, "<br />"),
html = "<p>"+html+"</p>";
return html;
};
var sync_text = function(input, preview) {
var text = input.val();
if (input.is('textarea')) {
text = perserve_linebreaks(text);
}
preview.text(text);
}
// sync preview on page load
$.each(fields, function(index, field) {
sync_text(field.input, field.preview);
});
// create keyup events
$.each(fields, function(index, field) {
field.input.keyup(function() {
sync_text(field.input, field.preview);
});
});
}();
一切都很好,除了rails想要转义我的html并使其看起来像垃圾:
通常情况下,我知道这没有问题:只需添加 raw
或 html_safe
即可。但我不能在这里这样做,因为标记是在 keyup (或页面加载)上动态构建的。我是否缺少一些简单的解决方案?有解决方法的想法吗?我唯一的想法是在 iframe 中加载预览,这看起来很奇怪。
这真是令人沮丧得可笑。请有人帮忙!
On one side of my page, I have a very simple email form. On the other side I have a preview of the proposed email. For example:
As the user completes the fields, I'd like to update the preview on keyup. I wrote a little js function to do just that:
var email_previewer = function() {
var fields = [
{ input: $('input#editor_invitation_to'), preview: $('dt.to') },
{ input: $('#editor_invitation_message'), preview: $('#message') }
];
var perserve_linebreaks = function(text) {
var html = text.replace(/\r\n\r\n/g, "</p><p>"),
html = html.replace(/\r\n/g, "<br />"),
html = html.replace(/\n\n/g, "</p><p>"),
html = html.replace(/\n/g, "<br />"),
html = "<p>"+html+"</p>";
return html;
};
var sync_text = function(input, preview) {
var text = input.val();
if (input.is('textarea')) {
text = perserve_linebreaks(text);
}
preview.text(text);
}
// sync preview on page load
$.each(fields, function(index, field) {
sync_text(field.input, field.preview);
});
// create keyup events
$.each(fields, function(index, field) {
field.input.keyup(function() {
sync_text(field.input, field.preview);
});
});
}();
Everything works great except rails wants to escape my html and make it look like crap:
Normally, I know this is no problem: just add raw
or html_safe
. But I can't do that here since the markup is being dynamically built on keyup (or page load). Am I missing some simple solution? Any ideas for a workaround? My only idea is to load the preview in an iframe, which seems totally weird.
This is ridiculously frustrating. Someone please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会立即怀疑您的
sync_text
函数中的preview.text(text)
。这应该是在 DOM 中创建一个文本节点,您想要的是设置 innerHTML,以便创建多个节点。I would immediately suspect
preview.text(text)
in yoursync_text
function. That should be creating a text node in the DOM, and what you want is to set the innerHTML so several nodes get created.