Skip to content

Commit

Permalink
Item11353: Don't create TOC entries for HTML comments
Browse files Browse the repository at this point in the history
This isn't a particularly accurate regex for precisely matching HTML
comments, but it works for simple comment blocks.

Also clean up a duplicate call to takeOutBlocks.  It only should be
needed once.

Added unit test,  TOC tests pass.
  • Loading branch information
gac410 committed Dec 30, 2014
1 parent 20992c4 commit d788c30
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
44 changes: 44 additions & 0 deletions UnitTestContrib/test/unit/TOCTests.pm
Expand Up @@ -226,6 +226,50 @@ s/<div class="foswikiToc" id="foswikiTOC">/<a name="foswikiTOC"><\/a><div class=
$this->assert_html_equals( $expected, $res2 );
}

sub test_Item11353 {
my $this = shift;

my $url = $this->{session}->getScriptUrl( 0, 'view' );

my $text = <<'HERE';
---+ A level 1 head!line
<!--
---++ Followed by a commented level 2! headline
-->
---++!! Another level 2 headline
HERE
my ($topicObject) =
Foswiki::Func::readTopic( $this->{test_web}, $this->{test_topic} );
$topicObject->text($text);
$topicObject->save();

my $text2 = <<HERE;
%TOC{"$this->{test_web}.$this->{test_topic}"}%
HERE
my ($topicObject2) =
Foswiki::Func::readTopic( $this->{test_web}, $this->{test_topic} . "2" );
$topicObject2->text($text2);
$topicObject->save();
my $res2 = $topicObject2->expandMacros($text2);
$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></ul>
</div>
HTML
if ( $this->check_dependency('Foswiki,<,1.2') ) {
$expected =~
s/<div class="foswikiToc" id="foswikiTOC">/<a name="foswikiTOC"><\/a><div class="foswikiToc">/;
$expected =~ s/<h([1-6]) id="([^"]+)">/<h$1><a name="$2"><\/a>/g;
}
$this->assert_html_equals( $expected, $res2 );
}

sub test_Item2458 {
my $this = shift;

Expand Down
8 changes: 3 additions & 5 deletions core/lib/Foswiki/Macros/TOC.pm
Expand Up @@ -83,6 +83,7 @@ sub TOC {
my %junk;
$text = Foswiki::takeOutBlocks( $text, 'verbatim', \%junk );
$text = Foswiki::takeOutBlocks( $text, 'pre', \%junk );
$text =~ s/<!--.*?-->//sg; #Brute force, Remove html comments

my $maxDepth = $params->{depth};
$maxDepth ||= $session->{prefs}->getPreference('TOC_MAX_DEPTH')
Expand All @@ -97,11 +98,8 @@ sub TOC {
|| '';
$title = CGI::span( { class => 'foswikiTocTitle' }, $title ) if ($title);

my $highest = 99;
my $result = '';
my $verbatim = {};
$text = Foswiki::takeOutBlocks( $text, 'verbatim', $verbatim );
$text = Foswiki::takeOutBlocks( $text, 'pre', $verbatim );
my $highest = 99;
my $result = '';

# Find URL parameters
my $query = $session->{request};
Expand Down

0 comments on commit d788c30

Please sign in to comment.