Re: HTML::Tree - literal attributes get escaped anyway [PATCH]
am 28.01.2006 00:34:35 von metaperl------=_Part_9340_26657691.1138404875945
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Ok test case now passes. Attached file updates Changes, a test case
and HTML/Element.pm as needed.
On 1/27/06, Terrence Brannon
> 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:
>
>
v'">
>
> but that is currently impossible. It gets rendered like this:
>
>
llcsv'">
>
> If you try to set the attribute value to a super-literal, then the
> object just gets stringified:
>
>
>
> 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=3Dtbrannon
>
--
Play me in correspondence chess:
http://slowchess.com/profile.php?username=3Dtbrannon
------=_Part_9340_26657691.1138404875945
Content-Type: text/plain; name=patch.txt; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="patch.txt"
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'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
+
+ * 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
------=_Part_9340_26657691.1138404875945--