Skip to content

Commit 4b7bd1e

Browse files
committed
Added ability to set the specialCase opening and closing tags used when diffing
1 parent e9b4836 commit 4b7bd1e

File tree

1 file changed

+62
-4
lines changed

1 file changed

+62
-4
lines changed

HtmlDiff.php

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,62 @@ class HtmlDiff {
99
private $newWords = array();
1010
private $wordIndices;
1111
private $encoding;
12-
private $specialCaseOpeningTags = array( "/<strong[^>]*/i", "/<b[^>]*/i", "/<i[^>]*/i", "/<big[^>]*/i", "/<small[^>]*/i", "/<u[^>]*/i", "/<sub[^>]*/i", "/<sup[^>]*/i", "/<strike[^>]*/i", "/<s[^>]*/i", '/<p[^>]*/i' );
13-
private $specialCaseClosingTags = array( "</strong>", "</b>", "</i>", "</big>", "</small>", "</u>", "</sub>", "</sup>", "</strike>", "</s>", '</p>' );
12+
private $specialCaseOpeningTags = array();
13+
private $specialCaseClosingTags = array();
14+
private $specialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p');
1415

15-
public function __construct( $oldText, $newText, $encoding = 'UTF-8' ) {
16+
public function __construct( $oldText, $newText, $encoding = 'UTF-8', $specialCaseTags = array() ) {
1617
$this->oldText = $this->purifyHtml( trim( $oldText ) );
1718
$this->newText = $this->purifyHtml( trim( $newText ) );
1819
$this->encoding = $encoding;
1920
$this->content = '';
21+
22+
if (!empty($specialCaseTags)) {
23+
$this->specialCaseTags = $specialCaseTags;
24+
}
25+
26+
foreach ($this->specialCaseTags as $tag) {
27+
$this->addSpecialCaseTag($tag);
28+
}
29+
}
30+
31+
public function addSpecialCaseTag($tag)
32+
{
33+
if (!in_array($tag, $this->specialCaseTags)) {
34+
$this->specialCaseTags[] = $tag;
35+
}
36+
37+
$opening = $this->getOpeningTag($tag);
38+
$closing = $this->getClosingTag($tag);
39+
40+
if (!in_array($opening, $this->specialCaseOpeningTags)) {
41+
$this->specialCaseOpeningTags[] = $opening;
42+
}
43+
if (!in_array($closing, $this->specialCaseClosingTags)) {
44+
$this->specialCaseClosingTags[] = $closing;
45+
}
46+
}
47+
48+
public function removeSpecialCaseTag($tag)
49+
{
50+
if (($key = array_search($tag, $this->specialCaseTags)) !== false) {
51+
unset($this->specialCaseTags[$key]);
52+
53+
$opening = $this->getOpeningTag($tag);
54+
$closing = $this->getClosingTag($tag);
55+
56+
if (($key = array_search($opening, $this->specialCaseOpeningTags)) !== false) {
57+
unset($this->specialCaseOpeningTags[$key]);
58+
}
59+
if (($key = array_search($closing, $this->specialCaseClosingTags)) !== false) {
60+
unset($this->specialCaseClosingTags[$key]);
61+
}
62+
}
63+
}
64+
65+
public function getSpecialCaseTags()
66+
{
67+
return $this->specialCaseTags;
2068
}
2169

2270
public function getOldHtml() {
@@ -31,6 +79,16 @@ public function getDifference() {
3179
return $this->content;
3280
}
3381

82+
private function getOpeningTag($tag)
83+
{
84+
return "/<".$tag."[^>]*/i";
85+
}
86+
87+
private function getClosingTag($tag)
88+
{
89+
return "</".$tag.">";
90+
}
91+
3492
private function getStringBetween( $str, $start, $end ) {
3593
$expStr = explode( $start, $str, 2 );
3694
if( count( $expStr ) > 1 ) {
@@ -458,4 +516,4 @@ public function __construct( $action, $startInOld, $endInOld, $startInNew, $endI
458516
$this->StartInNew = $startInNew;
459517
$this->EndInNew = $endInNew;
460518
}
461-
}
519+
}

0 commit comments

Comments
 (0)