Skip to content

Commit 231250a

Browse files
di-maroojschroed91
authored andcommitted
Fixed warnings "count(): Parameter must be an array... (#65)
* Fixed warnings "count(): Parameter must be an array or an object that implements Countable" in PHP7.1 * Added ability to preserve newlines in the diff (disabled by default)
1 parent d7540c9 commit 231250a

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

lib/Caxy/HtmlDiff/AbstractDiff.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ protected function convertHtmlToListOfWords($characterString)
474474
$mode = 'character';
475475
$current_word = '';
476476
$words = array();
477+
$keepNewLines = $this->getConfig()->isKeepNewLines();
477478
foreach ($characterString as $i => $character) {
478479
switch ($mode) {
479480
case 'character':
@@ -488,7 +489,7 @@ protected function convertHtmlToListOfWords($characterString)
488489
if ($current_word !== '') {
489490
$words[] = $current_word;
490491
}
491-
$current_word = preg_replace('/\s+/S', ' ', $character);
492+
$current_word = $keepNewLines ? $character : preg_replace('/\s+/S', ' ', $character);
492493
$mode = 'whitespace';
493494
} else {
494495
if (
@@ -526,7 +527,7 @@ protected function convertHtmlToListOfWords($characterString)
526527
$mode = 'tag';
527528
} elseif (preg_match("/\s/", $character)) {
528529
$current_word .= $character;
529-
$current_word = preg_replace('/\s+/S', ' ', $current_word);
530+
if (!$keepNewLines) $current_word = preg_replace('/\s+/S', ' ', $current_word);
530531
} else {
531532
if ($current_word != '') {
532533
$words[] = $current_word;

lib/Caxy/HtmlDiff/HtmlDiff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ protected function insertTag($tag, $cssClass, &$words)
567567
}
568568
}
569569
}
570-
if (count($words) == 0 && count($specialCaseTagInjection) == 0) {
570+
if (count($words) == 0 && strlen($specialCaseTagInjection) == 0) {
571571
break;
572572
}
573573
if ($specialCaseTagInjectionIsBefore) {

lib/Caxy/HtmlDiff/HtmlDiffConfig.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ class HtmlDiffConfig
2727
*/
2828
protected $insertSpaceInReplace = false;
2929

30+
/**
31+
* Whether to keep newlines in the diff
32+
* @var bool
33+
*/
34+
protected $keepNewLines = false;
35+
3036
/**
3137
* @var string
3238
*/
@@ -48,6 +54,7 @@ class HtmlDiffConfig
4854
'i' => '[[REPLACE_EM]]',
4955
'a' => '[[REPLACE_A]]',
5056
'img' => '[[REPLACE_IMG]]',
57+
'pre' => '[[REPLACE_PRE]]',
5158
);
5259

5360
/**
@@ -293,6 +300,22 @@ public function setInsertSpaceInReplace($insertSpaceInReplace)
293300
return $this;
294301
}
295302

303+
/**
304+
* @return bool
305+
*/
306+
public function isKeepNewLines()
307+
{
308+
return $this->keepNewLines;
309+
}
310+
311+
/**
312+
* @param bool $keepNewLines
313+
*/
314+
public function setKeepNewLines($keepNewLines)
315+
{
316+
$this->keepNewLines = $keepNewLines;
317+
}
318+
296319
/**
297320
* @return array
298321
*/

lib/Caxy/HtmlDiff/Match.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Caxy\HtmlDiff;
44

5-
class Match
5+
class Match implements \Countable
66
{
77
public $startInOld;
88
public $startInNew;
@@ -24,4 +24,8 @@ public function endInNew()
2424
{
2525
return $this->startInNew + $this->size;
2626
}
27+
28+
public function count() {
29+
return (int)$this->size;
30+
}
2731
}

0 commit comments

Comments
 (0)