diff --git a/app/SearchDisplace/SearchAndDisplaceXML.php b/app/SearchDisplace/SearchAndDisplaceXML.php index 353ac89..84165e7 100644 --- a/app/SearchDisplace/SearchAndDisplaceXML.php +++ b/app/SearchDisplace/SearchAndDisplaceXML.php @@ -59,26 +59,28 @@ class SearchAndDisplaceXML $filePath = $this->storage->path("contracts/$this->fileDirectory"); $dom->load($filePath . "/document.xml"); - foreach($dom->getElementsByTagName('p') as $p) { - if( - !$p instanceof DOMText && - count($p->childNodes) > 0 && - isset($p->parentNode->tagName) && - $p->parentNode->tagName !== "table:table-cell" - ) { - $replacements = []; - foreach($p->childNodes as $child) { - if (in_array($child, $replacements)) { - continue; - } - - if (!$child instanceof DOMText) { - continue; - } - - $replacements = array_merge($replacements, $this->replace($child, $dom)); - } - } + // foreach($dom->getElementsByTagName('p') as $p) { + foreach($dom->getElementsByTagName('body') as $p) { + // if( + // !$p instanceof DOMText && + // count($p->childNodes) > 0 && + // isset($p->parentNode->tagName) && + // $p->parentNode->tagName !== "table:table-cell" + // ) { + // $replacements = []; + // foreach($p->childNodes as $child) { + // if (in_array($child, $replacements)) { + // continue; + // } + + // if (!$child instanceof DOMText) { + // continue; + // } + + // $replacements = array_merge($replacements, $this->replace($child, $dom)); + // } + // } + $this->processElement($p, $dom); } $dom->save($filePath . "/document_sdapplied.xml"); @@ -86,6 +88,41 @@ class SearchAndDisplaceXML return $filePath . "/document_sdapplied.xml"; } + public function processElement(DOMNode &$element, DOMDocument &$dom) + { + # When processing (replacing) a DOMText node, we replace the initial node with 3 nodes. + # In order to avoid an infinite loop when processing the nodes inside a foreach statement, + # We will keep a list of all new nodes created(which replaced the old nodes), + # and skip those when processing the child nodes of the element + $replacements = []; + + # If the element is NOT a DOMText, and has at least one child node, + # iterate the child nodes and process them + if( + !$element instanceof DOMText && + count($element->childNodes) > 0 + ) { + foreach($element->childNodes as $child) { + + # If the child is in the list of newly created nodes (which replaced an earlier child node), skip it + if (in_array($child, $replacements)) { + continue; + } + + # If the child is NOT a DOMText node, recursively call this method to process it. + if (!$child instanceof DOMText) { + $this->processElement($child, $dom); + } else { + # Otherwise, replace the child (if it contains the proper text) + $replacements = array_merge($replacements, $this->replace($child, $dom)); + } + } + } + // else if ($element instanceof DOMText) { + // $this->replace($element, $dom); + // } + } + /** * Apply SD on document's paragraph * @@ -133,12 +170,12 @@ class SearchAndDisplaceXML // $firstNode = $dom->createElement("text:span", $firstContent); - $element->textContent = $firstContent; + $element->textContent = htmlspecialchars($firstContent); - $changedNode = $dom->createElement("text:span", $changedContent); + $changedNode = $dom->createElement("text:span", htmlspecialchars($changedContent)); $changedNode->setAttribute('text:style-name', 'mark'); - $lastNode = $dom->createElement("text:span", $lastContent); + $lastNode = $dom->createElement("text:span", htmlspecialchars($lastContent)); // Add the changed and last nodes after the current (element) node // $element->parentNode->insertBefore($firstNode, $element->nextSibling); @@ -155,8 +192,8 @@ class SearchAndDisplaceXML if(!$this->markedStyleCreated) { $this->createMarkedStyle($dom); + $this->markedStyleCreated = true; } - $this->markedStyleCreated = true; } return $replacementNodes;