diff --git a/Changes b/Changes
index 4a066ef..d8a0c91 100644
--- a/Changes
+++ b/Changes
@@ -1,3 +1,6 @@
+ Bug Fixes
+ * Fencepost recrufting order [github 12] (lazarus)
+
20160806 Sat Aug 6 13:36:14 PDT 2016
Distribution
* Homepage is now metacpan
diff --git a/lib/URI/Find.pm b/lib/URI/Find.pm
index ad2ec75..46e5199 100644
--- a/lib/URI/Find.pm
+++ b/lib/URI/Find.pm
@@ -331,13 +331,19 @@ sub decruft {
if( $orig_match =~ s/([\Q$cruftSet\E]+)$// ) {
# urls can end with HTML entities if found in HTML so let's put back semicolons
# if this looks like the case
- my $cruft = $1;
+ my $cruft = my $orig_cruft = $1;
+
if( $cruft =~ /^;/ && $orig_match =~ /\&(\#[1-9]\d{1,3}|[a-zA-Z]{2,8})$/) {
$orig_match .= ';';
$cruft =~ s/^;//;
}
- while( my($open, $close) = each %balanced_cruft ) {
+ my $compare = sub { index( $orig_cruft, $_[ 1 ] )
+ <=> index( $orig_cruft, $_[ 0 ] ) };
+
+ for my $open (sort { $compare->( $a, $b ) } keys %balanced_cruft) {
+ my $close = $balanced_cruft{ $open };
+
$self->recruft_balanced(\$orig_match, \$cruft, $open, $close);
}
@@ -355,9 +361,12 @@ sub recruft_balanced {
my $open_count = () = $$orig_match =~ m{\Q$open}g;
my $close_count = () = $$orig_match =~ m{\Q$close}g;
- if ( $$cruft =~ /\Q$close\E$/ && $open_count == ( $close_count + 1 ) ) {
+ $open eq $close and $open_count and $open_count == $close_count
+ and $open_count % 2 > 0 and $close_count--;
+
+ if ($$cruft =~ m{ \A \Q$close\E }mx && $open_count == ($close_count + 1) ) {
$$orig_match .= $close;
- $$cruft =~ s/\Q$close\E$//;
+ $$cruft =~ s{ \A \Q$close\E }{}mx;
}
return;
diff --git a/t/filter.t b/t/filter.t
index b1dbbc2..5a94a06 100644
--- a/t/filter.t
+++ b/t/filter.t
@@ -30,6 +30,10 @@ my @tasks = (
# Non-URL nested inside brackets
[q{}, q{}],
+ # Does not reorder the fencepost cruft. Issue #12
+ [q{e:('')}, q{e:('')}],
+ [q{ Supporter Number:[*data('1.supporter_number')|html*] },
+ q{ Supporter Number:[*data('1.supporter_number')|html*] }],
);
for my $task (@tasks) {