Skip to content

Commit

Permalink
Item9259: Item11312: Protect markup in tables
Browse files Browse the repository at this point in the history
Also add unit tests and extend some to be ROUNDTRIP tests for more
thorough testing.


Conflicts:

	WysiwygPlugin/test/unit/WysiwygPlugin/TranslatorTests.pm

git-svn-id: http://svn.foswiki.org/branches/Release01x01@14562 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
GeorgeClark authored and GeorgeClark committed Apr 5, 2012
1 parent 7e204ca commit f6c2554
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 56 deletions.
72 changes: 44 additions & 28 deletions WysiwygPlugin/lib/Foswiki/Plugins/WysiwygPlugin/TML2HTML.pm
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,14 @@ WARNING
$this->{protectExistingTags}->{$tag} = 1;
}

my $tagBlocksToProtect = Foswiki::Func::getPreferencesValue(
'WYSIWYGPLUGIN_PROTECT_TAG_BLOCKS')
my $tagBlocksToProtect =
Foswiki::Func::getPreferencesValue('WYSIWYGPLUGIN_PROTECT_TAG_BLOCKS')
|| 'script,style';
foreach my $tag ( split /[,\s]+/, $tagBlocksToProtect ) {
next unless $tag =~ /^\w+$/;
$this->{protectExistingTagBlocks}->{$tag} = 1;
}


# Convert TML to HTML for wysiwyg editing

$content =~ s/[$TT0$TT1$TT2]/?/go;
Expand Down Expand Up @@ -397,7 +396,7 @@ sub _getRenderedVersion {
$text = $this->_liftOutBlocks( $text, 'pre', {} );

# protect some automatic sticky tags.
foreach my $stickyTag ( keys %{$this->{protectExistingTagBlocks}} ) {
foreach my $stickyTag ( keys %{ $this->{protectExistingTagBlocks} } ) {
$text =~ s/(<(?i:$stickyTag)[^>]*>.*?<\/(?i:$stickyTag)>)/
$this->_liftOut($1, 'PROTECTED')/geis;
}
Expand All @@ -406,7 +405,8 @@ sub _getRenderedVersion {
$text =~ s/(<!--.*?-->)/$this->_liftOut($1, 'PROTECTED')/ges;

# Protect anchors
$text =~ s/^($Foswiki::regex{anchorRegex})/$this->_liftOut("\n$1", 'PROTECTED')/gems;
$text =~
s/^($Foswiki::regex{anchorRegex})/$this->_liftOut("\n$1", 'PROTECTED')/gems;

# Handle inline IMG tags specially
$text =~ s/(<img [^>]*>)/$this->_takeOutIMGTag($1)/gei;
Expand Down Expand Up @@ -541,24 +541,28 @@ s/((^|(?<=[-*\s(]))$Foswiki::regex{linkProtocolPattern}:[^\s<>"]+[^\s*.,!?;:)<])
foreach my $line ( split( /\n/, $text ) ) {
my $tableEnded = 0;

# Native HTML tables should pass through to editor unmodified as possible
# However need to preserve blank lines. On save, table will be converted
# to TML if compatible.
if ( $inHTMLTable ) {
if ($line =~ m/<\/table>/i ) {
push( @result, $line );
if ($inHTMLTable) {
if ( $line =~ m/<\/table>/i ) {
$inHTMLTable = 0;
$this->_addListItem( \@result, '', '', '' ) if $inList;
$inList = 0;
push( @result, $line );
next;
}
elsif ( !$line || $line =~ m/^\s*$/ ) {
push( @result, '</p>' ) if ($inParagraph);
push( @result, '<p></p>' );
$inParagraph = 0;
next;
}
$line = '<p></p>' if ( !$line || $line =~ m/^\s*$/);
push( @result, $line );
next;
}

if ( $line =~ m/<table/i ) {
$inHTMLTable = 1;
push( @result , $line );
next;
if ($inParagraph) {
push( @result, '</p>' );
$inParagraph = 0;
}
}

# Table: | cell | cell |
Expand Down Expand Up @@ -613,13 +617,19 @@ s/((^|(?<=[-*\s(]))$Foswiki::regex{linkProtocolPattern}:[^\s<>"]+[^\s*.,!?;:)<])
push( @result, '</p>' ) if $inParagraph;
$inParagraph = 0;

$line = '<p' . $class . '>';

$this->_addListItem( \@result, '', '', '', '' ) if $inList;
$inList = 0;
if ($inHTMLTable) {
$line = '<p' . $class . '></p>';
$this->_addListItem( \@result, '', '', '', '' ) if $inList;
$inList = 0;
}
else {
$line = '<p' . $class . '>';

$inParagraph = 1;
$this->_addListItem( \@result, '', '', '', '' ) if $inList;
$inList = 0;

$inParagraph = 1;
}
}
elsif ( $line =~
s/^((\t| )+)\$\s(([^:]+|:[^\s]+)+?):\s/<dt> $3 <\/dt><dd> /o )
Expand Down Expand Up @@ -654,8 +664,9 @@ s/((^|(?<=[-*\s(]))$Foswiki::regex{linkProtocolPattern}:[^\s<>"]+[^\s*.,!?;:)<])
$line =~ s/^(<li>)\s*$/$1&nbsp;/;

}
elsif ( $spi &&
$line =~ s/^((\t| )+): /<div class='foswikiIndent'> /o ) {
elsif ($spi
&& $line =~ s/^((\t| )+): /<div class='foswikiIndent'> /o )
{

# Indent pseudo-list
$this->_addListItem( \@result, '', 'div', 'class="foswikiIndent"',
Expand Down Expand Up @@ -718,7 +729,7 @@ s/((^|(?<=[-*\s(]))$Foswiki::regex{linkProtocolPattern}:[^\s<>"]+[^\s*.,!?;:)<])
# Other line
$this->_addListItem( \@result, '', '', '' ) if $inList;
$inList = 0;
if ( $inParagraph
if ( ( $inParagraph or $inHTMLTable )
and @result
and $result[-1] !~ /<p(?: class='[^']+')?>$/ )
{
Expand All @@ -740,12 +751,16 @@ s/((^|(?<=[-*\s(]))$Foswiki::regex{linkProtocolPattern}:[^\s<>"]+[^\s*.,!?;:)<])
if length($whitespace);
}
unless ( $inParagraph or $inDiv ) {
push( @result, '<p>' );
$inParagraph = 1;
unless ($inHTMLTable) {
push( @result, '<p>' );
$inParagraph = 1;
}
}
$line =~ s/(\s\s+)/$this->_hideWhitespace($1)/ge;
$result[-1] .= $line;
$line = '';
if ( defined $result[-1] ) {
$result[-1] .= $line;
$line = '';
}
}

push( @result, $line ) if length($line) > 0;
Expand Down Expand Up @@ -806,6 +821,7 @@ s/$WC::STARTWW(($Foswiki::regex{webNameRegex}\.)?$Foswiki::regex{wikiWordRegex}(
$text = '<p class="foswikiDeleteMe">&nbsp;</p>' . $text;
}

#print STDERR "DEBUG\n$text\n";
return $text;
}

Expand Down
78 changes: 50 additions & 28 deletions WysiwygPlugin/test/unit/WysiwygPlugin/TranslatorTests.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2647,14 +2647,28 @@ Mad Fish',
},
{
name => 'Item5076',
exec => $HTML2TML,
exec => $TML2HTML | $HTML2TML | $ROUNDTRIP,
html => <<HERE,
<table border="0"><tbody><tr><td><h2>Argh</h2><ul><li>Ergh&nbsp;</li></ul></td><td>&nbsp;</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td></tr></tbody></table>
<p class="foswikiDeleteMe">&nbsp;</p><table border="0"><tbody><tr><td>
<h2 class="TML"> Argh </h2>
<ul>
<li> Ergh
</li>
</ul>
</td><td> </td></tr><tr><td> </td><td> </td></tr></tbody></table>
HERE
tml => '<table border="0"><tbody><tr><td>
tml => <<'HERE',
<table border="0"><tbody><tr><td>
---++ Argh
* Ergh
</td><td> </td></tr><tr><td> </td><td> </td></tr></tbody></table>',
</td><td> </td></tr><tr><td> </td><td> </td></tr></tbody></table>
HERE
finaltml => <<'HERE',
<table border="0"><tbody><tr><td>
---++ Argh
* Ergh
</td><td> </td></tr><tr><td> </td><td> </td></tr></tbody></table>
HERE
},
{
name => 'Item5132',
Expand Down Expand Up @@ -2917,28 +2931,26 @@ HERE
</table>
HERE
html => <<'HERE',
<p class="foswikiDeleteMe">&nbsp;</p>
<table border="1" cellpadding="0" cellspacing="1">
<tbody>
<tr>
<td>A</td>
<p class="foswikiDeleteMe">&nbsp;</p><table border="1" cellpadding="0" cellspacing="1"> <span style="{encoded:'ns3'}" class="WYSIWYG_HIDDENWHITESPACE"> </span><tbody> <span style="{encoded:'ns6'}" class="WYSIWYG_HIDDENWHITESPACE"> </span><tr> <span style="{encoded:'ns9'}" class="WYSIWYG_HIDDENWHITESPACE"> </span><td>A</td> <span style="{encoded:'ns9'}" class="WYSIWYG_HIDDENWHITESPACE"> </span><td>B
<p></p><span style="{encoded:'ns12'}" class="WYSIWYG_HIDDENWHITESPACE"> </span>C<span style="{encoded:'ns9'}" class="WYSIWYG_HIDDENWHITESPACE"> </span></td><span style="{encoded:'s2'}" class="WYSIWYG_HIDDENWHITESPACE"> </span><span style="{encoded:'ns9'}" class="WYSIWYG_HIDDENWHITESPACE"> </span><td>D</td> <span style="{encoded:'ns6'}" class="WYSIWYG_HIDDENWHITESPACE"> </span></tr><span style="{encoded:'s3'}" class="WYSIWYG_HIDDENWHITESPACE"> </span><span style="{encoded:'ns3'}" class="WYSIWYG_HIDDENWHITESPACE"> </span></tbody>
</table>
HERE
finaltml => <<'HERE'
<table border="1" cellpadding="0" cellspacing="1">
<tbody>
<tr>
<td>A</td>
<td>B
<p></p>
C
</td>
<td>D</td>
<td>D</td>
</tr>
</tbody>
</table>
HERE
# This table will convert from html to tml
# Durning the roundtrip
finaltml => <<'HERE'
| A | B <p></p> C | D |
</tbody> </table>
HERE
},
{
exec => $TML2HTML | $ROUNDTRIP,
exec => $TML2HTML,
name => 'protectScriptFromWysiwyg_Item11603',
tml => <<'HERE',
<script option="blah">
Expand Down Expand Up @@ -2972,7 +2984,7 @@ HERE
HERE
},
{
exec => $TML2HTML | $ROUNDTRIP,
exec => $TML2HTML,
name => 'protectAnchorsFromWrap_Item10125',
tml => <<'HERE',
---++ Accepted
Expand All @@ -2981,21 +2993,31 @@ TBD
---++ Approved
blah
HERE
html => <<'HERE',
html => <<'HERE'
<h2 class="TML"> Accepted </h2>
<p>TBD <span style="{encoded:'n'}" class="WYSIWYG_HIDDENWHITESPACE"> </span><span class="WYSIWYG_PROTECTED"><br />#ApprovedTerm</span>
</p>
<h2 class="TML"> Approved </h2>
<p>blah</p>
HERE
},
{
exec => $TML2HTML | $ROUNDTRIP,
name => 'protectHtmlHeadingsInTables_Item9259',
tml => <<'HERE',
<table> <tbody>
<tr> <td> <h3> b </h3> </td> </tr>
</tbody> </table>
HERE
html => <<'HERE',
<p class="foswikiDeleteMe">&nbsp;</p><table> <tbody> <span style="{encoded:'n'}" class="WYSIWYG_HIDDENWHITESPACE"> </span><tr> <td> <h3> b </h3> </td> </tr>
</tbody> </table>
HERE
finaltml => <<'HERE'
---++ Accepted
TBD
#ApprovedTerm
---++ Approved
blah
<table> <tbody>
<tr> <td>
---+++ b
</td> </tr> </tbody> </table>
HERE
},
];
Expand Down

0 comments on commit f6c2554

Please sign in to comment.