// If we run from a Safari instance, we don't // have a Controller object. Instead, we fake it by // using the console if (typeof Controller == 'undefined') { Controller = console; Controller.log_ = console.log; } var highlightDiff = function(diff, element) { var start = new Date().getTime(); element.className = "diff" var content = diff.escapeHTML().replace(/\t/g, " ");; var file_index = 0; var filename = ""; var line1 = ""; var line2 = ""; var diffContent = ""; var finalContent = ""; var lines = content.split('\n'); var hunk_start_line_1 = -1; var hunk_start_line_2 = -1; var header = false; for (var lineno = 0; lineno < lines.length; lineno++) { var l = lines[lineno]; var firstChar = l.charAt(0); if (header) { if (firstChar == "+" || firstChar == "-") continue; } else if (firstChar == "d") { // New file, we have to reset everything header = true; if (file_index++) // Finish last file { finalContent += '
' + '
' + filename + '
' + '
' + '
' + line1 + "
" + '
' + line2 + "
" + '
' + diffContent + "
" + '
' + '
'; line1 = ""; line2 = ""; diffContent = ""; } if(match = l.match(/diff --git a\/(\S*)/)) filename = match[1]; continue; } if (firstChar == "+") { // Highlight trailing whitespace if (m = l.match(/\s+$/)) l = l.replace(/\s+$/, "" + m + ""); line1 += "\n"; line2 += ++hunk_start_line_2 + "\n"; diffContent += "
" + l + "
"; } else if (firstChar == "-") { line1 += ++hunk_start_line_1 + "\n"; line2 += "\n"; diffContent += "
" + l + "
"; } else if (firstChar == "@") { if (header) { header = false; } if (m = l.match(/@@ \-([0-9]+),\d+ \+(\d+),\d+ @@/)) { hunk_start_line_1 = parseInt(m[1]) - 1; hunk_start_line_2 = parseInt(m[2]) - 1; } line1 += "...\n"; line2 += "...\n"; diffContent += "
" + l + "
"; } else if (firstChar == " ") { line1 += ++hunk_start_line_1 + "\n"; line2 += ++hunk_start_line_2 + "\n"; diffContent += l + "\n"; } } finalContent += '
' + '
' + filename + '
' + '
' + '
' + line1 + "
" + '
' + line2 + "
" + '
' + diffContent + "
" + '
' + '
'; // This takes about 7ms element.innerHTML = finalContent; // TODO: Replace this with a performance pref call if (false) Controller.log_("Total time:" + (new Date().getTime() - start)); }