package Markup::Tag;

use base 'Object';

use strict; use warnings;

sub parse {
	my ($pkg, $tag) = @_;
	$tag =~ s,^(\s*)(.*?)(\s*)\z,$2,s;
	my ($space_before, $space_after) = ($1, $3);
	my $e;

	if ($tag =~ m,^<!--(.*?)-->$,s) {
		# comment
		$e = Markup::Comment->new($1);
	} elsif ($tag =~ m,^<!(.*?)>,s) {
		# declaration
		$e = Markup::Declaration->new($1);
	} elsif ($tag =~ m,^</(.*?)>,s) {
		# close tag
		$e = Markup::Close->new($1);
	} elsif ($tag =~ m,^<([^>]*?)(/?)>$,s) {
		# open or single tag
		my $single = $2 ne "";
		$e = Markup::Open->new($1, $single);
	} else {
		# brokenness
		die "unknown sort of tag:\n$tag";
	}

	$e->{space_before} = $space_before;
	$e->{space_after} = $space_after;

	if ($e->can("ensure_required_spacing")) {
		$e->ensure_required_spacing;
	}
	
	return $e;
}

sub new {
	my ($class) = @_;
	my $self = $class->SUPER::new({
		space_before => '',
		space_after => '',
	});
	return $self;
}

sub html {
	my ($self) = @_;
	return $self->{space_before} .
		$self->html_without_padding_spaces .
		$self->{space_after};
}

1
