Skip to content

Commit 64b2fb9

Browse files
committed
Abstracted logic in add/remove special case tag functions
1 parent 93ea2a2 commit 64b2fb9

File tree

1 file changed

+41
-38
lines changed

1 file changed

+41
-38
lines changed

HtmlDiff.php

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -30,55 +30,58 @@ public function __construct( $oldText, $newText, $encoding = 'UTF-8', $specialCa
3030

3131
public function addSpecialCaseTag($tag)
3232
{
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-
}
33+
$this->addToArray($tag, $this->specialCaseTags);
34+
$this->addToArray($this->getOpeningTag($tag), $this->specialCaseOpeningTags);
35+
$this->addToArray($this->getClosingTag($tag), $this->specialCaseClosingTags);
4636
}
4737

4838
public function removeSpecialCaseTag($tag)
4939
{
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-
}
40+
$this->removeFromArray($tag, $this->specialCaseTags);
41+
$this->removeFromArray($this->getOpeningTag($tag), $this->specialCaseOpeningTags);
42+
$this->removeFromArray($this->getClosingTag($tag), $this->specialCaseClosingTags);
6343
}
6444

6545
public function getSpecialCaseTags()
6646
{
6747
return $this->specialCaseTags;
6848
}
6949

70-
public function getOldHtml() {
50+
public function getOldHtml()
51+
{
7152
return $this->oldText;
7253
}
7354

74-
public function getNewHtml() {
55+
public function getNewHtml()
56+
{
7557
return $this->newText;
7658
}
7759

78-
public function getDifference() {
60+
public function getDifference()
61+
{
7962
return $this->content;
8063
}
8164

65+
private function addToArray($item, &$array)
66+
{
67+
if (!in_array($item, $array)) {
68+
$array[] = $item;
69+
return true;
70+
}
71+
72+
return false;
73+
}
74+
75+
private function removeFromArray($item, &$array)
76+
{
77+
if (($key = array_search($item, $array)) !== false) {
78+
unset($array[$key]);
79+
return true;
80+
}
81+
82+
return false;
83+
}
84+
8285
private function getOpeningTag($tag)
8386
{
8487
return "/<".$tag."[^>]*/i";
@@ -324,15 +327,15 @@ private function InsertTag( $tag, $cssClass, &$words ) {
324327
$this->content .= $specialCaseTagInjection . implode( "", $this->ExtractConsecutiveWords( $words, 'tag' ) );
325328
} else {
326329
$workTag = $this->ExtractConsecutiveWords( $words, 'tag' );
327-
if( isset( $workTag[ 0 ] ) && $this->IsOpeningTag( $workTag[ 0 ] ) && !$this->IsClosingTag( $workTag[ 0 ] ) ) {
328-
if( strpos( $workTag[ 0 ], 'class=' ) ) {
329-
$workTag[ 0 ] = str_replace( 'class="', 'class="diffmod ', $workTag[ 0 ] );
330-
$workTag[ 0 ] = str_replace( "class='", 'class="diffmod ', $workTag[ 0 ] );
331-
} else {
332-
$workTag[ 0 ] = str_replace( ">", ' class="diffmod">', $workTag[ 0 ] );
333-
}
334-
}
335-
$this->content .= implode( "", $workTag ) . $specialCaseTagInjection;
330+
if( isset( $workTag[ 0 ] ) && $this->IsOpeningTag( $workTag[ 0 ] ) && !$this->IsClosingTag( $workTag[ 0 ] ) ) {
331+
if( strpos( $workTag[ 0 ], 'class=' ) ) {
332+
$workTag[ 0 ] = str_replace( 'class="', 'class="diffmod ', $workTag[ 0 ] );
333+
$workTag[ 0 ] = str_replace( "class='", 'class="diffmod ', $workTag[ 0 ] );
334+
} else {
335+
$workTag[ 0 ] = str_replace( ">", ' class="diffmod">', $workTag[ 0 ] );
336+
}
337+
}
338+
$this->content .= implode( "", $workTag ) . $specialCaseTagInjection;
336339
}
337340
}
338341
}

0 commit comments

Comments
 (0)