HTML::Element->asHTML patch for whitespace

Andreas Koenig (k@anna.mind.de)
Mon, 18 Sep 1995 21:47:29 +0200


This patch solves the 'intruding whitespace problem' of the asHTML
method according to a suggestion by Gerard Hickey
<hickey@ctron.com>. Thanks, Gerard!!

That's the outcome:

RAW DATA:
<H2>The Perl 5 Module List databasified</H2>
The plan is to have a master server (<I>franz</I>) that collects


BEFORE:
    <H2>The Perl 5 Module List databasified
    </H2>
    <P> The plan is to have a master server (
      <I>franz
      </I>
      ) that collects efficiently new modules by allowing uploads to


AFTER:
    <H2>The Perl 5 Module List databasified
    </H2>
    <P> The plan is to have a master server (<I
      >franz</I
      >) that collects efficiently new modules by allowing uploads to

Hope, you like it,
andreas

*** /usr/sources/perl/module/lwp/libwww-perl-5b5/lib/HTML/Element.pm	Sat Sep 16 11:29:16 1995
--- Element.pm	Mon Sep 18 21:19:13 1995
***************
*** 67,72 ****
--- 67,80 ----
  );
  
  
+ # Elements that act as paragraph 
+ for (qw(p form h1 h2 h3 h4 h5 h6
+ 	blockquote hr title body
+        )){
+     $blockElements{$_} = 1;
+ }
+ 
+ 
  
  =item new HTML::Element 'tag', 'attrname' => 'value',...
  
***************
*** 463,469 ****
  
  =item ->asHTML()
  
! Retuns a string (the HTML document) that represents the element and
  its children.
  
  =cut
--- 471,477 ----
  
  =item ->asHTML()
  
! Returns a string (the HTML document) that represents the element and
  its children.
  
  =cut
***************
*** 472,489 ****
  {
      my $self = shift;
      my $depth = shift || 0;
      my $tag = $self->tag;
      my $pre = $self->isInside('pre');
      my $html = '';
!     $html .= "  " x $depth unless $pre;
!     $html .= $self->starttag;
  
      my $pos = 0;
  
      for (@{$self->{_content}}) {
  	if (ref $_) {
! 	    $html .= "\n" unless $pre;
! 	    $html .= $_->asHTML($depth+1);
  	} else {
  	    if ($pre) {
  		$html .= "$_";
--- 480,509 ----
  {
      my $self = shift;
      my $depth = shift || 0;
+     my $black = shift || 0; # protect string from whitespace
      my $tag = $self->tag;
      my $pre = $self->isInside('pre');
      my $html = '';
!     if ($pre) {
! 	$html .= $self->starttag;
!     } else {
! 	if ($black) {
! 	    $html .= substr($self->starttag,0,length($self->starttag)-1)
! 		. "\n" . ("  " x $depth) . ">" ;
! 	} else {
! 	    $html .= "  " x $depth;
! 	    $html .= $self->starttag;
! 	}
!     }
  
      my $pos = 0;
  
      for (@{$self->{_content}}) {
  	if (ref $_) {
! 	    unless ($pre || $black) {
! 		$html .= "\n";
! 	    }
! 	    $html .= $_->asHTML($depth+1,$black);
  	} else {
  	    if ($pre) {
  		$html .= "$_";
***************
*** 491,513 ****
  		if ($pos + length $_ < 60) {
  		    $html .= $_;
  		    $pos += length $_;
! 		    next;
  		}
! 		my $copy = $_;
! 		while ($copy =~ s/^(.{60,}?)\s//) {
! 		    $html .= "\n" . ("  " x ($depth+1)) . $1;
  		}
! 		$html .= "\n" . ("  " x ($depth+1)) . $copy;
! 		$pos = length $copy;
  	    }
  	}
      }
      unless ($noEndTag{$tag} || $tag eq 'p') {
! 	unless ($pre) {
! 	    $html .= "\n";
! 	    $html .= "  " x $depth;
  	}
- 	$html .= $self->endtag;
      }
      $html .= "\n" if $depth == 0;
      $html;
--- 511,550 ----
  		if ($pos + length $_ < 60) {
  		    $html .= $_;
  		    $pos += length $_;
! 		} else {
! 		    my $copy = $_;
! 		    my @m;
! 		    while ($copy =~ s/^(.{60,}?)\s//) {
! 			push @m,  $1;
! 		    }
! 		    $html .= join "\n" . "  " x ($depth+1), @m, $copy;
! 		    $pos = length $copy;
  		}
! 		if (substr($html,length($html)-1) =~ /\s/) {
! 		    $black = 0;
! 		    $html .= "\n" . "  " x ($depth+1);
! 		    $pos = 0;
! 		} else {
! 		    $black = 1;
  		}
! 		next;
  	    }
  	}
      }
      unless ($noEndTag{$tag} || $tag eq 'p') {
! 	if ($pre) {
! 	    $html .= $self->endtag;
! 	} else {
! 	    $black = 0 if $blockElements{$tag};
! 	    if ($black) {
! 		$html .= substr($self->endtag,0,length($self->endtag)-1)
! 		    . "\n" . ("  " x $depth) . ">" ;
! 	    } else {
! 		$html .= "\n";
! 		$html .= "  " x $depth;
! 		$html .= $self->endtag;
! 	    }
  	}
      }
      $html .= "\n" if $depth == 0;
      $html;