Unescaped text in REXML

F

Farrel Lifson

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 =3D 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
 
E

Eero Saynatkari

Farrel said:
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:

element.text =
::REXML::Text::new(
string_we_want_untouched,
respect_whitespace=true,
parent=nil,
raw=true,
entity_filter=nil,
illegal=%r/.^/m # match nothing!
)



E
 
F

Farrel Lifson

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

James Britt

Farrel said:
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

<script type="text/javascript">
//<![CDATA[
...
//]]>
</script>

James


--

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

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top