如何删除特定标签但保留允许的标签
How to remove specific tags but leave allowed tags
在某些 HTML 中,我想删除一些特定的标签,但保留标签的 contents/HTML。例如,在下面的行中,我
想要删除列入黑名单的 <strong>
和 <div>
标签,但保留标签的内容,并单独保留我的白名单标签中的 <p>
、<img>
和其他标签:
原文:
<div>
some text
<strong>text</strong>
<p>other text</p>
<img src="http://example.com" />
</div>
结果:
some text
text
<p>other text</p>
<img src="http://example.com" />
我想要剥离特定的标签,有些标签不能被剥离。它必须像 PHP 中的 strip_tags
一样工作。所以 inner_html
帮不了我。
使用Rails::Html::WhiteListSanitizer
:
white_list_sanitizer = Rails::Html::WhiteListSanitizer.new
original = <<EOD
<div>
some text
<strong>text</strong>
<p>other text</p>
<img src="http://example.com" />
</div>
EOD
puts white_list_sanitizer.sanitize(original, tags: %w(p img))
输出:
some text
text
<p>other text</p>
<img src="http://example.com">
如果你只想使用 Nokogiri,你可以遍历节点以递归地删除所有不需要的标签:
def clean_node(node, whitelist)
node.children.each do |n|
clean_node(n, whitelist)
unless whitelist.include?(n.name)
n.before(n.children)
n.remove
end
end
node
end
def strip_tags(html, whitelist)
whitelist += %w(text)
node = Nokogiri::HTML(html).children.last
clean_node(node, whitelist).inner_html
end
strip_tags
函数将删除所有不在白名单中的标签。对于你的例子,你会做:
original = <<HTML
<div>
some text
<strong>text</strong>
<p>other text</p>
<img src="http://example.com" />
</div>
HTML
puts strip_tags(original, %w(p img))
输出为:
some text
text
<p>other text</p>
<img src="http://example.com">
我会这样做:
require 'nokogiri'
doc = Nokogiri::HTML(<<EOT)
<div>
some text
<strong>text</strong>
<p>other text</p>
<img src="http://example.com" />
</div>
EOT
BLACKLIST = %w[strong div]
doc.search(BLACKLIST.join(',')).each do |node|
node.replace(node.children)
end
puts doc.to_html
# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html><body>
# >> some text
# >> text
# >> <p>other text</p>
# >> <img src="http://example.com">
# >>
# >> </body></html>
基本上它在 BLACKLIST
中查找节点并在文档中的任何位置找到它们,用节点的 children
替换它们,有效地将子节点提升到它们的父节点。
您可以使用 xmp 标签来显示 HTML 个标签。
<div>
some text
<strong>text</strong>
<xmp><p>other text</p>
<img src="http://example.com" />
</xmp>
</div>
HTML 元素 "xmp" 在不解释 HTML 的情况下呈现开始和结束标记之间的文本。
在某些 HTML 中,我想删除一些特定的标签,但保留标签的 contents/HTML。例如,在下面的行中,我
想要删除列入黑名单的 <strong>
和 <div>
标签,但保留标签的内容,并单独保留我的白名单标签中的 <p>
、<img>
和其他标签:
原文:
<div>
some text
<strong>text</strong>
<p>other text</p>
<img src="http://example.com" />
</div>
结果:
some text
text
<p>other text</p>
<img src="http://example.com" />
我想要剥离特定的标签,有些标签不能被剥离。它必须像 PHP 中的 strip_tags
一样工作。所以 inner_html
帮不了我。
使用Rails::Html::WhiteListSanitizer
:
white_list_sanitizer = Rails::Html::WhiteListSanitizer.new
original = <<EOD
<div>
some text
<strong>text</strong>
<p>other text</p>
<img src="http://example.com" />
</div>
EOD
puts white_list_sanitizer.sanitize(original, tags: %w(p img))
输出:
some text
text
<p>other text</p>
<img src="http://example.com">
如果你只想使用 Nokogiri,你可以遍历节点以递归地删除所有不需要的标签:
def clean_node(node, whitelist)
node.children.each do |n|
clean_node(n, whitelist)
unless whitelist.include?(n.name)
n.before(n.children)
n.remove
end
end
node
end
def strip_tags(html, whitelist)
whitelist += %w(text)
node = Nokogiri::HTML(html).children.last
clean_node(node, whitelist).inner_html
end
strip_tags
函数将删除所有不在白名单中的标签。对于你的例子,你会做:
original = <<HTML
<div>
some text
<strong>text</strong>
<p>other text</p>
<img src="http://example.com" />
</div>
HTML
puts strip_tags(original, %w(p img))
输出为:
some text
text
<p>other text</p>
<img src="http://example.com">
我会这样做:
require 'nokogiri'
doc = Nokogiri::HTML(<<EOT)
<div>
some text
<strong>text</strong>
<p>other text</p>
<img src="http://example.com" />
</div>
EOT
BLACKLIST = %w[strong div]
doc.search(BLACKLIST.join(',')).each do |node|
node.replace(node.children)
end
puts doc.to_html
# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html><body>
# >> some text
# >> text
# >> <p>other text</p>
# >> <img src="http://example.com">
# >>
# >> </body></html>
基本上它在 BLACKLIST
中查找节点并在文档中的任何位置找到它们,用节点的 children
替换它们,有效地将子节点提升到它们的父节点。
您可以使用 xmp 标签来显示 HTML 个标签。
<div>
some text
<strong>text</strong>
<xmp><p>other text</p>
<img src="http://example.com" />
</xmp>
</div>
HTML 元素 "xmp" 在不解释 HTML 的情况下呈现开始和结束标记之间的文本。