How to wrap Nokogiri nodeset in ONE span

≯℡__Kan透↙ 提交于 2019-12-12 06:27:15

问题


So my goal is to wrap all paragraphs after the initial paragraph within a span. I'm trying to figure out how to wrap a nodeset within a single span and .wrap() wraps each node in its own span. As in want:

<p>First</p>
<p>Second</p>
<p>Third</p>

To become:

<p>First</p>
<span>
<p>Second</p>
<p>Third</p>
</span>

Any sample code to help? Thanks!


回答1:


I'd do as below :

require 'nokogiri'

doc =  Nokogiri::HTML::DocumentFragment.parse(<<-html)
<p>First</p>
<p>Second</p>
<p>Third</p>
html

nodeset = doc.css("p")
new_node = Nokogiri::XML::Node.new('span',doc)
new_node << nodeset[1..-1]
nodeset.first.after(new_node)
puts doc.to_html
# >> <p>First</p><span><p>Second</p>
# >> <p>Third</p></span>
# >> 



回答2:


I'd do it something like this:

require 'nokogiri'

html = '<p>First</p>
<p>Second</p>
<p>Third</p>
'

doc = Nokogiri::HTML(html)

paragraphs = doc.search('p')[1..-1].unlink 
doc.at('p').after('<span>')
doc.at('span').add_child(paragraphs)
puts doc.to_html

Which results in HTML looking like:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<p>First</p>
<span><p>Second</p>
<p>Third</p></span>

</body></html>

To give you an idea what's happening, here's a more verbose output showing intermediate changes to the doc:

paragraphs = doc.search('p')[1..-1].unlink 
paragraphs.to_html 
# => "<p>Second</p><p>Third</p>"
doc.at('p').after('<span>')
doc.to_html 
# => "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><body>\n<p>First</p>\n<span></span>\n\n</body></html>\n"
doc.at('span').add_child(paragraphs)
doc.to_html
# => "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><body>\n<p>First</p>\n<span><p>Second</p>\n<p>Third</p></span>\n\n</body></html>\n"

Looking at the initial HTML, I'm not sure the question asked is going to work well for normal, everyday HTML, however, if you are absolutely sure it'll never change from the

<p>...</p>
<p>...</p>
<p>...</p>

layout then you should be OK. Any answer based on the initial, sample, HTML, will blow up miserably if the HTML really is something like:

<div>
  <p>...</p>
  <p>...</p>
  <p>...</p>
</div>
...
<div>
  <p>...</p>
  <p>...</p>
  <p>...</p>
</div>


来源:https://stackoverflow.com/questions/21957190/how-to-wrap-nokogiri-nodeset-in-one-span

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!