防止 Nokogiri 转义字符? [英] Preventing Nokogiri from escaping characters?

查看:90
本文介绍了防止 Nokogiri 转义字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个文本节点并插入到我的文档中,如下所示:

I have created a text node and inserted into my document like so:

#<Nokogiri::XML::Text:0x3fcce081481c "<%= stylesheet_link_tag 'style'%>">]>

当我尝试用这个保存文档时:

When I try to save the document with this:

File.open('ng.html', 'w+'){|f| f << page.to_html}

我在实际文档中得到了这个:

I get this in the actual document:

&lt;%= stylesheet_link_tag 'style'%&gt;

有没有办法禁用转义并在我的 erb 标签完好无损的情况下保存我的页面?

Is there a way to disable the escaping and save my page with my erb tags intact?

谢谢!

推荐答案

您必须对文本元素中的某些字符进行转义,例如:

You are obliged to escape some characters in text elements like:

"   &quot;
'   &apos;
<   &lt;
>   &gt;
&   &amp;

如果您希望文本逐字使用 CDATA 部分,因为解析器会忽略 CDATA 部分中的所有内容.

If you want your text verbatim use a CDATA section since everything inside a CDATA section is ignored by the parser.

Nokogiri 示例:

Nokogiri example:

builder = Nokogiri::HTML::Builder.new do |b|
  b.html do
    b.head do
      b.cdata "<%= stylesheet_link_tag 'style'%>"
   end
  end
end
builder.to_html

这应该能让你的 erb 标签保持完整!

This should keep you erb tags intact!

这篇关于防止 Nokogiri 转义字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆