/[Frey]/trunk/lib/Continuity/Widget/DomNode.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/lib/Continuity/Widget/DomNode.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 69 - (hide annotations)
Wed Jul 9 15:45:59 2008 UTC (15 years, 10 months ago) by dpavlin
File size: 1907 byte(s)
include local copy of Continuity::Widget

$ darcs show repo
          Type: darcs
        Format: darcs-1.0
          Root: /rest/cvs/Continuity-Widget
      Pristine: PlainPristine "_darcs/pristine"
         Cache: thisrepo:/rest/cvs/Continuity-Widget
Default Remote: http://thelackthereof.org/projects/perl/Continuity-Widget
   Num Patches: 29

1 dpavlin 69 package Continuity::Widget::DomNode;
2    
3     use Moose;
4    
5     # This might be just text
6     has text => ( is => 'rw', isa => 'Str', default => undef );
7    
8     # Or it is a tag
9     has name => ( is => 'rw', isa => 'Str', default => undef );
10     has attr => ( is => 'rw', isa => 'HashRef', default => sub {{}} );
11    
12     # And it is part of a heirarchy
13     has parent => ( is => 'rw' );
14     has children => ( is => 'rw', isa => 'ArrayRef', default => sub {[]} );
15    
16     sub create {
17     my ($class, @node_list) = @_;
18     my @new_nodes;
19     while(@node_list) {
20     my $node = shift @node_list;
21     my $self = $class->new;
22     push @new_nodes, $self;
23     if(!(ref $node_list[0])) {
24     if($node eq 'br' || $node eq 'hr') {
25     $self->name($node);
26     } else {
27     $self->text($node);
28     }
29     next;
30     }
31     if(ref $node_list[0] eq 'HASH') {
32     $self->attr( shift @node_list );
33     }
34     if(ref $node_list[0] eq 'ARRAY') {
35     $self->children([$class->create(@{ shift @node_list })]);
36     foreach my $child (@{$self->children}) {
37     $child->parent($self);
38     }
39     }
40     if(%{$self->attr} || @{$self->children}) {
41     $self->name($node);
42     } else {
43     $self->text($node);
44     }
45     }
46     return wantarray ? @new_nodes : $new_nodes[0];
47     }
48    
49     sub siblings {
50     my ($self) = @_;
51     if($self->parent) {
52     my @siblings = @{$self->parent->children};
53     @siblings = grep { $_ != $self } @siblings;
54     return @siblings;
55     }
56     return undef;
57     }
58    
59     sub to_string {
60     my ($self) = @_;
61     if($self->text) {
62     return $self->text;
63     }
64     if($self->name) {
65     my $attrs = '';
66     my ($key, $val);
67     $attrs .= qq{ $key="$val"} while ($key, $val) = each %{$self->attr};
68     if(@{$self->children}) {
69     my $children_string = join '', map { $_->to_string } @{$self->children};
70     return '<' . $self->name . $attrs . '>' . $children_string . '</' . $self->name . '>';
71     } else {
72     return '<' . $self->name . $attrs . ' />';
73     }
74     }
75     }
76    
77    
78     1;
79    

  ViewVC Help
Powered by ViewVC 1.1.26