Skip to content

Commit 832f9b0

Browse files
usmanjschroed91
authored andcommitted
created sup script place holders
1 parent 4d74d11 commit 832f9b0

File tree

3 files changed

+60
-27
lines changed

3 files changed

+60
-27
lines changed

.DS_Store

6 KB
Binary file not shown.

demo/demo.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,26 @@
1717
<div class="row">
1818
<h2>Old HTML</h2>
1919
<div class="html-edit">
20-
<textarea ng-model="oldText" name="old_text" ng-change="update()"></textarea>
20+
<textarea ng-model="oldText" name="old_text"></textarea>
2121
</div>
2222
<div class="html-preview" ng-bind-html="trustHtml(oldText)"></div>
2323
</div>
2424
<div class="row">
2525
<h2>New HTML</h2>
2626
<div class="html-edit">
27-
<textarea ng-model="newText" name="new_text" ng-change="update()"></textarea>
27+
<textarea ng-model="newText" name="new_text"></textarea>
2828
</div>
2929
<div class="html-preview" ng-bind-html="trustHtml(newText)"></div>
3030
</div>
3131

3232
<div class="row">
3333
<h2>Compared HTML <span ng-show="loading || waiting">- {{ loading ? 'Loading' : 'Waiting' }}...</span></h2>
3434
<div class="html-edit">
35-
<textarea ng-model="diff" name="diff" disabled ng-change="update()"></textarea>
35+
<textarea ng-model="diff" name="diff" disabled></textarea>
3636
</div>
3737
<div class="html-preview" ng-bind-html="trustHtml(diff)"></div>
3838
</div>
39+
<button ng-click="update()">ACTION</button>
3940
</div>
4041
</body>
4142
</html>

lib/Caxy/HtmlDiff/HtmlDiff.php

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class HtmlDiff extends AbstractDiff
1010
protected $oldTables;
1111
protected $newTables;
1212
protected $insertSpaceInReplace = false;
13+
protected $newSuperScript;
14+
protected $oldSuperScript;
1315

1416
/**
1517
* @param boolean $boolean
@@ -35,6 +37,7 @@ public function build()
3537
$this->splitInputsToWords();
3638
$this->replaceTables();
3739
$this->indexNewWords();
40+
$this->replaceSuperScripts();
3841
$operations = $this->operations();
3942
foreach ($operations as $item) {
4043
$this->performOperation( $item );
@@ -64,7 +67,54 @@ protected function replaceTables()
6467
$this->newTables = $this->createTablePlaceholders($this->newWords);
6568
}
6669

67-
protected function createTablePlaceholders(&$words)
70+
protected function replaceSuperScripts()
71+
{
72+
$this->oldSuperScript = $this->createSuperPlaceholders($this->oldWords);
73+
$this->newSuperScript = $this->createSuperPlaceholders($this->newWords);
74+
}
75+
76+
77+
protected function createSuperPlaceholders(&$words)
78+
{
79+
$openSuperScripts = 0;
80+
$superScriptIndicies = array();
81+
$superScriptStart = 0;
82+
foreach ($words as $index => $word) {
83+
if ($this->isOpeningSuperScript($word)) {
84+
if ($openSuperScripts === 0) {
85+
$superScriptStart = $index;
86+
}
87+
$openSuperScripts++;
88+
} elseif($openSuperScripts > 0 && $this->isClosingSuperScript($word)) {
89+
$openSuperScripts--;
90+
if($openSuperScripts == 0){
91+
$superScriptIndicies[] = array ('start' => $superScriptStart, 'length' => $index - $superScriptStart + 1);
92+
}
93+
}
94+
}
95+
$superScripts = array();
96+
$offset = 0;
97+
foreach ($superScriptIndicies as $superScriptIndex) {
98+
$start = $superScriptIndex['start'] - $offset;
99+
$superScripts[$start] = array_splice($words, $start, $superScriptIndex['length'], '[[REPLACE_SUPER_SCRIPT]]');
100+
$offset += $superScriptIndex['length'] - 1;
101+
}
102+
103+
return $superScripts;
104+
105+
}
106+
107+
private function isOpeningSuperScript($item)
108+
{
109+
return preg_match("#<sup[^>]*>\\s*#iU", $item);
110+
}
111+
112+
private function isClosingSuperScript($item)
113+
{
114+
return preg_match("#</sup[^>]*>\\s*#iU", $item);
115+
}
116+
117+
private function createTablePlaceholders(&$words)
68118
{
69119
$openTables = 0;
70120
$tableIndices = array();
@@ -96,20 +146,14 @@ protected function createTablePlaceholders(&$words)
96146

97147
protected function isOpeningTable($item)
98148
{
99-
return preg_match("#<table[^>]+>\\s*#iU", $item);
149+
return preg_match("#<table[^>]*>\\s*#iU", $item);
100150
}
101151

102152
protected function isClosingTable($item)
103153
{
104154
return preg_match("#</table[^>]*>\\s*#iU", $item);
105155
}
106156

107-
protected function splitInputsToWords()
108-
{
109-
$this->oldWords = $this->convertHtmlToListOfWords( $this->explode( $this->oldText ) );
110-
$this->newWords = $this->convertHtmlToListOfWords( $this->explode( $this->newText ) );
111-
}
112-
113157
protected function performOperation($operation)
114158
{
115159
switch ($operation->action) {
@@ -130,22 +174,10 @@ protected function performOperation($operation)
130174
}
131175
}
132176

133-
protected function processReplaceOperation($operation)
177+
private function processReplaceOperation($operation)
134178
{
135-
$processDelete = strlen($this->oldText) > 0;
136-
$processInsert = strlen($this->newText) > 0;
137-
138-
if ($processDelete) {
139-
$this->processDeleteOperation( $operation, "diffmod" );
140-
}
141-
142-
if ($this->insertSpaceInReplace && $processDelete && $processInsert) {
143-
$this->content .= ' ';
144-
}
145-
146-
if ($processInsert) {
147-
$this->processInsertOperation( $operation, "diffmod" );
148-
}
179+
$this->processDeleteOperation( $operation, "diffmod" );
180+
$this->processInsertOperation( $operation, "diffmod" );
149181
}
150182

151183
protected function processInsertOperation($operation, $cssClass)
@@ -194,7 +226,7 @@ protected function processEqualOperation($operation)
194226
foreach ($this->newWords as $pos => $s) {
195227
if ($pos >= $operation->startInNew && $pos < $operation->endInNew) {
196228
if ($s === '[[REPLACE_TABLE]]' && isset($this->newTables[$pos])) {
197-
$oldText = implode("", $this->oldTables[$operation->startInOld]);
229+
$oldText = implode("", $this->findMatchingTableInOld($operation, $pos));
198230
$newText = implode("", $this->newTables[$pos]);
199231
$result[] = $this->diffTables($oldText, $newText);
200232
} else {

0 commit comments

Comments
 (0)