diff --git a/lib/Caxy/HtmlDiff/AbstractDiff.php b/lib/Caxy/HtmlDiff/AbstractDiff.php index c234644..3506c74 100644 --- a/lib/Caxy/HtmlDiff/AbstractDiff.php +++ b/lib/Caxy/HtmlDiff/AbstractDiff.php @@ -474,6 +474,7 @@ protected function convertHtmlToListOfWords($characterString) $mode = 'character'; $current_word = ''; $words = array(); + $keepNewLines = $this->getConfig()->isKeepNewLines(); foreach ($characterString as $i => $character) { switch ($mode) { case 'character': @@ -488,7 +489,7 @@ protected function convertHtmlToListOfWords($characterString) if ($current_word !== '') { $words[] = $current_word; } - $current_word = preg_replace('/\s+/S', ' ', $character); + $current_word = $keepNewLines ? $character : preg_replace('/\s+/S', ' ', $character); $mode = 'whitespace'; } else { if ( @@ -526,7 +527,7 @@ protected function convertHtmlToListOfWords($characterString) $mode = 'tag'; } elseif (preg_match("/\s/", $character)) { $current_word .= $character; - $current_word = preg_replace('/\s+/S', ' ', $current_word); + if (!$keepNewLines) $current_word = preg_replace('/\s+/S', ' ', $current_word); } else { if ($current_word != '') { $words[] = $current_word; diff --git a/lib/Caxy/HtmlDiff/HtmlDiff.php b/lib/Caxy/HtmlDiff/HtmlDiff.php index 067ea98..28d1e63 100644 --- a/lib/Caxy/HtmlDiff/HtmlDiff.php +++ b/lib/Caxy/HtmlDiff/HtmlDiff.php @@ -567,7 +567,7 @@ protected function insertTag($tag, $cssClass, &$words) } } } - if (count($words) == 0 && count($specialCaseTagInjection) == 0) { + if (count($words) == 0 && strlen($specialCaseTagInjection) == 0) { break; } if ($specialCaseTagInjectionIsBefore) { diff --git a/lib/Caxy/HtmlDiff/HtmlDiffConfig.php b/lib/Caxy/HtmlDiff/HtmlDiffConfig.php index 0d0cbd1..fc98425 100644 --- a/lib/Caxy/HtmlDiff/HtmlDiffConfig.php +++ b/lib/Caxy/HtmlDiff/HtmlDiffConfig.php @@ -27,6 +27,12 @@ class HtmlDiffConfig */ protected $insertSpaceInReplace = false; + /** + * Whether to keep newlines in the diff + * @var bool + */ + protected $keepNewLines = false; + /** * @var string */ @@ -48,6 +54,7 @@ class HtmlDiffConfig 'i' => '[[REPLACE_EM]]', 'a' => '[[REPLACE_A]]', 'img' => '[[REPLACE_IMG]]', + 'pre' => '[[REPLACE_PRE]]', ); /** @@ -293,6 +300,22 @@ public function setInsertSpaceInReplace($insertSpaceInReplace) return $this; } + /** + * @return bool + */ + public function isKeepNewLines() + { + return $this->keepNewLines; + } + + /** + * @param bool $keepNewLines + */ + public function setKeepNewLines($keepNewLines) + { + $this->keepNewLines = $keepNewLines; + } + /** * @return array */ diff --git a/lib/Caxy/HtmlDiff/Match.php b/lib/Caxy/HtmlDiff/Match.php index c76cfba..621e5dc 100644 --- a/lib/Caxy/HtmlDiff/Match.php +++ b/lib/Caxy/HtmlDiff/Match.php @@ -2,7 +2,7 @@ namespace Caxy\HtmlDiff; -class Match +class Match implements \Countable { public $startInOld; public $startInNew; @@ -24,4 +24,8 @@ public function endInNew() { return $this->startInNew + $this->size; } + + public function count() { + return (int)$this->size; + } }