Re: HTML::Tree - literal attributes get escaped anyway [PATCH]

Terrence Brannon <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.lwp
Message-ID <[email protected]>
Ok test case now passes. Attached file updates Changes, a test case
and HTML/Element.pm as needed.

On 1/27/06, Terrence Brannon <[email protected]> wrote:
> I have added a test to the HTML-Tree svn and plan to post a fix by the
> end of the day, but thought I would give everyone a heads up first.
>
> The idea is that sometimes you want to toss a bit of javascript into
> an attribute. For example, we want the onclick attribute to look like
> this:
>
> <tr class="alternate" onclick="window.location.href='/report/fullcsv'">
>
> but that is currently impossible. It gets rendered like this:
>
> <tr class="alternate" onclick="window.location.href=&#39;/report/fullcsv&#39;">
>
> If you try to set the attribute value to a super-literal, then the
> object just gets stringified:
>
> <tr class="alternate" onclick="HTML::Element=HASH(0x8311dcc)">
>
> So my strategy is to recognize a super-literal and put
> $literal->{text} in as the value of the attribute without calling the
> encode_entities() routine.
>
> That's all for now... more later (hopefully in an hour or two),
> --
> Play me in correspondence chess:
> http://slowchess.com/profile.php?username=tbrannon
>


--
Play me in correspondence chess:
http://slowchess.com/profile.php?username=tbrannon
patch.txt (text/plain, 2.8 KB)
Index: t/tag-rendering.t
===================================================================
--- t/tag-rendering.t	(revision 2459)
+++ t/tag-rendering.t	(working copy)
@@ -1,18 +1,29 @@
 #!perl -w
 
-use Test::More tests => 7;
+use Test::More tests => 8;
 use strict;
 
 BEGIN {
     use_ok( "HTML::Element" );
 }
 
+
 my $img =
     HTML::Element->new( 'img', (
         src => 'damian-conway-in-a-dress.jpg',
         height=>540, width=>100, border=>0,
         alt => "A few bottles of Chech'tluth later...",
     ) );
+
+my $href = '/report/fullcsv';
+my $literal_href = HTML::Element->new(
+  '~literal', 'text' => "window.location.href='$href'"
+ );
+$img->attr(onClick => $literal_href);
+
+
+
+
 isa_ok( $img, 'HTML::Element' );
 my $html = $img->as_HTML;
 print $html, "\n";
@@ -21,4 +32,9 @@
 like( $html, qr/ height="540" /, "Height is quoted" );
 like( $html, qr/ border="0" /,   "Border is quoted" );
 like( $html, qr/ width="100" /,  "Width is quoted" );
+like( 
+  $html, 
+  qr! onclick="window.location.href='$href'!,
+  "Literal text is preserved" 
+ );
 like( $html, qr/ alt="A few bottles of Chech&#39;tluth later..." /, "Alt tag is quoted and escaped" );
Index: lib/HTML/Element.pm
===================================================================
--- lib/HTML/Element.pm	(revision 2459)
+++ lib/HTML/Element.pm	(working copy)
@@ -1865,13 +1865,25 @@
               ? $HTML::Element::boolean_attr{$name}{$_}
               : $HTML::Element::boolean_attr{$name} eq $_)
         ) {
-            $tag .= $html_uc ? " \U$_" : " \L$_";
-        } else { # non-boolean attribute
-            HTML::Entities::encode_entities($val, $entities);
-            $val = qq{"$val"};
-            $tag .= $html_uc ? qq{ \U$_\E=$val} : qq{ \L$_\E=$val};
+	  $tag .= $html_uc ? " \U$_" : " \L$_";
+
+	  }	
+	
+	else { # non-boolean attribute
+
+	  if (ref $val eq 'HTML::Element' and
+		  $val->{_tag} eq '~literal') {
+	    $val = $val->{text};
+	  } else {
+	    HTML::Entities::encode_entities($val, $entities);
+	  }
+
+	  $val = qq{"$val"};
+	  $tag .= $html_uc ? qq{ \U$_\E=$val} : qq{ \L$_\E=$val};
         }
-    }
+      }
+    
+  
     if ( scalar $self->content_list == 0 && $self->_empty_element_map->{ $self->tag } ) {
         return $tag . " />";
     } else {
Index: Changes
===================================================================
--- Changes	(revision 2459)
+++ Changes	(working copy)
@@ -1,8 +1,15 @@
-
 TODO
     Must have all versions in sync.
     Add the info section to the docs.
 
+	
+2006-01-27  metaperl  <[email protected]>
+
+	* modified starttag() so that it could render a literal
+	HTML::Element correctly. added a test case for this in
+	tag-rendering.t 
+
+
 3.19_03 Fri Nov 25 22:20:51 CST 2005
     [THINGS THAT MAY BREAK YOUR CODE]
     * The store_declarations() method has been restored, but defaults
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.