Forum: Ruby Unescaped text in REXML

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
05be5d6610e2c3f1780aa0e39e902e93?d=identicon&s=25 Farrel Lifson (Guest)
on 2005-12-18 21:45
(Received via mailing list)
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
Fe9b2d0628c0943af374b2fe5b320a82?d=identicon&s=25 Eero Saynatkari (rue)
on 2005-12-18 22:01
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:

  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
05be5d6610e2c3f1780aa0e39e902e93?d=identicon&s=25 Farrel Lifson (Guest)
on 2005-12-18 23:20
(Received via mailing list)
That worked! Thanks a stack!
Bc6d88907ce09158581fbb9b469a35a3?d=identicon&s=25 James Britt (Guest)
on 2005-12-18 23:20
(Received via mailing list)
Farrel Lifson 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/c...

<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
05be5d6610e2c3f1780aa0e39e902e93?d=identicon&s=25 Farrel Lifson (Guest)
on 2005-12-21 21:54
(Received via mailing list)
Although I removed the 'm' at the end of the illegal parameter. It
seemed to be matching to '' otherwise.
This topic is locked and can not be replied to.