# Stripping unwanted html

**URL:** <https://rubytalk.org/t/stripping-unwanted-html/31654>\
**Category:** ruby-talk\
**Created:** [6 October 2006 02:13 UTC](https://rubytalk.org/t/stripping-unwanted-html/31654 "2006-10-06T02:13:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wild\_Al](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@Wild\_Al](https://rubytalk.org/u/Wild_Al)\
**Post date:** [6 October 2006 02:13 UTC](https://rubytalk.org/t/stripping-unwanted-html/31654/1 "2006-10-06T02:13:35Z")

</div>

Hi everyone:

I'm trying to strip html with the exception of a few html tags.

I have found the following code:

&nbsp;&nbsp;def strip\_tags(html)  
&nbsp;&nbsp;&nbsp;&nbsp;if html.index("\<")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text = ""  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tokenizer = HTML::Tokenizer.new(html)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while token = tokenizer.next  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node = HTML::Node.parse(nil, 0, 0, token, false)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# result is only the content of any Text nodes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text \<\< node.to\_s if node.class == HTML::Text  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# strip any comments, and if they have a newline at the end (ie.  
line with  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# only a comment) strip that too  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text.gsub(/\<!--(.\*?)--\>[\n]?/m, "")  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;html # already plain text  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

I'm trying to understand what is going on in this code but cannot find  
documenation for HTML::Tokenizer or HTML::Node.parse. Does anyone know  
the use of the parameters in the parse method?

In the while loop, how do you access the html tag. If I could access  
the html tags, I could then decide if I wanted to keep the tag or not.

Thanks for reading,  
Wild Al

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Michael\_Moen](https://avatars.discourse-cdn.com/v4/letter/m/eada6e/32.png) [@Michael\_Moen](https://rubytalk.org/u/Michael_Moen)\
**Post date:** [6 October 2006 03:38 UTC](https://rubytalk.org/t/stripping-unwanted-html/31654/2 "2006-10-06T03:38:45Z")

</div>

Wild Al wrote:

> Hi everyone:
> 
> I'm trying to strip html with the exception of a few html tags.
> 
> I have found the following code:
> 
> &nbsp;&nbsp;def strip\_tags(html)  
> &nbsp;&nbsp;&nbsp;&nbsp;if html.index("\<")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text = ""  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tokenizer = HTML::Tokenizer.new(html)
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while token = tokenizer.next  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node = HTML::Node.parse(nil, 0, 0, token, false)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# result is only the content of any Text nodes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text \<\< node.to\_s if node.class == HTML::Text  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# strip any comments, and if they have a newline at the end (ie.  
> line with  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# only a comment) strip that too  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text.gsub(/\<!--(.\*?)--\>[\n]?/m, "")  
> &nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;html # already plain text  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> end
> 
> I'm trying to understand what is going on in this code but cannot find  
> documenation for HTML::Tokenizer or HTML::Node.parse. Does anyone know  
> the use of the parameters in the parse method?
> 
> In the while loop, how do you access the html tag. If I could access  
> the html tags, I could then decide if I wanted to keep the tag or not.
> 
> Thanks for reading,  
> Wild Al

Al- I recently needed a parser similar to Perl's HTML::Scrubber. \<a  
href="[http://www.underpantsgnome.com/2006/09/09/using-hpricot-to-scrub-html/&quot;&gt;This&lt;/a&gt](http://www.underpantsgnome.com/2006/09/09/using-hpricot-to-scrub-html/&quot;&gt;This&lt;/a&gt);  
is what I came up with , you may find it useful.

Michael

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Paul\_Lutus](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@Paul\_Lutus](https://rubytalk.org/u/Paul_Lutus)\
**Post date:** [6 October 2006 05:50 UTC](https://rubytalk.org/t/stripping-unwanted-html/31654/3 "2006-10-06T05:50:06Z")

</div>

Wild Al wrote:

/ ...

> In the while loop, how do you access the html tag. If I could access  
> the html tags, I could then decide if I wanted to keep the tag or not.

Why not create your own method? This should give you some ideas:

#!/usr/bin/ruby -w

data = File.read("/path/page.html")

data.scan(/\<(\w+?)\>/) { |tag|  
&nbsp;&nbsp;puts tag  
}

This puts out the tag name extracted from each HTML tag in the page.

> **···**
>
> --  
> Paul Lutus  
> [http://www.arachnoid.com](http://www.arachnoid.com)

---

<div class="post-metadata">

**Author:** ![eden](https://avatars.discourse-cdn.com/v4/letter/e/5f9b8f/32.png) [@eden](https://rubytalk.org/u/eden)\
**Post date:** [6 October 2006 09:20 UTC](https://rubytalk.org/t/stripping-unwanted-html/31654/4 "2006-10-06T09:20:16Z")

</div>

Hey, that's RoR's strip\_tags method 🙂

I ran into the same issue so went down your path and instead of  
fretting about no docs, just hacked my way through using IRB.

Here's a modified version of what I came up with, maybe you'll find it  
useful?

&nbsp;&nbsp;def strip\_tags\_except(html, exceptions = )  
&nbsp;&nbsp;&nbsp;&nbsp;if html.index("\<")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text = ""  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tokenizer = HTML::Tokenizer.new(html)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while token = tokenizer.next  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case node = HTML::Node.parse(nil, 0, 0, token, false)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when HTML::Tag  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text \<\< node.to\_s if exceptions.include?(node.name)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when HTML::Text  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text \<\< node.to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;html  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

The one I had also stripped attributes and closed up dangling tags if  
it found any. Have a look at RoR's strip\_links for more examples of  
HTML::Node/HTML::Tokenizer usage.

Wild Al wrote:

> **···**
>
> > Hi everyone:
> > 
> > I'm trying to strip html with the exception of a few html tags.
> > 
> > I have found the following code:
> > 
> > &nbsp;&nbsp;def strip\_tags(html)  
> > &nbsp;&nbsp;&nbsp;&nbsp;if html.index("\<")  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text = ""  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tokenizer = HTML::Tokenizer.new(html)
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while token = tokenizer.next  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node = HTML::Node.parse(nil, 0, 0, token, false)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# result is only the content of any Text nodes  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text \<\< node.to\_s if node.class == HTML::Text  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# strip any comments, and if they have a newline at the end (ie.  
> > line with  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# only a comment) strip that too  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text.gsub(/\<!--(.\*?)--\>[\n]?/m, "")  
> > &nbsp;&nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;html # already plain text  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > I'm trying to understand what is going on in this code but cannot find  
> > documenation for HTML::Tokenizer or HTML::Node.parse. Does anyone know  
> > the use of the parameters in the parse method?
> > 
> > In the while loop, how do you access the html tag. If I could access  
> > the html tags, I could then decide if I wanted to keep the tag or not.
> > 
> > Thanks for reading,  
> > Wild Al
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![eden](https://avatars.discourse-cdn.com/v4/letter/e/5f9b8f/32.png) [@eden](https://rubytalk.org/u/eden)\
**Post date:** [6 October 2006 09:23 UTC](https://rubytalk.org/t/stripping-unwanted-html/31654/5 "2006-10-06T09:23:44Z")

</div>

eden wrote:

> Have a look at RoR's strip\_links for more examples of  
> HTML::Node/HTML::Tokenizer usage.

Sorry, I meant sanitize:  
[http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M000516](http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M000516)

---

<div class="post-metadata">

**Author:** ![Wild\_Al](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@Wild\_Al](https://rubytalk.org/u/Wild_Al)\
**Post date:** [8 October 2006 19:54 UTC](https://rubytalk.org/t/stripping-unwanted-html/31654/6 "2006-10-08T19:54:16Z")

</div>

eden wrote:

> Here's a modified version of what I came up with, maybe you'll find it  
> useful?
> 
> &nbsp;&nbsp;def strip\_tags\_except(html, exceptions = )  
> &nbsp;&nbsp;&nbsp;&nbsp;if html.index("\<")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text = ""  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tokenizer = HTML::Tokenizer.new(html)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while token = tokenizer.next  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case node = HTML::Node.parse(nil, 0, 0, token, false)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when HTML::Tag  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text \<\< node.to\_s if exceptions.include?(node.name)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when HTML::Text  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text \<\< node.to\_s  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text  
> &nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;html  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end

I found this method very useful; it is exactly what I needed. Thanks.  
To all others: your suggestions helped too, especially in understanding  
ruby. Thanks again...

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![eden](https://avatars.discourse-cdn.com/v4/letter/e/5f9b8f/32.png) [@eden](https://rubytalk.org/u/eden)\
**Post date:** [9 October 2006 05:36 UTC](https://rubytalk.org/t/stripping-unwanted-html/31654/7 "2006-10-09T05:36:03Z")

</div>

Glad I could help. One security-related caveat.

The method I posted doesn't strip attributes, so it may be possible for  
someone to "hack" your site by putting javascript onto one of the  
allowed tags.

You can fix that by changing the last line of the first branch of the  
if statement from "text" to "sanitize(text)", eg:

&nbsp;&nbsp;if html.index("\<")  
&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;sanitize(text)  
&nbsp;&nbsp;else  
&nbsp;&nbsp;...

Wild Al wrote:

> **···**
>
> > I found this method very useful; it is exactly what I needed. Thanks.  
> > To all others: your suggestions helped too, especially in understanding  
> > ruby. Thanks again...
