Nokogiri:为什么这个 return 只有删除的标签而不是剩余的标签?

Nokogiri: why does this return only the removed tag and not the remainder?

其中body是这样的:

<img src='/Users/test/Documents/cd-rum/whatever/spec/factories/images/rda.jpg' />Et qui incidunt provident sed nemo modi pariatur quia.

为什么会这样:

parsed_body = Nokogiri::HTML(body).search('img').first.remove
parsed_body.to_html

Return 只有 图片标签?

Failure/Error: specify { expect(post.parsed_body).to_not include('<img') }
expected "<img src=\"/Users/test/Documents/cd-rum/whatever/spec/factories/images/rda.jpg\">" not to include "<img"

因为您要将 .remove 的结果赋值给该变量 - 只需删除赋值

body = "<img src='/Users/test/Documents/cd-rum/whatever/spec/factories/images/rda.jpg' />Et qui incidunt provident sed nemo modi pariatur quia."
parsed_body = Nokogiri::HTML::fragment(body)
parsed_body.search('img').first.remove
parsed_body.to_html # => "Et qui incidunt provident sed nemo modi pariatur quia."