File: Node.pm

package info (click to toggle)
movabletype-opensource 5.1.4%2Bdfsg-4%2Bdeb7u3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 32,996 kB
  • sloc: perl: 197,285; php: 62,405; sh: 166; xml: 117; makefile: 83; sql: 32
file content (293 lines) | stat: -rw-r--r-- 6,919 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# Movable Type (r) Open Source (C) 2001-2012 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$

package MT::Template::Node;

use strict;

sub EL_NODE_NAME ()     {0}
sub EL_NODE_TEXT ()     {1}
sub EL_NODE_ATTR ()     {1}
sub EL_NODE_CHILDREN () {2}
sub EL_NODE_VALUE ()    {3}
sub EL_NODE_ATTRLIST () {4}
sub EL_NODE_PARENT ()   {5}
sub EL_NODE_TEMPLATE () {6}

sub NODE_TEXT ()     {1}
sub NODE_BLOCK ()    {2}
sub NODE_FUNCTION () {3}

use MT::Util qw( weaken );

sub mk_wref_accessor {
    my ( $class, $field, $index ) = @_;
    return sub {
        return $_[0]->[$index] unless @_ > 1;
        my $self = shift;
        weaken( $self->[$index] = ( @_ == 1 ? $_[0] : [@_] ) );
    };
}
*parentNode = __PACKAGE__->mk_wref_accessor( 'parentNode', EL_NODE_PARENT );
*template   = __PACKAGE__->mk_wref_accessor( 'template',   EL_NODE_TEMPLATE );

sub new {
    my $pkg = shift;
    my ($param) = ref $_[0] ? $_[0] : {@_};
    my $self = $param->{tag} eq 'TEXT'
        ? [
        'TEXT',
        delete $param->{nodeValue},
        undef,    # unused for TEXT nodes
        undef,
        undef,
        delete $param->{parentNode},
        delete $param->{template},
        ]
        : [
        delete $param->{tag},
        delete $param->{attributes},
        delete $param->{childNodes},
        delete $param->{nodeValue},
        delete $param->{attribute_list},
        delete $param->{parentNode},
        delete $param->{template},
        ];
    bless $self, $pkg;
    weaken( $self->[EL_NODE_PARENT] )   if defined $self->[EL_NODE_PARENT];
    weaken( $self->[EL_NODE_TEMPLATE] ) if defined $self->[EL_NODE_TEMPLATE];
    $self;
}

sub nodeValue {
    my $node = shift;
    my $index = $node->[0] eq 'TEXT' ? EL_NODE_TEXT : EL_NODE_VALUE;
    $node->[$index] = shift if @_;
    $node->[$index];
}

sub childNodes {
    my $node = shift;
    return $node->[EL_NODE_CHILDREN] = shift if @_;
    return $node->[EL_NODE_CHILDREN] ||= [];
}

sub attributes {
    my $node = shift;
    return $node->[EL_NODE_ATTR] = shift if @_;
    return $node->[EL_NODE_ATTR] ||= {};
}

sub attribute_list {
    my $node = shift;
    return $node->[EL_NODE_ATTRLIST] = shift if @_;
    return $node->[EL_NODE_ATTRLIST] ||= [];
}

sub tag {
    return $_[1] ? $_[0]->[EL_NODE_NAME] = $_[1] : $_[0]->[EL_NODE_NAME];
}

sub setAttribute {
    my $node = shift;
    my ( $attr, $val ) = @_;
    if ( $attr eq 'id' ) {

        # assign into ids
        my $tmpl   = $node->template;
        my $ids    = $tmpl->token_ids;
        my $old_id = $node->getAttribute("id");
        if ( $old_id && $ids ) {
            delete $ids->{$old_id};
        }
    }
    elsif ( $attr eq 'class' ) {

        # assign into classes
        my $tmpl      = $node->template;
        my $classes   = $tmpl->token_classes;
        my $old_class = $node->getAttribute("class");
        if ( $old_class && $classes->{$old_class} ) {
            @{ $classes->{$old_class} }
                = grep { $_ != $node } @{ $classes->{$old_class} };
        }
        push @{ $classes->{$val} ||= [] }, $node;
    }
    ( $node->[EL_NODE_ATTR] ||= {} )->{$attr} = $val;
}

sub getAttribute {
    my $node = shift;
    my ($attr) = @_;
    ( $node->attributes )->{$attr};
}

sub firstChild {
    my $node     = shift;
    my $children = $node->childNodes;
    @$children ? $children->[0] : undef;
}

sub lastChild {
    my $node     = shift;
    my $children = $node->childNodes;
    @$children ? $children->[ scalar @$children - 1 ] : undef;
}

sub nextSibling {
    my $node     = shift;
    my $siblings = $node->parentNode->childNodes;
    my $max      = ( scalar @$siblings ) - 1;
    return undef unless $max;
    my $last = $siblings->[0];
    foreach my $n ( $siblings->[ 1 .. $max ] ) {
        return $n if $node == $last;
        $last = $n;
    }
    return $siblings->[$max] if $node == $last;
    return undef;
}

sub previousSibling {
    my $node     = shift;
    my $siblings = $node->parentNode->childNodes;
    my $last;
    foreach my $n (@$siblings) {
        return $last if $node == $n;
        $last = $n;
    }
    return undef;
}

sub ownerDocument {    #template
    my $node = shift;
    return $node->template;
}

sub hasChildNodes {
    my $node     = shift;
    my $children = $node->childNodes;
    $children && (@$children) ? 1 : 0;
}

sub nodeType {
    my $node = shift;
    if ( $node->tag eq 'TEXT' ) {
        return NODE_TEXT();
    }
    elsif ( defined $node->childNodes ) {
        return NODE_BLOCK();
    }
    else {
        return NODE_FUNCTION();
    }
}

sub nodeName {
    my $node = shift;
    my $tag  = $node->tag;
    if ( $tag eq 'TEXT' ) {
        return undef;
    }

    # normalize:
    #    MTEntry => mt:entry
    #    MTAPP:WIDGET => mtapp:widget
    $tag = lc $tag;
    if ( ( $tag !~ m/:/ ) && ( $tag =~ m/^mt/ ) ) {
        $tag =~ s/^mt/mt:/;
    }
    return $tag;
}

sub innerHTML {
    my $node = shift;
    if (@_) {
        my ($text) = @_;
        $node->nodeValue($text);
        require MT::Builder;
        my $builder = new MT::Builder;
        require MT::Template::Context;
        my $ctx = MT::Template::Context->new;
        $node->childNodes( $builder->compile( $ctx, $text ) );
        my $tmpl = $node->ownerDocument;

        if ($tmpl) {
            $tmpl->reset_markers;
            $tmpl->{reflow_flag} = 1;
        }
    }
    return $node->nodeValue;
}

# TBD: what about new nodes that are added with id elements?
sub appendChild {
    my $node       = shift;
    my ($new_node) = @_;
    my $nodes      = $node->childNodes;
    push @$nodes, $new_node;
    my $tmpl = $node->ownerDocument;
    if ($tmpl) {
        $tmpl->{reflow_flag} = 1;
    }
}

sub removeChild {
    my $node = shift;
}

## Decode all items
sub upgrade {
    my $node = shift;
    my $i;
    for my $v (@$node) {
        _upgrade( \$v );
        $i++;
        ## Don't decode parents.
        last if $i >= 5;
    }
}

sub _upgrade {
    my $ref = ref $_[0];
    if ( !$ref ) {
        Encode::_utf8_on( $_[0] )
            if !Encode::is_utf8( $_[0] );
    }
    elsif ( $ref eq 'HASH' ) {
        for my $v ( values %{ $_[0] } ) {
            _upgrade( \$v );
        }
    }
    elsif ( $ref eq 'ARRAY' ) {
        for my $v ( @{ $_[0] } ) {
            _upgrade( \$v );
        }
    }
    elsif ( $ref eq 'SCALAR' ) {
        Encode::_utf8_on( ${ $_[0] } )
            if !Encode::is_utf8( ${ $_[0] } );
    }
    elsif ( $ref eq 'MT::Template::Node' ) {
        $_[0]->upgrade;
    }
    elsif ( $ref eq 'MT::Template::Tokens' ) {
        for my $n ( @{ $_[0] } ) {
            $n->upgrade;
        }
    }
    elsif ( $ref eq 'REF' ) {
        _upgrade( ${ $_[0] } );
    }
}

*inner_html    = \&innerHTML;
*append_child  = \&appendChild;
*insert_before = \&insertBefore;
*remove_child  = \&removeChild;

1;
__END__