lib/LaTeXML/Common/Object.pm

filter: Statementstmt 19.1% 22 / 115 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 20.0% 6 / 30 Podpod 50.0% 12 / 24 totaltotal 15.1% 40 / 264 CC 38 SCAR 49.8
CC
38
Cov
11%
Equals
306.0
Stringify
272.0
ToString
30.0
CRAP 145.8
« lib/LaTeXML/Core/Definition/Constructor/Compiler.pm ↑ Summary lib/LaTeXML/Common/Model/DTD.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 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 32 ▶use LaTeXML::Global;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 32 ▶use XML::LibXML; # Need XML_xxx constants!
Subroutine BEGIN: called CC=1 SCAR=0.0
17 32 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 32 our @EXPORT = (
19 qw(&Stringify &ToString &Revert &Equals),
20 );
21  
22 #======================================================================
23 # Exported generic functions for dealing with LaTeXML's objects
24 #======================================================================
25  
26 32 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 sub Stringify {
31 0 ▶ my ($object) = @_;
Subroutine Stringify: not called CC=16 SCAR=56.1
Pod documented
32 0 my $string = eval {
33 0 local $LaTeXML::IGNORE_ERRORS = 1;
34 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
35 0 elsif (!ref $object) { return $object; }
36 0 elsif ($NOBLESS{ ref $object }) { return "$object"; }
37 0 elsif ($object->can('stringify')) { return $object->stringify; }
38 # Have to handle LibXML stuff explicitly (unless we want to add methods...?)
39 elsif ($object->isa('XML::LibXML::Node')) {
40 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
41 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
42 0 ▶ my $tag = ($model ? $model->getNodeQName($object)
Branch: 1 branch
BranchTrueFalseTotal
$model ? : 0 0 0
43 : $object->nodeName);
44 0 my $attributes = '';
45 0 foreach my $attr ($object->attributes) {
46 0 my $name = $attr->nodeName;
47 0 my $val = $attr->getData;
48 0 ▶ $val = substr($val, 0, 30) . "..." if length($val) > 35;
Branch: 1 branch
BranchTrueFalseTotal
if length $val > 35 0 0 0
49 0 $attributes .= ' ' . $name . "=\"" . $val . "\""; }
50 0 ▶ return "<" . $tag . $attributes . ($object->hasChildNodes ? ">..." : "/>");
Branch: 1 branch
BranchTrueFalseTotal
$object->hasChildNodes ? : 0 0 0
51 }
52 elsif ($object->nodeType == XML_TEXT_NODE) {
53 0 return "XMLText[" . $object->data . "]"; }
54 elsif ($object->nodeType == XML_DOCUMENT_NODE) {
55 0 return "XMLDocument[" . $$object . "]"; }
56 elsif ($object->nodeType == XML_DOCUMENT_FRAG_NODE) {
57 0 return "XMLFragment[" . join('', map { Stringify($_) } $object->childNodes) . "]"; }
58 0 else { return "$object"; } }
59 0 else { return "$object"; } };
60 0 ▶ return (defined $string ? $string : overload::StrVal($object)); } # Fallback, if errors
Branch: 1 branch
BranchTrueFalseTotal
defined $string ? : 0 0 0
61  
62 sub ToString {
63 0 ▶ my ($object) = @_;
Subroutine ToString: not called CC=5 SCAR=34.0
Pod documented
64 0 ▶ return '' unless defined $object;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object 0 0 0
65 0 my $string = eval {
66 0 local $LaTeXML::IGNORE_ERRORS = 1;
67 0 my $r;
68 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
69 0 ▶ return (defined $string ? $string : overload::StrVal($object)); } # Fallback, if errors
Branch: 1 branch
BranchTrueFalseTotal
defined $string ? : 0 0 0
70  
71 # Just how deep of an equality test should this be?
72 sub Equals {
73 0 ▶ my ($x, $y) = @_;
Subroutine Equals: not called CC=17 SCAR=57.2
Pod documented
74 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
75 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
76 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
77 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
78  
79 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
80 0 ▶ if ($refx eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if ($refx eq 'CODE') { } 0 0 0
elsif ($refy eq 'CODE') { } 0 0 0
81 0 my $flaty = ToString($y);
82 0 ▶ if ($flaty =~ /^CODE/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flaty =~ /^CODE/) { } 0 0 0
83 0 $y = $flaty;
84 0 $x = "$x";
85 0 $refx = $refy = '_notype_'; }
86 0 else { return 0; } }
87 elsif ($refy eq 'CODE') {
88 0 my $flatx = ToString($x);
89 0 ▶ if ($flatx =~ /^CODE/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flatx =~ /^CODE/) { } 0 0 0
90 0 $x = $flatx;
91 0 $y = "$y";
92 0 $refx = $refy = '_notype_'; }
93 0 else { return 0; } }
94 0 else { return 0; } }
95  
96 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
97 # Special cases? (should be methods, but that embeds State knowledge too low)
98  
99 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
100 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
101 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
102 }
103 0 return $x->equals($y); } # semi-shallow comparison?
104  
105 # Reverts an object into TeX code, as a Tokens list, that would create it.
106 # Note that this is not necessarily the original TeX.
107 sub Revert {
108 0 ▶ my ($thing) = @_;
Subroutine Revert: not called CC=3 SCAR=18.1
Pod documented
109 32 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
110 return (defined $thing
111 0 ▶ ? (ref $thing ? map { $_->unlist } $thing->revert
Branch: 2 branches
BranchTrueFalseTotal
defined $thing ? : 0 0 0
ref $thing ? : 0 0 0
112 : LaTeXML::Core::Token::Explode($thing)) # Ugh!!
113 : ()); }
114  
115 #======================================================================
116 # LaTeXML Object
117 # Base object for all LaTeXML Objects;
118 # Defines basic default methods for comparison, printing
119 # Tried to use overloading, but the Magic methods lead to hard-to-find
120 # (and occasionally quite serious) performance issues -- at least, if you
121 # try to have stringify do too much.
122 #======================================================================
123  
124 sub stringify {
125 0 ▶ my ($object) = @_;
Subroutine stringify: not called CC=1 SCAR=6.9
Pod documented
126 0 my $string = "$object"; overload::StrVal($object);
127 0 $string =~ s/^LaTeXML:://;
128 0 $string =~ s/=(SCALAR|HASH|ARRAY|CODE|REF|GLOB|LVALUE|)\(/\[@/;
129 0 $string =~ s/\)$/\]/;
130 0 return $string; }
131  
132 sub toString {
133 0 ▶ my ($self) = @_;
Subroutine toString: not called CC=1 SCAR=6.9
Pod documented
134 0 return $self->stringify; }
135  
136 sub toAttribute {
137 0 ▶ my ($self) = @_;
Subroutine toAttribute: not called CC=1 SCAR=6.9
Pod undocumented
138 0 return $self->toString; }
139  
140 sub equals {
141 0 ▶ my ($x, $y) = @_;
Subroutine equals: not called CC=1 SCAR=6.9
Pod documented
142 0 return "$x" eq "$y"; } # overload::StrVal($x) eq overload::StrVal($y); }
143  
144 sub notequals {
145 0 ▶ my ($x, $y) = @_;
Subroutine notequals: not called CC=1 SCAR=6.9
Pod undocumented
146 0 return !($x->equals($y)); }
147  
148 0 ▶sub isaToken { return 0; }
Subroutine isaToken: not called CC=1 SCAR=6.9
Pod documented
149 0 ▶sub isaBox { return 0; }
Subroutine isaBox: not called CC=1 SCAR=6.9
Pod documented
150 0 ▶sub isaDefinition { return 0; }
Subroutine isaDefinition: not called CC=1 SCAR=6.9
Pod documented
151  
152 # These should really only make sense for Data objects within the
153 # processing stream.
154 # Defaults (probably poor)
155 sub beDigested {
156 0 ▶ my ($self) = @_;
Subroutine beDigested: not called CC=1 SCAR=6.9
Pod documented
157 0 return $self; }
158  
159 sub beAbsorbed {
160 0 ▶ my ($self, $document) = @_;
Subroutine beAbsorbed: not called CC=1 SCAR=6.9
Pod documented
161 0 return $document->openText($self->toString, $document->getNodeFont($document->getElement)); }
162  
163 sub unlist {
164 0 ▶ my ($self) = @_;
Subroutine unlist: not called CC=1 SCAR=6.9
Pod undocumented
165 0 return $self; }
166  
167 #**********************************************************************
168 # What about Kern, Glue, Penalty ...
169  
170 # stubs to avoid "shallow" Fatal errors on broken documents
171 0 ▶sub multiply { return LaTeXML::Common::Dimension::Dimension(0); }
Subroutine multiply: not called CC=1 SCAR=6.9
Pod undocumented
172 0 ▶sub subtract { return LaTeXML::Common::Dimension::Dimension(0); }
Subroutine subtract: not called CC=1 SCAR=6.9
Pod undocumented
173 0 ▶sub add { return LaTeXML::Common::Dimension::Dimension(0); }
Subroutine add: not called CC=1 SCAR=6.9
Pod undocumented
174 0 ▶sub negate { return LaTeXML::Common::Dimension::Dimension(0); }
Subroutine negate: not called CC=1 SCAR=6.9
Pod undocumented
175 0 ▶sub divide { return LaTeXML::Common::Dimension::Dimension(0); }
Subroutine divide: not called CC=1 SCAR=6.9
Pod undocumented
176 0 ▶sub getX { return LaTeXML::Common::Dimension::Dimension(0); }
Subroutine getX: not called CC=1 SCAR=6.9
Pod undocumented
177 0 ▶sub getY { return LaTeXML::Common::Dimension::Dimension(0); }
Subroutine getY: not called CC=1 SCAR=6.9
Pod undocumented
178 0 ▶sub ptValue { return 0.0; }
Subroutine ptValue: not called CC=1 SCAR=6.9
Pod undocumented
179 0 ▶sub valueOf { return 0.0; }
Subroutine valueOf: not called CC=1 SCAR=6.9
Pod undocumented
180 #**********************************************************************
181  
182 32 1;
183  
184 __END__
« lib/LaTeXML/Core/Definition/Constructor/Compiler.pm ↑ Summary lib/LaTeXML/Common/Model/DTD.pm »