Skip to content

Commit

Permalink
Item11722: Item11788 Partial fixes to these tasks
Browse files Browse the repository at this point in the history
Verbatim blocks are only merged if they have identical class attributes.
foswiki* classes are no longer "cleaned" from verbatim blocks
brute-force merge of literal and noautolink tags now only is done when
separated by horizontal white space.

git-svn-id: http://svn.foswiki.org/trunk@14667 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
GeorgeClark authored and GeorgeClark committed Apr 27, 2012
1 parent 459a543 commit 948afe3
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 13 deletions.
3 changes: 3 additions & 0 deletions WysiwygPlugin/lib/Foswiki/Plugins/WysiwygPlugin/HTML2TML.pm
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ sub debugEncode {
sub convert {
my ( $this, $text, $options ) = @_;

#print STDERR "++++++++ CONVERT ENTERED [\n",$text,"\n]--------\n";

$this->{opts} = $options;

my $opts = 0;
Expand Down Expand Up @@ -197,6 +199,7 @@ sub convert {
}

# $text is octets, encoded as per the $Foswiki::cfg{Site}{CharSet}
#print STDERR "++++++++ CONVERT DONE [\n",$text,"\n]--------\n";
return $text;
}

Expand Down
73 changes: 61 additions & 12 deletions WysiwygPlugin/lib/Foswiki/Plugins/WysiwygPlugin/HTML2TML/Node.pm
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ sub hasClass {
return hasClass( $this->{attrs}, $class );
}
return 0 unless defined $this->{class};

#print STDERR "HASCLASS tests $this->{class} for $class\n";
return $this->{class} =~ /\b$class\b/ ? 1 : 0;
}

Expand All @@ -163,6 +165,8 @@ sub _removeClass {
return _removeClass( $this->{attrs}, $class );
}
return 0 unless hasClass( $this, $class );

#print STDERR "DDDDDDD DELETE $class from $this->{class}\n";
$this->{class} =~ s/\b$class\b//;
$this->{class} =~ s/\s+/ /g;
$this->{class} =~ s/^\s+//;
Expand Down Expand Up @@ -289,13 +293,17 @@ s/$WC::CHECKw(($WC::PON|$WC::POFF)?[$WC::CHECKn$WC::CHECKs$WC::NBSP $WC::NBBR])/

# isolate $NBBR and convert to \n.
unless ($protect) {

#print STDERR "NBBR START [",WC::debugEncode($tml),"]\n";
$tml =~ s/\n$WC::NBBR/$WC::NBBR$WC::NBBR/go;
$tml =~ s/$WC::NBBR\n/$WC::NBBR$WC::NBBR/go;
$tml =~ s/$WC::NBBR( |$WC::NBSP)+$WC::NBBR/$WC::NBBR$WC::NBBR/go;
$tml =~ s/ +$WC::NBBR/$WC::NBBR/go;
$tml =~ s/$WC::NBBR +/$WC::NBBR/go;
$tml =~ s/$WC::NBBR$WC::NBBR+/$WC::NBBR$WC::NBBR/go;

#print STDERR "NBBR collapse [",WC::debugEncode($tml),"]\n";

# Now convert adjacent NBBRs to recreate empty lines
# 1 NBBR -> 1 newline
# 2 NBBRs -> <p /> - 1 blank line - 2 newlines
Expand All @@ -308,6 +316,8 @@ s/$WC::CHECKw(($WC::PON|$WC::POFF)?[$WC::CHECKn$WC::CHECKs$WC::NBSP $WC::NBBR])/
$tml =~ s.($WC::NBBR$WC::NBBR$WC::NBBR$WC::NBBR+).
"\n" x ((length($1) + 1) / 2 + 1)
.geo;

#print STDERR "NBBR done [",WC::debugEncode($tml),"]\n";
}

# isolate $CHECKn and convert to $NBBR
Expand All @@ -319,6 +329,8 @@ s/$WC::CHECKw(($WC::PON|$WC::POFF)?[$WC::CHECKn$WC::CHECKs$WC::NBSP $WC::NBBR])/

$tml =~ s/$WC::NBBR/\n/gos;

#print STDERR "Isolate CHECKn done [",WC::debugEncode($tml),"]\n";

# Convert tabs to NBSP
$tml =~ s/$WC::TAB/$WC::NBSP$WC::NBSP$WC::NBSP/go;

Expand All @@ -329,6 +341,8 @@ s/$WC::CHECKw(($WC::PON|$WC::POFF)?[$WC::CHECKn$WC::CHECKs$WC::NBSP $WC::NBBR])/
}
$tml =~ s/$WC::NBSP/ /go;

#print STDERR "Tabs and NBSP done [",WC::debugEncode($tml),"]\n";

$tml =~ s/$WC::CHECK1$WC::CHECK1+/$WC::CHECK1/go;
$tml =~ s/$WC::CHECK2$WC::CHECK2+/$WC::CHECK2/go;
$tml =~ s/$WC::CHECK2$WC::CHECK1/$WC::CHECK2/go;
Expand All @@ -347,16 +361,21 @@ s/$WC::CHECKw(($WC::PON|$WC::POFF)?[$WC::CHECKn$WC::CHECKs$WC::NBSP $WC::NBBR])/
$tml =~ s/<br( \/)?>\n/\n/g;
}

#print STDERR WC::debugEncode($before);
#print STDERR " -> '",WC::debugEncode($tml),"'\n";
#print STDERR " -> [",WC::debugEncode($tml),"]\n";
$text .= $tml;
}

# Collapse adjacent tags
foreach my $tag (qw(noautolink verbatim literal)) {
$text =~ s#</$tag>(\s*)<$tag>#$1#gs;
#print STDERR "before Collapse [",WC::debugEncode($text),"]\n";
# SMELL: Can't collapse verbatim based upon simple close/open compare
# because the previous opening verbatim tag might have different
# class from the next one.
foreach my $tag (qw(noautolink literal)) {
$text =~ s#</$tag>(\h*)<$tag>#$1#gs;
}

#print STDERR "After Collapse [",WC::debugEncode($text),"]\n";

# Top and tail, and terminate with a single newline
$text =~ s/^\n*//s;
$text =~ s/\s*$/\n/s;
Expand All @@ -366,6 +385,17 @@ s/$WC::CHECKw(($WC::PON|$WC::POFF)?[$WC::CHECKn$WC::CHECKs$WC::NBSP $WC::NBBR])/
return $text;
}

sub _compareClass {
my ( $node1, $node2 ) = @_;

my $n1Class = $node1->{attrs}->{class} || '';
my $n1Sort = join( ' ', sort( split( / /, $n1Class ) ) );
my $n2Class = $node2->{attrs}->{class} || '';
my $n2Sort = join( ' ', sort( split( / /, $n2Class ) ) );

return ( $n1Sort eq $n2Sort );
}

# collapse adjacent nodes together, if they share the same class
sub _collapseOneClass {
my $node = shift;
Expand All @@ -379,11 +409,14 @@ sub _collapseOneClass {
&& (
( !$next->{tag} && $next->{text} =~ /^\s*$/ )
|| ( $node->{tag} eq $next->{tag}
&& $next->hasClass($class) )
&& $next->hasClass($class)
&& ( _compareClass( $node, $next ) ) )
)
)
{
push( @edible, $next );

#print STDERR "Added $next->{tag} to edible\n";
$collapsible ||= $next->hasClass($class);
$next = $next->{next};
}
Expand Down Expand Up @@ -414,6 +447,10 @@ sub _collapse {
my @jobs = ($this);
while ( scalar(@jobs) ) {
my $node = shift(@jobs);

# SMELL: Not sure if we really still have to collapse consecutive verbatim.
# Extra whitespace to separate verbatim blocks is removed, and they will
# still eventually be merged.
_collapseOneClass( $node, 'TMLverbatim' );
_collapseOneClass( $node, 'WYSIWYG_STICKY' );
if ( $node->{tag} eq 'p'
Expand Down Expand Up @@ -629,16 +666,27 @@ sub _htmlParams {
my @classes;
$v =~ s/^\s*(.*?)\s*$/$1/;
CLASS: for my $class ( split /\s+/, $v ) {

#print STDERR "_htmlParams considering CLASS $class\n";
next CLASS unless $class =~ /\S/;

#print STDERR " .. passed S test\n";
next CLASS if $tml2htmlClass{$class};

#print STDERR " .. passed tml2htmlClass test\n";

# if cleaning aggressively, remove class attributes
# except for the JQuery "Chili" classes
next CLASS
if ( $options & $WC::VERY_CLEAN
and not $jqueryChiliClass{$class} );
if ( $options & $WC::VERY_CLEAN
and not $jqueryChiliClass{$class}
and not $class =~ /^foswiki/ );

#print STDERR " .. passed VERY_CLEAN test\n";

push @classes, $class;

#print STDERR "Accepted CLASS $class\n";
}
next ATTR unless @classes;

Expand Down Expand Up @@ -1281,6 +1329,8 @@ sub _verbatim {
# &nbsp; decodes to \240, which we want to make a space.
$text =~ s/\240/$WC::NBSP/g;
my $p = _htmlParams( $this->{attrs}, $options );

#print STDERR "BUILT VERBATIM: <$tag$p>$text</$tag>";
return ( $flags, "<$tag$p>$text</$tag>" );
}

Expand Down Expand Up @@ -1806,11 +1856,10 @@ sub _handleSPAN {
delete $atts{class};
}

if ( $options & $WC::VERY_CLEAN ) {

# remove style attribute if cleaning aggressively.
# delete $atts{style} if defined $atts{style};
}
# if ( $options & $WC::VERY_CLEAN ) {
# remove style attribute if cleaning aggressively.
# delete $atts{style} if defined $atts{style};
# }

# ignore the span tag if there are no other attrs
if ( scalar( keys %atts ) == 0 ) {
Expand Down
10 changes: 9 additions & 1 deletion WysiwygPlugin/lib/Foswiki/Plugins/WysiwygPlugin/TML2HTML.pm
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,9 @@ sub _dropBack {
sub _dropIn {
my ( $this, $n, $protecting ) = @_;
my $thing = $this->{refs}->[$n];
my $text = $thing->{text};

my $text = $thing->{text};
#print STDERR "DROPPING IN $text\n";

# Drop back recursively
$text = $this->_dropBack( $text, $protecting || $thing->{protect} );
Expand Down Expand Up @@ -256,6 +257,8 @@ sub _dropIn {
_addClass( $thing->{params}->{class}, $thing->{class} ) if $thing->{class};

no strict 'refs';

#print STDERR "RETURNED ",&$method( $thing->{params}, $text );
return &$method( $thing->{params}, $text );
use strict 'refs';
}
Expand Down Expand Up @@ -745,6 +748,7 @@ s/((^|(?<=[-*\s(]))$Foswiki::regex{linkProtocolPattern}:[^\s<>"]+[^\s*.,!?;:)<])
and $this->{refs}->[$1]->{text} =~ /^\n?%/ )
{

#print STDERR "------- NEWLINE IS ALREADY PROTECTED\n";
# The newline is already protected
$whitespace = "";
}
Expand All @@ -756,6 +760,8 @@ s/((^|(?<=[-*\s(]))$Foswiki::regex{linkProtocolPattern}:[^\s<>"]+[^\s*.,!?;:)<])
}
unless ( $inParagraph or $inDiv ) {
unless ($inHTMLTable) {

#print STDERR "pushed <p>\n";
push( @result, '<p>' );
$inParagraph = 1;
}
Expand Down Expand Up @@ -785,6 +791,8 @@ s/((^|(?<=[-*\s(]))$Foswiki::regex{linkProtocolPattern}:[^\s<>"]+[^\s*.,!?;:)<])

$text = join( "\n", @result );

#print STDERR "BEFORE DROPINS \n[",$text,"]\n";

# Trim any extra Ps from the top and bottom.
$text =~ s#^(\s*<p>\s*</p>)+##s;
$text =~ s#(<p>\s*</p>\s*)+$##s;
Expand Down
56 changes: 56 additions & 0 deletions WysiwygPlugin/test/unit/WysiwygPlugin/TranslatorTests.pm
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,62 @@ _this_ _should_ _italicise_ _each_ _word_
*and* *this* *should* *embolden* *each* *word*
_mixing_ *them* _should_ *work*
HERE
},
{
exec => $TML2HTML | $HTML2TML | $ROUNDTRIP,
name => 'hiddenVerbatim',
tml => <<'HERE',
<verbatim class="foswikiHidden">
hidden verbatim
</verbatim>
HERE
html => <<'HERE',
<p><pre class="foswikiHidden TMLverbatim"><br />hidden&nbsp;verbatim<br /></pre>
</p>
HERE
},
{

# SMELL: This test will fail if run through TMCE Editor.
# TMCE removes the surrounding <p>..</p> tags which
# looses the whitespace, and the tags are merged by HTML2TML.
exec => $TML2HTML | $HTML2TML | $ROUNDTRIP,
name => 'consecutiveVerbatim',
tml => <<'HERE',
<verbatim>
verbatim 1
</verbatim>
<verbatim>
verbatim 2
</verbatim>
HERE
html => <<'HERE',
<p><pre class="TMLverbatim"><br />verbatim&nbsp;1<br /></pre>
</p>
<p><pre class="TMLverbatim"><br />verbatim&nbsp;2<br /></pre>
</p>
HERE
},
{
exec => $ROUNDTRIP | $TML2HTML | $HTML2TML,
name => 'preserveClass',
html => <<'HERE',
<p><pre class="foswikiHidden TMLverbatim"><br />Verbatim&nbsp;1<br />Line&nbsp;2<br />Line&nbsp;3</pre> <pre class="html tml TMLverbatim"><br />Verbatim&nbsp;2<br /><br /></pre><span style="{encoded:'n'}" class="WYSIWYG_HIDDENWHITESPACE"> </span><pre class="tml html TMLverbatim"><br /><br />Verbatim&nbsp;3</pre>
</p>
HERE
tml => <<'HERE',
<verbatim class="foswikiHidden">
Verbatim 1
Line 2
Line 3</verbatim> <verbatim class="html tml">
Verbatim 2
</verbatim>
<verbatim class="tml html">
Verbatim 3</verbatim>
HERE
},
{
Expand Down

0 comments on commit 948afe3

Please sign in to comment.