|
|
@ -60,27 +60,8 @@ class SearchAndDisplaceXML |
|
|
|
$dom->load($filePath . "/document.xml"); |
|
|
|
|
|
|
|
// 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); |
|
|
|
foreach($dom->getElementsByTagName('body') as $body) { |
|
|
|
$this->processElement($body, $dom); |
|
|
|
} |
|
|
|
|
|
|
|
$dom->save($filePath . "/document_sdapplied.xml"); |
|
|
@ -126,7 +107,7 @@ class SearchAndDisplaceXML |
|
|
|
/** |
|
|
|
* Apply SD on document's paragraph |
|
|
|
* |
|
|
|
* @param DOMNode $element DOM element |
|
|
|
* @param DOMText $element DOM element |
|
|
|
* @param DOMDocument $dom The document |
|
|
|
* |
|
|
|
* @return array |
|
|
@ -154,40 +135,38 @@ class SearchAndDisplaceXML |
|
|
|
$content = $element->textContent; |
|
|
|
$indexes = $changed; |
|
|
|
} else { |
|
|
|
$content = $changed['content']; |
|
|
|
$element->textContent = $content = $changed['content']; |
|
|
|
$indexes = $changed['indexes']; |
|
|
|
} |
|
|
|
|
|
|
|
foreach($indexes as $searcher => $changes) { |
|
|
|
if(empty($changes)) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
foreach($changes as $change) { |
|
|
|
$firstContent = substr($content, 0, $change['start']); |
|
|
|
$changedContent = substr($content, $change['start'], $change['end'] - $change['start'] + 1); |
|
|
|
$lastContent = substr($content, $change['end'] + 1); |
|
|
|
|
|
|
|
|
|
|
|
// $firstNode = $dom->createElement("text:span", $firstContent);
|
|
|
|
$element->textContent = htmlspecialchars($firstContent); |
|
|
|
# The changed indexes are filed into arrays based on which searcher they belong.
|
|
|
|
# This doesn't concern us here. Merge all the changed indexes into a single array, so we can sort them
|
|
|
|
$indexes = array_merge( ...array_values($indexes)); # Unpack and merge the arrays into a single array
|
|
|
|
|
|
|
|
$changedNode = $dom->createElement("text:span", htmlspecialchars($changedContent)); |
|
|
|
$changedNode->setAttribute('text:style-name', 'mark'); |
|
|
|
# Sort all the indexes in descending order
|
|
|
|
usort($indexes, function($first, $second) { |
|
|
|
return $second['start'] - $first['start']; |
|
|
|
}); |
|
|
|
|
|
|
|
$lastNode = $dom->createElement("text:span", htmlspecialchars($lastContent)); |
|
|
|
|
|
|
|
// Add the changed and last nodes after the current (element) node
|
|
|
|
// $element->parentNode->insertBefore($firstNode, $element->nextSibling);
|
|
|
|
|
|
|
|
# element->parentNode->insertBefore(... $element->nextSibling) inserts a new node before the node AFTER this one
|
|
|
|
# So we need to add the `last` node first, and then the `changed` node BEFORE the last.
|
|
|
|
$element->parentNode->insertBefore($lastNode, $element->nextSibling); |
|
|
|
$element->parentNode->insertBefore($changedNode, $element->nextSibling); |
|
|
|
foreach($indexes as $index) { |
|
|
|
$content = $element->textContent; |
|
|
|
|
|
|
|
$replacementNodes[] = $changedNode; |
|
|
|
$replacementNodes[] = $lastNode; |
|
|
|
} |
|
|
|
# Split the element at the specified indexes
|
|
|
|
# (end needs to b e + 1, since end is where the changed text finishes),
|
|
|
|
# so we need to split the text one character after it ends
|
|
|
|
$lastNode = $element->splitText($index['end'] + 1); |
|
|
|
$changedNode = $element->splitText($index['start']); |
|
|
|
|
|
|
|
# DOMText::splitText creates DOMText nodes.
|
|
|
|
# The changed node needs to be a DOMElement so we can add the mark style
|
|
|
|
# Create a element with the same content, then replace the node.
|
|
|
|
$changedNodeElement = $dom->createElement("text:span", $changedNode->textContent); |
|
|
|
$changedNodeElement->setAttribute('text:style-name', 'mark'); |
|
|
|
$changedNode->parentNode->replaceChild($changedNodeElement, $changedNode); |
|
|
|
$changedNode = $changedNodeElement; |
|
|
|
|
|
|
|
$replacementNodes[] = $changedNode; |
|
|
|
$replacementNodes[] = $lastNode; |
|
|
|
} |
|
|
|
|
|
|
|
if(!$this->markedStyleCreated) { |
|
|
|