Reputation: 67
I check the text of each terminal node. The A tag breaks the text, so the text doesn't match /PART/. Is it possible to move the position of A tag:
<P><FONT SIZE=3><B>PA<A NAME="name"></A>RT</B></FONT></P>
to
<P><FONT SIZE=3><B><A NAME="name"></A>PART</B></FONT></P>
But if the A tag doesn't break the text of the parent node, I will leave as it.
I don't know how to tell if a tag breaks the text of the parent node or not.
This is the code I tried, without condition on if a tag breaks the text.
require 'nokogiri'
require 'awesome_print'
html = '<P><FONT SIZE=3><B>PA<A NAME="name"></A>RT II</B></FONT></P>
<P><FONT SIZE=3><B><A NAME="name"></A>PART II</B></FONT></P>'
doc = Nokogiri::HTML(html)
doc.search('a').each do |n|
text = n.parent.text
n.parent.replace n
n.parent.content = text
end
output
<p><font size="3">PART II</font></p>
<p><font size="3">PART II</font></p>
Upvotes: 0
Views: 113
Reputation: 54984
To move the previous text to the other side of the a you can do:
a.add_next_sibling(a.previous) if a.previous && a.previous.text?
Upvotes: 1