Skip to content
This repository
Browse code

BUGFIX Fixed tag stacking in Diff.php thirdparty lib (AIR-71)

  • Loading branch information...
commit 3a5b3af7c697235ace7ce5c66df074e9368199a2 1 parent 03db289
Ingo Schommer authored September 12, 2011
2  core/Diff.php
@@ -749,7 +749,7 @@ static function compareHTML($from, $to, $escape = false) {
749 749
 						if($tagStack[$listName]) $rechunked[$listName][sizeof($rechunked[$listName])-1] .= ' ' . $item;
750 750
 						else $rechunked[$listName][] = $item;
751 751
 	
752  
-						if($lookForTag && isset($item[0]) && $item[0] == "<" && substr($item,0,2) != "</") {
  752
+						if($lookForTag && !$tagStack[$listName] && isset($item[0]) && $item[0] == "<" && substr($item,0,2) != "</") { 
753 753
 							$tagStack[$listName] = 1;
754 754
 						} else if($tagStack[$listName]) {
755 755
 							if(substr($item,0,2) == "</") $tagStack[$listName]--;
56  tests/core/DiffTest.php
... ...
@@ -0,0 +1,56 @@
  1
+<?php
  2
+/**
  3
+ * @package cms
  4
+ * @subpackage tests
  5
+ */
  6
+
  7
+class DiffTest extends SapphireTest {
  8
+	
  9
+	/**
  10
+	 * @see https://groups.google.com/forum/#!topic/silverstripe-dev/yHcluCvuszo
  11
+	 */
  12
+	function testTableDiff() {
  13
+		if(!class_exists('DOMDocument')) {
  14
+			$this->markTestSkipped('"DOMDocument" required');
  15
+			return;
  16
+		}
  17
+		
  18
+		$from = "<table> 
  19
+		<tbody> 
  20
+			<tr class=\"blah\"> 
  21
+				<td colspan=\"2\">Row 1</td> 
  22
+			</tr> 
  23
+			<tr class=\"foo\"> 
  24
+				<td>Row 2</td> 
  25
+				<td>Row 2</td> 
  26
+			</tr> 
  27
+			<tr> 
  28
+				<td>Row 3</td> 
  29
+				<td>Row 3</td> 
  30
+			</tr> 
  31
+			</tbody> 
  32
+		</table>";
  33
+
  34
+		$to = "<table class=\"new-class\"> 
  35
+		<tbody> 
  36
+			<tr class=\"blah\"> 
  37
+				<td colspan=\"2\">Row 1</td> 
  38
+			</tr> 
  39
+			<tr class=\"foo\"> 
  40
+				<td>Row 2</td> 
  41
+				<td>Row 2</td> 
  42
+			</tr> 
  43
+		</tbody> 
  44
+		</table>";
  45
+		
  46
+		$expected = "<ins>" . $to . "</ins>" . "<del>" . $from . "</del>";
  47
+		$compare = Diff::compareHTML($from, $to);
  48
+		
  49
+		// Very hard to debug this way, wouldn't need to do this if PHP had an *actual* DOM parsing lib,
  50
+		// and not just the poor excuse that is DOMDocument
  51
+		$compare = preg_replace('/[\s\t\n\r]*/', '', $compare);
  52
+		$expected = preg_replace('/[\s\t\n\r]*/', '', $expected);
  53
+		
  54
+		$this->assertEquals($compare, $expected);
  55
+	}
  56
+}

0 notes on commit 3a5b3af

Please sign in to comment.
Something went wrong with that request. Please try again.