From e75a9a77e833d5defeded5c4ffdf7de5b8a21341 Mon Sep 17 00:00:00 2001
From: Peter <pdrake@241computers.com>
Date: Mon, 14 Mar 2011 09:26:19 -0400
Subject: [PATCH] Enable recursive row matching and account for certain TR tag attributes.

---
 table_altrow.module |   77 ++++++++++++++++++++++++++++++++------------------
 1 files changed, 49 insertions(+), 28 deletions(-)

diff --git a/table_altrow.module b/table_altrow.module
index 0242263..29f563d 100644
--- a/table_altrow.module
+++ b/table_altrow.module
@@ -1,4 +1,5 @@
 <?php
+// $Id: table_altrow.module,v 1.1.2.2 2008/02/17 18:59:11 deviantintegral Exp $
 /**
  * table_altrow.module
  * Insert even and odd classes for tables via input filters to allow for proper
@@ -58,40 +59,60 @@ function table_altrow_filter($op, $delta = 0, $format = -1, $text = '') {
           $matches = array();
           $offset = 0;
           // Find a tbody
-          while(preg_match('!(<tbody ?[^>]*>)!', $text, $matches, PREG_OFFSET_CAPTURE, $offset)) {
-            $offset = $matches[0][1];
-            $count = 1;
-            // While the tbody is still open
-            while(preg_match('!(<tr( ?[^>]*)>)|(</tbody>)!', $text, $matches, PREG_OFFSET_CAPTURE, $offset)) {
-              // +1 so we don't match the same string
-              $offset = $matches[0][1] + 1;
-              
-              // Don't process tr's until we find a tbody.
-              if($matches[0][0] == '</tbody>') {
-                break;
-              }
-              
-              // Don't replace existing classes. Perhaps this should append a class instead?
-              if(!strstr($matches[2][0], 'class=')) {
-                if(($count % 2) == 0) {
-                  $new_tag = '<tr class="even"' . $matches[2][0] . '>';
-                  $text = str_replace_count($matches[0][0], $new_tag, $text, $offset - 1, 1);
-                }
-                else {
-                  $new_tag = '<tr class="odd"' . $matches[2][0] . '>';
-                  $text = str_replace_count($matches[0][0], $new_tag, $text, $offset - 1, 1);
-                }
-              }
-              $count++;
-            }
+          while(preg_match('!(<tbody ?.*>)!', $text, $matches, PREG_OFFSET_CAPTURE, $offset)) {
+            $offset = table_altrow_match_tr($text, $matches);
           }
-
           return $text;
       }
       break;
   }
 }
 
+function table_altrow_match_tr(&$text, $matches) {
+	$offset = $matches[0][1] + 1;
+    $count = 1;
+    // While the tbody is still open
+    while(preg_match('!(<tr(\s*)?(class=\"([a-zA-Z]+)\")?>)|(</tbody>)|(<tbody ?.*>)!', $text, $matches, PREG_OFFSET_CAPTURE, $offset)) {
+
+      // +1 so we don't match the same string
+      $offset = $matches[0][1] + 1;
+      
+      // return processing of tr's when we find a /tbody.
+      if($matches[0][0] == '</tbody>') {
+      	if (is_array($last_row)) {
+      		$new_tag = '<tr class="'.$last_row['classes'].' last">';
+      		$text = table_altrow_str_replace_count($last_row['match'], $new_tag, $text, $last_row['offset'], 1);
+      		$diff = strlen($new_tag) - strlen($last_row['match']);
+      	} else {
+      		$diff = 0;
+      	}
+        return $offset + $diff;
+
+      // Begin a new process when we find a tbody
+      } elseif ($matches[0][0] == '<tbody>') {
+      	$offset = table_altrow_match_tr($text, $matches);
+      } else {
+      	  $new_classes = '';
+      	  // Don't replace existing classes
+	      if(strpos($matches[3][0], 'class="') !== FALSE) {
+	       $new_classes .= $matches[4][0].' ';
+	      }
+	      if ($count == 1) {
+	      	  $new_classes .= 'first odd';
+	      } elseif(($count % 2) == 0) {
+	          $new_classes .= 'even';
+	      } else {
+	          $new_classes .= 'odd';
+	      }
+	      $new_tag = '<tr '.$matches[2][0].' class="'.$new_classes.'">';
+	      $last_row = array('match' => $new_tag, 'offset' => $offset - 1, 'classes' => $new_classes);
+	      $text = table_altrow_str_replace_count($matches[0][0], $new_tag, $text, $offset - 1, 1);
+	      $count++;
+      }
+    }
+    return $offset;
+}
+
 /**
  * Replace every instance of a string with a count parameter like PHP5.
  * This can probably be removed with Drupal goes to PHP5 only.
@@ -105,7 +126,7 @@ function table_altrow_filter($op, $delta = 0, $format = -1, $text = '') {
  * @param boolean $replace_first
  * @return string
  */
-function str_replace_count($needle, $replace, $haystack, $offset = null, $count = null) {
+function table_altrow_str_replace_count($needle, $replace, $haystack, $offset = null, $count = null) {
   if ($count == null) $count = 0;
   if ($offset == null) $offset = strpos($haystack, $needle);
   $rpl_count = 0;
-- 
1.7.2.3

