Skip to content

Commit

Permalink
Merge pull request #24 from caxy/feature/list-diff-fixes
Browse files Browse the repository at this point in the history
Misc. list diffing updates and fixes
  • Loading branch information
jschroed91 committed Jan 12, 2016
2 parents 24da9e8 + 7e7b3f2 commit 18bd755
Show file tree
Hide file tree
Showing 5 changed files with 498 additions and 1 deletion.
23 changes: 23 additions & 0 deletions lib/Caxy/HtmlDiff/AbstractDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ abstract class AbstractDiff
protected $specialCaseTags;
protected $specialCaseChars;
protected $groupDiffs;
protected $matchThreshold = 80;

public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCaseTags = null, $groupDiffs = null)
{
Expand All @@ -39,6 +40,28 @@ public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCas
$this->setSpecialCaseChars(static::$defaultSpecialCaseChars);
}

/**
* @return int
*/
public function getMatchThreshold()
{
return $this->matchThreshold;
}

/**
* @param int $matchThreshold
*
* @return AbstractDiff
*/
public function setMatchThreshold($matchThreshold)
{
$this->matchThreshold = $matchThreshold;

return $this;
}



public function setSpecialCaseChars(array $chars)
{
$this->specialCaseChars = $chars;
Expand Down
4 changes: 3 additions & 1 deletion lib/Caxy/HtmlDiff/HtmlDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ protected function diffElements($oldText, $newText, $stripWrappingTags = true)

protected function diffList($oldText, $newText)
{
$diff = new ListDiff($oldText, $newText, $this->encoding, $this->specialCaseTags, $this->groupDiffs);
$diff = new ListDiffNew($oldText, $newText, $this->encoding, $this->specialCaseTags, $this->groupDiffs);
$diff->setMatchThreshold($this->matchThreshold);

return $diff->build();
}

Expand Down
102 changes: 102 additions & 0 deletions lib/Caxy/HtmlDiff/ListDiff/DiffList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Caxy\HtmlDiff\ListDiff;

class DiffList
{
protected $listType;

protected $listItems = array();

protected $attributes = array();

protected $startTag;

protected $endTag;

public function __construct($listType, $startTag, $endTag, $listItems = array(), $attributes = array())
{
$this->listType = $listType;
$this->startTag = $startTag;
$this->endTag = $endTag;
$this->listItems = $listItems;
$this->attributes = $attributes;
}

/**
* @return mixed
*/
public function getListType()
{
return $this->listType;
}

/**
* @param mixed $listType
*
* @return DiffList
*/
public function setListType($listType)
{
$this->listType = $listType;

return $this;
}

/**
* @return mixed
*/
public function getStartTag()
{
return $this->startTag;
}

public function getStartTagWithDiffClass($class = 'diff-list')
{
return str_replace('>', ' class="'.$class.'">', $this->startTag);
}

/**
* @param mixed $startTag
*/
public function setStartTag($startTag)
{
$this->startTag = $startTag;
}

/**
* @return mixed
*/
public function getEndTag()
{
return $this->endTag;
}

/**
* @param mixed $endTag
*/
public function setEndTag($endTag)
{
$this->endTag = $endTag;
}

/**
* @return mixed
*/
public function getListItems()
{
return $this->listItems;
}

/**
* @param mixed $listItems
*
* @return DiffList
*/
public function setListItems($listItems)
{
$this->listItems = $listItems;

return $this;
}
}
124 changes: 124 additions & 0 deletions lib/Caxy/HtmlDiff/ListDiff/DiffListItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace Caxy\HtmlDiff\ListDiff;

class DiffListItem
{
protected $attributes = array();

protected $text;

protected $startTag;

protected $endTag;

public function __construct($text, $attributes = array(), $startTag, $endTag)
{
$this->text = $text;
$this->attributes = $attributes;
$this->startTag = $startTag;
$this->endTag = $endTag;
}

/**
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}

/**
* @param array $attributes
*
* @return DiffListItem
*/
public function setAttributes($attributes)
{
$this->attributes = $attributes;

return $this;
}

/**
* @return mixed
*/
public function getText()
{
return $this->text;
}

/**
* @param mixed $text
*
* @return DiffListItem
*/
public function setText($text)
{
$this->text = $text;

return $this;
}

/**
* @return mixed
*/
public function getStartTag()
{
return $this->startTag;
}

public function getStartTagWithDiffClass($class = 'normal')
{
return str_replace('>', ' class="'.$class.'">', $this->startTag);
}

/**
* @param mixed $startTag
*
* @return DiffListItem
*/
public function setStartTag($startTag)
{
$this->startTag = $startTag;

return $this;
}

/**
* @return mixed
*/
public function getEndTag()
{
return $this->endTag;
}

/**
* @param mixed $endTag
*
* @return DiffListItem
*/
public function setEndTag($endTag)
{
$this->endTag = $endTag;

return $this;
}

public function getHtml($class = 'normal', $wrapTag = null)
{
$startWrap = $wrapTag ? sprintf('<%s>', $wrapTag) : '';
$endWrap = $wrapTag ? sprintf('</%s>', $wrapTag) : '';
return sprintf('%s%s%s%s%s', $this->getStartTagWithDiffClass($class), $startWrap, $this->getInnerHtml(), $endWrap, $this->endTag);
}

public function getInnerHtml()
{
return implode('', $this->text);
}

public function __toString()
{
return $this->getHtml();
}
}
Loading

0 comments on commit 18bd755

Please sign in to comment.