Nokogiri usage

F

Fernando Perez

Hi,

In one of my Rails app, I have a content attribute that hosts some html
code. I want to parse this content and replace <img> tags by something
else. I used to do Nokogiri::HTML(content), and then do my parsing, then
return the content.

The problem is that nokogiri injects the doctype and <html><body> tags
which obviously messes up my final html page because of these duplicate
tags.

So how to perform the parsing without having to first initialize the
content with Nokogiri::HTML, or how to not initialize a full html page
from my content?

Cheers,
 
M

Mike Dalessio

[Note: parts of this message were removed to make it a legal post.]

Hi,

In one of my Rails app, I have a content attribute that hosts some html
code. I want to parse this content and replace <img> tags by something
else. I used to do Nokogiri::HTML(content), and then do my parsing, then
return the content.

The problem is that nokogiri injects the doctype and <html><body> tags
which obviously messes up my final html page because of these duplicate
tags.

So how to perform the parsing without having to first initialize the
content with Nokogiri::HTML, or how to not initialize a full html page
from my content?

Try Nokogiri::HTML::DocumentFragment.parse(content).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top