Skip to content

Commit 7e7b3f2

Browse files
committed
Misc. list diffing updates and fixes
1 parent 084747e commit 7e7b3f2

File tree

5 files changed

+498
-1
lines changed

5 files changed

+498
-1
lines changed

lib/Caxy/HtmlDiff/AbstractDiff.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ abstract class AbstractDiff
1919
protected $specialCaseTags;
2020
protected $specialCaseChars;
2121
protected $groupDiffs;
22+
protected $matchThreshold = 80;
2223

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

43+
/**
44+
* @return int
45+
*/
46+
public function getMatchThreshold()
47+
{
48+
return $this->matchThreshold;
49+
}
50+
51+
/**
52+
* @param int $matchThreshold
53+
*
54+
* @return AbstractDiff
55+
*/
56+
public function setMatchThreshold($matchThreshold)
57+
{
58+
$this->matchThreshold = $matchThreshold;
59+
60+
return $this;
61+
}
62+
63+
64+
4265
public function setSpecialCaseChars(array $chars)
4366
{
4467
$this->specialCaseChars = $chars;

lib/Caxy/HtmlDiff/HtmlDiff.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ protected function diffElements($oldText, $newText, $stripWrappingTags = true)
215215

216216
protected function diffList($oldText, $newText)
217217
{
218-
$diff = new ListDiff($oldText, $newText, $this->encoding, $this->specialCaseTags, $this->groupDiffs);
218+
$diff = new ListDiffNew($oldText, $newText, $this->encoding, $this->specialCaseTags, $this->groupDiffs);
219+
$diff->setMatchThreshold($this->matchThreshold);
220+
219221
return $diff->build();
220222
}
221223

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace Caxy\HtmlDiff\ListDiff;
4+
5+
class DiffList
6+
{
7+
protected $listType;
8+
9+
protected $listItems = array();
10+
11+
protected $attributes = array();
12+
13+
protected $startTag;
14+
15+
protected $endTag;
16+
17+
public function __construct($listType, $startTag, $endTag, $listItems = array(), $attributes = array())
18+
{
19+
$this->listType = $listType;
20+
$this->startTag = $startTag;
21+
$this->endTag = $endTag;
22+
$this->listItems = $listItems;
23+
$this->attributes = $attributes;
24+
}
25+
26+
/**
27+
* @return mixed
28+
*/
29+
public function getListType()
30+
{
31+
return $this->listType;
32+
}
33+
34+
/**
35+
* @param mixed $listType
36+
*
37+
* @return DiffList
38+
*/
39+
public function setListType($listType)
40+
{
41+
$this->listType = $listType;
42+
43+
return $this;
44+
}
45+
46+
/**
47+
* @return mixed
48+
*/
49+
public function getStartTag()
50+
{
51+
return $this->startTag;
52+
}
53+
54+
public function getStartTagWithDiffClass($class = 'diff-list')
55+
{
56+
return str_replace('>', ' class="'.$class.'">', $this->startTag);
57+
}
58+
59+
/**
60+
* @param mixed $startTag
61+
*/
62+
public function setStartTag($startTag)
63+
{
64+
$this->startTag = $startTag;
65+
}
66+
67+
/**
68+
* @return mixed
69+
*/
70+
public function getEndTag()
71+
{
72+
return $this->endTag;
73+
}
74+
75+
/**
76+
* @param mixed $endTag
77+
*/
78+
public function setEndTag($endTag)
79+
{
80+
$this->endTag = $endTag;
81+
}
82+
83+
/**
84+
* @return mixed
85+
*/
86+
public function getListItems()
87+
{
88+
return $this->listItems;
89+
}
90+
91+
/**
92+
* @param mixed $listItems
93+
*
94+
* @return DiffList
95+
*/
96+
public function setListItems($listItems)
97+
{
98+
$this->listItems = $listItems;
99+
100+
return $this;
101+
}
102+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
namespace Caxy\HtmlDiff\ListDiff;
4+
5+
class DiffListItem
6+
{
7+
protected $attributes = array();
8+
9+
protected $text;
10+
11+
protected $startTag;
12+
13+
protected $endTag;
14+
15+
public function __construct($text, $attributes = array(), $startTag, $endTag)
16+
{
17+
$this->text = $text;
18+
$this->attributes = $attributes;
19+
$this->startTag = $startTag;
20+
$this->endTag = $endTag;
21+
}
22+
23+
/**
24+
* @return array
25+
*/
26+
public function getAttributes()
27+
{
28+
return $this->attributes;
29+
}
30+
31+
/**
32+
* @param array $attributes
33+
*
34+
* @return DiffListItem
35+
*/
36+
public function setAttributes($attributes)
37+
{
38+
$this->attributes = $attributes;
39+
40+
return $this;
41+
}
42+
43+
/**
44+
* @return mixed
45+
*/
46+
public function getText()
47+
{
48+
return $this->text;
49+
}
50+
51+
/**
52+
* @param mixed $text
53+
*
54+
* @return DiffListItem
55+
*/
56+
public function setText($text)
57+
{
58+
$this->text = $text;
59+
60+
return $this;
61+
}
62+
63+
/**
64+
* @return mixed
65+
*/
66+
public function getStartTag()
67+
{
68+
return $this->startTag;
69+
}
70+
71+
public function getStartTagWithDiffClass($class = 'normal')
72+
{
73+
return str_replace('>', ' class="'.$class.'">', $this->startTag);
74+
}
75+
76+
/**
77+
* @param mixed $startTag
78+
*
79+
* @return DiffListItem
80+
*/
81+
public function setStartTag($startTag)
82+
{
83+
$this->startTag = $startTag;
84+
85+
return $this;
86+
}
87+
88+
/**
89+
* @return mixed
90+
*/
91+
public function getEndTag()
92+
{
93+
return $this->endTag;
94+
}
95+
96+
/**
97+
* @param mixed $endTag
98+
*
99+
* @return DiffListItem
100+
*/
101+
public function setEndTag($endTag)
102+
{
103+
$this->endTag = $endTag;
104+
105+
return $this;
106+
}
107+
108+
public function getHtml($class = 'normal', $wrapTag = null)
109+
{
110+
$startWrap = $wrapTag ? sprintf('<%s>', $wrapTag) : '';
111+
$endWrap = $wrapTag ? sprintf('</%s>', $wrapTag) : '';
112+
return sprintf('%s%s%s%s%s', $this->getStartTagWithDiffClass($class), $startWrap, $this->getInnerHtml(), $endWrap, $this->endTag);
113+
}
114+
115+
public function getInnerHtml()
116+
{
117+
return implode('', $this->text);
118+
}
119+
120+
public function __toString()
121+
{
122+
return $this->getHtml();
123+
}
124+
}

0 commit comments

Comments
 (0)