# Unescaped text in REXML

**URL:** <https://www.ruby-forum.com/t/unescaped-text-in-rexml/50598>\
**Category:** Ruby\
**Created:** [December 18, 2005, 9:45pm UTC](https://www.ruby-forum.com/t/unescaped-text-in-rexml/50598 "2005-12-18T21:45:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [December 18, 2005, 9:45pm UTC](https://www.ruby-forum.com/t/unescaped-text-in-rexml/50598/1 "2005-12-18T21:45:05Z")

</div>

Hi Rubyists,

I’m writing a small app that will be generating HTML programatically  
using REXML. Part of the HTML that will be generated is a tag  
with some JavaScript in it. However the JavaScript is full of  
characters that would normally be escaped elsewhere in HTML and as  
such REXML renders it pretty useless by replacing all the \<,\>,’ and  
such like with character entity codes. I’m currently trying to add the  
JavaScript as a text element like so:

scriptElement.text = javaScriptCode

which causes all the characters to be escaped. I’ve tried setting the  
:raw context to :all for the element but it just causes an exception  
to be raised when an escapable character is encountered.

Any ideas on how to get unescaped text in using REXML?

Farrel

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [December 18, 2005, 11:20pm UTC](https://www.ruby-forum.com/t/unescaped-text-in-rexml/50598/2 "2005-12-18T23:20:34Z")

</div>

That worked! Thanks a stack!

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [December 18, 2005, 10:01pm UTC](https://www.ruby-forum.com/t/unescaped-text-in-rexml/50598/3 "2005-12-18T22:01:51Z")

</div>

Farrel L. wrote:

> Hi Rubyists,
> 
> I’m writing a small app that will be generating HTML programatically  
> using REXML. Part of the HTML that will be generated is a tag  
> with some JavaScript in it. However the JavaScript is full of  
> characters that would normally be escaped elsewhere in HTML and as  
> such REXML renders it pretty useless by replacing all the \<,\>,’ and  
> such like with character entity codes. I’m currently trying to add the  
> JavaScript as a text element like so:
> 
> scriptElement.text = javaScriptCode
> 
> which causes all the characters to be escaped. I’ve tried setting the  
> :raw context to :all for the element but it just causes an exception  
> to be raised when an escapable character is encountered.
> 
> Any ideas on how to get unescaped text in using REXML?

I think this is what Ara Howard was battling a few  
days ago. This is what he came up with:

element.text =  
::REXML::Text::new(  
string\_we\_want\_untouched,  
respect\_whitespace=true,  
parent=nil,  
raw=true,  
entity\_filter=nil,  
illegal=%r/.^/m # match nothing!  
)

> Farrel

E

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [December 21, 2005, 9:54pm UTC](https://www.ruby-forum.com/t/unescaped-text-in-rexml/50598/4 "2005-12-21T21:54:22Z")

</div>

Although I removed the ‘m’ at the end of the illegal parameter. It  
seemed to be matching to ‘’ otherwise.

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [December 18, 2005, 11:20pm UTC](https://www.ruby-forum.com/t/unescaped-text-in-rexml/50598/5 "2005-12-18T23:20:38Z")

</div>

Farrel L. wrote:

> scriptElement.text = javaScriptCode
> 
> which causes all the characters to be escaped. I’ve tried setting the  
> :raw context to :all for the element but it just causes an exception  
> to be raised when an escapable character is encountered.
> 
> Any ideas on how to get unescaped text in using REXML?

Consider a CDATA section:

[http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/classes/REXML/CData.html](http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/classes/REXML/CData.html)

James

–

[http://www.ruby-doc.org](http://www.ruby-doc.org) - Ruby Help & Documentation  
[Ruby Code & Style](http://www.artima.com/rubycs/) - Ruby Code & Style: Writers wanted  
[http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
[http://www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys  
[http://www.30secondrule.com](http://www.30secondrule.com) - Building Better Tools
