r95427 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r95426‎ | r95427 | r95428 >
Date:19:52, 24 August 2011
Author:hashar
Status:resolved (Comments)
Tags:
Comment:
add an id to each line of the CR diff
Modified paths:
  • /trunk/extensions/CodeReview/backend/DiffHighlighter.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CodeReview/backend/DiffHighlighter.php
@@ -4,9 +4,14 @@
55 * Highlight a SVN diff for easier readibility
66 */
77 class CodeDiffHighlighter {
 8+ /* chunk line count for the original file */
89 private $left = 0;
 10+ /* chunk line count for the changed file */
911 private $right = 0;
 12+ /* number of chunks */
1013 private $chunk = 0;
 14+ /* line number inside patch */
 15+ private $lineNumber = 0;
1116
1217 /**
1318 * Main entry point. Given a diff text, highlight it
@@ -40,6 +45,8 @@
4146 * @return string HTML table line (with <tr></tr>)
4247 */
4348 function parseLine( $line ) {
 49+ $this->lineNumber++;
 50+
4451 if( $line === '' ) { return ""; } // do not create bogus lines
4552
4653 # Dispatch diff lines to the proper handler
@@ -82,8 +89,9 @@
8390 . "<td class=\"linenumbers\">%s</td>"
8491 ;
8592
 93+ $idAttr = $this->getLineIdAttr();
8694 if( is_null($class) ) {
87 - return sprintf( "<tr>{$formatLN}<td>%s</td></tr>\n",
 95+ return sprintf( "<tr {$idAttr}>{$formatLN}<td>%s</td></tr>\n",
8896 $this->left,
8997 $this->right,
9098 $content
@@ -113,7 +121,7 @@
114122 }
115123
116124 $classAttr = is_null($class) ? '' : " class=\"$class\"";
117 - return sprintf( "<tr>{$formatLN}<td%s>%s</td></tr>\n",
 125+ return sprintf( "<tr {$idAttr}>{$formatLN}<td%s>%s</td></tr>\n",
118126 $left, $right,
119127 $classAttr, $content
120128 );
@@ -150,10 +158,16 @@
151159 }
152160
153161 function handleLineFile( $line ) {
154 - return "<tr class=\"patchedfile\"><td colspan=\"3\">$line</td></tr>";
 162+ $this->chunk = 0;
 163+ $idAttr = $this->getLineIdAttr();
 164+ return "<tr $idAttr class=\"patchedfile\"><td colspan=\"3\">$line</td></tr>";
155165 }
156166 #### END OF LINES HANDLERS #########################################
157167
 168+ function getLineIdAttr() {
 169+ return "id=\"line-{$this->lineNumber}\"";
 170+ }
 171+
158172 /**
159173 * Turn a diff line into a properly formatted string suitable
160174 * for output

Follow-up revisions

RevisionCommit summaryAuthorDate
r105542use Html::element instead of sprintf / htmlspecialchars...hashar12:19, 8 December 2011

Comments

#Comment by Tim Starling (talk | contribs)   05:20, 8 December 2011

getLineIdAttr() should return an array and the HTML construction part should use Html::element() so the security is easier to verify. sprintf() should not be used for constructing HTML in new code.

Status & tagging log