# Unescaped text in REXML

**URL:** <https://rubytalk.org/t/unescaped-text-in-rexml/23400>\
**Category:** ruby-talk\
**Created:** [18 December 2005 20:43 UTC](https://rubytalk.org/t/unescaped-text-in-rexml/23400 "2005-12-18T20:43:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Farrel\_Lifson](https://avatars.discourse-cdn.com/v4/letter/f/3da27b/32.png) [@Farrel\_Lifson](https://rubytalk.org/u/Farrel_Lifson)\
**Post date:** [18 December 2005 20:43 UTC](https://rubytalk.org/t/unescaped-text-in-rexml/23400/1 "2005-12-18T20:43:12Z")

</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 \<SCRIPT\> 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:** ![Eero\_Saynatkari1](https://avatars.discourse-cdn.com/v4/letter/e/46a35a/32.png) [@Eero\_Saynatkari1](https://rubytalk.org/u/Eero_Saynatkari1)\
**Post date:** [18 December 2005 21:02 UTC](https://rubytalk.org/t/unescaped-text-in-rexml/23400/2 "2005-12-18T21:02:03Z")

</div>

Farrel Lifson 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 \<SCRIPT\> 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:

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

> Farrel

E

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

---

<div class="post-metadata">

**Author:** ![James\_Britt4](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@James\_Britt4](https://rubytalk.org/u/James_Britt4)\
**Post date:** [18 December 2005 21:53 UTC](https://rubytalk.org/t/unescaped-text-in-rexml/23400/3 "2005-12-18T21:53:06Z")

</div>

Farrel Lifson 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 \<SCRIPT\> 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?

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)

\<script type="text/javascript"\>  
&nbsp;&nbsp;//\<![CDATA[  
...  
&nbsp;&nbsp;//]]\>  
&nbsp;&nbsp;&nbsp;\</script\>

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

---

<div class="post-metadata">

**Author:** ![Farrel\_Lifson](https://avatars.discourse-cdn.com/v4/letter/f/3da27b/32.png) [@Farrel\_Lifson](https://rubytalk.org/u/Farrel_Lifson)\
**Post date:** [18 December 2005 21:23 UTC](https://rubytalk.org/t/unescaped-text-in-rexml/23400/4 "2005-12-18T21:23:39Z")

</div>

That worked! Thanks a stack!

---

<div class="post-metadata">

**Author:** ![Farrel\_Lifson](https://avatars.discourse-cdn.com/v4/letter/f/3da27b/32.png) [@Farrel\_Lifson](https://rubytalk.org/u/Farrel_Lifson)\
**Post date:** [18 December 2005 21:25 UTC](https://rubytalk.org/t/unescaped-text-in-rexml/23400/5 "2005-12-18T21:25:58Z")

</div>

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

> **···**
>
> On 12/18/05, Farrel Lifson \<farrel.lifson@gmail.com\> wrote:
> 
> > That worked! Thanks a stack!
