MDOM/Element.pm

filter: Statementstmt 38.4% 60 / 156 Branchbran 14.7% 13 / 88 Conditioncond 13.3% 2 / 15 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 45.8% 22 / 48 Podpod 90.9% 20 / 22 totaltotal 34.8% 118 / 339 SCAR 68.9
CC
51
Cov
29%
_flush_locations
56.0
next_token
42.0
previous_token
42.0
CRAP 983.6
↑ Summary MDOM/Node.pm »
1 package MDOM::Element;
2  
3 =pod
4  
5 =head1 NAME
6  
7 MDOM::Element - The abstract Element class, a base for all source objects
8  
9 =head1 INHERITANCE
10  
11 MDOM::Element is the root of the PDOM tree
12  
13 =head1 DESCRIPTION
14  
15 The abstract C<MDOM::Element> serves as a base class for all source-related
16 objects, from a single whitespace token to an entire document. It provides
17 a basic set of methods to provide a common interface and basic
18 implementations.
19  
20 =head1 METHODS
21  
22 =cut
23  
24 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 18 ▶use Scalar::Util 'refaddr';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 18 use Params::Util '_INSTANCE',
27 18 ▶ '_ARRAY';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 18 ▶use MDOM::Node ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 18 ▶use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 18 ▶use List::MoreUtils ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 use overload 'bool' => sub () { 1 },
32 18 '""' => 'content',
33 '==' => '__equals',
34 '!=' => '__nequals',
35 'eq' => '__eq',
36 18 ▶ 'ne' => '__ne';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 18 ▶use vars qw{$VERSION $errstr %_PARENT};
Subroutine BEGIN: called CC=1 SCAR=0.0
39 BEGIN {
40 18 ▶ $VERSION = '0.006';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 18 $errstr = '';
42  
43 # Master Child -> Parent index
44 18 %_PARENT = ();
45 }
46  
47  
48  
49  
50  
51 #####################################################################
52 # General Properties
53  
54 =pod
55  
56 =head2 significant
57  
58 Because we treat whitespace and other non-code items as Tokens (in order to
59 be able to "round trip" the L<MDOM::Document> back to a file) the
60 C<significant> method allows us to distinguish between tokens that form a
61 part of the code, and tokens that aren't significant, such as whitespace,
62 POD, or the portion of a file after (and including) the C<__END__> token.
63  
64 Returns true if the Element is significant, or false it not.
65  
66 =cut
67  
68 ### XS -> MDOM/XS.xs:_MDOM_Element__significant 0.845+
69 314 ▶sub significant { 1 }
Subroutine significant: called CC=1 SCAR=0.0
Pod documented
70  
71 sub lineno {
72 0 ▶ $_[0]->{lineno};
Subroutine lineno: not called CC=1 SCAR=6.9
Pod undocumented
73 }
74  
75 =pod
76  
77 =head2 class
78  
79 The C<class> method is provided as a convenience, and really does nothing
80 more than returning C<ref($self)>. However, some people have found that
81 they appreciate the laziness of C<$Foo-E<gt>class eq 'whatever'>, so I
82 have caved to popular demand and included it.
83  
84 Returns the class of the Element as a string
85  
86 =cut
87  
88 142 ▶sub class { ref($_[0]) }
Subroutine class: called CC=1 SCAR=0.0
Pod documented
89  
90 =pod
91  
92 =head2 tokens
93  
94 The C<tokens> method returns a list of L<MDOM::Token> objects for the
95 Element, essentially getting back that part of the document as if it had
96 not been lexed.
97  
98 This also means there are no Statements and no Structures in the list,
99 just the Token classes.
100  
101 =cut
102  
103 0 ▶sub tokens { $_[0] }
Subroutine tokens: not called CC=1 SCAR=6.9
Pod documented
104  
105 =pod
106  
107 =head2 content
108  
109 For B<any> C<MDOM::Element>, the C<content> method will reconstitute the
110 base code for it as a single string. This method is also the method used
111 for overloading stringification. When an Element is used in a double-quoted
112 string for example, this is the method that is called.
113  
114 B<WARNING:>
115  
116 You should be aware that because of the way that here-docs are handled, any
117 here-doc content is not included in C<content>, and as such you should
118 B<not> eval or execute the result if it contains any L<MDOM::Token::HereDoc>.
119  
120 The L<MDOM::Document> method C<serialize> should be used to stringify a PDOM
121 document into something that can be executed as expected.
122  
123 Returns the basic code as a string (excluding here-doc content).
124  
125 =cut
126  
127 ### XS -> MDOM/XS.xs:_MDOM_Element__content 0.900+
128 0 ▶sub content { '' }
Subroutine content: not called CC=1 SCAR=6.9
Pod documented
129  
130  
131 #####################################################################
132 # Naigation Methods
133  
134 =pod
135  
136 =head2 parent
137  
138 Elements themselves are not intended to contain other Elements, that is
139 left to the L<MDOM::Node> abstract class, a subclass of C<MDOM::Element>.
140 However, all Elements can be contained B<within> a parent Node.
141  
142 If an Element is within a parent Node, the C<parent> method returns the
143 Node.
144  
145 =cut
146  
147 27 ▶sub parent { $_PARENT{refaddr $_[0]} }
Subroutine parent: called CC=1 SCAR=0.0
Pod documented
148  
149 =pod
150  
151 =head2 statement
152  
153 For a C<MDOM::Element> that is contained (at some depth) within a
154 L<MDOM::Statment>, the C<statement> method will return the first parent
155 Statement object lexically 'above' the Element.
156  
157 Returns a L<MDOM::Statement> object, which may be the same Element if the
158 Element is itself a L<MDOM::Statement> object.
159  
160 Returns false if the Element is not within a Statement and is not itself
161 a Statement.
162  
163 =cut
164  
165 sub statement {
166 0 ▶ my $cursor = shift;
Subroutine statement: not called CC=3 SCAR=24.8
Pod documented
167 0 ▶ while ( ! _INSTANCE($cursor, 'MDOM::Statement') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&_INSTANCE($cursor, "MDOM::Statement")) 0 0 0
168 0 ▶ $cursor = $_PARENT{refaddr $cursor} or return '';
Branch: 1 branch
BranchTrueFalseTotal
$cursor = $MDOM::Element::_PARENT{builtin::refaddr($cursor)} or return "" 0 0 0
169 }
170 0 $cursor;
171 }
172  
173 =pod
174  
175 =head2 top
176  
177 For a C<MDOM::Element> that is contained within a PDOM tree, the C<top> method
178 will return the top-level Node in the tree. Most of the time this should be
179 a L<MDOM::Document> object, however this will not always be so. For example,
180 if a subroutine has been removed from its Document, to be moved to another
181 Document.
182  
183 Returns the top-most PDOM object, which may be the same Element, if it is
184 not within any parent PDOM object.
185  
186 =cut
187  
188 sub top {
189 0 ▶ my $cursor = shift;
Subroutine top: not called CC=2 SCAR=17.9
Pod documented
190 0 ▶ while ( my $parent = $_PARENT{refaddr $cursor} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $MDOM::Element::_PARENT{builtin::refaddr($cursor)}) 0 0 0
191 0 $cursor = $parent;
192 }
193 0 $cursor;
194 }
195  
196 =pod
197  
198 For an Element that is contained within a L<MDOM::Document> object,
199 the C<document> method will return the top-level Document for the Element.
200  
201 Returns the L<MDOM::Document> for this Element, or false if the Element is not
202 contained within a Document.
203  
204 =cut
205  
206 sub document {
207 0 ▶ my $top = shift->top;
Subroutine document: not called CC=2 SCAR=17.9
Pod undocumented
208 0 ▶ _INSTANCE($top, 'MDOM::Document') and $top;
Branch: 1 branch
BranchTrueFalseTotal
&_INSTANCE($top, "MDOM::Document") and $top 0 0 0
209 }
210  
211 =pod
212  
213 =head2 next_sibling
214  
215 All L<MDOM::Node> objects (specifically, our parent Node) contain a number of
216 C<MDOM::Element> objects. The C<next_sibling> method returns the C<MDOM::Element>
217 immediately after the current one, or false if there is no next sibling.
218  
219 =cut
220  
221 sub next_sibling {
222 4 ▶ my $self = shift;
Subroutine next_sibling: called CC=3 SCAR=11.0
Pod documented
223 4 ▶ my $parent = $_PARENT{refaddr $self} or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $MDOM::Element::_PARENT{builtin::refaddr($self)} or return "" 0 4 4
224 4 my $key = refaddr $self;
225 4 my $elements = $parent->{children};
226 my $position = List::MoreUtils::firstidx {
227 8 ▶ refaddr $_ == $key
Subroutine __ANON__: called CC=1 SCAR=0.0
228 4 } @$elements;
229 4 ▶ $elements->[$position + 1] || '';
Branch: 1 branch
BranchTrueFalseTotal
$$elements[$position + 1] or "" 3 1 4
230 }
231  
232 =pod
233  
234 =head2 snext_sibling
235  
236 As per the other 's' methods, the C<snext_sibling> method returns the next
237 B<significant> sibling of the C<MDOM::Element> object.
238  
239 Returns a C<MDOM::Element> object, or false if there is no 'next' significant
240 sibling.
241  
242 =cut
243  
244 sub snext_sibling {
245 0 ▶ my $self = shift;
Subroutine snext_sibling: not called CC=4 SCAR=30.0
Pod documented
246 0 ▶ my $parent = $_PARENT{refaddr $self} or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $MDOM::Element::_PARENT{builtin::refaddr($self)} or return "" 0 0 0
247 0 my $key = refaddr $self;
248 0 my $elements = $parent->{children};
249 my $position = List::MoreUtils::firstidx {
250 0 ▶ refaddr $_ == $key
Subroutine __ANON__: not called CC=1 SCAR=6.9
251 0 } @$elements;
252 0 ▶ while ( defined(my $it = $elements->[++$position]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $it = $$elements[++$position])) 0 0 0
253 0 ▶ return $it if $it->significant;
Branch: 1 branch
BranchTrueFalseTotal
if $it->significant 0 0 0
254 }
255 0 '';
256 }
257  
258 =pod
259  
260 =head2 previous_sibling
261  
262 All L<MDOM::Node> objects (specifically, our parent Node) contain a number of
263 C<MDOM::Element> objects. The C<previous_sibling> method returns the Element
264 immediately before the current one, or false if there is no 'previous'
265 C<MDOM::Element> object.
266  
267 =cut
268  
269 sub previous_sibling {
270 4 ▶ my $self = shift;
Subroutine previous_sibling: called CC=4 SCAR=14.0
Pod documented
271 4 ▶ my $parent = $_PARENT{refaddr $self} or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $MDOM::Element::_PARENT{builtin::refaddr($self)} or return "" 0 4 4
272 4 my $key = refaddr $self;
273 4 my $elements = $parent->{children};
274 my $position = List::MoreUtils::firstidx {
275 8 ▶ refaddr $_ == $key
Subroutine __ANON__: called CC=1 SCAR=0.0
276 4 } @$elements;
277 4 ▶ $position and $elements->[$position - 1] or '';
Branch: 1 branch
BranchTrueFalseTotal
$position and $$elements[$position - 1] or "" 1 3 4
Condition66%2/3
$position and $$elements[$position - 1]
!ll&&!rl&&r
103
MC/DC50%1/2
$position and $$elements[$position - 1]
$position $$elements[$position - 1]
Truth table66%2/3
A and B
A= $positionB= $$elements[$position - 1]
ABresult
0X0
100
111
278 }
279  
280 =pod
281  
282 =head2 sprevious_sibling
283  
284 As per the other 's' methods, the C<sprevious_sibling> method returns
285 the previous B<significant> sibling of the C<MDOM::Element> object.
286  
287 Returns a C<MDOM::Element> object, or false if there is no 'previous' significant
288 sibling.
289  
290 =cut
291  
292 sub sprevious_sibling {
293 0 ▶ my $self = shift;
Subroutine sprevious_sibling: not called CC=5 SCAR=34.0
Pod documented
294 0 ▶ my $parent = $_PARENT{refaddr $self} or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $MDOM::Element::_PARENT{builtin::refaddr($self)} or return "" 0 0 0
295 0 my $key = refaddr $self;
296 0 my $elements = $parent->{children};
297 my $position = List::MoreUtils::firstidx {
298 0 ▶ refaddr $_ == $key
Subroutine __ANON__: not called CC=1 SCAR=6.9
299 0 } @$elements;
300 0 ▶ while ( $position-- and defined(my $it = $elements->[$position]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($position-- and defined(my $it = $elements->[$position])) 0 0 0
Condition0%0/3
$position-- and defined(my $it = $elements->[$position])
!ll&&!rl&&r
000
MC/DC0%0/2
$position-- and defined(my $it = $elements->[$position])
$position-- defined(my $it = $elements->[$position])
Truth table0%0/3
A and B
A= $position--B= defined(my $it = $elements->[$position])
ABresult
0X0
100
111
301 0 ▶ return $it if $it->significant;
Branch: 1 branch
BranchTrueFalseTotal
if $it->significant 0 0 0
302 }
303 0 '';
304 }
305  
306 =pod
307  
308 =head2 first_token
309  
310 As a support method for higher-order algorithms that deal specifically with
311 tokens and actual Perl content, the C<first_token> method finds the first
312 MDOM::Token object within or equal to this one.
313  
314 That is, if called on a L<MDOM::Node> subclass, it will descend until it
315 finds a L<MDOM::Token>. If called on a L<MDOM::Token> object, it will return
316 the same object.
317  
318 Returns a L<MDOM::Token> object, or dies on error (which should be extremely
319 rare and only occur if an illegal empty L<MDOM::Statement> exists below the
320 current Element somewhere.
321  
322 =cut
323  
324 sub first_token {
325 0 ▶ my $cursor = shift;
Subroutine first_token: not called CC=3 SCAR=24.8
Pod documented
326 0 ▶ while ( $cursor->isa('MDOM::Node') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cursor->isa("MDOM::Node")) 0 0 0
327 0 ▶ $cursor = $cursor->first_element
Branch: 1 branch
BranchTrueFalseTotal
$cursor = $cursor->first_element or die "Found empty MDOM::Node while getting first token" 0 0 0
328 or die "Found empty MDOM::Node while getting first token";
329 }
330 0 $cursor;
331 }
332  
333  
334 =pod
335  
336 =head2 last_token
337  
338 As a support method for higher-order algorithms that deal specifically with
339 tokens and actual Perl content, the C<last_token> method finds the last
340 MDOM::Token object within or equal to this one.
341  
342 That is, if called on a L<MDOM::Node> subclass, it will descend until it
343 finds a L<MDOM::Token>. If called on a L<MDOM::Token> object, it will return
344 the itself.
345  
346 Returns a L<MDOM::Token> object, or dies on error (which should be extremely
347 rare and only occur if an illegal empty L<MDOM::Statement> exists below the
348 current Element somewhere.
349  
350 =cut
351  
352 sub last_token {
353 44 ▶ my $cursor = shift;
Subroutine last_token: called CC=3 SCAR=11.0
Pod documented
354 44 ▶ while ( $cursor->isa('MDOM::Node') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cursor->isa("MDOM::Node")) 82 44 126
355 82 ▶ $cursor = $cursor->last_element
Branch: 1 branch
BranchTrueFalseTotal
$cursor = $cursor->last_element or die "Found empty MDOM::Node while getting first token" 0 82 82
356 or die "Found empty MDOM::Node while getting first token";
357 }
358 44 $cursor;
359 }
360  
361 =pod
362  
363 =head2 next_token
364  
365 As a support method for higher-order algorithms that deal specifically with
366 tokens and actual Perl content, the C<next_token> method finds the
367 L<MDOM::Token> object that is immediately after the current Element, even if
368 it is not within the same parent L<MDOM::Node> as the one for which the
369 method is being called.
370  
371 Note that this is B<not> defined as a L<MDOM::Token>-specific method,
372 because it can be useful to find the next token that is after, say, a
373 L<MDOM::Statement>, although obviously it would be useless to want the
374 next token after a L<MDOM::Document>.
375  
376 Returns a L<MDOM::Token> object, or false if there are no more tokens after
377 the Element.
378  
379 =cut
380  
381 sub next_token {
382 0 ▶ my $cursor = shift;
Subroutine next_token: not called CC=6 SCAR=37.4
Pod documented
383  
384 # Find the next element, going upwards as needed
385 0 while ( 1 ) {
386 0 my $element = $cursor->next_sibling;
387 0 ▶ if ( $element ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element) 0 0 0
388 0 ▶ return $element if $element->isa('MDOM::Token');
Branch: 1 branch
BranchTrueFalseTotal
if $element->isa("MDOM::Token") 0 0 0
389 0 return $element->first_token;
390 }
391 0 ▶ $cursor = $cursor->parent or return '';
Branch: 1 branch
BranchTrueFalseTotal
$cursor = $cursor->parent or return "" 0 0 0
392 0 ▶ if ( $cursor->isa('MDOM::Structure') and $cursor->finish ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cursor->isa("MDOM::Structure") and $cursor->finish) 0 0 0
Condition0%0/3
$cursor->isa("MDOM::Structure") and $cursor->finish
!ll&&!rl&&r
000
MC/DC0%0/2
$cursor->isa("MDOM::Structure") and $cursor->finish
$cursor->isa("MDOM::Structure") $cursor->finish
Truth table0%0/3
A and B
A= $cursor->isa("MDOM::Structure")B= $cursor->finish
ABresult
0X0
100
111
393 0 return $cursor->finish;
394 }
395 }
396 }
397  
398 =pod
399  
400 =head2 previous_token
401  
402 As a support method for higher-order algorithms that deal specifically with
403 tokens and actual Perl content, the C<previous_token> method finds the
404 L<MDOM::Token> object that is immediately before the current Element, even
405 if it is not within the same parent L<MDOM::Node> as this one.
406  
407 Note that this is not defined as a L<MDOM::Token>-only method, because it can
408 be useful to find the token is before, say, a L<MDOM::Statement>, although
409 obviously it would be useless to want the next token before a
410 L<MDOM::Document>.
411  
412 Returns a L<MDOM::Token> object, or false if there are no more tokens before
413 the C<Element>.
414  
415 =cut
416  
417 sub previous_token {
418 0 ▶ my $cursor = shift;
Subroutine previous_token: not called CC=6 SCAR=37.4
Pod documented
419  
420 # Find the previous element, going upwards as needed
421 0 while ( 1 ) {
422 0 my $element = $cursor->previous_sibling;
423 0 ▶ if ( $element ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element) 0 0 0
424 0 ▶ return $element if $element->isa('MDOM::Token');
Branch: 1 branch
BranchTrueFalseTotal
if $element->isa("MDOM::Token") 0 0 0
425 0 return $element->last_token;
426 }
427 0 ▶ $cursor = $cursor->parent or return '';
Branch: 1 branch
BranchTrueFalseTotal
$cursor = $cursor->parent or return "" 0 0 0
428 0 ▶ if ( $cursor->isa('MDOM::Structure') and $cursor->start ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cursor->isa("MDOM::Structure") and $cursor->start) 0 0 0
Condition0%0/3
$cursor->isa("MDOM::Structure") and $cursor->start
!ll&&!rl&&r
000
MC/DC0%0/2
$cursor->isa("MDOM::Structure") and $cursor->start
$cursor->isa("MDOM::Structure") $cursor->start
Truth table0%0/3
A and B
A= $cursor->isa("MDOM::Structure")B= $cursor->start
ABresult
0X0
100
111
429 0 return $cursor->start;
430 }
431 }
432 }
433  
434  
435  
436  
437  
438 #####################################################################
439 # Manipulation
440  
441 =pod
442  
443 =head2 clone
444  
445 As per the L<Clone> module, the C<clone> method makes a perfect copy of
446 an Element object. In the generic case, the implementation is done using
447 the L<Clone> module's mechanism itself. In higher-order cases, such as for
448 Nodes, there is more work involved to keep the parent-child links intact.
449  
450 =cut
451  
452 sub clone {
453 1 ▶ Clone::clone(shift);
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
454 }
455  
456 =pod
457  
458 =head2 insert_before @Elements
459  
460 The C<insert_before> method allows you to insert lexical perl content, in
461 the form of C<MDOM::Element> objects, before the calling C<Element>. You
462 need to be very careful when modifying perl code, as it's easy to break
463 things.
464  
465 In its initial incarnation, this method allows you to insert a single
466 Element, and will perform some basic checking to prevent you inserting
467 something that would be structurally wrong (in PDOM terms).
468  
469 In future, this method may be enhanced to allow the insertion of multiple
470 Elements, inline-parsed code strings or L<MDOM::Document::Fragment> objects.
471  
472 Returns true if the Element was inserted, false if it can not be inserted,
473 or C<undef> if you do not provide a L<MDOM::Element> object as a parameter.
474  
475 =cut
476  
477 sub __insert_before {
478 0 ▶ my $self = shift;
Subroutine __insert_before: not called CC=1 SCAR=6.9
479 0 $self->parent->__insert_before_child( $self, @_ );
480 }
481  
482 =pod
483  
484 =head2 insert_after @Elements
485  
486 The C<insert_after> method allows you to insert lexical perl content, in
487 the form of C<MDOM::Element> objects, after the calling C<Element>. You need
488 to be very careful when modifying perl code, as it's easy to break things.
489  
490 In its initial incarnation, this method allows you to insert a single
491 Element, and will perform some basic checking to prevent you inserting
492 something that would be structurally wrong (in PDOM terms).
493  
494 In future, this method may be enhanced to allow the insertion of multiple
495 Elements, inline-parsed code strings or L<MDOM::Document::Fragment> objects.
496  
497 Returns true if the Element was inserted, false if it can not be inserted,
498 or C<undef> if you do not provide a L<MDOM::Element> object as a parameter.
499  
500 =cut
501  
502 sub __insert_after {
503 0 ▶ my $self = shift;
Subroutine __insert_after: not called CC=1 SCAR=6.9
504 0 $self->parent->__insert_after_child( $self, @_ );
505 }
506  
507 =pod
508  
509 =head2 remove
510  
511 For a given C<MDOM::Element>, the C<remove> method will remove it from its
512 parent B<intact>, along with all of its children.
513  
514 Returns the C<Element> itself as a convenience, or C<undef> if an error
515 occurs while trying to remove the C<Element>.
516  
517 =cut
518  
519 sub remove {
520 5 ▶ my $self = shift;
Subroutine remove: called CC=2 SCAR=7.1
Pod documented
521 5 ▶ my $parent = $self->parent or return $self;
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $self->parent or return $self 0 5 5
522 5 $parent->remove_child( $self );
523 }
524  
525 =pod
526  
527 =head2 delete
528  
529 For a given C<MDOM::Element>, the C<remove> method will remove it from its
530 parent, immediately deleting the C<Element> and all of its children (if it
531 has any).
532  
533 Returns true if the C<Element> was successfully deleted, or C<undef> if
534 an error occurs while trying to remove the C<Element>.
535  
536 =cut
537  
538 sub delete {
539 5 ▶ $_[0]->remove or return undef;
Subroutine delete: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->remove or return undef 0 5 5
540 5 $_[0]->DESTROY;
541 5 1;
542 }
543  
544 =pod
545  
546 =head2 replace $Element
547  
548 Although some higher level class support more exotic forms of replace,
549 at the basic level the C<replace> method takes a single C<Element> as
550 an argument and replaces the current C<Element> with it.
551  
552 To prevent accidental damage to code, in this initial implementation the
553 replacement element B<must> be of the same class (or a subclass) as the
554 one being replaced.
555  
556 =cut
557  
558 sub replace {
559 0 ▶ my $self = ref $_[0] ? shift : return undef;
Subroutine replace: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
560 0 ▶ my $Element = _INSTANCE(shift, ref $self) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $Element = &_INSTANCE(shift(), ref $self) or return undef 0 0 0
561 0 die "The ->replace method has not yet been implemented";
562 }
563  
564 =pod
565  
566 =head2 location
567  
568 If the Element exists within a L<MDOM::Document> that has
569 indexed the Element locations using C<MDOM::Document::index_locations>, the
570 C<location> method will return the location of the first character of the
571 Element within the Document.
572  
573 Returns the location as a reference to a three-element array in the form
574 C<[ $line, $rowchar, $col ]>. The values are in a human format, with the
575 first character of the file located at C<[ 1, 1, 1 ]>.
576  
577 The second and third numbers are similar, except that the second is the
578 literal horizontal character, and the third is the visual column, taking
579 into account tabbing.
580  
581 Returns C<undef> on error, or if the L<MDOM::Document> object has not been indexed.
582  
583 =cut
584  
585 sub location {
586 0 ▶ my $self = shift;
Subroutine location: not called CC=6 SCAR=37.4
Pod documented
587 0 ▶ unless ( exists $self->{_location} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_location"}) 0 0 0
588 # Are we inside a normal document?
589 0 ▶ my $Document = $self->document or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $Document = $self->document or return undef 0 0 0
590 0 ▶ if ( $Document->isa('MDOM::Document::Fragment') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Document->isa("MDOM::Document::Fragment")) 0 0 0
591 # Because they can't be serialized, document fragments
592 # do not support the concept of location.
593 0 return undef;
594 }
595  
596 # Generate the locations. If they need one location, then
597 # the chances are they'll want more, and it's better that
598 # everything is already pre-generated.
599 0 ▶ $Document->index_locations or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$Document->index_locations or return undef 0 0 0
600 0 ▶ unless ( exists $self->{_location} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_location"}) 0 0 0
601 # erm... something went very wrong here
602 0 return undef;
603 }
604 }
605  
606 # Return a copy, not the original
607 0 return [ @{$self->{_location}} ];
608 }
609  
610 # Although flush_locations is only publically a Document-level method,
611 # we are able to implement it at an Element level, allowing us to
612 # selectively flush only the part of the document that occurs after the
613 # element for which the flush is called.
614 sub _flush_locations {
615 0 ▶ my $self = shift;
Subroutine _flush_locations: not called CC=7 SCAR=40.3
616 0 ▶ unless ( $self == $self->top ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self == $self->top) 0 0 0
617 0 return $self->top->_flush_locations( $self );
618 }
619  
620 # Get the full list of all Tokens
621 0 my @Tokens = $self->tokens;
622  
623 # Optionally allow starting from an arbitrary element (or rather,
624 # the first Token equal-to-or-within an arbitrary element)
625 0 ▶ if ( _INSTANCE($_[0], 'MDOM::Element') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_INSTANCE($_[0], "MDOM::Element")) 0 0 0
626 0 my $start = shift->first_token;
627 0 ▶ while ( my $Token = shift @Tokens ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $Token = shift @Tokens) 0 0 0
628 0 ▶ return 1 unless $Token->{_location};
Branch: 1 branch
BranchTrueFalseTotal
unless $Token->{"_location"} 0 0 0
629 0 ▶ next unless refaddr($Token) == refaddr($start);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::refaddr($Token) == builtin::refaddr($start) 0 0 0
630  
631 # Found the start. Flush it's location
632 0 delete $$Token->{_location};
633 0 last;
634 }
635 }
636  
637 # Iterate over any remaining Tokens and flush their location
638 0 foreach my $Token ( @Tokens ) {
639 0 delete $Token->{_location};
640 }
641  
642 0 1;
643 }
644  
645  
646  
647  
648  
649 #####################################################################
650 # XML Compatibility Methods
651  
652 sub _xml_name {
653 0 ▶ my $class = ref $_[0] || $_[0];
Subroutine _xml_name: not called CC=2 SCAR=17.9
Condition0%0/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
654 0 my $name = lc join( '_', split /::/, $class );
655 0 substr($name, 4);
656 }
657  
658 sub _xml_attr {
659 0 ▶ return {};
Subroutine _xml_attr: not called CC=1 SCAR=6.9
660 }
661  
662 sub _xml_content {
663 0 ▶ defined $_[0]->{content} ? $_[0]->{content} : '';
Subroutine _xml_content: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'content'} ? : 0 0 0
664 }
665  
666  
667  
668  
669  
670 #####################################################################
671 # Internals
672  
673 # Set the error string
674 sub _error {
675 0 ▶ $errstr = $_[1];
Subroutine _error: not called CC=1 SCAR=6.9
676 0 undef;
677 }
678  
679 # Clear the error string
680 sub _clear {
681 0 ▶ $errstr = '';
Subroutine _clear: not called CC=1 SCAR=6.9
682 0 $_[0];
683 }
684  
685 # Being DESTROYed in this manner, rather than by an explicit
686 # ->delete means our reference count has probably fallen to zero.
687 # Therefore we don't need to remove ourselves from our parent,
688 # just the index ( just in case ).
689 ### XS -> MDOM/XS.xs:_MDOM_Element__DESTROY 0.900+
690 866 ▶sub DESTROY { delete $_PARENT{refaddr $_[0]} }
Subroutine DESTROY: called CC=1 SCAR=0.0
691  
692 # Operator overloads
693 0 ▶sub __equals { ref $_[1] and refaddr($_[0]) == refaddr($_[1]) }
Subroutine __equals: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] and builtin::refaddr($_[0]) == builtin::refaddr($_[1]) 0 0 0
694 0 ▶sub __nequals { !__equals(@_) }
Subroutine __nequals: not called CC=1 SCAR=6.9
695 sub __eq {
696 1024 ▶ my $self = _INSTANCE($_[0], 'MDOM::Element') ? $_[0]->content : $_[0];
Subroutine __eq: called CC=3 SCAR=11.7
Branch: 1 branch
BranchTrueFalseTotal
&_INSTANCE($_[0], 'MDOM::Element') ? : 1024 0 1024
697 1024 ▶ my $other = _INSTANCE($_[1], 'MDOM::Element') ? $_[1]->content : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
&_INSTANCE($_[1], 'MDOM::Element') ? : 0 1024 1024
698 1024 $self eq $other;
699 }
700 0 ▶sub __ne { !__eq(@_) }
Subroutine __ne: not called CC=1 SCAR=6.9
701  
702 18 1;
703  
704 =pod
705  
706 =head1 TO DO
707  
708 It would be nice if C<location> could be used in an ad-hoc manner. That is,
709 if called on an Element within a Document that has not been indexed, it will
710 do a one-off calculation to find the location. It might be very painful if
711 someone started using it a lot, without remembering to index the document,
712 but it would be handy for things that are only likely to use it once, such
713 as error handlers.
714  
715 =head1 SUPPORT
716  
717 See the L<support section|MDOM/SUPPORT> in the main module.
718  
719 =head1 AUTHOR
720  
721 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
722  
723 =head1 COPYRIGHT
724  
725 Copyright 2001 - 2006 Adam Kennedy.
726  
727 This program is free software; you can redistribute
728 it and/or modify it under the same terms as Perl itself.
729  
730 The full text of the license can be found in the
731 LICENSE file included with this module.
732  
733 =cut
↑ Summary MDOM/Node.pm »