Skip to content

Commit

Permalink
Item11353: Item12888: AutoTOC test case was failing
Browse files Browse the repository at this point in the history
Can't remove all comments. Comments on a heading line become part of the
heading id. This fixes the failing test cases,
  • Loading branch information
gac410 committed Feb 11, 2015
1 parent 0ce1f63 commit 28dc5dc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
6 changes: 2 additions & 4 deletions UnitTestContrib/test/unit/TOCTests.pm
Expand Up @@ -232,7 +232,7 @@ sub test_Item11353 {
my $url = $this->{session}->getScriptUrl( 0, 'view' );

my $text = <<'HERE';
---+ A level 1 head!line
---+ A level 1 head!line <!--1-->
<!--
---++ Followed by a commented level 2! headline
-->
Expand All @@ -254,11 +254,9 @@ HERE
$res2 = $topicObject->renderTML($res2);
$topicObject->finish();

#return;

my $expected = <<HTML;
<div class="foswikiToc" id="foswikiTOC"> <ul>
<li> <a href="$url/TemporaryTOCTestsTestWebTOCTests/TestTopicTOCTests#A_level_1_head_33line">A level 1 head!line</a>
<li> <a href="$url/TemporaryTOCTestsTestWebTOCTests/TestTopicTOCTests#A_level_1_head_33line_60_33_45_451_45_45_62">A level 1 head!line <!--1--> </a>
</li></ul>
</div>
HTML
Expand Down
20 changes: 19 additions & 1 deletion core/lib/Foswiki/Macros/TOC.pm
Expand Up @@ -83,7 +83,14 @@ sub TOC {
my %junk;
$text = Foswiki::takeOutBlocks( $text, 'verbatim', \%junk );
$text = Foswiki::takeOutBlocks( $text, 'pre', \%junk );

# Item11353. Remove HTML comments, but not comments that are on the heading line itself
# Comments on the line become part of the heading ID, so they are needed.
# SMELL: This is not perfect. Multi-line comments that start on a heading
# line are going have issues.
$text =~ s/^(---+.*?)<!--(.*?)$/_protectComments($1,$2)/mge;
$text =~ s/<!--.*?-->//sg; #Brute force, Remove html comments
$text =~ s/\0/</g; # restore "protected" comments

my $maxDepth = $params->{depth};
$maxDepth ||= $session->{prefs}->getPreference('TOC_MAX_DEPTH')
Expand Down Expand Up @@ -245,11 +252,22 @@ sub TOC {
}
}

# Temporarily protect HTML comments that are on a ---+.. heading line.
sub _protectComments {
my ( $left, $right ) = @_;
if ( $right =~ m/-->/ ) {
return $left . "\0!--" . $right;
}
else {
return $left . '<!..' . $right;
}
}

1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2008-2012 Foswiki Contributors. Foswiki Contributors
Copyright (C) 2008-2015 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.
Expand Down

0 comments on commit 28dc5dc

Please sign in to comment.