[Bps-public-commit] r11940 - in HTML-RewriteAttributes: . lib/HTML lib/HTML/RewriteAttributes

sartak at bestpractical.com sartak at bestpractical.com
Mon Apr 28 21:54:25 EDT 2008


Author: sartak
Date: Mon Apr 28 21:54:23 2008
New Revision: 11940

Modified:
   HTML-RewriteAttributes/   (props changed)
   HTML-RewriteAttributes/lib/HTML/RewriteAttributes.pm
   HTML-RewriteAttributes/lib/HTML/RewriteAttributes/Resources.pm
   HTML-RewriteAttributes/t/001-basic.t

Log:
 r54624 at onn:  sartak | 2008-04-28 21:34:04 -0400
 Revise how the callback is invoked, make tests more exacting


Modified: HTML-RewriteAttributes/lib/HTML/RewriteAttributes.pm
==============================================================================
--- HTML-RewriteAttributes/lib/HTML/RewriteAttributes.pm	(original)
+++ HTML-RewriteAttributes/lib/HTML/RewriteAttributes.pm	Mon Apr 28 21:54:23 2008
@@ -63,7 +63,7 @@
         next if $attr eq '/';
 
         if ($self->_should_rewrite($tag, $attr)) {
-            $attrs->{$attr} = $self->{rewrite_callback}->($attrs->{$attr}, tag => $tag, attr => $attr, rewriter => $self);
+            $attrs->{$attr} = $self->_invoke_callback($tag, $attr, $attrs->{$attr});
         }
 
         $self->{rewrite_html} .= sprintf ' %s="%s"',
@@ -80,5 +80,12 @@
     $self->{rewrite_html} .= $text;
 }
 
+sub _invoke_callback {
+    my $self = shift;
+    my ($tag, $attr, $value) = @_;
+
+    return $self->{rewrite_callback}->($tag, $attr, $value);
+}
+
 1;
 

Modified: HTML-RewriteAttributes/lib/HTML/RewriteAttributes/Resources.pm
==============================================================================
--- HTML-RewriteAttributes/lib/HTML/RewriteAttributes/Resources.pm	(original)
+++ HTML-RewriteAttributes/lib/HTML/RewriteAttributes/Resources.pm	Mon Apr 28 21:54:23 2008
@@ -23,5 +23,12 @@
     return ( $rewritable_attrs{$tag} || {} )->{$attr};
 }
 
+sub _invoke_callback {
+    my $self = shift;
+    my ($tag, $attr, $value) = @_;
+
+    return $self->{rewrite_callback}->($value, tag => $tag, attr => $attr, rewriter => $self);
+}
+
 1;
 

Modified: HTML-RewriteAttributes/t/001-basic.t
==============================================================================
--- HTML-RewriteAttributes/t/001-basic.t	(original)
+++ HTML-RewriteAttributes/t/001-basic.t	Mon Apr 28 21:54:23 2008
@@ -2,49 +2,42 @@
 use strict;
 use warnings;
 use HTML::RewriteAttributes::Resources;
-use Test::More tests => 8;
+use Test::More tests => 2;
 
 my $html = << "END";
 <html>
     <body>
         <img src="moose.jpg" />
         <img src="http://example.com/nethack.png">
-        <p align="justified">
+        <p align="justified" style="color: red">
             hooray
         </p>
     </body>
 </html>
 END
 
-my %seen;
+my @seen;
 
-my $rewrote = HTML::RewriteAttributes::Resources->rewrite($html, sub {
-    my $uri  = shift;
-    my %args = @_;
+my $rewrote = HTML::RewriteAttributes->rewrite($html, sub {
+    my ($tag, $attr, $value) = @_;
+    push @seen, [$tag, $attr, $value];
 
-    $seen{uri}{$uri}++;
-    $seen{tag}{$args{tag}}++;
-    $seen{attr}{$args{attr}}++;
-
-    return uc $uri;
+    return uc $value;
 });
 
-is(keys %{ $seen{uri} }, 2, "saw two resources");
-is($seen{uri}{"moose.jpg"}, 1, "saw moose.jpg once");
-is($seen{uri}{"http://example.com/nethack.png"}, 1, "saw http://example.com/nethack.png once");
-
-is(keys %{ $seen{tag} }, 1, "saw one tag");
-is($seen{tag}{img}, 2, "saw img twice");
-
-is(keys %{ $seen{attr} }, 1, "saw one attr");
-is($seen{attr}{src}, 2, "saw src twice");
+is_deeply(\@seen, [
+    [img => src => "moose.jpg"],
+    [img => src => "http://example.com/nethack.png"],
+    [p => align => "justified"],
+    [p => style => "color: red"],
+]);
 
 is($rewrote, << "END", "rewrote the html correctly");
 <html>
     <body>
         <img src="MOOSE.JPG" />
         <img src="HTTP://EXAMPLE.COM/NETHACK.PNG">
-        <p align="justified">
+        <p align="JUSTIFIED" style="COLOR: RED">
             hooray
         </p>
     </body>



More information about the Bps-public-commit mailing list