Skip to content

Commit 7b06a87

Browse files
usmanjschroed91
authored andcommitted
ICC-4313 Replace HTML elements with placeholder token
1 parent 832f9b0 commit 7b06a87

File tree

6 files changed

+46
-27
lines changed

6 files changed

+46
-27
lines changed

.DS_Store

0 Bytes
Binary file not shown.

demo/.DS_Store

6 KB
Binary file not shown.

lib/.DS_Store

6 KB
Binary file not shown.

lib/Caxy/.DS_Store

6 KB
Binary file not shown.

lib/Caxy/HtmlDiff/.DS_Store

6 KB
Binary file not shown.

lib/Caxy/HtmlDiff/HtmlDiff.php

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ class HtmlDiff extends AbstractDiff
1010
protected $oldTables;
1111
protected $newTables;
1212
protected $insertSpaceInReplace = false;
13-
protected $newSuperScript;
14-
protected $oldSuperScript;
13+
protected $newSpecialScript;
14+
protected $oldSpecialScript;
15+
protected $specialElements = array ('ol' => '[[REPLACE_ORDERED_LIST]]', 'ul' => '[[REPLACE_UNORDERED_LIST]]', 'sub' => '[[REPLACE_SUB_SCRIPT]]' , 'sup' => '[[REPLACE_SUPER_SCRIPT]]', 'dl' => '[[REPLACE_DEFINITION_LIST]]');
1516

1617
/**
1718
* @param boolean $boolean
@@ -36,8 +37,9 @@ public function build()
3637
{
3738
$this->splitInputsToWords();
3839
$this->replaceTables();
40+
$this->replaceSpecialScripts();
3941
$this->indexNewWords();
40-
$this->replaceSuperScripts();
42+
4143
$operations = $this->operations();
4244
foreach ($operations as $item) {
4345
$this->performOperation( $item );
@@ -69,49 +71,66 @@ protected function replaceTables()
6971

7072
protected function replaceSuperScripts()
7173
{
72-
$this->oldSuperScript = $this->createSuperPlaceholders($this->oldWords);
73-
$this->newSuperScript = $this->createSuperPlaceholders($this->newWords);
74+
$this->oldSpecialScript = $this->createSpecialPlaceholders($this->oldWords);
75+
$this->newSpecialScript = $this->createSpecialPlaceholders($this->newWords);
7476
}
7577

7678

77-
protected function createSuperPlaceholders(&$words)
79+
protected function createSpecialPlaceholders(&$words)
7880
{
79-
$openSuperScripts = 0;
80-
$superScriptIndicies = array();
81-
$superScriptStart = 0;
81+
$openSpecialScripts = 0;
82+
$specialScriptIndicies = array();
83+
$specialScriptStart = 0;
84+
$currentSpecialTag = null;
8285
foreach ($words as $index => $word) {
83-
if ($this->isOpeningSuperScript($word)) {
84-
if ($openSuperScripts === 0) {
85-
$superScriptStart = $index;
86+
$openSpecialTag = $this->isOpeningSpecialScript($word, $currentSpecialTag);
87+
if ($openSpecialTag) {
88+
if ($openSpecialScripts === 0) {
89+
$specialScriptStart = $index;
8690
}
87-
$openSuperScripts++;
88-
} elseif($openSuperScripts > 0 && $this->isClosingSuperScript($word)) {
89-
$openSuperScripts--;
90-
if($openSuperScripts == 0){
91-
$superScriptIndicies[] = array ('start' => $superScriptStart, 'length' => $index - $superScriptStart + 1);
91+
$openSpecialScripts++;
92+
$currentSpecialTag = $openSpecialTag;
93+
} elseif($openSpecialScripts > 0 && $this->isClosingSpecialScript($word, $currentSpecialTag)) {
94+
$openSpecialScripts--;
95+
if($openSpecialScripts == 0){
96+
$specialScriptIndicies[] = array ('start' => $specialScriptStart, 'length' => $index - $specialScriptStart + 1, 'tagType' => $currentSpecialTag);
97+
$currentSpecialTag = null;
9298
}
9399
}
94100
}
95-
$superScripts = array();
101+
$specialScripts = array();
96102
$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;
103+
foreach ($specialScriptIndicies as $specialScriptIndex) {
104+
$start = $specialScriptIndex['start'] - $offset;
105+
$placeholderString = $this->specialElements[$specialScriptIndex['tagType']];
106+
$specialScripts[$start] = array_splice($words, $start, $specialScriptIndex['length'], $placeholderString);
107+
$offset += $specialScriptIndex['length'] - 1;
101108
}
102109

103-
return $superScripts;
110+
return $specialScripts;
104111

105112
}
106113

107-
private function isOpeningSuperScript($item)
114+
private function isOpeningSpecialScript($item, $currentSpecialTag = null)
108115
{
109-
return preg_match("#<sup[^>]*>\\s*#iU", $item);
116+
$tagsToMatch = $currentSpecialTag !== null ? array($currentSpecialTag => $this->specialElements[$currentSpecialTag]) : $this->specialElements;
117+
foreach ($tagsToMatch as $key => $value) {
118+
if (preg_match("#<".$key."[^>]*>\\s*#iU", $item)) {
119+
return $key;
120+
}
121+
}
122+
return false;
110123
}
111124

112-
private function isClosingSuperScript($item)
125+
private function isClosingSpecialScript($item, $currentSpecialTag = null)
113126
{
114-
return preg_match("#</sup[^>]*>\\s*#iU", $item);
127+
$tagsToMatch = $currentSpecialTag !== null ? array($currentSpecialTag => $this->specialElements[$currentSpecialTag]) : $this->specialElements;
128+
foreach ($tagsToMatch as $key => $value) {
129+
if (preg_match("#</".$key."[^>]*>\\s*#iU", $item)) {
130+
return $key;
131+
}
132+
}
133+
return false;
115134
}
116135

117136
private function createTablePlaceholders(&$words)

0 commit comments

Comments
 (0)