Skip to content

Commit

Permalink
Add spaceMatching setting that matches all spaces (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
berarma authored Nov 5, 2023
1 parent 5e19401 commit de416bc
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/Caxy/HtmlDiff/HtmlDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ protected function checkCondition($word, $condition)

protected function wrapText(string $text, string $tagName, string $cssClass) : string
{
if (trim($text) === '') {
if (!$this->config->isSpaceMatching() && trim($text) === '') {
return '';
}

Expand Down
21 changes: 21 additions & 0 deletions lib/Caxy/HtmlDiff/HtmlDiffConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ class HtmlDiffConfig
*/
protected $purifierCacheLocation = null;

/**
* @var bool
*/
protected $spaceMatching = false;

/**
* @return HtmlDiffConfig
*/
Expand Down Expand Up @@ -441,6 +446,22 @@ public function getPurifierCacheLocation()
return $this->purifierCacheLocation;
}

/**
* @return bool
*/
public function isSpaceMatching()
{
return $this->spaceMatching;
}

/**
* @param bool $keepNewLines
*/
public function setSpaceMatching($spaceMatching)
{
$this->spaceMatching = $spaceMatching;
}

/**
* @param string $tag
*
Expand Down
40 changes: 20 additions & 20 deletions tests/fixtures/HtmlDiff/simple-list.html
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
<options></options>

<oldText>
<ol><li>List item one</li>
<li>List item two with subitems:
<ul><li>Subitem 1</li>
<li>Subitem 2</li>
<li>
<p>
Hi there<br />
<ul><li>B</li>
<li>A</li>
<li>B</li>
</ul>
</p>
</li>
<li>
<dl><li>Deleted list</li></dl>
</li>
</ul>
</li>
<li>Final list item</li>
</ol>
<ol><li>List item one</li>
<li>List item two with subitems:
<ul><li>Subitem 1</li>
<li>Subitem 2</li>
<li>
<p>
Hi there<br />
<ul><li>B</li>
<li>A</li>
<li>B</li>
</ul>
</p>
</li>
<li>
<dl><li>Deleted list</li></dl>
</li>
</ul>
</li>
<li>Final list item</li>
</ol>
</oldText>

<newText>
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/HtmlDiff/spaces-added.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<options>
<option type="boolean" name="setSpaceMatching" value="true" />
</options>
<oldtext>He said:"OK!"</oldtext>
<newtext>He said: "OK!"</newtext>
<expected>He said:<ins class="diffins"> </ins>"OK!"</expected>
6 changes: 6 additions & 0 deletions tests/fixtures/HtmlDiff/spaces-removed-inside-tag.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<options>
<option type="boolean" name="setSpaceMatching" value="true" />
</options>
<oldtext><strong>this </strong>is</oldtext>
<newtext><strong>this</strong>is</newtext>
<expected><strong>this<del class="diffdel"> </del></strong>is</expected>

0 comments on commit de416bc

Please sign in to comment.