MDOM/Element.pm

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