lib/LaTeXML/Core/Document.pm untested

filter: Statementstmt 0.0% 0 / 1387 Branchbran 0.0% 0 / 686 Conditioncond 0.0% 0 / 479 MC/DCmcdc 0.0% 0 / 287 Subroutinesub 0.0% 0 / 126 Podpod 0.0% 0 / 109 totaltotal 0.0% 0 / 3074 CC 344 SCAR 43.8
CC
344
Cov
0%
finalize_rec
380.0
doctest_rec
272.0
serialize_aux
240.0
CRAP 80.0
1 # /=====================================================================\ #
2 # | LaTeXML::Core::Document | #
3 # | Constructs the Document from digested material | #
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  
13 package LaTeXML::Core::Document;
14 use strict;
15 use warnings;
16 use LaTeXML::Global;
17 use LaTeXML::Common::Object;
18 use LaTeXML::Core::List;
19 use LaTeXML::Common::Error;
20 use LaTeXML::Common::XML;
21 use LaTeXML::Util::Radix;
22 use Unicode::Normalize;
23 use Scalar::Util qw(blessed);
24 use base qw(LaTeXML::Common::Object);
25  
26 #**********************************************************************
27 # These two element names are `leaks' of the document structure into
28 # the Core of LaTeXML... In principle, we should be more abstract!
29  
30 our $FONT_ELEMENT_NAME = "ltx:text";
31 our $MATH_TOKEN_NAME = "ltx:XMTok";
32 our $MATH_HINT_NAME = "ltx:XMHint";
33 DebuggableFeature('document');
34  
35 #**********************************************************************
36  
37 # [could conceivable make more sense to let the Stomach create the Document?]
38  
39 # Mystery attributes:
40 # font :
41 # Probably should keep font ONLY in extra properties,
42 # THEN once complete, compute the relative font at each node that accepts
43 # a font attribute, and add the attribute.
44 # locator : a box
45  
46 sub new {
47 my ($class, $model) = @_;
48 my $doc = XML::LibXML::Document->new("1.0", "UTF-8");
49 # We'll set the DocType when the 1st Element gets added.
50 return bless { document => $doc, node => $doc, model => $model,
51 idstore => {}, labelstore => {},
52 node_fonts => {}, node_boxes => {}, node_properties => {},
53 pending => [], progress => 0 }, $class; }
54  
55 our $CONSTRUCTION_PROGRESS_QUANTUM = 500;
56  
57 #**********************************************************************
58 # Basic Accessors
59  
60 # This will be a node of type XML_DOCUMENT_NODE
61 sub getDocument { my ($self) = @_; return $$self{document}; }
62 sub getModel { my ($self) = @_; return $$self{model}; }
63 sub documentElement { my ($self) = @_; return $$self{document}->documentElement; }
64  
65 # Get the node representing the current insertion point.
66 # The node will have nodeType of
67 # XML_DOCUMENT_NODE if the document is empty, so far.
68 # XML_ELEMENT_NODE for normal elements.
69 # XML_TEXT_NODE if the last insertion was text
70 # The other node types will not appear here.
71 sub getNode { my ($self) = @_; return $$self{node}; }
72  
73 sub setNode {
74 my ($self, $node) = @_;
75 closeText_internal($self); # Close any open text node, so ligatures run.
76 my $type = $node->nodeType;
77 if ($type == XML_DOCUMENT_FRAG_NODE) { # Whoops
78 my @n = $node->childNodes;
79 if (@n > 1) {
80 Error('unexpected', 'multiple-nodes', $self,
81 "Cannot set insertion point to a DOCUMENT_FRAG_NODE", Stringify($node)); }
82 elsif (@n < 1) {
83 Error('unexpected', 'empty-nodes', $self,
84 "Cannot set insertion point to an empty DOCUMENT_FRAG_NODE"); }
85 $node = $n[0]; }
86 $$self{node} = $node;
87 return; }
88  
89 sub getLocator {
90 my ($self) = @_;
91 if (my $box = getNodeBox($self, $$self{node})) {
92 return $box->getLocator; }
93 else {
94 return; } } # well?
95  
96 # Get the element at (or containing) the current insertion point.
97 sub getElement {
98 my ($self) = @_;
99 my $node = $$self{node};
100 $node = $node->parentNode if $node->getType == XML_TEXT_NODE;
101 return ($node->getType == XML_DOCUMENT_NODE ? undef : $node); }
102  
103 # Get the child elements of the given $node
104 sub getChildElements {
105 my ($self, $node) = @_;
106 return (!$node ? () : grep { $_->nodeType == XML_ELEMENT_NODE } $node->childNodes); }
107  
108 # Get the last element node (if any) in $node
109 sub getLastChildElement {
110 my ($self, $node) = @_;
111 if ($node->hasChildNodes) {
112 my $n = $node->lastChild;
113 while ($n && $n->nodeType != XML_ELEMENT_NODE) {
114 $n = $node->previousSibling; }
115 return $n; } }
116  
117 # get the first element node (if any) in $node
118 sub getFirstChildElement {
119 my ($self, $node) = @_;
120 if ($node->hasChildNodes) {
121 my $n = $node->firstChild;
122 while ($n && $n->nodeType != XML_ELEMENT_NODE) {
123 $n = $n->nextSibling; }
124 return $n; }
125 return; }
126  
127 # get the second element node (if any) in $node
128 sub getSecondChildElement {
129 my ($self, $node) = @_;
130 my $first_child = getFirstChildElement($self, $node);
131 my $second_child = $first_child && $first_child->nextSibling;
132 while ($second_child && $second_child->nodeType != XML_ELEMENT_NODE) {
133 $second_child = $second_child->nextSibling; }
134 return $second_child; }
135  
136 # Find the nodes according to the given $xpath expression,
137 # the xpath is relative to $node (if given), otherwise to the document node.
138 sub findnodes {
139 my ($self, $xpath, $node) = @_;
140 return $$self{model}->getXPath->findnodes($xpath, ($node || $$self{document})); }
141  
142 # Like findnodes, but only returns the first matched node
143 sub findnode {
144 my ($self, $xpath, $node) = @_;
145 my @nodes = $$self{model}->getXPath->findnodes($xpath, ($node || $$self{document}));
146 return (@nodes ? $nodes[0] : undef); }
147  
148 # Get the node's qualified name in standard form
149 # Ie. using the registered prefix for that namespace.
150 # NOTE: Reconsider how _Capture_ & _WildCard_ should be integrated!?!
151 # NOTE: Should Deprecate! (use model)
152 sub getNodeQName {
153 my ($self, $node) = @_;
154 return $node && $$self{model}->getNodeQName($node); }
155  
156 #**********************************************************************
157 # Extensions of Model.
158  
159 sub canContain {
160 my ($self, $tag, $child) = @_;
161 my $model = $$self{model};
162 $tag = $model->getNodeQName($tag) if ref $tag; # In case tag is a node.
163 $child = $model->getNodeQName($child) if ref $child; # In case child is a node.
164 return $model->canContain($tag, $child); }
165  
166 # Can an element with (qualified name) $tag contain a $childtag element indirectly?
167 # That is, by openning some number of autoOpen'able tags?
168 # And if so, return the tag to open.
169 sub canContainIndirect {
170 my ($self, $tag, $child) = @_;
171 my $model = $$self{model};
172 $tag = $model->getNodeQName($tag) if ref $tag; # In case tag is a node.
173 $child = $model->getNodeQName($child) if ref $child; # In case child is a node.
174 # $imodel{$tag}{$child} => $intermediate || $child
175 my $imodel = $STATE->lookupValue('INDIRECT_MODEL');
176 if (!$imodel) {
177 $imodel = computeIndirectModel($self);
178 $STATE->assignValue(INDIRECT_MODEL => $imodel, 'global'); }
179 return $$imodel{$tag}{$child}; }
180  
181 # The indirect model includes all elements allowed as direct children,
182 # and all descendents of a node that can be inserted after autoOpen'ing intermediate elements.
183 # This model therefor includes information from the Schema, as well as
184 # autoOpen information that may be introduced in binding files.
185 # [Thus it should NOT be modifying the Model object, which may cover several documents in Daemon]
186 # $imodel{$tag}{$child} => $open means if in $tag, to open $child, we must first open $open
187 sub computeIndirectModel {
188 my ($self) = @_;
189 my $model = $$self{model};
190 my $imodel = {};
191 # Determine any indirect paths to each descendent via an `autoOpen-able' tag.
192 local %::OPENABILITY = ();
193 foreach my $tag ($model->getTags) {
194 my $x;
195 if (($x = $STATE->lookupMapping('TAG_PROPERTIES', $tag)) && ($x = $$x{autoOpen})) {
196 $::OPENABILITY{$tag} = ($x =~ /^\d*\.\d*$/ ? $x : 1); }
197 else {
198 $::OPENABILITY{$tag} = 0; } }
199 foreach my $tag ($model->getTags) {
200 local %::DESC = ();
201 computeIndirectModel_aux($model, $tag, '', 1);
202 foreach my $kid (sort keys %::DESC) {
203 my $best = 0; # Find best path to $kid.
204 foreach my $start (sort keys %{ $::DESC{$kid} }) {
205 if (($tag ne $kid) && ($tag ne $start) && ($::DESC{$kid}{$start} > $best)) {
206 $$imodel{$tag}{$kid} = $start; $best = $::DESC{$kid}{$start}; } } } }
207 # PATCHUP
208 if ($$model{permissive}) { # !!! Alarm!!!
209 $$imodel{'#Document'}{'#PCDATA'} = 'ltx:p'; }
210 return $imodel; }
211  
212 sub computeIndirectModel_aux {
213 my ($model, $tag, $start, $desirability) = @_;
214 my $x;
215 foreach my $kid ($model->getTagContents($tag)) {
216 next if $::DESC{$kid}{$start}; # Already solved
217 $::DESC{$kid}{$start} = $desirability if $start;
218 if (($kid ne '#PCDATA') && ($x = $::OPENABILITY{$kid})) {
219 computeIndirectModel_aux($model, $kid, $start || $kid, $desirability * $x); } }
220 return; }
221  
222 sub canContainSomehow {
223 my ($self, $tag, $child) = @_;
224 my $model = $$self{model};
225 $tag = $model->getNodeQName($tag) if ref $tag; # In case tag is a node.
226 $child = $model->getNodeQName($child) if ref $child; # In case child is a node.
227 return $model->canContain($tag, $child) || canContainIndirect($self, $tag, $child); }
228  
229 sub canHaveAttribute {
230 my ($self, $tag, $attrib) = @_;
231 my $model = $$self{model};
232 $tag = $model->getNodeQName($tag) if ref $tag; # In case tag is a node.
233 return $model->canHaveAttribute($tag, $attrib); }
234  
235 sub canAutoOpen {
236 my ($self, $tag) = @_;
237 if (my $props = $STATE->lookupMapping('TAG_PROPERTIES', $tag)) {
238 return $$props{autoOpen}; } }
239  
240 # Dirty little secrets:
241 # You can generically allow an element to autoClose using Tag.
242 # OR you can indicate a specific node can autoClose, or forbid it, using
243 # the _autoclose or _noautoclose attributes!
244 sub canAutoClose {
245 my ($self, $node) = @_;
246 my $t = $node->nodeType;
247 my $model = $$self{model};
248 my $props;
249 return ($t == XML_TEXT_NODE) || ($t == XML_COMMENT_NODE) # text or comments auto close
250 || (($t == XML_ELEMENT_NODE) # otherwise must be element
251 && !$node->getAttribute('_noautoclose') # without _noautoclose
252 && ($node->getAttribute('_autoclose') # and either with _autoclose
253 # OR it has autoClose set on tag properties
254 || (($props = $STATE->lookupMapping('TAG_PROPERTIES', getNodeQName($self, $node)))
255 && $$props{autoClose})));
256 }
257  
258 # get the actions that should be performed on afterOpen or afterClose
259 sub getTagActionList {
260 my ($self, $tag, $when) = @_;
261 $tag = $$self{model}->getNodeQName($tag) if ref $tag; # In case tag is a node.
262 my ($p, $n) = (undef, $tag);
263 if ($tag =~ /^([^:]+):(.+)$/) {
264 ($p, $n) = ($1, $2); }
265 my $when0 = $when . ':early';
266 my $when1 = $when . ':late';
267 my $taghash = $STATE->lookupMapping('TAG_PROPERTIES', $tag) || {};
268 my $nshash = ((defined $p) && $STATE->lookupMapping('TAG_PROPERTIES', $p . ':*')) || {};
269 my $allhash = $STATE->lookupMapping('TAG_PROPERTIES', '*') || {};
270 my $v;
271 return (
272 (($v = $$taghash{$when0}) ? @$v : ()),
273 (($v = $$nshash{$when0}) ? @$v : ()),
274 (($v = $$allhash{$when0}) ? @$v : ()),
275 (($v = $$taghash{$when}) ? @$v : ()),
276 (($v = $$nshash{$when}) ? @$v : ()),
277 (($v = $$allhash{$when}) ? @$v : ()),
278 (($v = $$taghash{$when1}) ? @$v : ()),
279 (($v = $$nshash{$when1}) ? @$v : ()),
280 (($v = $$allhash{$when1}) ? @$v : ()),
281 ); }
282  
283 #**********************************************************************
284 # This is a diagnostic tool that MIGHT help locate XML::LibXML bugs;
285 # It simply walks through the document tree. Use it before and after
286 # places where some sort of data corruption might have taken place.
287 sub doctest {
288 my ($self, $when, $severe) = @_;
289 local $LaTeXML::NNODES = 0;
290 Debug("START DOC TEST $when.....");
291 if (my $root = getDocument($self)->documentElement) {
292 doctest_rec($self, undef, $root, $severe); }
293 Debug("...(" . $LaTeXML::NNODES . " nodes)....DONE");
294 return; }
295  
296 sub doctest_rec {
297 my ($self, $parent, $node, $severe) = @_;
298 # Check consistency of document, parent & type, before proceeding
299 doctest_head($self, $parent, $node, $severe);
300 my $type = $node->nodeType;
301 if ($type == XML_ELEMENT_NODE) {
302 Debug("ELEMENT "
303 . join(' ', "<" . $$self{model}->getNodeQName($node),
304 (map { $_->nodeName . '="' . $_->getValue . '"' } $node->attributes)) . ">")
305 if $severe;
306 doctest_children($self, $node, $severe); }
307 elsif ($type == XML_ATTRIBUTE_NODE) {
308 Debug("ATTRIBUTE " . $node->nodeName . "=>" . $node->getValue) if $severe; }
309 elsif ($type == XML_TEXT_NODE) {
310 Debug("TEXT " . $node->textContent) if $severe; }
311 elsif ($type == XML_CDATA_SECTION_NODE) {
312 Debug("CDATA " . $node->textContent) if $severe; }
313 # elsif($type == XML_ENTITY_REF_NODE){}
314 # elsif($type == XML_ENTITY_NODE){}
315 elsif ($type == XML_PI_NODE) {
316 Debug("PI " . $node->localname . " " . $node->getData) if $severe; }
317 elsif ($type == XML_COMMENT_NODE) {
318 Debug("COMMENT " . $node->textContent) if $severe; }
319 # elsif($type == XML_DOCUMENT_NODE){}
320 # elsif($type == XML_DOCUMENT_TYPE_NODE){
321 elsif ($type == XML_DOCUMENT_FRAG_NODE) {
322 Debug("DOCUMENT_FRAG") if $severe;
323 doctest_children($self, $node, $severe); }
324 # elsif($type == XML_NOTATION_NODE){}
325 # elsif($type == XML_HTML_DOCUMENT_NODE){}
326 # elsif($type == XML_DTD_NODE){}
327 else {
328 Debug("OTHER $type") if $severe; }
329 return; }
330  
331 sub doctest_head {
332 my ($self, $parent, $node, $severe) = @_;
333 # Check consistency of document, parent & type, before proceeding
334 Debug(" NODE $$node [") if $severe; # BEFORE checking nodeType!
335 if (!$node->ownerDocument->isSameNode(getDocument($self))) {
336 Debug("d!") if $severe; }
337 if ($parent && !$node->parentNode->isSameNode($parent)) {
338 Debug("p!") if $severe; }
339 my $type = $node->nodeType;
340 Debug("t] ") if $severe;
341 return; }
342  
343 sub doctest_children {
344 my ($self, $node, $severe) = @_;
345 Debug("[fc") if $severe;
346 my $c = $node->firstChild;
347 while ($c) {
348 Debug("]") if $severe;
349 doctest_rec($self, $node, $c, $severe);
350 Debug("[nc") if $severe;
351 $c = $c->nextSibling; }
352 Debug("]done") if $severe;
353 return; }
354  
355 #**********************************************************************
356 # This should be called before returning the final XML::LibXML::Document to the
357 # outside world. It resolves the fonts for each node relative to it's ancestors.
358 # It removes the `helper' attributes that store fonts, source box, etc.
359 sub finalize {
360 my ($self) = @_;
361 pruneXMDuals($self);
362 if (my $root = getDocument($self)->documentElement) {
363 local $LaTeXML::FONT = LaTeXML::Common::Font->textDefault;
364 finalize_rec($self, $root);
365 set_RDFa_prefixes(getDocument($self), $STATE->lookupValue('RDFa_prefixes')); }
366 # return $$self{document}; }
367 return $self; }
368  
369 sub finalize_rec {
370 my ($self, $node) = @_;
371 my $model = $$self{model};
372 no warnings 'recursion';
373 my $qname = $model->getNodeQName($node);
374 # _standalone_font is typically for metadata that gets extracted out of context
375 my $declared_font = ($node->getAttribute('_standalone_font')
376 ? LaTeXML::Common::Font->textDefault : $LaTeXML::FONT);
377 my $desired_font = $LaTeXML::FONT;
378 my %pending_declaration = ();
379 if (my $comment = $node->getAttribute('_pre_comment')) {
380 $node->parentNode->insertBefore(XML::LibXML::Comment->new($comment), $node); }
381 if (my $comment = $node->getAttribute('_comment')) {
382 $node->parentNode->insertAfter(XML::LibXML::Comment->new($comment), $node); }
383  
384 if (my $font_attr = $node->getAttribute('_font')) {
385 $desired_font = $$self{node_fonts}{$font_attr};
386 %pending_declaration = $desired_font->relativeTo($declared_font);
387 if (($node->hasChildNodes || $node->getAttribute('_force_font'))
388 && scalar(keys %pending_declaration)) {
389 foreach my $attr (keys %pending_declaration) {
390 # Add (or combine, for @class) the attributes to the current node.
391 if ($model->canHaveAttribute($qname, $attr)) {
392 my $value = $pending_declaration{$attr}{value};
393 if ($attr eq 'class') { # Generalize?
394 if (my $ovalue = $node->getAttribute('class')) {
395 $value .= ' ' . $ovalue; } }
396 setAttribute($self, $node, $attr => $value);
397  
398 # Merge to set the font currently in effect
399 $declared_font = $declared_font->merge(%{ $pending_declaration{$attr}{properties} });
400 delete $pending_declaration{$attr}; } }
401 } }
402 # Optionally add ids to all nodes (AFTER all parsing, rearrangement, etc)
403 if ($STATE && $STATE->lookupValue('GENERATE_IDS')
404 && !$node->hasAttribute('xml:id')
405 && canHaveAttribute($self, $qname, 'xml:id')
406 && ($qname ne 'ltx:document')) {
407 LaTeXML::Package::GenerateID($self, $node); }
408  
409 local $LaTeXML::FONT = $declared_font;
410 foreach my $child ($node->childNodes) {
411 my $type = $child->nodeType;
412 if ($type == XML_ELEMENT_NODE) {
413 my $was_forcefont = $child->getAttribute('_force_font');
414 finalize_rec($self, $child);
415 # Also check if child is $FONT_ELEMENT_NAME AND has no attributes
416 # AND providing $node can contain that child's content, we'll collapse it.
417 if (($model->getNodeQName($child) eq $FONT_ELEMENT_NAME)
418 && !$was_forcefont && !$child->hasAttributes) {
419 my @grandchildren = $child->childNodes;
420 if (!grep { !canContain($self, $qname, $_) } @grandchildren) {
421 replaceNode($self, $child, @grandchildren); } }
422 }
423 # On the other hand, if the font declaration has NOT been effected,
424 # We'll need to put an extra wrapper around the text!
425 # This is usually ltx:text, but Font information can override this (eg. for \emph)
426 elsif ($type == XML_TEXT_NODE) {
427 # Remove any pending declarations that can't be on $FONT_ELEMENT_NAME
428 my $elementname = $pending_declaration{element}{value} || $FONT_ELEMENT_NAME;
429 delete $pending_declaration{element}; # If any...
430 foreach my $key (keys %pending_declaration) {
431 delete $pending_declaration{$key} unless canHaveAttribute($self, $elementname, $key); }
432 if (canContain($self, $qname, $elementname)
433 && scalar(keys %pending_declaration)) {
434 # Too late to do wrapNodes?
435 my $text = wrapNodes($self, $elementname, $child);
436 # Add (or combine) attributes
437 foreach my $attr (keys %pending_declaration) {
438 my $value = $pending_declaration{$attr}{value};
439 if ($attr eq 'class') { # Generalize?
440 if (my $ovalue = $text->getAttribute('class')) {
441 $value .= ' ' . $ovalue; } }
442 setAttribute($self, $text, $attr => $value); }
443 finalize_rec($self, $text); # Now have to clean up the new node!
444 }
445 } }
446  
447 # Attributes (non-namespaced) that begin with "_" are for internal, temporary, Bookkeeping.
448 # Remove them now.
449 foreach my $attr ($node->attributes) {
450 my $n = $attr->nodeName;
451 $node->removeAttribute($n) if $n && $n =~ /^_/; }
452 return; }
453  
454 #======================================================================
455 # Experimental Serializer
456 # inserts formatting whitespace ONLY where allowed by the schema
457 #======================================================================
458 use Encode;
459  
460 sub toString {
461 #sub serialize {
462 my ($self, $format) = @_;
463 # This line is to use libxml2's built-in serializer w/indentation heuristic.
464 # Apparently, libxml2 is giving us "binary" or byte strings which we'd prefer to have as text.
465 # return decode('UTF-8',getDocument($self)->toString($format)); }
466 # This uses our own serializer emulating libxml2's heuristic indentation.
467 # return serialize_aux($self, getDocument($self), 0, 0, 1); }
468 # This uses our own serializer w/ correct indentation rules.
469 return serialize_aux($self, getDocument($self), 0, 0, 0); }
470  
471 # We ought to try for something close to C14N (http://www.w3.org/TR/xml-c14n),
472 # but keep XML declaration, comments and don't convert empty elements.
473 sub serialize_aux {
474 my ($self, $node, $depth, $noindent, $heuristic) = @_;
475 no warnings 'recursion';
476 my $type = $node->nodeType;
477 my $model = $$self{model};
478 my $indent = (' ' x $depth);
479 if ($type == XML_DOCUMENT_NODE) {
480 my @children = $node->childNodes;
481 return join('', '<?xml version="1.0" encoding="UTF-8"?>', "\n",
482 (map { serialize_aux($self, $_, $depth, $noindent, $heuristic) } @children)); }
483 elsif ($type == XML_ELEMENT_NODE) {
484 my $tag = $model->getNodeDocumentQName($node);
485 my @children = $node->childNodes;
486 # since we're pretty-printing, we _could_ wrap attributes to nominal line length!
487 my @anodes = $node->attributes;
488 my %nsnodes = map { $model->getNodeDocumentQName($_) => serialize_attr($_->nodeValue) }
489 grep { $_->nodeType == XML_NAMESPACE_DECL } @anodes;
490 my %atnodes = map { $model->getNodeDocumentQName($_) => serialize_attr($_->nodeValue) }
491 grep { $_->nodeType == XML_ATTRIBUTE_NODE } @anodes;
492 my $start = join(' ',
493 # start of tag
494 '<' . $tag,
495 # Namespace declarations
496 (map { $_ . '="' . $nsnodes{$_} . '"' } sort keys %nsnodes),
497 # Regular attributes
498 (map { $_ . '="' . $atnodes{$_} . '"' } sort keys %atnodes)
499 );
500 my $noindent_children = ($heuristic
501 # This emulates libxml2's heuristic
502 # ? $noindent || grep { $_->nodeType != XML_ELEMENT_NODE } @children
503 ? $noindent || grep { $_->nodeType == XML_TEXT_NODE } @children
504 # This is the "Correct" way to determine whether to add indentation
505 : $model->canContain(getNodeQName($self, $node), '#PCDATA'));
506 return join('',
507 ($noindent ? '' : $indent), $start,
508 (scalar(@children) # with contents.
509 ? ('>', ($noindent_children ? '' : "\n"),
510 (map { serialize_aux($self, $_, $depth + 1, $noindent_children, $heuristic) } @children),
511 ($noindent_children ? '' : $indent), '</' . $tag . '>', ($noindent ? '' : "\n"))
512 : ('/>' . ($noindent ? '' : "\n")))); } # empty element.
513 elsif ($type == XML_TEXT_NODE) { # NO indentation!
514 return serialize_string($node->textContent); }
515 elsif ($type == XML_PI_NODE) {
516 # should code this by hand, as well...
517 return join('', ($noindent ? '' : $indent), $node->toString, ($noindent ? '' : "\n")); }
518 elsif ($type == XML_COMMENT_NODE) {
519 return join('', '<!-- ', serialize_string($node->textContent), '-->'); }
520 else {
521 return ''; } }
522  
523 sub serialize_string {
524 my ($string) = @_;
525 # Basic entities
526 $string =~ s/&/&amp;/g;
527 $string =~ s/>/&gt;/g;
528 $string =~ s/</&lt;/g;
529 # Remove dis-allowed code-points.
530 # $string =~ s/(?:\x{00}-\x{08}|\x{0B}|\x{0C}|\x{0D}-\x{19}|\x{D800}-\x{DFFF}|\x{FFFE}-\x{FFFF})//g;
531 # Hmm... the upper ranges gives warning in some Perls...
532 $string =~ s/(?:\x{00}-\x{08}|\x{0B}|\x{0C}|\x{0D}-\x{19})//g;
533 return $string; }
534  
535 sub serialize_attr {
536 my ($string) = @_;
537 $string = serialize_string($string);
538 # And escape any remaining special code points
539 $string =~ s/"/&quot;/g;
540 $string =~ s/\n/&#10;/gs;
541 $string =~ s/\t/&#9;/gs;
542 return $string; }
543  
544 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
545 # Document construction at the Current Insertion Point.
546 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
547  
548 #**********************************************************************
549 # absorb the given $box into the DOM (called from constructors).
550 # This will return a list of whatever nodes were created.
551 # Note that this may include nodes that are children of other nodes in the list
552 # or nodes that are no longer in the document.
553 # Also, note that when a text nodes is appended to, the complete text node is in the list,
554 # not just the portion that was added.
555 # [Note that recording the nodes being constructed isn't all that costly,
556 # but filtering them for parent/child relations IS, particularly since it usually isn't needed]
557 #
558 # A $box that is a Box, or List, or Whatsit, is responsible for carrying out
559 # its own insertion, but it should ultimately call methods of Document
560 # that will record the nodes that were created.
561 # $box can also be a plain string which will be inserted according to whatever
562 # font, mode, etc, are in %props.
563 sub absorb {
564 my ($self, $object, %props) = @_;
565 no warnings 'recursion';
566 # Nothing? Skip it
567 my @boxes = ($object);
568 my @results = ();
569 while (@boxes) {
570 my $box = shift(@boxes);
571 next unless defined $box;
572 # Simply unwind Lists to avoid unneccessary recursion; This occurs quite frequently!
573 if (((ref $box) || 'nothing') eq 'LaTeXML::Core::List') {
574 unshift(@boxes, $box->unlist);
575 next; }
576 # A Proper Box or Whatsit? It will handle it.
577 if (ref $box) {
578 local $LaTeXML::BOX = $box;
579 # [ATTEMPT to] only record if we're running in NON-VOID context.
580 # [but wantarray seems defined MUCH more than I would have expected!?]
581 if ($LaTeXML::RECORDING_CONSTRUCTION || defined wantarray) {
582 my @n = ();
583 { local $LaTeXML::RECORDING_CONSTRUCTION = 1;
584 local @LaTeXML::CONSTRUCTED_NODES = ();
585 $box->beAbsorbed($self);
586 @n = @LaTeXML::CONSTRUCTED_NODES; } # These were created just now
587 map { recordConstructedNode($self, $_) } @n; # record these for OUTER caller!
588 push(@results, @n); } # but return only the most recent set.
589 else {
590 push(@results, $box->beAbsorbed($self)); } }
591 # Else, plain string in text mode.
592 elsif (!$props{isMath}) {
593 push(@results, openText($self, $box, $props{font} || ($LaTeXML::BOX && $LaTeXML::BOX->getFont))); }
594 # Or plain string in math mode.
595 # Note text nodes can ONLY appear in <XMTok> or <text>!!!
596 # Have we already opened an XMTok? Then insert into it.
597 elsif ($$self{model}->getNodeQName($$self{node}) eq $MATH_TOKEN_NAME) {
598 push(@results, openMathText_internal($self, $box)); }
599 # Else create the XMTok now.
600 else {
601 # Odd case: constructors that work in math & text can insert raw strings in Math mode.
602 push(@results, insertMathToken($self, $box, font => $props{font})); } }
603 return @results; }
604  
605 # Note that a box has been absorbed creating $node;
606 # This does book keeping so that we can return the sequence of nodes
607 # that were added by absorbing material.
608 sub recordConstructedNode {
609 my ($self, $node) = @_;
610 if ((defined $LaTeXML::RECORDING_CONSTRUCTION) # If we're recording!
611 && (!@LaTeXML::CONSTRUCTED_NODES # and this node isn't already recorded
612 || !$node->isSameNode($LaTeXML::CONSTRUCTED_NODES[-1]))) {
613 push(@LaTeXML::CONSTRUCTED_NODES, $node); }
614 return; }
615  
616 sub filterDeletions {
617 my ($self, @nodes) = @_;
618 my $doc = $$self{document};
619 # This test seems to successfully determine inclusion,
620 # without requiring the (dangerous? & dubious?) unbindNode to be used.
621 return grep { isDescendantOrSelf($_, $doc) } @nodes; }
622  
623 # Given a list of nodes such as from ->absorb,
624 # filter out all the nodes that are children of other nodes in the list.
625 sub filterChildren {
626 my ($self, @node) = @_;
627 # return @node;
628 # return ();
629 return () unless @node;
630 my @n = (shift(@node));
631 foreach my $n (@node) {
632 push(@n, $n) unless grep { isDescendantOrSelf($n, $_); } @n; }
633 return @n; }
634  
635 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
636  
637 # Shorthand for open,absorb,close, but returns the new node.
638 sub insertElement {
639 my ($self, $qname, $content, %attrib) = @_;
640 my $node = openElement($self, $qname, %attrib);
641 if (ref $content eq 'ARRAY') {
642 map { absorb($self, $_) } @$content; }
643 elsif (defined $content) {
644 absorb($self, $content); }
645 # In obscure situations, $node may have already gotten closed?
646 # close it if it is still open.
647 my $c = $$self{node};
648 while ($c && ($c->nodeType != XML_DOCUMENT_NODE) && !$c->isSameNode($node)) {
649 $c = $c->parentNode; }
650 if ($c->isSameNode($node)) {
651 closeElement($self, $qname); }
652 return $node; }
653  
654 sub insertMathToken {
655 my ($self, $string, %attributes) = @_;
656 $attributes{role} = 'UNKNOWN' unless $attributes{role};
657 my $qname = ($attributes{isSpace} ? $MATH_HINT_NAME : $MATH_TOKEN_NAME);
658 my $cur_qname = $$self{model}->getNodeQName($$self{node});
659 if ($attributes{isSpace} && (defined $string) && ($string =~ /^\s*$/)) {
660 $string = undef; } # Make empty hint, of only spaces
661 if (($qname eq $MATH_TOKEN_NAME) && ($cur_qname eq $qname)) { # Already INSIDE a token!
662 openMathText_internal($self, $string) if defined $string;
663 return $$self{node}; }
664 else {
665 my $node = openElement($self, $qname, %attributes);
666 my $box = $attributes{_box} || $LaTeXML::BOX;
667 my $font = $attributes{font} || $box->getFont;
668 setNodeFont($self, $node, $font);
669 setNodeBox($self, $node, $box);
670 openMathText_internal($self, $string) if defined $string;
671 closeNode_internal($self, $node); # Should be safe.
672 return $node; } }
673  
674 # Insert a new comment, or append to previous comment.
675 # Does NOT move the current insertion point to the Comment,
676 # but may move up past a text node.
677 sub insertComment {
678 my ($self, $text) = @_;
679 chomp($text);
680 $text =~ s/\-\-+/__/g;
681 my $comment;
682 my $node = getElement($self);
683 my $prev = $node && $node->lastChild;
684 my $prevtype = $prev && $prev->nodeType;
685 if ($$self{node}->nodeType == XML_DOCUMENT_NODE) {
686 push(@{ $$self{pending} }, $comment = $$self{document}->createComment(' ' . $text . ' ')); }
687 elsif ($prevtype && ($prevtype == XML_COMMENT_NODE)) {
688 $comment = $prev;
689 $comment->setData($comment->data . "\n " . $text . ' '); }
690 elsif ($prevtype && ($prevtype == XML_TEXT_NODE)) { # Put comment BEFORE text node
691 if (($comment = $prev->previousSibling) && ($comment->nodeType == XML_COMMENT_NODE)) {
692 $comment = $node->appendChild($$self{document}->createComment(' ' . $text . ' ')); }
693 else {
694 $comment = $node->insertBefore($$self{document}->createComment(' ' . $text . ' '), $prev); } }
695 else {
696 $comment = $node->appendChild($$self{document}->createComment(' ' . $text . ' ')); }
697 return $comment; }
698  
699 # Insert a ProcessingInstruction of the form <?op attr=value ...?>
700 # Does NOT move the current insertion point to the PI,
701 # but may move up past a text node.
702 sub insertPI {
703 my ($self, $op, %attrib) = @_;
704 # We'll just put these on the document itself.
705 # Put these in an attractive order, main "operator" first
706 my @keys = ((map { ($attrib{$_} ? ($_) : ()) } qw(class package options)),
707 (grep { $_ !~ /^(?:class|package|options)$/ } sort keys %attrib));
708 my $data = join(' ', map { $_ . "=\"" . ToString($attrib{$_}) . "\"" } @keys);
709 my $pi = $$self{document}->createProcessingInstruction($op, $data);
710 closeText_internal($self); # Close any open text node
711 if ($$self{node}->nodeType == XML_DOCUMENT_NODE) {
712 push(@{ $$self{pending} }, $pi); }
713 else {
714 $$self{document}->insertBefore($pi, $$self{document}->documentElement); }
715 return $pi; }
716  
717 # Insert an empty element before a given node
718 # Does NOT move the current insertion point
719 sub insertElementBefore {
720 my ($self, $point, $name, %attrib) = @_;
721 my $new = $$self{document}->createElement($name);
722 $new->setNamespace($LaTeXML::Common::Model::LTX_NAMESPACE, '', 1);
723 for my $key (keys %attrib) {
724 $new->setAttribute($key, $attrib{$key}); }
725 return $point->parentNode->insertBefore($new, $point); }
726  
727 #**********************************************************************
728 # Middle level, mostly public, API.
729 # Handlers for various construction operations.
730 # General naming: 'open' opens a node at current pos and sets it to current,
731 # 'close' closes current node(s), inserts opens & closes, ie. w/o moving current
732  
733 # Tricky: Insert some text in a particular font.
734 # We need to find the current effective -- being the closest _declared_ font,
735 # (ie. it will appear in the elements attributes). We may also want
736 # to open/close some elements in such a way as to minimize the font switchiness.
737 # I guess we should only open/close "text" elements, though.
738 # [Actually, we'd like the user to _declare_ what element to use....
739 # I don't like having "text" built in here!
740 # AND, we've assumed that "font" names the relevant attribute!!!]
741  
742 sub openText {
743 my ($self, $text, $font) = @_;
744 my $node = $$self{node};
745 my $t = $node->nodeType;
746 return if ((!defined $text) || $text =~ /^\s*$/) &&
747 (($t == XML_DOCUMENT_NODE) # Ignore initial whitespace
748 || (($t == XML_ELEMENT_NODE) && !canContain($self, $node, '#PCDATA')));
749 return if $font->getFamily eq 'nullfont';
750 Debug("openText \"$text\" /" . Stringify($font) . " at " . Stringify($node))
751 if $LaTeXML::DEBUG{document};
752 # Get the desired font attributes, particularly the desired element
753 # (usually ltx:text, but let Font override, eg for \emph)
754 my $declared_font = getNodeFont($self, $node);
755 my %pending_declaration = $font->relativeTo($declared_font);
756 my $elementname = $pending_declaration{element}{value} || $FONT_ELEMENT_NAME;
757 if (($t != XML_DOCUMENT_NODE) # If not at document begin
758 && !(($t == XML_TEXT_NODE) && # And not appending text in same font.
759 ($font->distance(getNodeFont($self, $node->parentNode)) == 0))) {
760 # then we'll need to do some open/close to get fonts matched.
761 $node = closeText_internal($self); # Close text node, if any.
762 my ($bestdiff, $closeto) = (99999, $node);
763 my $n = $node;
764 while ($n->nodeType != XML_DOCUMENT_NODE) {
765 my $d = $font->distance(getNodeFont($self, $n));
766 if ($d < $bestdiff) {
767 $bestdiff = $d;
768 $closeto = $n;
769 last if ($d == 0); }
770 last if ($$self{model}->getNodeQName($n) ne $elementname) || $n->getAttribute('_noautoclose');
771 $n = $n->parentNode; }
772 closeToNode($self, $closeto) if $closeto ne $node; # Move to best starting point for this text.
773 openElement($self, $elementname, font => $font,
774 _fontswitch => 1, _autoopened => 1)
775 if $bestdiff > 0; # Open if needed.
776 }
777 # Finally, insert the darned text.
778 my $tnode = openText_internal($self, $text);
779 recordConstructedNode($self, $tnode);
780 return $tnode; }
781  
782 # Mystery:
783 # How to deal with font declarations?
784 # font vs _font; either must redirect to Font object until they are relativized, at end.
785 # When relativizing, should it depend on font attribute on element and/or DTD allowed attribute?
786 sub openElement {
787 my ($self, $qname, %attributes) = @_;
788 ProgressStep() if ($$self{progress}++ % $CONSTRUCTION_PROGRESS_QUANTUM) == 0;
789 Debug("openElement $qname at " . Stringify($$self{node})) if $LaTeXML::DEBUG{document};
790 my $point = find_insertion_point($self, $qname);
791 $attributes{_box} = $LaTeXML::BOX unless $attributes{_box};
792 my $newnode = openElementAt($self, $point, $qname,
793 _font => $attributes{font} || $attributes{_box}->getFont,
794 %attributes);
795 setNode($self, $newnode);
796 return $newnode; }
797  
798 # Note: This closes the deepest open node of a given type.
799 # This can cause problems with auto-opened nodes, esp. ones for fontswitches!
800 # Since this is an "explicit request", we're currently skipping over those nodes,
801 # ie. we're automatically closing them, even if they're the same type as we're asking to close!!!
802 # This is kinda risky! Maybe we should try to request closing of specific nodes.
803 sub closeElement {
804 my ($self, $qname) = @_;
805 Debug("closeElement $qname at " . Stringify($$self{node})) if $LaTeXML::DEBUG{document};
806 closeText_internal($self);
807 my ($node, @cant_close) = ($$self{node});
808 while ($node->nodeType != XML_DOCUMENT_NODE) {
809 my $t = $$self{model}->getNodeQName($node);
810 # autoclose until node of same name BUT also close nodes opened' for font switches!
811 last if ($t eq $qname) && !(($t eq $FONT_ELEMENT_NAME) && $node->getAttribute('_fontswitch'));
812 push(@cant_close, $node) unless canAutoClose($self, $node);
813 $node = $node->parentNode; }
814 if ($node->nodeType == XML_DOCUMENT_NODE) { # Didn't find $qname at all!!
815 Error('malformed', $qname, $self,
816 "Attempt to close " . ($qname eq '#PCDATA' ? $qname : '</' . $qname . '>') . ", which isn't open",
817 "Currently in " . getInsertionContext($self));
818 return; }
819 else { # Found node.
820 # Intervening non-auto-closeable nodes!!
821 Error('malformed', $qname, $self,
822 "Closing " . ($qname eq '#PCDATA' ? $qname : '</' . $qname . '>')
823 . " whose open descendents do not auto-close",
824 "Descendents are " . join(', ', map { Stringify($_) } @cant_close))
825 if @cant_close;
826 # So, now close up to the desired node.
827 closeNode_internal($self, $node);
828 return $node; } }
829  
830 # Check whether it is possible to open $qname at this point,
831 # possibly by autoOpen'ing & autoClosing other tags.
832 sub isOpenable {
833 my ($self, $qname) = @_;
834 my $node = $$self{node};
835 while ($node) {
836 return 1 if canContainSomehow($self, $node, $qname);
837 return 0 unless canAutoClose($self, $node); # could close, then check if parent can contain
838 $node = $node->parentNode; }
839 return 0; }
840  
841 # Check whether it is possible to close each element in @tags,
842 # any intervening nodes must be autocloseable.
843 # returning the last node that would be closed if it is possible,
844 # otherwise undef.
845 sub isCloseable {
846 my ($self, @tags) = @_;
847 my $node = $$self{node};
848 $node = $node->parentNode if $node->nodeType == XML_TEXT_NODE;
849 while (my $qname = shift(@tags)) {
850 while (1) {
851 return if $node->nodeType == XML_DOCUMENT_NODE;
852 my $this_qname = $$self{model}->getNodeQName($node);
853 last if $this_qname eq $qname;
854 return unless canAutoClose($self, $node);
855 $node = $node->parentNode; }
856 $node = $node->parentNode if @tags; }
857 return $node; }
858  
859 # Close $qname, if it is closeable.
860 sub maybeCloseElement {
861 my ($self, $qname) = @_;
862 Debug("maybeCloseNode(int) $qname") if $LaTeXML::DEBUG{document};
863 if (my $node = isCloseable($self, $qname)) {
864 closeNode_internal($self, $node);
865 return $node; } }
866  
867 # This closes all nodes until $node becomes the current point.
868 sub closeToNode {
869 my ($self, $node, $ifopen) = @_;
870 my $model = $$self{model};
871 my ($t, @cant_close) = ();
872 my $n = $$self{node};
873 my $lastopen;
874 # go up the tree from current node, till we find $node
875 while ((($t = $n->getType) != XML_DOCUMENT_NODE) && !$n->isSameNode($node)) {
876 push(@cant_close, $n) unless canAutoClose($self, $n);
877 $lastopen = $n;
878 $n = $n->parentNode; }
879 if ($t == XML_DOCUMENT_NODE) { # Didn't find $node at all!!
880 Error('malformed', $model->getNodeQName($node), $self,
881 "Attempt to close to " . Stringify($node) . ", which isn't open",
882 "Currently in " . getInsertionContext($self)) unless $ifopen;
883 return; }
884 else { # Found node.
885 Error('malformed', $model->getNodeQName($node), $self,
886 "Closing to " . Stringify($node) . " whose open descendents do not auto-close",
887 "Descendents are " . join(', ', map { Stringify($_) } @cant_close))
888 if @cant_close; # But found has intervening non-auto-closeable nodes!!
889 closeNode_internal($self, $lastopen) if $lastopen; }
890 return; }
891  
892 # This closes all nodes until $node is closed.
893 sub closeNode {
894 my ($self, $node) = @_;
895 my $model = $$self{model};
896 my ($t, @cant_close) = ();
897 my $n = $$self{node};
898 Debug("To closeNode " . Stringify($node)) if $LaTeXML::DEBUG{document};
899 while ((($t = $n->getType) != XML_DOCUMENT_NODE) && !$n->isSameNode($node)) {
900 push(@cant_close, $n) unless canAutoClose($self, $n);
901 $n = $n->parentNode; }
902 if ($t == XML_DOCUMENT_NODE) { # Didn't find $qname at all!!
903 Error('malformed', $model->getNodeQName($node), $self,
904 "Attempt to close " . Stringify($node) . ", which isn't open",
905 "Currently in " . getInsertionContext($self)); }
906 else { # Found node.
907 # Intervening non-auto-closeable nodes!!
908 Error('malformed', $model->getNodeQName($node), $self,
909 "Closing " . Stringify($node) . " whose open descendents do not auto-close",
910 "Descendents are " . join(', ', map { Stringify($_) } @cant_close))
911 if @cant_close;
912 closeNode_internal($self, $node); }
913 return; }
914  
915 sub maybeCloseNode {
916 my ($self, $node) = @_;
917 my $model = $$self{model};
918 my ($t, @cant_close) = ();
919 my $n = $$self{node};
920 Debug("To closeNode " . Stringify($node)) if $LaTeXML::DEBUG{document};
921 while ((($t = $n->getType) != XML_DOCUMENT_NODE) && !$n->isSameNode($node)) {
922 push(@cant_close, $n) unless canAutoClose($self, $n);
923 $n = $n->parentNode; }
924 if ($t == XML_DOCUMENT_NODE) { } # Didn't find $qname at all!!
925 else { # Found node.
926 # Intervening non-auto-closeable nodes!!
927 Info('malformed', $model->getNodeQName($node), $self,
928 "Closing " . Stringify($node) . " whose open descendents do not auto-close",
929 "Descendents are " . join(', ', map { Stringify($_) } @cant_close))
930 if @cant_close;
931 closeNode_internal($self, $node); }
932 return; }
933  
934 # Add the given attribute to the nearest node that is allowed to have it.
935 sub addAttribute {
936 my ($self, $key, $value) = @_;
937 return unless defined $value;
938 my $node = $$self{node};
939 $node = $node->parentNode if $node->nodeType == XML_TEXT_NODE;
940 while (($node->nodeType != XML_DOCUMENT_NODE) && !$$self{model}->canHaveAttribute($node, $key)) {
941 $node = $node->parentNode; }
942 if ($node->nodeType == XML_DOCUMENT_NODE) {
943 Error('malformed', $key, $self,
944 "Attribute $key not allowed in this node or ancestors"); }
945 else {
946 setAttribute($self, $node, $key, $value); }
947 return; }
948  
949 #**********************************************************************
950 # Low level internal interface
951  
952 # Return a string indicating the path to the current insertion point in the document.
953 # if $levels is defined, show only that many levels
954 sub getInsertionContext {
955 my ($self, $levels) = @_;
956 if (!defined $levels) { # Default depth is based on verbosity
957 $levels = 5 if ($LaTeXML::Common::Error::VERBOSITY <= 1); }
958 my $node = $$self{node};
959 my $type = $node->nodeType;
960 if (($type != XML_TEXT_NODE) && ($type != XML_ELEMENT_NODE) && ($type != XML_DOCUMENT_NODE)) {
961 Error('internal', 'context', $self,
962 "Insertion point is not an element, document or text: ", Stringify($node));
963 return; }
964 my $path = Stringify($node);
965 while ($node = $node->parentNode) {
966 if ((defined $levels) && (--$levels <= 0)) { $path = '...' . $path; last; }
967 $path = Stringify($node) . $path; }
968 return $path; }
969  
970 # Find the node where an element with qualified name $qname can be inserted.
971 # This will move up the tree (closing auto-closable elements),
972 # or down (inserting auto-openable elements), as needed.
973 sub find_insertion_point {
974 my ($self, $qname, $has_opened) = @_;
975 closeText_internal($self); # Close any current text node.
976 my $cur_qname = $$self{model}->getNodeQName($$self{node});
977 my $inter;
978 # If $qname is allowed at the current point, we're done.
979 if (canContain($self, $cur_qname, $qname)) {
980 return $$self{node}; }
981 # Else, if we can create an intermediate node that accepts $qname, we'll do that.
982 elsif (($inter = canContainIndirect($self, $cur_qname, $qname))
983 && ($inter ne $qname) && ($inter ne $cur_qname)) {
984 Debug("Need intermediate $inter to open $qname") if $LaTeXML::DEBUG{document};
985 openElement($self, $inter, _autoopened => 1,
986 font => getNodeFont($self, $$self{node}));
987 return find_insertion_point($self, $qname, $inter); } # And retry insertion (should work now).
988 elsif ($has_opened) { # out of options if already inside an auto-open chain
989 Error('malformed', $qname, $self,
990 ($qname eq '#PCDATA' ? $qname : '<' . $qname . '>') . " failed auto-open through <$has_opened> at inadmissible <$cur_qname>",
991 "Currently in " . getInsertionContext($self));
992 return $$self{node}; } # But we'll do it anyway, unless Error => Fatal.
993 else { # Now we're getting more desparate...
994 # Check if we can auto close some nodes, and _then_ insert the $qname.
995 my ($node, $closeto) = ($$self{node});
996 while (($node->nodeType != XML_DOCUMENT_NODE) && canAutoClose($self, $node)) {
997 my $parent = $node->parentNode;
998 if (canContainSomehow($self, $parent, $qname)) {
999 $closeto = $node; last; }
1000 $node = $parent; }
1001 if ($closeto) {
1002 my $closeto_qname = $$self{model}->getNodeQName($closeto);
1003 closeNode_internal($self, $closeto); # Close the auto closeable nodes.
1004 return find_insertion_point($self, $qname); } # Then retry, possibly w/auto open's
1005 else { # Didn't find a legit place.
1006 Error('malformed', $qname, $self,
1007 ($qname eq '#PCDATA' ? $qname : '<' . $qname . '>') . " isn't allowed in <$cur_qname>",
1008 "Currently in " . getInsertionContext($self));
1009 return $$self{node}; } } } # But we'll do it anyway, unless Error => Fatal.
1010  
1011 sub getInsertionCandidates {
1012 my ($node) = @_;
1013 my @nodes = ();
1014 # Check the current element FIRST, then build list of candidates.
1015 my $first = $node;
1016 $first = $first->parentNode if $first && $first->getType == XML_TEXT_NODE;
1017 my $isCapture = $first && ($first->localname || '') eq '_Capture_';
1018 push(@nodes, $first) if $first && $first->getType != XML_DOCUMENT_NODE && !$isCapture;
1019 # Collect previous siblings, if node is a text node.
1020 # OR if it is a effectively a text node (ltx:para/ltx:p/text)!!!
1021 my $do_sibs = $node->getType == XML_TEXT_NODE;
1022 # Now collect (element) node & ancestors
1023 while ($node && ($node->nodeType != XML_DOCUMENT_NODE)) {
1024 my $n = $node;
1025 if (($node->localname || '') eq '_Capture_') {
1026 push(@nodes, element_nodes($node)); }
1027 else {
1028 push(@nodes, $node); }
1029 if ($do_sibs && ($n = $node->previousSibling)) {
1030 $node = $n;
1031 }
1032 else {
1033 $node = $node->parentNode;
1034 my $t = $node->localname || '';
1035 $do_sibs = 0 unless ($t eq 'p') || ($t eq 'para');
1036 } }
1037 push(@nodes, $first) if $isCapture;
1038 return @nodes; }
1039  
1040 # The following "floatTo" operations find an appropriate point
1041 # within the document tree preceding the current insertion point.
1042 # They return undef (& issue a warning) if such a point cannot be found.
1043 # Otherwise, they move the current insertion point to the appropriate node,
1044 # and return the previous insertion point.
1045 # After you make whatever changes (insertions or whatever) to the tree,
1046 # you should do
1047 # $document->setNode($savenode)
1048 # to reset the insertion point to where it had been.
1049  
1050 # Find a node in the document that can contain an element $qname
1051 sub floatToElement {
1052 my ($self, $qname, $closeifpossible) = @_;
1053 my @candidates = getInsertionCandidates($$self{node});
1054 my $closeable = 1;
1055 # If the current node can contain already, we're fine right here - just return
1056 if (@candidates && canContain($self, $candidates[0], $qname)) {
1057 # Edge case: Don't resume at a text node, if it is current. Don't append more to it after other insertions.
1058 setNode($self, $candidates[0]) if $$self{node}->getType == XML_TEXT_NODE;
1059 return $candidates[0]; }
1060 while (@candidates && !canContain($self, $candidates[0], $qname)) {
1061 $closeable &&= canAutoClose($self, $candidates[0]);
1062 shift(@candidates); }
1063 if (my $n = shift(@candidates)) {
1064 if ($closeifpossible && $closeable) {
1065 closeToNode($self, $n); }
1066 else {
1067 my $savenode = $$self{node};
1068 setNode($self, $n);
1069 Debug("Floating from " . Stringify($savenode) . " to " . Stringify($n) . " for $qname")
1070 if ($$savenode ne $$n) && $LaTeXML::DEBUG{document};
1071 return $savenode; } }
1072 else {
1073 Warn('malformed', $qname, $self, "No open node can contain element '$qname'",
1074 getInsertionContext($self))
1075 unless canContainSomehow($self, $$self{node}, $qname); }
1076 return; }
1077  
1078 # Find a node in the document that can accept the attribute $key
1079 sub floatToAttribute {
1080 my ($self, $key) = @_;
1081 my @candidates = getInsertionCandidates($$self{node});
1082 while (@candidates && !canHaveAttribute($self, $candidates[0], $key)) {
1083 shift(@candidates); }
1084 if (my $n = shift(@candidates)) {
1085 my $savenode = $$self{node};
1086 setNode($self, $n);
1087 return $savenode; }
1088 else {
1089 Warn('malformed', $key, $self, "No open node can get attribute '$key'",
1090 getInsertionContext($self));
1091 return; } }
1092  
1093 # find a node that can accept a label.
1094 # A bit more than just whether the element can have the attribute, but
1095 # whether it has an id (and ideally either a refnum or title)
1096 # Moreover, can commonly occur after an already-closed (probably empty) element like bibliography
1097 sub floatToLabel {
1098 my ($self) = @_;
1099 my $key = 'labels';
1100 my $start = $$self{node};
1101 if ($start && ($start->nodeType == XML_ELEMENT_NODE)) {
1102 if (my $last = $start->lastChild) {
1103 $start = $last; } }
1104 my @ancestors = grep { $_->nodeType == XML_ELEMENT_NODE } getInsertionCandidates($start);
1105 my @candidates = @ancestors;
1106 # Should we only accept a node that already has an id, or should we create an id?
1107 while (@candidates
1108 && !(canHaveAttribute($self, $candidates[0], $key)
1109 && $candidates[0]->hasAttribute('xml:id'))) {
1110 shift(@candidates); }
1111 my $node = shift(@candidates);
1112 if (!$node) { # No appropriate ancestor?
1113 my $sib = $ancestors[0] && $ancestors[0]->lastChild;
1114 if ($sib && canHaveAttribute($self, $sib, $key)
1115 && $sib->hasAttribute('xml:id')) {
1116 $node = $sib; }
1117 elsif (@ancestors) { # just take root element?
1118 $node = $ancestors[-1]; } }
1119 if ($node) {
1120 my $savenode = $$self{node};
1121 setNode($self, $node);
1122 return $savenode; }
1123 else {
1124 Warn('malformed', $key, $self, "No open node with an xml:id can get attribute '$key'",
1125 getInsertionContext($self));
1126 return; } }
1127  
1128 sub openText_internal {
1129 my ($self, $text) = @_;
1130 return $$self{node} unless defined $text;
1131 my ($qname, $p, $pp);
1132 if ($$self{node}->nodeType == XML_TEXT_NODE) { # current node already is a text node.
1133 Debug("Appending text \"$text\" to " . Stringify($$self{node})) if $LaTeXML::DEBUG{document};
1134 my $parent = $$self{node}->parentNode;
1135 if ($LaTeXML::BOX && $parent->getAttribute('_autoopened')) {
1136 appendTextBox($self, $parent, $LaTeXML::BOX); }
1137 $$self{node}->appendData($text); }
1138 elsif (($p = $$self{node}->lastChild) && ($p->nodeType == XML_COMMENT_NODE)
1139 && ($pp = $p->previousSibling) && ($pp->nodeType == XML_TEXT_NODE)) {
1140 # Avoid spliting text runs: Swap <text><comment> to <comment><text> and THEN append $text
1141 $$self{node}->insertAfter($pp, $p);
1142 $$self{node} = $pp;
1143 openText_internal($self, $text); }
1144 elsif (($text =~ /\S/) # If non space
1145 || canContain($self, $$self{node}, '#PCDATA')) { # or text allowed here
1146 my $point = find_insertion_point($self, '#PCDATA');
1147 my $node = $$self{document}->createTextNode($text);
1148 if ($point->getAttribute('_autoopened')) {
1149 appendTextBox($self, $point, $LaTeXML::BOX); }
1150 Debug("Inserting text node for \"$text\" into " . Stringify($point))
1151 if $LaTeXML::DEBUG{document};
1152 $point->appendChild($node);
1153 setNode($self, $node); }
1154 return $$self{node}; } # return the text node (current)
1155  
1156 # Since xml text nodes don't have attributes to record the origining box,
1157 # we need to manage the accumulation of autoOpen'ed boxes
1158 # Indeed, propogate it to ancestors if they were autoOpened for same cause (box)
1159 sub appendTextBox {
1160 my ($self, $node, $box) = @_;
1161 my $origbox = getNodeBox($self, $node);
1162 if ($origbox && ($box ne $origbox)) { # if not already the same box
1163 my $newbox = List($origbox, $box);
1164 setNodeBox($self, $node, $newbox);
1165 my $p = $node;
1166 # AND, propogate change to autoOpen'd ancestors based on same initial box
1167 while (($p = $p->parentNode) && ($p->nodeType == XML_ELEMENT_NODE)
1168 && $p->getAttribute('_autoopened')
1169 && ((getNodeBox($self, $p) || '') eq $origbox)) {
1170 setNodeBox($self, $p, $newbox); } }
1171 return; }
1172  
1173 # Question: Why do I have math ligatures handled within openMathText_internal,
1174 # but text ligatures handled within closeText_internal ???
1175  
1176 sub openMathText_internal {
1177 my ($self, $string) = @_;
1178 # And if there's already text???
1179 my $node = $$self{node};
1180 my $font = getNodeFont($self, $node);
1181 $node->appendText($string);
1182 if (!$STATE->lookupValue('NOMATHPARSE')) {
1183 applyMathLigatures($self, $node); }
1184 return $node; }
1185  
1186 # New stategy (but inefficient): apply ligatures until one succeeds,
1187 # then remove it, and repeat until ALL (remaining) fail.
1188 sub applyMathLigatures {
1189 my ($self, $node) = @_;
1190 if (my $ligatures = $STATE->lookupValue('MATH_LIGATURES')) {
1191 my @ligatures = @$ligatures;
1192 while (@ligatures) {
1193 my $matched = 0;
1194 foreach my $ligature (@ligatures) {
1195 if (applyMathLigature($self, $node, $ligature)) {
1196 @ligatures = grep { $_ ne $ligature } @ligatures;
1197 $matched = 1;
1198 last; } }
1199 return unless $matched; } }
1200 return; }
1201  
1202 # Apply ligature operation to $node, presumed the last insertion into it's parent(?)
1203 # and presumably an ltx:XMTok
1204 sub applyMathLigature {
1205 my ($self, $node, $ligature) = @_;
1206 my ($nmatched, $newstring, %attr) = &{ $$ligature{matcher} }($self, $node);
1207 if ($nmatched) {
1208 my @boxes = (getNodeBox($self, $node));
1209 $node->firstChild->setData($newstring);
1210 my $prev = $node;
1211 for (my $i = 0 ; $i < $nmatched - 1 ; $i++) {
1212 my $remove = $prev->previousSibling;
1213 unshift(@boxes, getNodeBox($self, $remove));
1214 if ($remove->nodeType == XML_COMMENT_NODE) { $prev = $remove; } # keep comments
1215 else { removeNode($self, $remove); } }
1216 ## This fragment replaces the node's box by the composite boxes it replaces
1217 ## HOWEVER, this gets things out of sync because parent lists of boxes still
1218 ## have the old ones. Unless we could recursively replace all of them, we'd better skip it(??)
1219 if (scalar(@boxes) > 1) {
1220 setNodeBox($self, $node, List(@boxes, mode => 'math')); }
1221 foreach my $key (sort keys %attr) {
1222 my $value = $attr{$key};
1223 if (defined $value) {
1224 $node->setAttribute($key => $value); }
1225 else {
1226 $node->removeAttribute($key); } }
1227 return 1; }
1228 else {
1229 return; } }
1230  
1231 # Closing a text node is a good time to apply regexps (aka. Ligatures)
1232 sub closeText_internal {
1233 my ($self) = @_;
1234 my $node = $$self{node};
1235 if ($node->nodeType == XML_TEXT_NODE) { # Current node is text?
1236 my $parent = $node->parentNode;
1237 my $font = getNodeFont($self, $parent);
1238 my $string = $node->data;
1239 my $ostring = $string;
1240 my $fonttest;
1241 if (my $ligatures = $STATE->lookupValue('TEXT_LIGATURES')) {
1242 foreach my $ligature (@$ligatures) {
1243 next if ($fonttest = $$ligature{fontTest}) && !&$fonttest($font);
1244 $string = &{ $$ligature{code} }($string); } }
1245 $node->setData($string) unless $string eq $ostring;
1246 Debug("LIGATURE $ostring => $string") if $LaTeXML::DEBUG{document} && ($string ne $ostring);
1247 $$self{node} = $parent; # Effectively closed (->setNode, but don't recurse)
1248 return $parent; }
1249 else {
1250 return $node; } }
1251  
1252 # Close $node, and any current nodes below it.
1253 # No checking! Use this when you've already verified that $node can be closed.
1254 # and, of course, $node must be current or some ancestor of it!!!
1255 sub closeNode_internal {
1256 my ($self, $node) = @_;
1257 my $closeto = $node->parentNode; # Grab now in case afterClose screws the structure.
1258 my $n = closeText_internal($self); # Close any open text node.
1259 while ($n->nodeType == XML_ELEMENT_NODE) {
1260 closeElementAt($self, $n);
1261 autoCollapseChildren($self, $n);
1262 last if $node->isSameNode($n);
1263 $n = $n->parentNode; }
1264 Debug("closeNode(int) " . Stringify($$self{node})) if $LaTeXML::DEBUG{document};
1265 setNode($self, $closeto);
1266 # autoCollapseChildren($self, $node);
1267 return $$self{node}; }
1268  
1269 # If these attributes are present on both of two nodes,
1270 # it should inhibit merging those two nodes (typically a child into parent).
1271 our %non_mergeable_attributes = map { $_ => 1; }
1272 qw(about aboutlabelref aboutidref
1273 resource resourcelabelref resourceidref
1274 property rel rev tyupeof datatype content
1275 data datamimetype dataencoding
1276 framed);
1277  
1278 # Avoid redundant nesting of font switching elements:
1279 # If we're closing a node that can take font switches and it contains
1280 # a single FONT_ELEMENT_NAME node; pull it up.
1281 sub autoCollapseChildren {
1282 my ($self, $node) = @_;
1283 my $model = $$self{model};
1284 my $qname = $model->getNodeQName($node);
1285 my @c;
1286 if (($qname ne 'ltx:_Capture_')
1287 && (scalar(@c = $node->childNodes) == 1) # with single child
1288 && ($model->getNodeQName($c[0]) eq $FONT_ELEMENT_NAME)
1289 # AND, $node can have all the attributes that the child has (but at least 'font')
1290 && !(grep { !$model->canHaveAttribute($qname, $_) }
1291 ('font', grep { /^[^_]/ } map { $_->nodeName } $c[0]->attributes))
1292 # AND, $node doesn't have any attributes which collide!
1293 && !(grep { $non_mergeable_attributes{ $_->nodeName }; } $c[0]->attributes)
1294 # BUT, it isn't being forced somehow
1295 && !$c[0]->hasAttribute('_force_font')) {
1296 my $c = $c[0];
1297 setNodeFont($self, $node, getNodeFont($self, $c));
1298 removeNode($self, $c);
1299 foreach my $gc ($c->childNodes) {
1300 $node->appendChild($gc);
1301 recordNodeIDs($self, $node); }
1302 # Merge the attributes from the child onto $node
1303 mergeAttributes($self, $c, $node); }
1304 return; }
1305  
1306 # When merging attributes of two nodes, some attributes should be combined
1307 our %merge_attribute_spacejoin = map { $_ => 1; } # Merged space separated
1308 qw(class lists inlist labels);
1309 our %merge_attribute_semicolonjoin = map { $_ => 1; } # Merged ";" separated
1310 qw(cssstyle);
1311 our %merge_attribute_sumlength = map { $_ => 1; } # Summed lengths
1312 qw(xoffset yoffset lpadding rpadding xtranslate ytranslate);
1313 # Merge the attributes from node $from into those of the node $to.
1314 # The presumption is that node $from will be removed afterwards.
1315 # If an attribute is already present on $to, it will be ignored, unless named in $override.
1316 sub mergeAttributes {
1317 my ($self, $from, $to, $override) = @_;
1318 # Merge the attributes from the node $from onto the node $to
1319 foreach my $attr ($from->attributes()) {
1320 if ($attr->nodeType == XML_ATTRIBUTE_NODE) {
1321 my $key = $attr->nodeName;
1322 my $val = $attr->getValue;
1323 # Special case attributes
1324 if ($key eq 'xml:id') { # Use the replacement id
1325 if (!$to->hasAttribute($key) || ($override && $$override{$key})) {
1326 # BUT: If $to DID have an attribute, we really should patch any idrefs!!!!!!!
1327 unRecordID($self, $val); # presuming that $from will be going away.
1328 $val = recordID($self, $val, $to);
1329 $to->setAttribute($key, $val); } }
1330 elsif ($merge_attribute_spacejoin{$key}) { # combine space separated values
1331 addSSValues($self, $to, $key, $val); }
1332 elsif ($merge_attribute_semicolonjoin{$key}) { # combine space separated values
1333 my $oldval = $to->getAttribute($key);
1334 if ($oldval) { # if duplicate?
1335 $to->setAttribute($key, $oldval . '; ' . $val); }
1336 else {
1337 $to->setAttribute($key, $val); } }
1338 # Several length attributes should be cummulative; sum them up, if present on both.
1339 elsif ($merge_attribute_sumlength{$key}) {
1340 if (my $val2 = $to->getAttribute($key)) {
1341 my $v1 = $val =~ /^([\+\-\d\.]*)pt$/ && $1;
1342 my $v2 = $val2 =~ /^([\+\-\d\.]*)pt$/ && $1;
1343 $to->setAttribute($key => ($v1 + $v2) . 'pt'); }
1344 else {
1345 $to->setAttribute($key => $val); } }
1346 # Else if attribute not present on $to, or if we specificallly override it, just copy
1347 elsif ((!$to->hasAttribute($key)) || ($override && $$override{$key})) {
1348 if (my $ns = $attr->namespaceURI) {
1349 $to->setAttributeNS($ns, $attr->name, $val); }
1350 else {
1351 $to->setAttribute($attr->localname, $val); } } } }
1352 return; }
1353  
1354 #======================================================================
1355 # Make an ltx:ERROR node.
1356 sub makeError {
1357 my ($self, $type, $content) = @_;
1358 my $savenode = undef;
1359 $savenode = floatToElement($self, 'ltx:ERROR')
1360 unless isOpenable($self, 'ltx:ERROR');
1361 openElement($self, 'ltx:ERROR', class => ToString($type));
1362 openText_internal($self, ToString($content));
1363 closeElement($self, 'ltx:ERROR');
1364 setNode($self, $savenode) if $savenode;
1365 return; }
1366  
1367 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1368 # Document surgery (?)
1369 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1370 # The following carry out DOM modification but NOT relative to any current
1371 # insertion point (eg $$self{node}), but rather relative to nodes specified
1372 # in the arguments.
1373  
1374 # Set any allowed attribute on a node, decoding the prefix, if any.
1375 # Also records, and checks, any id attributes.
1376 # [xml:id and namespaced attributes are always allowed]
1377 sub setAttribute {
1378 my ($self, $node, $key, $value) = @_;
1379 return if (ref $value) && ((!blessed($value)) || !$value->can('toAttribute'));
1380 $value = $value->toAttribute if ref $value;
1381 if ((defined $value) && ($value ne '')) { # Skip if `empty'; but 0 is OK!
1382 if ($key eq 'xml:id') { # If it's an ID attribute
1383 $value = recordID($self, $value, $node); # Do id book keeping
1384 $node->setAttributeNS($LaTeXML::Common::XML::XML_NS, 'id', $value); } # and bypass all ns stuff
1385 elsif ($key !~ /:/) { # No colon; no namespace (the common case!)
1386 # Ignore attributes not allowed by the model,
1387 # but accept "internal" attributes.
1388 my $model = $$self{model};
1389 my $qname = $model->getNodeQName($node);
1390 if ($model->canHaveAttribute($qname, $key) || $key =~ /^_/) {
1391 $node->setAttribute($key => $value); } }
1392 else { # Accept any namespaced attributes
1393 my ($ns, $name) = $$self{model}->decodeQName($key);
1394 if ($ns) { # If namespaced attribute (must have prefix!
1395 my $prefix = $node->lookupNamespacePrefix($ns); # namespace already declared?
1396 if (!$prefix) { # if namespace not already declared
1397 $prefix = $$self{model}->getDocumentNamespacePrefix($ns, 1); # get the prefix to use
1398 getDocument($self)->documentElement->setNamespace($ns, $prefix, 0); } # and declare it
1399 if ($prefix eq '#default') { # Probably shouldn't happen...?
1400 $node->setAttribute($name => $value); }
1401 else {
1402 $node->setAttributeNS($ns, "$prefix:$name" => $value); } }
1403 else {
1404 $node->setAttribute($name => $value); } } } # redundant case...
1405 return; }
1406  
1407 sub addSSValues {
1408 my ($self, $node, $key, $values) = @_;
1409 $values = $values->toAttribute if ref $values;
1410 if ((defined $values) && ($values ne '')) { # Skip if `empty'; but 0 is OK!
1411 my @values = split(/\s+/, $values);
1412 if (my $oldvalues = $node->getAttribute($key)) { # previous values?
1413 my @old = split(/\s+/, $oldvalues);
1414 foreach my $new (@values) {
1415 push(@old, $new) unless grep { $_ eq $new } @old; }
1416 setAttribute($self, $node, $key => join(' ', sort @old)); }
1417 else {
1418 setAttribute($self, $node, $key => join(' ', sort @values)); } }
1419 return; }
1420  
1421 sub addClass {
1422 my ($self, $node, $value) = @_;
1423 return addSSValues($self, $node, class => $value); }
1424  
1425 sub removeSSValues {
1426 my ($self, $node, $key, $values) = @_;
1427 $values = $values->toAttribute if ref $values;
1428 my @to_remove = split(/\s+/, ($values || ''));
1429 return unless @to_remove;
1430 if (my $current_values = $node->getAttribute($key)) {
1431 my @current_values = split(/\s+/, $current_values);
1432 my @updated = ();
1433 foreach my $current_value (@current_values) {
1434 push(@updated, $current_value) unless grep { $_ eq $current_value } @to_remove; }
1435 if (@updated) {
1436 setAttribute($self, $node, $key => join(' ', sort @updated)); }
1437 else { # if no remaining values, delete the attribute
1438 $node->removeAttribute($key); } }
1439 return; }
1440  
1441 sub removeClass {
1442 my ($self, $node, $value) = @_;
1443 removeSSValues($self, $node, class => $value);
1444 return; }
1445  
1446 #**********************************************************************
1447 # Association of nodes and ids (xml:id)
1448  
1449 sub recordID {
1450 my ($self, $id, $node) = @_;
1451 if (my $prev = $$self{idstore}{$id}) { # Whoops! Already assigned!!!
1452 # Can we recover?
1453 if (!$node->isSameNode($prev)) {
1454 my $badid = $id;
1455 $id = modifyID($self, $id);
1456 Info('malformed', 'id', $node, "Duplicated attribute xml:id",
1457 "Using id='$id' on " . Stringify($node),
1458 "id='$badid' already set on " . Stringify($prev)); } }
1459 $$self{idstore}{$id} = $node;
1460 return $id; }
1461  
1462 sub unRecordID {
1463 my ($self, $id) = @_;
1464 delete $$self{idstore}{$id};
1465 return; }
1466  
1467 # These are used to record or unrecord, in bulk, all the ids within a node (tree).
1468 sub recordNodeIDs {
1469 my ($self, $node) = @_;
1470 foreach my $idnode (findnodes($self, 'descendant-or-self::*[@xml:id]', $node)) {
1471 if (my $id = $idnode->getAttribute('xml:id')) {
1472 my $newid = recordID($self, $id, $idnode);
1473 $idnode->setAttribute('xml:id' => $newid) if $newid ne $id; } }
1474 return; }
1475  
1476 sub unRecordNodeIDs {
1477 my ($self, $node) = @_;
1478 foreach my $idnode (findnodes($self, 'descendant-or-self::*[@xml:id]', $node)) {
1479 if (my $id = $idnode->getAttribute('xml:id')) {
1480 unRecordID($self, $id); } }
1481 return; }
1482  
1483 # Get a new, related, but unique id
1484 # Sneaky option: try $LaTeXML::Core::Document::ID_SUFFIX as a suffix for id, first.
1485 sub modifyID {
1486 my ($self, $id) = @_;
1487 if (my $prev = $$self{idstore}{$id}) { # Whoops! Already assigned!!!
1488 # Can we recover?
1489 my $badid = $id;
1490 if (!$LaTeXML::Core::Document::ID_SUFFIX
1491 || $$self{idstore}{ $id = $badid . $LaTeXML::Core::Document::ID_SUFFIX }) {
1492 foreach my $s1 (1 .. 26 * 26 * 26) { # Gotta give up, eventually; is 3 letters enough?
1493 return $id unless $$self{idstore}{ $id = $badid . radix_alpha($s1) }; }
1494 Error('malformed', 'id', $self, "Automatic incrementing of ID counters failed",
1495 "Last alternative for '$id' is '$badid'"); } }
1496 return $id; }
1497  
1498 sub lookupID {
1499 my ($self, $id) = @_;
1500 return $$self{idstore}{$id}; }
1501  
1502 #======================================================================
1503 # Odd bit:
1504 # In an XMDual, in each branch (content, presentation) there will be atoms
1505 # that correspond to the input (one will be real, the other an XMRef to the first).
1506 # But also there will be additional "decoration" (delimiters, punctuation, etc on the presentation
1507 # side; other symbols, bindings, whatever, on the content side).
1508 # These decorations should NOT be subject to rewrite rules,
1509 # and in cross-linked parallel markup, they should be attributed to the
1510 # upper containing object's ID, rather than left dangling.
1511 #
1512 # To determine this, we mark all math nodes as to whether they are "visible" from
1513 # presentation, content or both (the default top-level being both).
1514 # Decorations are the nodes that are visible to only one mode.
1515 # Note that nodes that are not visible at all CAN occur (& do currently when the parser
1516 # creates XMDuals), pruneXMDuals (below) gets rid of them.
1517  
1518 # NOTE: This should ultimately be in a base Document class,
1519 # since it is also needed before conversion to parallel markup!
1520 sub markXMNodeVisibility {
1521 my ($self) = @_;
1522 my @xmath = findnodes($self, '//ltx:XMath/*');
1523 foreach my $math (@xmath) {
1524 foreach my $node (findnodes($self, 'descendant-or-self::*[@_pvis or @_cvis]', $math)) {
1525 $node->removeAttribute('_pvis');
1526 $node->removeAttribute('_cvis'); } }
1527 foreach my $math (@xmath) {
1528 markXMNodeVisibility_aux($self, $math, 1, 1); }
1529 return; }
1530  
1531 sub markXMNodeVisibility_aux {
1532 no warnings 'recursion';
1533 my ($self, $node, $cvis, $pvis) = @_;
1534 my $qname = getNodeQName($self, $node);
1535 return if (!$cvis || $node->getAttribute('_cvis')) && (!$pvis || $node->getAttribute('_pvis'));
1536 # Special case: for XMArg used to wrap "formal" arguments on the content side,
1537 # mark them as visible as presentation as well.
1538 $pvis = 1 if $cvis && ($qname eq 'ltx:XMArg');
1539 $node->setAttribute('_cvis' => 1) if $cvis;
1540 $node->setAttribute('_pvis' => 1) if $pvis;
1541 if ($qname eq 'ltx:XMDual') {
1542 my ($c, $p) = element_nodes($node);
1543 markXMNodeVisibility_aux($self, $c, 1, 0) if $cvis;
1544 markXMNodeVisibility_aux($self, $p, 0, 1) if $pvis; }
1545 elsif ($qname eq 'ltx:XMRef') {
1546 # markXMNodeVisibility_aux($self, realizeXMNode($self, $node),$cvis,$pvis); }
1547 my $id = $node->getAttribute('idref');
1548 if (!$id) {
1549 my $key = $node->getAttribute('_xmkey');
1550 Warn('expected', 'id', $self, "Missing idref on ltx:XMRef",
1551 ($key ? ("_xmkey is $key") : ()));
1552 return; }
1553 my $reffed = lookupID($self, $id);
1554 if (!$reffed) {
1555 Warn('expected', 'node', $self, "No node found with id=$id (referred to from ltx:XMRef)");
1556 return; }
1557 markXMNodeVisibility_aux($self, $reffed, $cvis, $pvis); }
1558 else {
1559 foreach my $child (element_nodes($node)) {
1560 markXMNodeVisibility_aux($self, $child, $cvis, $pvis); } }
1561 return; }
1562  
1563 # Reduce any ltx:XMDual's to just the visible branch, if the other is not visible
1564 # (according to markXMNodeVisibility)
1565 # If we could be 100% sure that the marking had stayed consistent (after various doc surgery)
1566 # we could avoid re-marking, but we'd better be sure before removing nodes!
1567 sub pruneXMDuals {
1568 my ($self) = @_;
1569 # RE-mark visibility!
1570 markXMNodeVisibility($self);
1571 # will reversing keep from problems removing nodes from trees that already have been removed?
1572 foreach my $dual (reverse findnodes($self, 'descendant-or-self::ltx:XMDual')) {
1573 my ($content, $presentation) = element_nodes($dual);
1574 if (!findnode($self, 'descendant-or-self::*[@_pvis or @_cvis]', $content)) { # content never seen
1575 collapseXMDual($self, $dual, $presentation); }
1576 elsif (!findnode($self, 'descendant-or-self::*[@_pvis or @_cvis]', $presentation)) { # pres.
1577 collapseXMDual($self, $dual, $content); }
1578 else { # compact aligned structures, where possible
1579 compactXMDual($self, $dual, $content, $presentation); } }
1580 return; }
1581  
1582 our $content_transfer_overrides = { map { ($_ => 1) } qw(decl_id meaning name omcd) };
1583 our $dual_transfer_overrides = { %$content_transfer_overrides,
1584 map { ($_ => 1) } qw(xml:id role) };
1585  
1586 sub compactXMDual {
1587 my ($self, $dual, $content, $presentation) = @_;
1588 my $c_name = getNodeQName($self, $content);
1589 my $p_name = getNodeQName($self, $presentation);
1590 # 1.Quick fix: merge two tokens
1591 if (($c_name eq 'ltx:XMTok') && ($p_name eq 'ltx:XMTok')) {
1592 mergeAttributes($self, $content, $presentation, $content_transfer_overrides);
1593 mergeAttributes($self, $dual, $presentation, $dual_transfer_overrides);
1594 replaceNode($self, $dual, $presentation);
1595 return; }
1596  
1597 # 2.For now, only main use case is compacting mirror XMApp nodes
1598 return if ($c_name ne 'ltx:XMApp') || ($p_name ne 'ltx:XMApp');
1599 my @content_args = element_nodes($content);
1600 my @pres_args = element_nodes($presentation);
1601 return if scalar(@content_args) != scalar(@pres_args);
1602  
1603 my @new_args = ();
1604 # walk the corresponding children, and double-check they are referenced in the same order
1605 while ((my $c_arg = shift(@content_args)) and (my $p_arg = shift(@pres_args))) {
1606 my $c_idref = $c_arg->getAttribute('idref');
1607 if ($c_idref && ($c_idref eq ($p_arg->getAttribute('xml:id') || ''))) {
1608 push @new_args, $p_arg;
1609 next; } # content-refs-pres, OK
1610 my $p_idref = $p_arg->getAttribute('idref');
1611 if ($p_idref && ($p_idref eq ($c_arg->getAttribute('xml:id') || ''))) {
1612 push @new_args, $c_arg;
1613 next; } # pres-refs-content, OK
1614  
1615 # we can handle content-side XMToks, to any XM* presentation subtree differing for now.
1616 if (getNodeQName($self, $c_arg) ne 'ltx:XMTok') {
1617 return; }
1618 else { # otherwise we can compact this case. but delay actual libxml changes until we are *sure* the entire tree is compactable
1619 push(@new_args, [$c_arg, $p_arg]); } }
1620  
1621 # If we made it here, this is a dual with two mirrored applications and a single XMTok difference, compact it.
1622 my $compact_apply = openElementAt($self, $dual->parentNode, 'ltx:XMApp');
1623 for my $n_arg (@new_args) {
1624 # one of the args has our dual node that needs compacting
1625 if (ref $n_arg eq 'ARRAY') {
1626 my ($c_arg, $p_arg) = @$n_arg;
1627 mergeAttributes($self, $c_arg, $p_arg, $content_transfer_overrides);
1628 $n_arg = $p_arg; }
1629 $n_arg->unbindNode;
1630 $compact_apply->appendChild($n_arg); }
1631 # if the dual has any attributes migrate them to the new XMApp
1632 mergeAttributes($self, $dual, $compact_apply, $dual_transfer_overrides);
1633 replaceNode($self, $dual, $compact_apply);
1634 closeElementAt($self, $compact_apply);
1635 return; }
1636  
1637 # Replace an XMDual with one of its branches
1638 sub collapseXMDual {
1639 my ($self, $dual, $branch) = @_;
1640 # The other branch is not visible, nor referenced,
1641 # but the dual may have an id and be referenced
1642 if (my $dualid = $dual->getAttribute('xml:id')) {
1643 unRecordID($self, $dualid); # We'll move or remove the ID from the dual
1644 if (my $branchid = $branch->getAttribute('xml:id')) { # branch has id too!
1645 foreach my $ref (findnodes($self, "//*[\@idref='$dualid']")) {
1646 $ref->setAttribute(idref => $branchid); } } # Change dualid refs to branchid
1647 else {
1648 $branch->setAttribute('xml:id' => $dualid); # Just use same ID on the branch
1649 recordID($self, $dualid => $branch); } }
1650 replaceTree($self, $branch, $dual);
1651 return; }
1652  
1653 #**********************************************************************
1654 # Record the Box that created this node.
1655 sub setNodeBox {
1656 my ($self, $node, $box) = @_;
1657 return unless $box;
1658 my $boxid = "$box";
1659 $$self{node_boxes}{$boxid} = $box;
1660 return $node->setAttribute(_box => $boxid); }
1661  
1662 sub getNodeBox {
1663 my ($self, $node) = @_;
1664 return unless $node;
1665 my $t = $node->nodeType;
1666 return if $t != XML_ELEMENT_NODE;
1667 if (my $boxid = $node->getAttribute('_box')) {
1668 return $$self{node_boxes}{$boxid}; } }
1669  
1670 sub setNodeFont {
1671 my ($self, $node, $font) = @_;
1672 return unless ref $font; # ?
1673 my $fontid = $font->toString;
1674 $$self{node_fonts}{$fontid} = $font;
1675 if ($node->nodeType == XML_ELEMENT_NODE) {
1676 $node->setAttribute(_font => $fontid); }
1677 # otherwise, probably just ignorable?
1678 return; }
1679  
1680 # Possibly a sign of a design flaw; Set the node's font & all children that HAD the same font.
1681 sub mergeNodeFontRec {
1682 my ($self, $node, $font) = @_;
1683 return unless ref $font; # ?
1684 my $oldfont = getNodeFont($self, $node);
1685 my %props = $oldfont->purestyleChanges($font);
1686 my @nodes = ($node);
1687 while (my $n = shift(@nodes)) {
1688 if ($n->nodeType == XML_ELEMENT_NODE) {
1689 setNodeFont($self, $n, getNodeFont($self, $n)->merge(%props));
1690 push(@nodes, $n->childNodes); } }
1691 return; }
1692  
1693 sub getNodeFont {
1694 my ($self, $node) = @_;
1695 my $t;
1696 while ($node && (($t = $node->nodeType) != XML_ELEMENT_NODE)) {
1697 $node = $node->parentNode; }
1698 return ($node && ($t == XML_ELEMENT_NODE) && $$self{node_fonts}{ $node->getAttribute('_font') })
1699 || LaTeXML::Common::Font->textDefault(); }
1700  
1701 sub getNodeLanguage {
1702 my ($self, $node) = @_;
1703 my ($font, $lang);
1704 while ($node && ($node->nodeType == XML_ELEMENT_NODE)
1705 && !(($lang = $node->getAttribute('xml:lang'))
1706 || (($font = $$self{node_fonts}{ $node->getAttribute('_font') })
1707 && ($lang = $font->getLanguage)))) {
1708 $node = $node->parentNode; }
1709 return $lang || 'en'; }
1710  
1711 sub decodeFont {
1712 my ($self, $fontid) = @_;
1713 return $$self{node_fonts}{$fontid} || LaTeXML::Common::Font->textDefault(); }
1714  
1715 # Remove a node from the document (from it's parent)
1716 sub removeNode {
1717 my ($self, $node) = @_;
1718 if ($node) {
1719 my $chopped = $$self{node}->isSameNode($node); # Note if we're removing insertion point
1720 if ($node->nodeType == XML_ELEMENT_NODE) { # If an element, do ID bookkeeping.
1721 if (my $id = $node->getAttribute('xml:id')) {
1722 unRecordID($self, $id); }
1723 $chopped ||= grep { removeNode_aux($self, $_) } $node->childNodes; }
1724 my $parent = $node->parentNode;
1725 if ($chopped) { # Don't remove insertion point!
1726 setNode($self, $parent); }
1727 $parent->removeChild($node);
1728 }
1729 return $node; }
1730  
1731 sub removeNode_aux {
1732 my ($self, $node) = @_;
1733 my $chopped = $$self{node}->isSameNode($node);
1734 if ($node->nodeType == XML_ELEMENT_NODE) { # If an element, do ID bookkeeping.
1735 if (my $id = $node->getAttribute('xml:id')) {
1736 unRecordID($self, $id); }
1737 $chopped ||= grep { removeNode_aux($self, $_) } $node->childNodes; }
1738 return $chopped; }
1739  
1740 #**********************************************************************
1741 # Inserting new nodes at random points into the document,
1742 # typically, later in the process or during some kind of rearrangement.
1743  
1744 # This is a somewhat strange situation; There are commands and environments
1745 # that do some interesting thing to their contents. This include things like
1746 # center, flushleft, or rotate, or ...
1747 # Naively one is tempted to create a containing block with appropriate type & attributes.
1748 # However, since these things can be allowed in so many places by LaTeX, that
1749 # one has a difficult time creating a sensible document model.
1750 # The purpose of transformingBlock is to set the contents (possibly creating a
1751 # consistent <p> around them, if called for), and returning the list of newly
1752 # created nodes. These nodes can then have appropriate attributes added as needed
1753 # for each specific case.
1754  
1755 # Since this situation can occur in both LaTeX and AmSTeX type documents,
1756 # we'll put it in the TeX pool so it can be reused.
1757  
1758 # Tricky bit for creating nodes late in the game,
1759 ######
1760 ### See createElementAt
1761 # This opens a new element at the _specified_ point, rather than the current insertion point.
1762 # This is useful during document rearrangement or augmentation that may be needed later
1763 # in the process.
1764 sub openElementAt {
1765 my ($self, $point, $qname, %attributes) = @_;
1766 my ($ns, $tag) = $$self{model}->decodeQName($qname);
1767 my $newnode;
1768 my $font = $attributes{_font} || $attributes{font};
1769 my $box = $attributes{_box};
1770 $box = $$self{node_boxes}{$box} if $box && !ref $box; # may already be the string key
1771 # If this will be the document root node, things are slightly more involved.
1772 if ($point->nodeType == XML_DOCUMENT_NODE) { # First node! (?)
1773 $$self{model}->addSchemaDeclaration($self, $tag);
1774 map { $$self{document}->appendChild($_) } @{ $$self{pending} }; # Add saved comments, PI's
1775 $newnode = $$self{document}->createElement($tag);
1776 recordConstructedNode($self, $newnode);
1777 $$self{document}->setDocumentElement($newnode);
1778 if ($ns) {
1779 # Here, we're creating the initial, document element, which will hold ALL of the namespace declarations.
1780 # If there is a default namespace (no prefix), that will also be declared, and applied here.
1781 # However, if there is ALSO a prefix associated with that namespace, we have to declare it FIRST
1782 # due to the (apparently) buggy way that XML::LibXML works with namespaces in setAttributeNS.
1783 my $prefix = $$self{model}->getDocumentNamespacePrefix($ns);
1784 my $attprefix = $$self{model}->getDocumentNamespacePrefix($ns, 1, 1);
1785 if (!$prefix && $attprefix) {
1786 $newnode->setNamespace($ns, $attprefix, 0); }
1787 $newnode->setNamespace($ns, $prefix, 1); } }
1788 else {
1789 $font = getNodeFont($self, $point) unless $font;
1790 $box = getNodeBox($self, $point) unless $box;
1791 $newnode = openElement_internal($self, $point, $ns, $tag); }
1792  
1793 foreach my $key (sort keys %attributes) {
1794 next if $key eq 'font'; # !!!
1795 next if $key eq 'locator'; # !!!
1796 setAttribute($self, $newnode, $key, $attributes{$key}); }
1797 setNodeFont($self, $newnode, $font) if $font;
1798 setNodeBox($self, $newnode, $box) if $box;
1799 appendElementBox($self, $newnode, $box) if $box;
1800 Debug("Inserting " . Stringify($newnode) . " into " . Stringify($point)) if $LaTeXML::DEBUG{document};
1801 # Run afterOpen operations
1802 afterOpen($self, $newnode);
1803 return $newnode; }
1804  
1805 # When appending nodes to an autoOpen'd node, we'll need to record the new boxes there, too.
1806 sub appendElementBox {
1807 my ($self, $node, $box) = @_;
1808 my ($p, $origbox);
1809 if (($p = $node->parentNode) && ($p->nodeType == XML_ELEMENT_NODE)
1810 && $p->getAttribute('_autoopened')
1811 && ($origbox = getNodeBox($self, $p)) && ($origbox ne $box)) {
1812 my $newbox = List($origbox, $box);
1813 setNodeBox($self, $p, $newbox);
1814 # AND, propogate to autoOpen'd ancestors due to same initial box (See appendTextBox)
1815 while (($p = $p->parentNode) && ($p->nodeType == XML_ELEMENT_NODE)
1816 && $p->getAttribute('_autoopened')
1817 && ((getNodeBox($self, $p) || '') eq $origbox)) {
1818 setNodeBox($self, $p, $newbox); } }
1819 return; }
1820  
1821 sub openElement_internal {
1822 my ($self, $point, $ns, $tag) = @_;
1823 my $newnode;
1824 if ($ns) {
1825 if (!defined $point->lookupNamespacePrefix($ns)) { # namespace not already declared?
1826 getDocument($self)->documentElement
1827 ->setNamespace($ns, $$self{model}->getDocumentNamespacePrefix($ns), 0); }
1828 $newnode = $point->addNewChild($ns, $tag); }
1829 else {
1830 $newnode = $point->appendChild($$self{document}->createElement($tag)); }
1831 recordConstructedNode($self, $newnode);
1832 return $newnode; }
1833  
1834 # Whenever a node has been created using openElementAt,
1835 # closeElementAt ought to be used to close it, when you're finished inserting into $node.
1836 # Basically, this just runs any afterClose operations.
1837 sub closeElementAt {
1838 my ($self, $node) = @_;
1839 return afterClose($self, $node); }
1840  
1841 sub afterOpen {
1842 my ($self, $node) = @_;
1843 # Set current point to this node, just in case the afterOpen's use it.
1844 my $savenode = $$self{node};
1845 setNode($self, $node);
1846 my $box = getNodeBox($self, $node);
1847 map { &$_($self, $node, $box) } getTagActionList($self, $node, 'afterOpen');
1848 setNode($self, $savenode);
1849 return $node; }
1850  
1851 sub afterClose {
1852 my ($self, $node) = @_;
1853 # Should we set point to this node? (or to last child, or something ??
1854 my $savenode = $$self{node};
1855 my $box = getNodeBox($self, $node);
1856 map { &$_($self, $node, $box) } getTagActionList($self, $node, 'afterClose');
1857 setNode($self, $savenode);
1858 return $node; }
1859  
1860 #**********************************************************************
1861 # Appending clones of nodes
1862  
1863 # Inserting clones of nodes into the document.
1864 # Nodes that exist in some other part of the document (or some other document)
1865 # will need to be cloned so that they can be part of the new document;
1866 # otherwise, they would be removed from thier previous document.
1867 # Also, we want to have a clean namespace node structure
1868 # (otherwise, libxml2 has a tendency to introduce annoying "default" namespace prefix declarations)
1869 # And, finally, we need to modify any id's present in the old nodes,
1870 # since otherwise they may be duplicated.
1871  
1872 # Should have variants here for prepend, insert before, insert after.... ???
1873 sub appendClone {
1874 my ($self, $node, @newchildren) = @_;
1875 # Expand any document fragments
1876 @newchildren = map { ($_->nodeType == XML_DOCUMENT_FRAG_NODE ? $_->childNodes : $_) } @newchildren;
1877 # Now find all xml:id's in the newchildren and record replacement id's for them
1878 local %LaTeXML::Core::Document::IDMAP = ();
1879 # Find all id's defined in the copy and change the id.
1880 foreach my $child (@newchildren) {
1881 foreach my $idnode (findnodes($self, './/@xml:id', $child)) {
1882 my $id = $idnode->getValue;
1883 $LaTeXML::Core::Document::IDMAP{$id} = modifyID($self, $id); } }
1884 # Now do the cloning (actually copying) and insertion.
1885 appendClone_aux($self, $node, @newchildren);
1886 return $node; }
1887  
1888 sub appendClone_aux {
1889 my ($self, $node, @newchildren) = @_;
1890 foreach my $child (@newchildren) {
1891 my $type = $child->nodeType;
1892 if ($type == XML_ELEMENT_NODE) {
1893 my $new = openElement_internal($self, $node, $child->namespaceURI, $child->localname);
1894 foreach my $attr ($child->attributes) {
1895 if ($attr->nodeType == XML_ATTRIBUTE_NODE) {
1896 my $key = $attr->nodeName;
1897 if ($key eq 'xml:id') { # Use the replacement id
1898 my $newid = $LaTeXML::Core::Document::IDMAP{ $attr->getValue };
1899 $newid = recordID($self, $newid, $new);
1900 $new->setAttribute($key, $newid); }
1901 elsif ($key eq 'idref') { # Refer to the replacement id if it was replaced
1902 my $id = $attr->getValue;
1903 $new->setAttribute($key, $LaTeXML::Core::Document::IDMAP{$id} || $id); }
1904 elsif (my $ns = $attr->namespaceURI) {
1905 $new->setAttributeNS($ns, $attr->name, $attr->getValue); }
1906 else {
1907 $new->setAttribute($attr->localname, $attr->getValue); } }
1908 }
1909 afterOpen($self, $new);
1910 appendClone_aux($self, $new, $child->childNodes);
1911 afterClose($self, $new); }
1912 elsif ($type == XML_TEXT_NODE) {
1913 $node->appendTextNode($child->textContent); } }
1914 return $node; }
1915  
1916 #**********************************************************************
1917 # Wrapping & Unwrapping nodes by another element.
1918  
1919 # Wrap @nodes with an element named $qname, making the new element replace the first $node,
1920 # and all @nodes becomes the child of the new node.
1921 # [this makes most sense if @nodes are a sequence of siblings]
1922 # Returns undef if $qname isn't allowed in the parent, or if @nodes aren't allowed in $qname,
1923 # otherwise, returns the newly created $qname.
1924 # This executes ->afterClose, only if one of the wrapped nodes is the current node.
1925 sub wrapNodes {
1926 my ($self, $qname, @nodes) = @_;
1927 return unless @nodes;
1928 my $leave_open = 0;
1929 # Check if any of @nodes, or any of it's children, are the current node, and thus still "open"
1930 foreach my $n (@nodes) {
1931 if (isOpen($self, $n)) {
1932 $leave_open = 1;
1933 last; } }
1934 my $model = $$self{model};
1935 my $parent = $nodes[0]->parentNode;
1936 my ($ns, $tag) = $model->decodeQName($qname);
1937 my $new = openElement_internal($self, $parent, $ns, $tag);
1938 afterOpen($self, $new);
1939 $parent->replaceChild($new, $nodes[0]);
1940  
1941 if (my $font = getNodeFont($self, $parent)) {
1942 setNodeFont($self, $new, $font); }
1943 if (my $box = getNodeBox($self, $parent)) {
1944 setNodeBox($self, $new, $box); }
1945 foreach my $node (@nodes) {
1946 $new->appendChild($node); }
1947 afterClose($self, $new) unless $leave_open;
1948 return $new; }
1949  
1950 # Check if $node, or any of it's children, are the current node, and thus still "open"
1951 # Maybe a better way, such as explicitly marking _open ?
1952 sub isOpen {
1953 my ($self, $node) = @_;
1954 my $current = $$self{node};
1955 if ($node->isSameNode($current)) {
1956 return 1; }
1957 else {
1958 foreach my $n ($node->childNodes) {
1959 return 1 if isOpen($self, $n); }
1960 return 0; } }
1961  
1962 # Unwrap the children of $node, by replacing $node by its children.
1963 sub unwrapNodes {
1964 my ($self, $node) = @_;
1965 return replaceNode($self, $node, $node->childNodes); }
1966  
1967 # Replace $node by @nodes (presumably descendants of some kind?)
1968 sub replaceNode {
1969 my ($self, $node, @nodes) = @_;
1970 my $parent = $node->parentNode;
1971 my $c0;
1972 while (my $c1 = shift(@nodes)) {
1973 if ($c0) { $parent->insertAfter($c1, $c0); }
1974 else { $parent->replaceChild($c1, $node); }
1975 $c0 = $c1; }
1976 removeNode($self, $node);
1977 return $node; }
1978  
1979 # initially since $node->setNodeName was broken in XML::LibXML 1.58
1980 # but this can provide for more options & correctness?
1981 sub renameNode {
1982 my ($self, $node, $newname, $reinsert) = @_;
1983 my $model = $$self{model};
1984 my ($ns, $tag) = $model->decodeQName($newname);
1985 my $parent = $node->parentNode;
1986 my $new = openElement_internal($self, $parent, $ns, $tag);
1987 my $id;
1988 # Move to the position AFTER $node
1989 $parent->insertAfter($new, $node);
1990 # Copy ALL attributes from $node to $newnode
1991 foreach my $attr ($node->attributes) {
1992 my $key = $attr->getName;
1993 my $value = $node->getAttribute($key);
1994 $id = $value if $key eq 'xml:id'; # Save to register after removal of old node.
1995 $new->setAttribute($key, $value) if $model->canHaveAttribute($newname, $key); }
1996 # AND move all content from $node to $newnode
1997 if (!$reinsert) {
1998 foreach my $child ($node->childNodes) {
1999 $new->appendChild($child); } }
2000 else {
2001 my $savenode = $$self{node};
2002 $$self{node} = $new;
2003 foreach my $child ($node->childNodes) {
2004 if ($child->nodeType == XML_TEXT_NODE) {
2005 openText_internal($self, $child->data);
2006 closeText_internal($self); }
2007 else {
2008 my $point = find_insertion_point($self, getNodeQName($self, $child));
2009 $point->appendChild($child); } }
2010 $$self{node} = $savenode; }
2011 ## THEN call afterOpen... ?
2012 # It would normally be called before children added,
2013 # but how can we know if we're duplicated auto-added stuff?
2014 afterOpen($self, $new);
2015 afterClose($self, $new);
2016 # Finally, remove the old node
2017 removeNode($self, $node);
2018 # and FINALLY, we can register the new node under the id.
2019 if ($id) {
2020 my $newid = recordID($self, $id, $new);
2021 $new->setAttribute('xml:id' => $newid) if $newid ne $id; }
2022 return $new; }
2023  
2024 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2025 # Finally, another set of surgery methods
2026 # These take an array representation of the XML Tree to append
2027 # [tagname,{attributes..}, children]
2028 # THESE SHOULD BE PART OF A COMMON BASE CLASS; DUPLICATED IN Post::Document
2029 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2030  
2031 sub replaceTree {
2032 my ($self, $new, $old) = @_;
2033 my $parent = $old->parentNode;
2034 my @following = (); # Collect the matching and following nodes
2035 while (my $sib = $parent->lastChild) {
2036 last if $sib->isSameNode($old);
2037 $parent->removeChild($sib); # We're putting these back, in a moment!
2038 unshift(@following, $sib); }
2039 removeNode($self, $old);
2040 appendTree($self, $parent, $new);
2041 my $inserted = $parent->lastChild;
2042 map { $parent->appendChild($_) } @following; # No need for clone
2043 return $inserted; }
2044  
2045 sub appendTree {
2046 no warnings 'recursion';
2047 my ($self, $node, @data) = @_;
2048 foreach my $child (@data) {
2049 if (ref $child eq 'ARRAY') {
2050 my ($tag, $attributes, @children) = @$child;
2051 if (!$tag && !$attributes) {
2052 appendTree($self, $node, @children); }
2053 else {
2054 my $new = openElementAt($self, $node, $tag, ($attributes ? %$attributes : ()));
2055 appendTree($self, $new, @children);
2056 closeElementAt($self, $new); } }
2057 elsif ((ref $child) =~ /^XML::LibXML::/) {
2058 my $type = $child->nodeType;
2059 if ($type == XML_ELEMENT_NODE) {
2060 my $tag = getNodeQName($self, $child);
2061 my %attributes = map { $_->nodeType == XML_ATTRIBUTE_NODE ? (getNodeQName($self, $_) => $_->getValue) : () }
2062 $child->attributes;
2063 # DANGER: REMOVE the xml:id attribute from $child!!!!
2064 # This protects against some versions of XML::LibXML that warn against duplicate id's
2065 # Hopefully, you shouldn't be using the node any more
2066 if (my $id = $attributes{'xml:id'}) {
2067 $child->removeAttribute('xml:id');
2068 unRecordID($self, $id); }
2069 my $new = openElementAt($self, $node, $tag, %attributes);
2070 appendTree($self, $new, $child->childNodes);
2071 closeElementAt($self, $new); }
2072 elsif ($type == XML_DOCUMENT_FRAG_NODE) {
2073 appendTree($self, $node, $child->childNodes); }
2074 elsif ($type == XML_TEXT_NODE) {
2075 $node->appendTextNode($child->textContent); }
2076 }
2077 elsif ((ref $child) && $child->isaBox) {
2078 my $savenode = getNode($self);
2079 setNode($self, $node);
2080 absorb($self, $child);
2081 setNode($self, $savenode); }
2082 elsif (ref $child) {
2083 Warn('malformed', $child, $node, "Dont know how to add '$child' to document; ignoring"); }
2084 elsif (defined $child) {
2085 $node->appendTextNode($child); } }
2086 return; }
2087  
2088 #**********************************************************************
2089 1;
2090  
2091 __END__