lib/LaTeXML/Common/Object.pm

filter: Statementstmt 20.3% 25 / 123 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 22.5% 7 / 31 Podpod 50.0% 12 / 24 totaltotal 15.6% 44 / 282 CC 42 SCAR 48.6
CC
42
Cov
12%
Equals
306.0
Stringify
233.7
ToString
30.0
CRAP 128.6
« lib/LaTeXML/Common/Model/DTD.pm ↑ Summary lib/LaTeXML/Common/Font/Metric.pm »
1 # /=====================================================================\ #
2 # | LaTeXML::Common::Object | #
3 # | Abstract base class for LaTeXML objects | #
4 # |=====================================================================| #
5 # | Part of LaTeXML: | #
6 # | Public domain software, produced as part of work done by the | #
7 # | United States Government & not subject to copyright in the US. | #
8 # |---------------------------------------------------------------------| #
9 # | Bruce Miller <bruce.miller@nist.gov> #_# | #
10 # | http://dlmf.nist.gov/LaTeXML/ (o o) | #
11 # \=========================================================ooo==U==ooo=/ #
12 package LaTeXML::Common::Object;
13 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 34 ▶use LaTeXML::Global;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 34 ▶use XML::LibXML; # Need XML_xxx constants!
Subroutine BEGIN: called CC=1 SCAR=0.0
17 34 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 34 our @EXPORT = (
19 qw(&Stringify &ToString &Revert &Equals),
20 );
21  
22 #======================================================================
23 # Exported generic functions for dealing with LaTeXML's objects
24 #======================================================================
25  
26 34 my %NOBLESS = map { ($_ => 1) } qw( SCALAR HASH ARRAY CODE REF GLOB LVALUE); # [CONSTANT]
27  
28 # Since the next two are used in debugging and error messages,
29 # be careful to avoid recursive errors
30 0 ▶sub Stringify {
Subroutine Stringify: not called CC=16 SCAR=54.5
Pod documented
31 34 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 0 my ($object) = @_;
33 0 my $string = eval {
34 0 local $LaTeXML::IGNORE_ERRORS = 1;
35 0 ▶ if (!defined $object) { return 'undef'; }
Branch: 5 branches
BranchTrueFalseTotal
if (not defined $object) { } 0 0 0
elsif (not ref $object) { } 0 0 0
elsif ($NOBLESS{ref $object}) { } 0 0 0
elsif ($object->can('stringify')) { } 0 0 0
elsif ($object->isa('XML::LibXML::Node')) { } 0 0 0
36 0 elsif (!ref $object) { return $object; }
37 0 elsif ($NOBLESS{ ref $object }) { return "$object"; }
38 0 elsif ($object->can('stringify')) { return $object->stringify; }
39 # Have to handle LibXML stuff explicitly (unless we want to add methods...?)
40 elsif ($object->isa('XML::LibXML::Node')) {
41 0 ▶ if ($object->nodeType == XML_ELEMENT_NODE) {
Branch: 4 branches
BranchTrueFalseTotal
if ($object->nodeType == 1) { } 0 0 0
elsif ($object->nodeType == 3) { } 0 0 0
elsif ($object->nodeType == 9) { } 0 0 0
elsif ($object->nodeType == 11) { } 0 0 0
42 0 ▶ my $model = $STATE && $STATE->getModel;
Condition0%0/3
$LaTeXML::Common::Object::STATE && $LaTeXML::Common::Object::STATE->getModel
!ll&&!rl&&r
000
MC/DC0%0/2
$LaTeXML::Common::Object::STATE && $LaTeXML::Common::Object::STATE->getModel
$LaTeXML::Common::Object::STATE $LaTeXML::Common::Object::STATE->getModel
Truth table0%0/3
A && B
A= $LaTeXML::Common::Object::STATEB= $LaTeXML::Common::Object::STATE->getModel
ABresult
0X0
100
111
43 0 ▶ my $tag = ($model ? $model->getNodeQName($object)
Branch: 1 branch
BranchTrueFalseTotal
$model ? : 0 0 0
44 : $object->nodeName);
45 0 my $attributes = '';
46 0 foreach my $attr ($object->attributes) {
47 0 my $name = $attr->nodeName;
48 0 my $val = $attr->getData;
49 0 ▶ $val = substr($val, 0, 30) . "..." if length($val) > 35;
Branch: 1 branch
BranchTrueFalseTotal
if length $val > 35 0 0 0
50 0 $attributes .= ' ' . $name . "=\"" . $val . "\""; }
51 0 ▶ return "<" . $tag . $attributes . ($object->hasChildNodes ? ">..." : "/>");
Branch: 1 branch
BranchTrueFalseTotal
$object->hasChildNodes ? : 0 0 0
52 }
53 elsif ($object->nodeType == XML_TEXT_NODE) {
54 0 return "XMLText[" . $object->data . "]"; }
55 elsif ($object->nodeType == XML_DOCUMENT_NODE) {
56 0 return "XMLDocument[" . $$object . "]"; }
57 elsif ($object->nodeType == XML_DOCUMENT_FRAG_NODE) {
58 0 return "XMLFragment[" . join('', map { Stringify($_) } $object->childNodes) . "]"; }
59 0 else { return "$object"; } }
60 0 else { return "$object"; } };
61 0 ▶ return (defined $string ? $string : overload::StrVal($object)); } # Fallback, if errors
Branch: 1 branch
BranchTrueFalseTotal
defined $string ? : 0 0 0
62  
63 0 ▶sub ToString {
Subroutine ToString: not called CC=5 SCAR=34.0
Pod documented
64 0 my ($object) = @_;
65 0 ▶ return '' unless defined $object;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object 0 0 0
66 0 my $string = eval {
67 0 local $LaTeXML::IGNORE_ERRORS = 1;
68 0 my $r;
69 0 ▶ return (($r = ref $object) && !$NOBLESS{$r} ? $object->toString : "$object"); };
Branch: 1 branch
BranchTrueFalseTotal
($r = ref $object) && !$NOBLESS{$r} ? : 0 0 0
Condition0%0/3
$r = ref $object and not $NOBLESS{$r}
!ll&&!rl&&r
000
MC/DC0%0/2
$r = ref $object and not $NOBLESS{$r}
$r = ref $object not $NOBLESS{$r}
Truth table0%0/3
A and not(B)
A= $r = ref $objectB= not $NOBLESS{$r}
ABresult
0X0
100
111
70 0 ▶ return (defined $string ? $string : overload::StrVal($object)); } # Fallback, if errors
Branch: 1 branch
BranchTrueFalseTotal
defined $string ? : 0 0 0
71  
72 # Just how deep of an equality test should this be?
73 0 ▶sub Equals {
Subroutine Equals: not called CC=17 SCAR=57.2
Pod documented
74 0 my ($x, $y) = @_;
75 0 ▶ return 1 if !(defined $x) && !(defined $y); # both undefined, equal, I guess
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x or defined $y 0 0 0
Condition0%0/3
defined $x or defined $y
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $x or defined $y
defined $x defined $y
Truth table0%0/3
A or B
A= defined $xB= defined $y
ABresult
1X1
011
000
76 0 ▶ return 0 unless (defined $x) && (defined $y); # else both must be defined
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x and defined $y 0 0 0
Condition0%0/3
defined $x and defined $y
!ll&&!rl&&r
000
MC/DC0%0/2
defined $x and defined $y
defined $x defined $y
Truth table0%0/3
A and B
A= defined $xB= defined $y
ABresult
0X0
100
111
77 0 ▶ my $refx = (ref $x) || '_notype_';
Condition0%0/2
ref $x || '_notype_'
l!l
00
MC/DC0%0/1
ref $x || '_notype_'
ref $x
Truth table0%0/2
A
A= ref $x
Aresult
11
00
78 0 ▶ my $refy = (ref $y) || '_notype_';
Condition0%0/2
ref $y || '_notype_'
l!l
00
MC/DC0%0/1
ref $y || '_notype_'
ref $y
Truth table0%0/2
A
A= ref $y
Aresult
11
00
79  
80 0 ▶ if ($refx ne $refy) { # same type required, beware CODE() of Perl subroutines passed in Tokens
Branch: 1 branch
BranchTrueFalseTotal
if ($refx ne $refy) 0 0 0
81 0 ▶ if ($refx eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if ($refx eq 'CODE') { } 0 0 0
elsif ($refy eq 'CODE') { } 0 0 0
82 0 my $flaty = ToString($y);
83 0 ▶ if ($flaty =~ /^CODE/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flaty =~ /^CODE/) { } 0 0 0
84 0 $y = $flaty;
85 0 $x = "$x";
86 0 $refx = $refy = '_notype_'; }
87 0 else { return 0; } }
88 elsif ($refy eq 'CODE') {
89 0 my $flatx = ToString($x);
90 0 ▶ if ($flatx =~ /^CODE/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flatx =~ /^CODE/) { } 0 0 0
91 0 $x = $flatx;
92 0 $y = "$y";
93 0 $refx = $refy = '_notype_'; }
94 0 else { return 0; } }
95 0 else { return 0; } }
96  
97 0 ▶ return $x eq $y if ($refx eq '_notype_') || $NOBLESS{$refx}; # Deep comparison of builtins?
Branch: 1 branch
BranchTrueFalseTotal
if $refx eq "_notype_" or $NOBLESS{$refx} 0 0 0
Condition0%0/3
$refx eq "_notype_" or $NOBLESS{$refx}
l!l&&r!l&&!r
000
MC/DC0%0/2
$refx eq "_notype_" or $NOBLESS{$refx}
$refx eq "_notype_" $NOBLESS{$refx}
Truth table0%0/3
A or B
A= $refx eq "_notype_"B= $NOBLESS{$refx}
ABresult
1X1
011
000
98 # Special cases? (should be methods, but that embeds State knowledge too low)
99  
100 0 ▶ if ($refx eq 'LaTeXML::Core::Token') { # Check if they've been \let to the same defn.
Branch: 1 branch
BranchTrueFalseTotal
if ($refx eq "LaTeXML::Core::Token") 0 0 0
101 0 ▶ $x = $STATE->lookupMeaning($x) || $x;
Condition0%0/3
$LaTeXML::Common::Object::STATE->lookupMeaning($x) || $x
l!l&&r!l&&!r
000
MC/DC0%0/2
$LaTeXML::Common::Object::STATE->lookupMeaning($x) || $x
$LaTeXML::Common::Object::STATE->lookupMeaning($x) $x
Truth table0%0/3
A || B
A= $LaTeXML::Common::Object::STATE->lookupMeaning($x)B= $x
ABresult
1X1
011
000
102 0 ▶ $y = $STATE->lookupMeaning($y) || $y;
Condition0%0/3
$LaTeXML::Common::Object::STATE->lookupMeaning($y) || $y
l!l&&r!l&&!r
000
MC/DC0%0/2
$LaTeXML::Common::Object::STATE->lookupMeaning($y) || $y
$LaTeXML::Common::Object::STATE->lookupMeaning($y) $y
Truth table0%0/3
A || B
A= $LaTeXML::Common::Object::STATE->lookupMeaning($y)B= $y
ABresult
1X1
011
000
103 }
104 0 return $x->equals($y); } # semi-shallow comparison?
105  
106 # Reverts an object into TeX code, as a Tokens list, that would create it.
107 # Note that this is not necessarily the original TeX.
108 0 ▶sub Revert {
Subroutine Revert: not called CC=4 SCAR=23.7
Pod documented
109 0 my ($thing) = @_;
110 34 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
111 return (defined $thing
112 0 ▶ ? (ref $thing ? map { $_ ? $_->unlist : () } $thing->revert
Branch: 3 branches
BranchTrueFalseTotal
defined $thing ? : 0 0 0
ref $thing ? : 0 0 0
$_ ? : 0 0 0
113 : LaTeXML::Core::Token::Explode($thing)) # Ugh!!
114 : ()); }
115  
116 #======================================================================
117 # LaTeXML Object
118 # Base object for all LaTeXML Objects;
119 # Defines basic default methods for comparison, printing
120 # Tried to use overloading, but the Magic methods lead to hard-to-find
121 # (and occasionally quite serious) performance issues -- at least, if you
122 # try to have stringify do too much.
123 #======================================================================
124  
125 0 ▶sub stringify {
Subroutine stringify: not called CC=1 SCAR=6.9
Pod documented
126 0 my ($object) = @_;
127 0 my $string = "$object"; overload::StrVal($object);
128 0 $string =~ s/^LaTeXML:://;
129 0 $string =~ s/=(SCALAR|HASH|ARRAY|CODE|REF|GLOB|LVALUE|)\(/\[@/;
130 0 $string =~ s/\)$/\]/;
131 0 return $string; }
132  
133 0 ▶sub toString {
Subroutine toString: not called CC=1 SCAR=6.9
Pod documented
134 0 my ($self) = @_;
135 0 return $self->stringify; }
136  
137 0 ▶sub toAttribute {
Subroutine toAttribute: not called CC=1 SCAR=6.9
Pod undocumented
138 0 my ($self) = @_;
139 0 return $self->toString; }
140  
141 0 ▶sub equals {
Subroutine equals: not called CC=1 SCAR=6.9
Pod documented
142 0 my ($x, $y) = @_;
143 0 return "$x" eq "$y"; } # overload::StrVal($x) eq overload::StrVal($y); }
144  
145 0 ▶sub notequals {
Subroutine notequals: not called CC=1 SCAR=6.9
Pod undocumented
146 0 my ($x, $y) = @_;
147 0 return !($x->equals($y)); }
148  
149 0 ▶sub isaToken { return 0; }
Subroutine isaToken: not called CC=1 SCAR=6.9
Pod documented
150 0 ▶sub isaBox { return 0; }
Subroutine isaBox: not called CC=1 SCAR=6.9
Pod documented
151 0 ▶sub isaDefinition { return 0; }
Subroutine isaDefinition: not called CC=1 SCAR=6.9
Pod documented
152  
153 # These should really only make sense for Data objects within the
154 # processing stream.
155 # Defaults (probably poor)
156 0 ▶sub beDigested {
Subroutine beDigested: not called CC=1 SCAR=6.9
Pod documented
157 0 my ($self) = @_;
158 0 return $self; }
159  
160 0 ▶sub beAbsorbed {
Subroutine beAbsorbed: not called CC=4 SCAR=30.0
Pod documented
161 0 my ($self, $document) = @_;
162 # Guard via the absorb limit to avoid infinite loops
163 0 ▶ if ($LaTeXML::ABSORB_LIMIT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LaTeXML::ABSORB_LIMIT) 0 0 0
164 0 ▶ my $absorb_counter = $STATE->lookupValue('absorb_count') || 0;
Condition0%0/2
$LaTeXML::Common::Object::STATE->lookupValue('absorb_count') || 0
l!l
00
MC/DC0%0/1
$LaTeXML::Common::Object::STATE->lookupValue('absorb_count') || 0
$LaTeXML::Common::Object::STATE->lookupValue('absorb_count')
Truth table0%0/2
A
A= $LaTeXML::Common::Object::STATE->lookupValue('absorb_count')
Aresult
11
00
165 0 $STATE->assignValue(absorb_count => ++$absorb_counter, 'global');
166 0 ▶ if ($absorb_counter > $LaTeXML::ABSORB_LIMIT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($absorb_counter > $LaTeXML::ABSORB_LIMIT) 0 0 0
167 0 Fatal('timeout', 'absorb_limit', $self,
168 "Whatsit absorb limit of $LaTeXML::ABSORB_LIMIT exceeded, infinite loop?"); } }
169  
170 0 return $document->openText($self->toString, $document->getNodeFont($document->getElement)); }
171  
172 0 ▶sub unlist {
Subroutine unlist: not called CC=1 SCAR=6.9
Pod undocumented
173 0 my ($self) = @_;
174 0 return $self; }
175  
176 #**********************************************************************
177 # What about Kern, Glue, Penalty ...
178  
179 # stubs to avoid "shallow" Fatal errors on broken documents
180 0 ▶sub multiply { return LaTeXML::Common::Dimension::Dimension(0); }
Subroutine multiply: not called CC=1 SCAR=6.9
Pod undocumented
181 0 ▶sub subtract { return LaTeXML::Common::Dimension::Dimension(0); }
Subroutine subtract: not called CC=1 SCAR=6.9
Pod undocumented
182 0 ▶sub add { return LaTeXML::Common::Dimension::Dimension(0); }
Subroutine add: not called CC=1 SCAR=6.9
Pod undocumented
183 0 ▶sub negate { return LaTeXML::Common::Dimension::Dimension(0); }
Subroutine negate: not called CC=1 SCAR=6.9
Pod undocumented
184 0 ▶sub divide { return LaTeXML::Common::Dimension::Dimension(0); }
Subroutine divide: not called CC=1 SCAR=6.9
Pod undocumented
185 0 ▶sub getX { return LaTeXML::Common::Dimension::Dimension(0); }
Subroutine getX: not called CC=1 SCAR=6.9
Pod undocumented
186 0 ▶sub getY { return LaTeXML::Common::Dimension::Dimension(0); }
Subroutine getY: not called CC=1 SCAR=6.9
Pod undocumented
187 0 ▶sub ptValue { return 0.0; }
Subroutine ptValue: not called CC=1 SCAR=6.9
Pod undocumented
188 0 ▶sub valueOf { return 0.0; }
Subroutine valueOf: not called CC=1 SCAR=6.9
Pod undocumented
189 #**********************************************************************
190  
191 34 1;
192  
193 __END__
« lib/LaTeXML/Common/Model/DTD.pm ↑ Summary lib/LaTeXML/Common/Font/Metric.pm »