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 1/3] 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


From 6f34b173f6fffcc66cd680cf483b9c37e184d592 Mon Sep 17 00:00:00 2001
From: Peter <pdrake@241computers.com>
Date: Mon, 14 Mar 2011 09:36:06 -0400
Subject: [PATCH 2/3] Fixed formatting.

---
 table_altrow.module |   74 +++++++++++++++++++++++++-------------------------
 1 files changed, 37 insertions(+), 37 deletions(-)

diff --git a/table_altrow.module b/table_altrow.module
index 29f563d..d9f036b 100644
--- a/table_altrow.module
+++ b/table_altrow.module
@@ -69,48 +69,48 @@ function table_altrow_filter($op, $delta = 0, $format = -1, $text = '') {
 }
 
 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)) {
+  $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;
+    // +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);
+    // 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 = '';
-      	  // 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++;
+          $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;
+  }
+  return $offset;
 }
 
 /**
-- 
1.7.2.3


From 8f4f9d90dcf8f83abbf9cc71139f9ea0c980427a Mon Sep 17 00:00:00 2001
From: Peter <pdrake@241computers.com>
Date: Mon, 14 Mar 2011 09:38:06 -0400
Subject: [PATCH 3/3] Fixed formatting.

---
 table_altrow.module |   22 ++++++++++++----------
 1 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/table_altrow.module b/table_altrow.module
index d9f036b..229259f 100644
--- a/table_altrow.module
+++ b/table_altrow.module
@@ -13,8 +13,7 @@ function table_altrow_filter_tips($delta, $format, $long = FALSE) {
     case 0:
       if ($long) {
         return t('Tables will be rendered with different styles for even and odd rows if supported.');
-      }
-      else {
+      } else {
         return t('Tables will be rendered with different styles for even and odd rows if supported.');
       }
       break;
@@ -30,7 +29,7 @@ function table_altrow_filter($op, $delta = 0, $format = -1, $text = '') {
   // the returned name should be passed through t() for translation.
   if ($op == 'list') {
     return array(
-    0 => t('Table alternate row filter'),
+      0 => t('Table alternate row filter'),
     );
   }
 
@@ -46,12 +45,14 @@ function table_altrow_filter($op, $delta = 0, $format = -1, $text = '') {
         // filter tips which are shown in the content editing interface.
         case 'description':
           return t('Adds required attributes to an HTML tag.');
-
+          break;
+          
           // We don't need the "prepare" operation for this filter, but it's required
           // to at least return the input text as-is.
         case 'prepare':
           return $text;
-
+          break;
+          
           // The actual filtering is performed here. The supplied text should be
           // returned, once any necessary substitutions have taken place.
         case 'process':
@@ -63,6 +64,7 @@ function table_altrow_filter($op, $delta = 0, $format = -1, $text = '') {
             $offset = table_altrow_match_tr($text, $matches);
           }
           return $text;
+          break;
       }
       break;
   }
@@ -98,11 +100,11 @@ function table_altrow_match_tr(&$text, $matches) {
        $new_classes .= $matches[4][0].' ';
       }
       if ($count == 1) {
-          $new_classes .= 'first odd';
+        $new_classes .= 'first odd';
       } elseif(($count % 2) == 0) {
-          $new_classes .= 'even';
+        $new_classes .= 'even';
       } else {
-          $new_classes .= 'odd';
+        $new_classes .= 'odd';
       }
       $new_tag = '<tr '.$matches[2][0].' class="'.$new_classes.'">';
       $last_row = array('match' => $new_tag, 'offset' => $offset - 1, 'classes' => $new_classes);
@@ -127,8 +129,8 @@ function table_altrow_match_tr(&$text, $matches) {
  * @return string
  */
 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);
+  if ($count == null) { $count = 0 };
+  if ($offset == null) { $offset = strpos($haystack, $needle) };
   $rpl_count = 0;
   while (($offset !== false) && ($rpl_count < $count)) {
     $haystack = substr_replace($haystack, $replace, $offset, strlen($needle));
-- 
1.7.2.3

