Skip to content

Commit d5470c5

Browse files
committed
Updated ListDiff to account for strings around place holders.
1 parent 71071c5 commit d5470c5

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

lib/Caxy/HtmlDiff/ListDiff.php

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ protected function compareChildLists()
141141
if (!in_array($key, $taken) && $percent > $highestMatch) {
142142
// If an absolute match, choose this one.
143143
if ($percent == $absoluteMatch) {
144-
//$this->dump("Absolute found");
145144
$highestMatch = $percent;
146145
$highestMatchKey = $key;
147146
$takenItemKey = $item;
@@ -253,20 +252,54 @@ protected function processPlaceholders($text, array $matches)
253252
$count = 0;
254253
// Loop through the text checking for placeholders. If a nested list is found, create a new ListDiff object for it.
255254
foreach (explode(' ', $text) as $word) {
256-
$content = $word;
257-
if (in_array($word, $this->isolatedDiffTags)) {
255+
$preContent = $this->checkWordForDiffTag($this->stripNewLine($word));
256+
257+
if (in_array(
258+
is_array($preContent) ? $preContent[1] : $preContent,
259+
$this->isolatedDiffTags
260+
)
261+
) {
258262
$oldText = implode('', $contentVault['old'][$count]);
259263
$newText = implode('', $contentVault['new'][$count]);
260-
$content = $this->diffList($oldText, $newText, true);
264+
$content = $this->diffList($oldText, $newText);
261265
$count++;
266+
} else {
267+
$content = $preContent;
262268
}
263269

264-
$returnText[] = $content;
270+
$returnText[] = is_array($preContent) ? $preContent[0] . $content . $preContent[2] : $content;
265271
}
266272
// Return the result.
267273
return implode(' ', $returnText);
268274
}
269275

276+
protected function checkWordForDiffTag($word)
277+
{
278+
foreach ($this->isolatedDiffTags as $diffTag) {
279+
if (strpos($word, $diffTag) > -1) {
280+
$position = strpos($word, $diffTag);
281+
$length = strlen($diffTag);
282+
$result = array(
283+
substr($word, 0, $position),
284+
$diffTag,
285+
substr($word, ($position + $length))
286+
);
287+
288+
return $result;
289+
}
290+
}
291+
292+
return $word;
293+
}
294+
295+
/**
296+
* Used to remove new lines.
297+
*/
298+
protected function stripNewLine($text)
299+
{
300+
return trim(preg_replace('/\s\s+/', ' ', $text));
301+
}
302+
270303
/**
271304
* Grab the list content using the listsIndex array.
272305
*/

0 commit comments

Comments
 (0)