Rails HTML 清理

发布于 2024-11-17 14:34:29 字数 792 浏览 3 评论 0原文

我正在尝试清理 HTML 文件,但它无法正常工作。除了段落和换行符标签之外,我希望全部都是纯文本。这是我的清理代码(点表示我的类中与问题无关的其他代码):

.
.
.
include ActionView::Helpers::SanitizeHelper
.
.
.
def remove_html(html_content)
    sanitized_content_1 = sanitize(html_content, :tags => %w(p br))
    sanitized_content_2 = Nokogiri::HTML(sanitized_content_1)
    sanitized_content_2.css("style","script").remove
    return sanitized_content_2
end

它无法正常工作。 这是原始 HTML 文件,函数从中读取其输入, 和 这是它返回的“已清理”代码。它留在 CSS 标签、JavaScript 和 HTML 注释标签的正文中。它可能还留下了我没有注意到的其他东西。请告知如何彻底删除除段落和换行标记之外的所有 CSS、HTML 和 JavaScript?

I am trying to sanitize an HTML file and it isn't working correctly. I want to all be entirely plain text except for paragraph and line break tags. Here is my sanitization code (the dots signify other code in my class that isn't relevant to the problem):

.
.
.
include ActionView::Helpers::SanitizeHelper
.
.
.
def remove_html(html_content)
    sanitized_content_1 = sanitize(html_content, :tags => %w(p br))
    sanitized_content_2 = Nokogiri::HTML(sanitized_content_1)
    sanitized_content_2.css("style","script").remove
    return sanitized_content_2
end

It isn't working correctly. Here is the original HTML file from which the function is reading its input, and here is the "sanitized" code it is returning. It is leaving in the body of CSS tags, JavaScript, and HTML Comment Tags. It might be leaving in other stuff as well that I have not noticed. Please advise on how to thoroughly remove all CSS, HTML, and JavaScript other than paragraph and line break tags?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开雨落又逢春i 2024-11-24 14:34:29

我认为你不想对其进行消毒。清理会去除 HTML,留下文本,但您认为正常的 HTML 元素除外。它的目的是允许用户输入字段包含一些标记。

相反,您可能想解析它。例如,下面的代码将打印给定 html 字符串中

标签的文本内容。

doc = Nokogiri::HTML.parse(html)

doc.search('p').each do |el|
  puts el.text
end

I don't think you want to sanitize it. Sanitizing strips HTML, leaving the text behind, except for the HTML elements you deem OK. It is intended for allowing a user-input field to contain some markup.

Instead, you probably want to parse it. For example, the following will print the text content of the <p> tags in a given html string.

doc = Nokogiri::HTML.parse(html)

doc.search('p').each do |el|
  puts el.text
end
掩于岁月 2024-11-24 14:34:29

您也可以使用 CGI 命名空间进行清理。

require 'CGI'
str = "<html><head><title>Hello</title></head><body></body></html>"
p str
p CGI::escapeHTML str

运行这个脚本,我们得到以下结果。

$ ruby sanitize.rb
"<html><head><title>Hello</title></head><body></body></html>"
"<html><head><title>Hello</title></head><body></body></html>"

You can sanitize with using CGI namespace too.

require 'CGI'
str = "<html><head><title>Hello</title></head><body></body></html>"
p str
p CGI::escapeHTML str

Run this script, we get following result.

$ ruby sanitize.rb
"<html><head><title>Hello</title></head><body></body></html>"
"<html><head><title>Hello</title></head><body></body></html>"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文