From 016dac664663f5f8286c7b3eb33c8a6ffc784b2a Mon Sep 17 00:00:00 2001 From: Alex Puiu Date: Wed, 23 Mar 2022 16:39:25 +0200 Subject: [PATCH] Don't lose original formatting on paragraphs if a value has been replaced/displaced. Fix #31 --- app/SearchDisplace/SearchAndDisplaceXML.php | 44 ++++++++------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/app/SearchDisplace/SearchAndDisplaceXML.php b/app/SearchDisplace/SearchAndDisplaceXML.php index bcaa9ce..02725fb 100644 --- a/app/SearchDisplace/SearchAndDisplaceXML.php +++ b/app/SearchDisplace/SearchAndDisplaceXML.php @@ -21,10 +21,10 @@ class SearchAndDisplaceXML { $this->applySD(); - $this->convertToOdt(); + $this->convertToOriginalFileType(); } - protected function convertToOdt() + protected function convertToOriginalFileType() { (new Process(['soffice', '--convert-to', $this->type, $this->file, '--outdir', storage_path('app/tmp/')]))->run(); } @@ -34,33 +34,23 @@ class SearchAndDisplaceXML $dom = new \DOMDocument(); $dom->load($this->file); - foreach($dom->getElementsByTagName('p') as $p) { - $is_image = false; - if($p->childNodes) { - foreach($p->childNodes as $child) { - if(isset($child->tagName) && $child->tagName == 'draw:frame') { - $is_image = true; - } - } - } - if(!$is_image) { - $search = new SearchAndDisplace( - stripslashes($p->textContent), - [ - 'searchers' => $this->searchers, - ], - false, - true - ); - - $changed = $search->execute(); - - if(!$changed) { - continue; - } + foreach($dom->getElementsByTagName('span') as $p) { + $search = new SearchAndDisplace( + stripslashes($p->textContent), + [ + 'searchers' => $this->searchers, + ], + false, + true + ); + + $changed = $search->execute(); - $p->textContent = $changed['content']; + if(!$changed) { + continue; } + + $p->textContent = $changed['content']; } $dom->save($this->file);