Text_Diff_Renderer_inline

"Inline" diff renderer.

Defined (1)

The class is defined in the following location(s).

/wp-includes/Text/Diff/Renderer/inline.php  
  1. class Text_Diff_Renderer_inline extends Text_Diff_Renderer { 
  2.  
  3. /** 
  4. * Number of leading context "lines" to preserve. 
  5. * @var integer 
  6. */ 
  7. var $_leading_context_lines = 10000; 
  8.  
  9. /** 
  10. * Number of trailing context "lines" to preserve. 
  11. * @var integer 
  12. */ 
  13. var $_trailing_context_lines = 10000; 
  14.  
  15. /** 
  16. * Prefix for inserted text. 
  17. * @var string 
  18. */ 
  19. var $_ins_prefix = '<ins>'; 
  20.  
  21. /** 
  22. * Suffix for inserted text. 
  23. * @var string 
  24. */ 
  25. var $_ins_suffix = '</ins>'; 
  26.  
  27. /** 
  28. * Prefix for deleted text. 
  29. * @var string 
  30. */ 
  31. var $_del_prefix = '<del>'; 
  32.  
  33. /** 
  34. * Suffix for deleted text. 
  35. * @var string 
  36. */ 
  37. var $_del_suffix = '</del>'; 
  38.  
  39. /** 
  40. * Header for each change block. 
  41. * @var string 
  42. */ 
  43. var $_block_header = ''; 
  44.  
  45. /** 
  46. * Whether to split down to character-level. 
  47. * @var boolean 
  48. */ 
  49. var $_split_characters = false; 
  50.  
  51. /** 
  52. * What are we currently splitting on? Used to recurse to show word-level 
  53. * or character-level changes. 
  54. * @var string 
  55. */ 
  56. var $_split_level = 'lines'; 
  57.  
  58. function _blockHeader($xbeg, $xlen, $ybeg, $ylen) 
  59. return $this->_block_header; 
  60.  
  61. function _startBlock($header) 
  62. return $header; 
  63.  
  64. function _lines($lines, $prefix = ' ', $encode = true) 
  65. if ($encode) { 
  66. array_walk($lines, array(&$this, '_encode')); 
  67.  
  68. if ($this->_split_level == 'lines') { 
  69. return implode("\n", $lines) . "\n"; 
  70. } else { 
  71. return implode('', $lines); 
  72.  
  73. function _added($lines) 
  74. array_walk($lines, array(&$this, '_encode')); 
  75. $lines[0] = $this->_ins_prefix . $lines[0]; 
  76. $lines[count($lines) - 1] .= $this->_ins_suffix; 
  77. return $this->_lines($lines, ' ', false); 
  78.  
  79. function _deleted($lines, $words = false) 
  80. array_walk($lines, array(&$this, '_encode')); 
  81. $lines[0] = $this->_del_prefix . $lines[0]; 
  82. $lines[count($lines) - 1] .= $this->_del_suffix; 
  83. return $this->_lines($lines, ' ', false); 
  84.  
  85. function _changed($orig, $final) 
  86. /** If we've already split on characters, just display. */ 
  87. if ($this->_split_level == 'characters') { 
  88. return $this->_deleted($orig) 
  89. . $this->_added($final); 
  90.  
  91. /** If we've already split on words, just display. */ 
  92. if ($this->_split_level == 'words') { 
  93. $prefix = ''; 
  94. while ($orig[0] !== false && $final[0] !== false && 
  95. substr($orig[0], 0, 1) == ' ' && 
  96. substr($final[0], 0, 1) == ' ') { 
  97. $prefix .= substr($orig[0], 0, 1); 
  98. $orig[0] = substr($orig[0], 1); 
  99. $final[0] = substr($final[0], 1); 
  100. return $prefix . $this->_deleted($orig) . $this->_added($final); 
  101.  
  102. $text1 = implode("\n", $orig); 
  103. $text2 = implode("\n", $final); 
  104.  
  105. /** Non-printing newline marker. */ 
  106. $nl = "\0"; 
  107.  
  108. if ($this->_split_characters) { 
  109. $diff = new Text_Diff('native',  
  110. array(preg_split('//', $text1),  
  111. preg_split('//', $text2))); 
  112. } else { 
  113. /** We want to split on word boundaries, but we need to preserve 
  114. * whitespace as well. Therefore we split on words, but include 
  115. * all blocks of whitespace in the wordlist. */ 
  116. $diff = new Text_Diff('native',  
  117. array($this->_splitOnWords($text1, $nl),  
  118. $this->_splitOnWords($text2, $nl))); 
  119.  
  120. /** Get the diff in inline format. */ 
  121. $renderer = new Text_Diff_Renderer_inline 
  122. (array_merge($this->getParams(),  
  123. array('split_level' => $this->_split_characters ? 'characters' : 'words'))); 
  124.  
  125. /** Run the diff and get the output. */ 
  126. return str_replace($nl, "\n", $renderer->render($diff)) . "\n"; 
  127.  
  128. function _splitOnWords($string, $newlineEscape = "\n") 
  129. // Ignore \0; otherwise the while loop will never finish. 
  130. $string = str_replace("\0", '', $string); 
  131.  
  132. $words = array(); 
  133. $length = strlen($string); 
  134. $pos = 0; 
  135.  
  136. while ($pos < $length) { 
  137. // Eat a word with any preceding whitespace. 
  138. $spaces = strspn(substr($string, $pos), " \n"); 
  139. $nextpos = strcspn(substr($string, $pos + $spaces), " \n"); 
  140. $words[] = str_replace("\n", $newlineEscape, substr($string, $pos, $spaces + $nextpos)); 
  141. $pos += $spaces + $nextpos; 
  142.  
  143. return $words; 
  144.  
  145. function _encode(&$string) 
  146. $string = htmlspecialchars($string); 
  147.