Skip to content

Commit 1a79b47

Browse files
usmanjschroed91
authored andcommitted
code review changes
1 parent 8b10968 commit 1a79b47

File tree

1 file changed

+46
-56
lines changed

1 file changed

+46
-56
lines changed

lib/Caxy/HtmlDiff/HtmlDiff.php

Lines changed: 46 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ class HtmlDiff extends AbstractDiff
88
{
99
protected $wordIndices;
1010
protected $insertSpaceInReplace = false;
11-
protected $newSpecialScript;
12-
protected $oldSpecialScript;
13-
protected $specialElements = array ('ol' => '[[REPLACE_ORDERED_LIST]]', 'ul' => '[[REPLACE_UNORDERED_LIST]]', 'sub' => '[[REPLACE_SUB_SCRIPT]]' , 'sup' => '[[REPLACE_SUPER_SCRIPT]]', 'dl' => '[[REPLACE_DEFINITION_LIST]]', 'table' => '[[REPLACE_TABLE]]');
11+
protected $newIsolatedDiffTags;
12+
protected $oldIsolatedDiffTags;
13+
protected $isolatedDiffTags = array ('ol' => '[[REPLACE_ORDERED_LIST]]', 'ul' => '[[REPLACE_UNORDERED_LIST]]', 'sub' => '[[REPLACE_SUB_SCRIPT]]' , 'sup' => '[[REPLACE_SUPER_SCRIPT]]', 'dl' => '[[REPLACE_DEFINITION_LIST]]', 'table' => '[[REPLACE_TABLE]]');
1414

1515
/**
1616
* @param boolean $boolean
@@ -34,7 +34,7 @@ public function getInsertSpaceInReplace()
3434
public function build()
3535
{
3636
$this->splitInputsToWords();
37-
$this->replaceSpecialScripts();
37+
$this->replaceIsolatedDiffTags();
3838
$this->indexNewWords();
3939

4040
$operations = $this->operations();
@@ -60,51 +60,50 @@ protected function indexNewWords()
6060
}
6161
}
6262

63-
private function replaceSpecialScripts()
63+
private function replaceIsolatedDiffTags()
6464
{
65-
$this->oldSpecialScript = $this->createSpecialPlaceholders($this->oldWords);
66-
$this->newSpecialScript = $this->createSpecialPlaceholders($this->newWords);
65+
$this->oldIsolatedDiffTags = $this->createIsolatedDiffTagPlaceholders($this->oldWords);
66+
$this->newIsolatedDiffTags = $this->createIsolatedDiffTagPlaceholders($this->newWords);
6767
}
6868

69-
70-
protected function createSpecialPlaceholders(&$words)
69+
private function createIsolatedDiffTagPlaceholders(&$words)
7170
{
72-
$openSpecialScripts = 0;
73-
$specialScriptIndicies = array();
74-
$specialScriptStart = 0;
75-
$currentSpecialTag = null;
71+
$openIsolatedDiffTags = 0;
72+
$isolatedDiffTagIndicies = array();
73+
$isolatedDiffTagStart = 0;
74+
$currentIsolatedDiffTag = null;
7675
foreach ($words as $index => $word) {
77-
$openSpecialTag = $this->isOpeningSpecialScript($word, $currentSpecialTag);
78-
if ($openSpecialTag) {
79-
if ($openSpecialScripts === 0) {
80-
$specialScriptStart = $index;
76+
$openIsolatedDiffTag = $this->isOpeningIsolatedDiffTag($word, $currentIsolatedDiffTag);
77+
if ($openIsolatedDiffTag) {
78+
if ($openIsolatedDiffTags === 0) {
79+
$isolatedDiffTagStart = $index;
8180
}
82-
$openSpecialScripts++;
83-
$currentSpecialTag = $openSpecialTag;
84-
} elseif($openSpecialScripts > 0 && $this->isClosingSpecialScript($word, $currentSpecialTag)) {
85-
$openSpecialScripts--;
86-
if($openSpecialScripts == 0){
87-
$specialScriptIndicies[] = array ('start' => $specialScriptStart, 'length' => $index - $specialScriptStart + 1, 'tagType' => $currentSpecialTag);
88-
$currentSpecialTag = null;
81+
$openIsolatedDiffTags++;
82+
$currentIsolatedDiffTag = $openIsolatedDiffTag;
83+
} elseif($openIsolatedDiffTags > 0 && $this->isClosingIsolatedDiffTag($word, $currentIsolatedDiffTag)) {
84+
$openIsolatedDiffTags--;
85+
if($openIsolatedDiffTags == 0){
86+
$isolatedDiffTagIndicies[] = array ('start' => $isolatedDiffTagStart, 'length' => $index - $isolatedDiffTagStart + 1, 'tagType' => $currentIsolatedDiffTag);
87+
$currentIsolatedDiffTag = null;
8988
}
9089
}
9190
}
92-
$specialScripts = array();
91+
$isolatedDiffTagScript = array();
9392
$offset = 0;
94-
foreach ($specialScriptIndicies as $specialScriptIndex) {
95-
$start = $specialScriptIndex['start'] - $offset;
96-
$placeholderString = $this->specialElements[$specialScriptIndex['tagType']];
97-
$specialScripts[$start] = array_splice($words, $start, $specialScriptIndex['length'], $placeholderString);
98-
$offset += $specialScriptIndex['length'] - 1;
93+
foreach ($isolatedDiffTagIndicies as $isolatedDiffTagIndex) {
94+
$start = $isolatedDiffTagIndex['start'] - $offset;
95+
$placeholderString = $this->isolatedDiffTags[$isolatedDiffTagIndex['tagType']];
96+
$isolatedDiffTagScript[$start] = array_splice($words, $start, $isolatedDiffTagIndex['length'], $placeholderString);
97+
$offset += $isolatedDiffTagIndex['length'] - 1;
9998
}
10099

101-
return $specialScripts;
100+
return $isolatedDiffTagScript;
102101

103102
}
104103

105-
private function isOpeningSpecialScript($item, $currentSpecialTag = null)
104+
private function isOpeningIsolatedDiffTag($item, $currentIsolatedDiffTag = null)
106105
{
107-
$tagsToMatch = $currentSpecialTag !== null ? array($currentSpecialTag => $this->specialElements[$currentSpecialTag]) : $this->specialElements;
106+
$tagsToMatch = $currentIsolatedDiffTag !== null ? array($currentIsolatedDiffTag => $this->isolatedDiffTags[$currentIsolatedDiffTag]) : $this->isolatedDiffTags;
108107
foreach ($tagsToMatch as $key => $value) {
109108
if (preg_match("#<".$key."[^>]*>\\s*#iU", $item)) {
110109
return $key;
@@ -113,9 +112,9 @@ private function isOpeningSpecialScript($item, $currentSpecialTag = null)
113112
return false;
114113
}
115114

116-
private function isClosingSpecialScript($item, $currentSpecialTag = null)
115+
private function isClosingIsolatedDiffTag($item, $currentIsolatedDiffTag = null)
117116
{
118-
$tagsToMatch = $currentSpecialTag !== null ? array($currentSpecialTag => $this->specialElements[$currentSpecialTag]) : $this->specialElements;
117+
$tagsToMatch = $currentIsolatedDiffTag !== null ? array($currentIsolatedDiffTag => $this->isolatedDiffTags[$currentIsolatedDiffTag]) : $this->isolatedDiffTags;
119118
foreach ($tagsToMatch as $key => $value) {
120119
if (preg_match("#</".$key."[^>]*>\\s*#iU", $item)) {
121120
return $key;
@@ -154,17 +153,12 @@ protected function processInsertOperation($operation, $cssClass)
154153
{
155154
$text = array();
156155
foreach ($this->newWords as $pos => $s) {
157-
$matchFound = false;
158156
if ($pos >= $operation->startInNew && $pos < $operation->endInNew) {
159-
foreach ($this->specialElements as $specialElement) {
160-
if($s === $specialElement && isset($this->newSpecialScript[$pos]) && $matchFound === false) {
161-
foreach ($this->newSpecialScript[$pos] as $word) {
162-
$text[] = $word;
163-
}
164-
$matchFound = true;
157+
if(in_array($s, $this->isolatedDiffTags) && isset($this->newIsolatedDiffTags[$pos])) {
158+
foreach ($this->newIsolatedDiffTags[$pos] as $word) {
159+
$text[] = $word;
165160
}
166-
}
167-
if($matchFound === false){
161+
} else {
168162
$text[] = $s;
169163
}
170164
}
@@ -176,16 +170,12 @@ protected function processDeleteOperation($operation, $cssClass)
176170
{
177171
$text = array();
178172
foreach ($this->oldWords as $pos => $s) {
179-
$matchFound = false;
180173
if ($pos >= $operation->startInOld && $pos < $operation->endInOld) {
181-
foreach ($this->specialElements as $specialElement)
182-
if ($s === $specialElement && isset($this->oldSpecialScript[$pos]) && $matchFound === false) {
183-
foreach ($this->oldSpecialScript[$pos] as $word) {
174+
if (in_array($s, $this->isolatedDiffTags) && isset($this->newIsolatedDiffTags[$pos])) {
175+
foreach ($this->oldIsolatedDiffTags[$pos] as $word) {
184176
$text[] = $word;
185177
}
186-
$matchFound = true;
187-
}
188-
if($matchFound === false){
178+
} else {
189179
$text[] = $s;
190180
}
191181
}
@@ -206,7 +196,7 @@ private function diffElements($oldText, $newText)
206196
$oldText = preg_replace($pattern, '', $oldText);
207197
$newText = preg_replace($pattern, '', $newText);
208198

209-
$diff = new HtmlDiff($oldText, $newText, $this->encoding, $this->specialCaseTags, $this->groupDiffs);
199+
$diff = new HtmlDiff($oldText, $newText, $this->encoding, $this->isolatedDiffTags, $this->groupDiffs);
210200
return $wrapStart . $diff->build() . $wrapEnd;
211201
}
212202

@@ -216,10 +206,10 @@ protected function processEqualOperation($operation)
216206
foreach ($this->newWords as $pos => $s) {
217207
$matchFound = false;
218208
if ($pos >= $operation->startInNew && $pos < $operation->endInNew) {
219-
foreach ($this->specialElements as $specialElement) {
220-
if ($s === $specialElement && isset($this->newSpecialScript[$pos]) && $matchFound === false) {
209+
foreach ($this->isolatedDiffTags as $isolatedDiffTag) {
210+
if ($s === $isolatedDiffTag && isset($this->newIsolatedDiffTags[$pos]) && $matchFound === false) {
221211
$oldText = implode("", $this->findMatchingScriptsInOld($operation, $pos));
222-
$newText = implode("", $this->newSpecialScript[$pos]);
212+
$newText = implode("", $this->newIsolatedDiffTags[$pos]);
223213
$result[] = $this->diffElements($oldText, $newText);
224214
$matchFound = true;
225215
}
@@ -236,7 +226,7 @@ private function findMatchingScriptsInOld($operation, $posInNew)
236226
{
237227
$offset = $posInNew - $operation->startInNew;
238228

239-
return $this->oldSpecialScript[$operation->startInOld + $offset];
229+
return $this->oldIsolatedDiffTags[$operation->startInOld + $offset];
240230
}
241231

242232
protected function insertTag($tag, $cssClass, &$words)

0 commit comments

Comments
 (0)