Re: Parsing comments - CSS1 stylesheets

WWW projekt (wwwproj@dna.lth.se)
Fri, 02 Aug 1996 17:35:01 +0200


Stuart Harris wrote:
> 
> I've had a look through the archive at previous suggestions about
> incorporating comment parsing.  Does anyone know if there is a solution
> yet? 

I made a new class, called Comment, that inherited from HTML::Element.
It looks like this:

package MEHL::Comment;
use strict;
use vars qw(@ISA);

use HTML::Element ();

@ISA = qw(HTML::Element);
sub new
{
    my $class = shift;
    my $comment = shift;

    # Make a new element with tag '!--' (could use any name)
    my $self = new HTML::Element('!--');

    # Set hidden variable _comment
    $self->{'_comment'} = $comment;

    # rebless to our class
    bless $self, $class; 
}

sub starttag
{
    my $self = shift;
    return "<!-- " . $self->{'_comment'} . "-->";
}

__END__


After that, you will have to add the method comment to
HTML::TreeBuilder, or you can inherit from TreeBuilder and then
implement comment in that new subclass. comment looks like this:

sub comment
{
    my($self, $comment) = @_;

    # Otherwise it's just a plain comment
    my $el = new MEHL::Comment($comment);
    $self->insert_element($el);
}

A problem is that you will have to add the tag name (!--) to
HTML::Tree::@emptyElement, else TreeBuilder will believe that it needs
an end-tag.

I think that should do it, really. I do not want to promise anything
though - I have done more to TreeBuilder and Element to make them more
easy to inherit from.

Among the things I have changed is that I have made references to the
hashes defined in TreeBuilder.pm and Element.pm into the objects.

(In HTML::Element::new I added:
    $self->{'_emptyElement'} = \%emptyElement;
    $self->{'_optionalEndTag'} = \%optionalEndTag;
    $self->{'_linkElements'} = \%linkElements;
    $self->{'_boolean_attr'} = \%boolean_attr;
    $self->{'_pre_text'} = \%pre_text;)

I have also changed so that TreeBuilder and Element uses these
references instead of the hashes direct. This means that you can inherit
from TreeBuilder and Element and make them understand an extension of
HTML.

I hope this helps.

---
Stefan Eriksson, Lund university, Sweden
wwwproj@dna.lth.se, dat93ser@ludat.lth.se