Skip to content

Commit 150bfb6

Browse files
committed
Fixed bug in code where the list was being formatted incorrectly and caused a null reference to be passed.
1 parent 39a4320 commit 150bfb6

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

lib/Caxy/HtmlDiff/ListDiff.php

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,42 @@ protected function diffListContent()
106106
*/
107107
protected function formatThisListContent()
108108
{
109-
foreach ($this->oldIsolatedDiffTags as $key => $diffTagArray) {
110-
$openingTag = $this->getAndStripTag($diffTagArray[0]);
111-
$closingTag = $this->getAndStripTag($diffTagArray[count($diffTagArray) - 1]);
112-
113-
if (array_key_exists($openingTag, $this->isolatedDiffTags) &&
114-
array_key_exists($closingTag, $this->isolatedDiffTags)
115-
) {
116-
$this->listType = $openingTag;
117-
array_shift($this->oldIsolatedDiffTags[$key]);
118-
array_pop($this->oldIsolatedDiffTags[$key]);
119-
array_shift($this->newIsolatedDiffTags[$key]);
120-
array_pop($this->newIsolatedDiffTags[$key]);
121-
$this->list['old'] = $this->oldIsolatedDiffTags[$key];
122-
$this->list['new'] = $this->newIsolatedDiffTags[$key];
109+
$formatArray = array(
110+
array('type' => 'old', 'array' => $this->oldIsolatedDiffTags),
111+
array('type' => 'new', 'array' => $this->newIsolatedDiffTags)
112+
);
113+
114+
foreach ($formatArray as $item) {
115+
$values = array_values($item['array']);
116+
$this->list[$item['type']] = count($values)
117+
? $this->formatList($values[0], $item['type'])
118+
: array();
119+
}
120+
}
121+
122+
/**
123+
*
124+
* @param array $arrayData
125+
* @param string $index
126+
* @return array
127+
*/
128+
protected function formatList(array $arrayData, $index = 'old')
129+
{
130+
$openingTag = $this->getAndStripTag($arrayData[0]);
131+
$closingTag = $this->getAndStripTag($arrayData[count($arrayData) - 1]);
132+
133+
if (array_key_exists($openingTag, $this->isolatedDiffTags) &&
134+
array_key_exists($closingTag, $this->isolatedDiffTags)
135+
) {
136+
if ($index == 'old') {
137+
$this->listType = $this->getAndStripTag($arrayData[0]);
123138
}
139+
140+
array_shift($arrayData);
141+
array_pop($arrayData);
124142
}
143+
144+
return $arrayData;
125145
}
126146

127147
/**
@@ -350,27 +370,27 @@ protected function convertListContentArrayToString($listContentArray)
350370
* @return string
351371
*/
352372
protected function processPlaceholders($text, array $matches)
353-
{
373+
{
354374
// Prepare return
355375
$returnText = array();
356376
// Save the contents of all list nodes, new and old.
357377
$contentVault = array(
358378
'old' => $this->getListContent('old', $matches),
359379
'new' => $this->getListContent('new', $matches)
360380
);
361-
381+
362382
$count = 0;
363383
// Loop through the text checking for placeholders. If a nested list is found, create a new ListDiff object for it.
364384
foreach (explode(' ', $text) as $word) {
365385
$preContent = $this->checkWordForDiffTag($this->stripNewLine($word));
366-
386+
367387
if (in_array(
368388
is_array($preContent) ? $preContent[1] : $preContent,
369389
$this->isolatedDiffTags
370390
)
371391
) {
372-
$oldText = implode('', $contentVault['old'][$count]);
373-
$newText = implode('', $contentVault['new'][$count]);
392+
$oldText = array_key_exists($count, $contentVault['old']) ? implode('', $contentVault['old'][$count]) : '';
393+
$newText = array_key_exists($count, $contentVault['new']) ? implode('', $contentVault['new'][$count]) : '';
374394
$content = $this->diffList($oldText, $newText);
375395
$count++;
376396
} else {
@@ -427,22 +447,21 @@ protected function stripNewLine($text)
427447
* @return array
428448
*/
429449
protected function getListContent($indexKey = 'new', array $matches)
430-
{
450+
{
431451
$bucket = array();
432452

433453
if (isset($matches[$indexKey]) && $matches[$indexKey] !== null) {
434454
$start = $this->listsIndex[$indexKey][$matches[$indexKey]];
435-
$stop = array_key_exists(($matches[$indexKey] + 1), $this->listsIndex[$indexKey])
436-
? $this->listsIndex[$indexKey][$matches[$indexKey] + 1]
437-
: $this->findEndForIndex($this->list[$indexKey], $start);
438-
439-
for ($x = $start; $x < $stop; $x++) {
455+
$stop = $this->findEndForIndex($this->list[$indexKey], $start);
456+
457+
for ($x = $start; $x <= $stop; $x++) {
458+
440459
if (in_array($this->list[$indexKey][$x], $this->isolatedDiffTags)) {
441460
$bucket[] = $this->listIsolatedDiffTags[$indexKey][$x];
442461
}
443462
}
444463
}
445-
464+
446465
return $bucket;
447466
}
448467

0 commit comments

Comments
 (0)