@@ -61,86 +61,167 @@ public function render()
61
61
return $ html ;
62
62
}
63
63
64
- $ html .= '<table class="Differences DifferencesInline"> ' ;
65
- $ html .= '<thead> ' ;
66
- $ html .= '<tr> ' ;
67
- $ html .= '<th>Old</th> ' ;
68
- $ html .= '<th>New</th> ' ;
69
- $ html .= '<th>Differences</th> ' ;
70
- $ html .= '</tr> ' ;
71
- $ html .= '</thead> ' ;
64
+ $ html .= $ this ->generateTableHeader ();
65
+
72
66
foreach ($ changes as $ i => $ blocks ) {
73
67
// If this is a separate block, we're condensing code so output ...,
74
68
// indicating a significant portion of the code has been collapsed as
75
69
// it is the same
76
70
if ($ i > 0 ) {
77
- $ html .= '<tbody class="Skipped"> ' ;
78
- $ html .= '<th>…</th> ' ;
79
- $ html .= '<th>…</th> ' ;
80
- $ html .= '<td> </td> ' ;
81
- $ html .= '</tbody> ' ;
71
+ $ html .= $ this ->generateSkippedTable ();
82
72
}
83
73
84
74
foreach ($ blocks as $ change ) {
85
75
$ html .= '<tbody class="Change ' .ucfirst ($ change ['tag ' ]).'"> ' ;
86
- // Equal changes should be shown on both sides of the diff
87
- if ($ change ['tag ' ] == 'equal ' ) {
88
- foreach ($ change ['base ' ]['lines ' ] as $ no => $ line ) {
89
- $ fromLine = $ change ['base ' ]['offset ' ] + $ no + 1 ;
90
- $ toLine = $ change ['changed ' ]['offset ' ] + $ no + 1 ;
91
- $ html .= '<tr> ' ;
92
- $ html .= '<th> ' .$ fromLine .'</th> ' ;
93
- $ html .= '<th> ' .$ toLine .'</th> ' ;
94
- $ html .= '<td class="Left"> ' .$ line .'</td> ' ;
95
- $ html .= '</tr> ' ;
96
- }
97
- }
98
- // Added lines only on the right side
99
- else if ($ change ['tag ' ] == 'insert ' ) {
100
- foreach ($ change ['changed ' ]['lines ' ] as $ no => $ line ) {
101
- $ toLine = $ change ['changed ' ]['offset ' ] + $ no + 1 ;
102
- $ html .= '<tr> ' ;
103
- $ html .= '<th> </th> ' ;
104
- $ html .= '<th> ' .$ toLine .'</th> ' ;
105
- $ html .= '<td class="Right"><ins> ' .$ line .'</ins> </td> ' ;
106
- $ html .= '</tr> ' ;
107
- }
108
- }
109
- // Show deleted lines only on the left side
110
- else if ($ change ['tag ' ] == 'delete ' ) {
111
- foreach ($ change ['base ' ]['lines ' ] as $ no => $ line ) {
112
- $ fromLine = $ change ['base ' ]['offset ' ] + $ no + 1 ;
113
- $ html .= '<tr> ' ;
114
- $ html .= '<th> ' .$ fromLine .'</th> ' ;
115
- $ html .= '<th> </th> ' ;
116
- $ html .= '<td class="Left"><del> ' .$ line .'</del> </td> ' ;
117
- $ html .= '</tr> ' ;
118
- }
119
- }
120
- // Show modified lines on both sides
121
- else if ($ change ['tag ' ] == 'replace ' ) {
122
- foreach ($ change ['base ' ]['lines ' ] as $ no => $ line ) {
123
- $ fromLine = $ change ['base ' ]['offset ' ] + $ no + 1 ;
124
- $ html .= '<tr> ' ;
125
- $ html .= '<th> ' .$ fromLine .'</th> ' ;
126
- $ html .= '<th> </th> ' ;
127
- $ html .= '<td class="Left"><span> ' .$ line .'</span></td> ' ;
128
- $ html .= '</tr> ' ;
129
- }
130
-
131
- foreach ($ change ['changed ' ]['lines ' ] as $ no => $ line ) {
132
- $ toLine = $ change ['changed ' ]['offset ' ] + $ no + 1 ;
133
- $ html .= '<tr> ' ;
134
- $ html .= '<th> </th> ' ;
135
- $ html .= '<th> ' .$ toLine .'</th> ' ;
136
- $ html .= '<td class="Right"><span> ' .$ line .'</span></td> ' ;
137
- $ html .= '</tr> ' ;
138
- }
76
+ switch ($ change ['tag ' ]){
77
+ // Equal changes should be shown on both sides of the diff
78
+ case 'equal ' :
79
+ $ html .= $ this ->generateTableRowsEqual ($ change );
80
+ break ;
81
+ // Added lines only on the right side
82
+ case 'insert ' :
83
+ $ html .= $ this ->generateTableRowsInsert ($ change );
84
+ break ;
85
+ // Show deleted lines only on the left side
86
+ case 'delete ' :
87
+ $ html .= $ this ->generateTableRowsDelete ($ change );
88
+ break ;
89
+ // Show modified lines on both sides
90
+ case 'replace ' :
91
+ $ html .= $ this ->generateTableRowsReplace ($ change );
92
+ break ;
139
93
}
140
94
$ html .= '</tbody> ' ;
141
95
}
142
96
}
143
97
$ html .= '</table> ' ;
144
98
return $ html ;
145
99
}
100
+
101
+
102
+ /**
103
+ * Generates a string representation of a predefined table and its head with
104
+ * titles from options.
105
+ *
106
+ * @return string Html code representation of the table's header.
107
+ */
108
+ private function generateTableHeader ()
109
+ {
110
+ $ html = '<table class="Differences DifferencesInline"> ' ;
111
+ $ html .= '<thead> ' ;
112
+ $ html .= '<tr> ' ;
113
+ $ html .= '<th>Old</th> ' ;
114
+ $ html .= '<th>New</th> ' ;
115
+ $ html .= '<th>Differences</th> ' ;
116
+ $ html .= '</tr> ' ;
117
+ $ html .= '</thead> ' ;
118
+ return $ html ;
119
+ }
120
+
121
+ /**
122
+ * Generates a string representation of empty table body.
123
+ *
124
+ * @return string Html code representing empty table body.
125
+ */
126
+ private function generateSkippedTable ()
127
+ {
128
+ $ html = '<tbody class="Skipped"> ' ;
129
+ $ html .= '<th>…</th> ' ;
130
+ $ html .= '<th>…</th> ' ;
131
+ $ html .= '<td> </td> ' ;
132
+ $ html .= '</tbody> ' ;
133
+ return $ html ;
134
+ }
135
+
136
+ /**
137
+ * Generates a string representation of one or more rows of a table of lines of text with no difference.
138
+ *
139
+ * @param array &$change Array with data about changes.
140
+ * @return string Html code representing one or more rows of text with no difference.
141
+ */
142
+ private function generateTableRowsEqual (&$ change )
143
+ {
144
+ $ html = "" ;
145
+ foreach ($ change ['base ' ]['lines ' ] as $ no => $ line ) {
146
+ $ fromLine = $ change ['base ' ]['offset ' ] + $ no + 1 ;
147
+ $ toLine = $ change ['changed ' ]['offset ' ] + $ no + 1 ;
148
+ $ html .= '<tr> ' ;
149
+ $ html .= '<th> ' .$ fromLine .'</th> ' ;
150
+ $ html .= '<th> ' .$ toLine .'</th> ' ;
151
+ $ html .= '<td class="Left"> ' .$ line .'</td> ' ;
152
+ $ html .= '</tr> ' ;
153
+ }
154
+ return $ html ;
155
+ }
156
+
157
+ /**
158
+ * Generates a string representation of one or more rows of a table of lines, where new text was added.
159
+ *
160
+ * @param array &$change Array with data about changes.
161
+ * @return string Html code representing one or more rows of added text.
162
+ */
163
+ private function generateTableRowsInsert (&$ change )
164
+ {
165
+ $ html = "" ;
166
+ foreach ($ change ['changed ' ]['lines ' ] as $ no => $ line ) {
167
+ $ toLine = $ change ['changed ' ]['offset ' ] + $ no + 1 ;
168
+ $ html .= '<tr> ' ;
169
+ $ html .= '<th> </th> ' ;
170
+ $ html .= '<th> ' .$ toLine .'</th> ' ;
171
+ $ html .= '<td class="Right"><ins> ' .$ line .'</ins> </td> ' ;
172
+ $ html .= '</tr> ' ;
173
+ }
174
+ return $ html ;
175
+ }
176
+
177
+ /**
178
+ * Generates a string representation of one or more rows of a table of lines, where text was removed.
179
+ *
180
+ * @param array &$change Array with data about changes.
181
+ * @return string Html code representing one or more rows of removed text.
182
+ */
183
+ private function generateTableRowsDelete (&$ change )
184
+ {
185
+ $ html = "" ;
186
+ foreach ($ change ['base ' ]['lines ' ] as $ no => $ line ) {
187
+ $ fromLine = $ change ['base ' ]['offset ' ] + $ no + 1 ;
188
+ $ html .= '<tr> ' ;
189
+ $ html .= '<th> ' .$ fromLine .'</th> ' ;
190
+ $ html .= '<th> </th> ' ;
191
+ $ html .= '<td class="Left"><del> ' .$ line .'</del> </td> ' ;
192
+ $ html .= '</tr> ' ;
193
+ }
194
+ return $ html ;
195
+ }
196
+
197
+ /**
198
+ * Generates a string representation of one or more rows of a table of lines, where text was partially modified.
199
+ *
200
+ * @param array &$change Array with data about changes.
201
+ * @return string Html code representing one or more rows of modified.
202
+ */
203
+ private function generateTableRowsReplace (&$ change )
204
+ {
205
+ $ html = "" ;
206
+
207
+ foreach ($ change ['base ' ]['lines ' ] as $ no => $ line ) {
208
+ $ fromLine = $ change ['base ' ]['offset ' ] + $ no + 1 ;
209
+ $ html .= '<tr> ' ;
210
+ $ html .= '<th> ' .$ fromLine .'</th> ' ;
211
+ $ html .= '<th> </th> ' ;
212
+ $ html .= '<td class="Left"><span> ' .$ line .'</span></td> ' ;
213
+ $ html .= '</tr> ' ;
214
+ }
215
+
216
+ foreach ($ change ['changed ' ]['lines ' ] as $ no => $ line ) {
217
+ $ toLine = $ change ['changed ' ]['offset ' ] + $ no + 1 ;
218
+ $ html .= '<tr> ' ;
219
+ $ html .= '<th> </th> ' ;
220
+ $ html .= '<th> ' .$ toLine .'</th> ' ;
221
+ $ html .= '<td class="Right"><span> ' .$ line .'</span></td> ' ;
222
+ $ html .= '</tr> ' ;
223
+ }
224
+
225
+ return $ html ;
226
+ }
146
227
}
0 commit comments