[Bps-public-commit] r11956 - in HTML-RewriteAttributes: . lib/HTML
sartak at bestpractical.com
sartak at bestpractical.com
Tue Apr 29 16:33:58 EDT 2008
Author: sartak
Date: Tue Apr 29 16:33:55 2008
New Revision: 11956
Added:
HTML-RewriteAttributes/t/004-misc.t
Modified:
HTML-RewriteAttributes/ (props changed)
HTML-RewriteAttributes/lib/HTML/RewriteAttributes.pm
Log:
r54648 at onn: sartak | 2008-04-29 16:33:26 -0400
Returning undef from the callback should delete the attribute
Modified: HTML-RewriteAttributes/lib/HTML/RewriteAttributes.pm
==============================================================================
--- HTML-RewriteAttributes/lib/HTML/RewriteAttributes.pm (original)
+++ HTML-RewriteAttributes/lib/HTML/RewriteAttributes.pm Tue Apr 29 16:33:55 2008
@@ -68,6 +68,7 @@
if ($self->_should_rewrite($tag, $attr)) {
$attrs->{$attr} = $self->_invoke_callback($tag, $attr, $attrs->{$attr});
+ next if !defined($attrs->{$attr});
}
$self->{rewrite_html} .= sprintf ' %s="%s"',
Added: HTML-RewriteAttributes/t/004-misc.t
==============================================================================
--- (empty file)
+++ HTML-RewriteAttributes/t/004-misc.t Tue Apr 29 16:33:55 2008
@@ -0,0 +1,48 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use HTML::RewriteAttributes;
+use Test::More tests => 2;
+
+# make sure returning undef deletes the attribute {{{
+my $html = << "END";
+<html>
+ <body>
+ <img src="moose.jpg" />
+ <img src="http://example.com/nethack.png">
+ <p align="justified" style="color: red">
+ hooray
+ </p>
+ </body>
+</html>
+END
+
+my @seen;
+
+my $rewrote = HTML::RewriteAttributes->rewrite($html, sub {
+ my ($tag, $attr, $value) = @_;
+ push @seen, [$tag, $attr, $value];
+ return if $attr =~ /s/;
+ $value;
+});
+
+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 />
+ <img>
+ <p align="justified">
+ hooray
+ </p>
+ </body>
+</html>
+END
+# }}}
+
More information about the Bps-public-commit
mailing list