@@ -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