Index: collapse_text.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/collapse_text/collapse_text.module,v
retrieving revision 1.4.2.3
diff -u -r1.4.2.3 collapse_text.module
--- collapse_text.module	15 Dec 2008 15:47:33 -0000	1.4.2.3
+++ collapse_text.module	8 Dec 2009 01:51:22 -0000
@@ -66,6 +66,8 @@
       \[                         # look for an opening bracket
          collapse                # followed by the word `collapse`
          (\ collapsed)?          # followed by (optionally) a space and the word `collapsed` (captured)
+         (?:\ style=([^\] ]*))?  # followed by (optionally) a space and a style, consisting of any
+                                 # characters except a close bracket (captured)
          (?:\ title=([^\]]*))?   # followed by (optionally) a space and a title, consisting of any
                                  # characters except a close bracket (captured)
       \]                         # followed by a closing bracket
@@ -77,11 +79,14 @@
 }
 
 function _collapse_text_replace_callback($matches) {
+  global $base_url;
+
   // 2008-12-15 REMorse (no issue number) added space to make
   // $collapsed work
   $collapsed = ($matches[1] == ' collapsed');
-  $title = trim($matches[2]);
-  $interior = $matches[3];
+  $style = trim($matches[2]);
+  $title = trim($matches[3]);
+  $interior = $matches[4];
   
   if (empty($title)) {
     // If a title is not supplied, look for a header (<h1>, <h2> ...)
@@ -97,18 +102,30 @@
     $title = t('Use the arrow to expand or collapse this section');
   }
   
-  $render_array = array(
+  $form = array(
+    '#prefix' => '<form action="' . $base_url . '/">',
+    '#suffix' => '</form>',
+    '#theme' => 'collapse_text_fieldset',
+  );
+  $form['fieldset'] = array(
     '#type' => 'fieldset',
     '#title' => $title,
     '#collapsible' => true,
     '#collapsed' => $collapsed,
   );
-  $render_array['text_contents'] = array(
+  if (!empty($style)) {
+    $form['fieldset']['#attributes'] = array(
+      'class' => collapse_text_id_safe($style),
+    );
+  }
+  $form['fieldset']['text_contents'] = array(
     '#type' => 'markup',
-    '#value' => '<div>' . $interior . '</div>',
+    '#prefix' => '<div class="collapse-text">',
+    '#value' => $interior,
+    '#suffix' => '</div>',
   );
-  
-  return drupal_render($render_array);
+
+  return drupal_render($form);
 }
 
 /**
@@ -125,3 +142,57 @@
 function collapse_text_init() {
   drupal_add_js('misc/collapse.js', 'core');
 }
+
+/**
+ * Implementation of hook_theme().
+ */
+function collapse_text_theme($existing, $type, $theme, $path) {
+  return array(
+    'collapse_text_fieldset' => array(
+      'arguments' => array('element'),
+    ),
+  );
+}
+
+/**
+ * Theme a section of collapsible text. By default, this function calls the
+ * default 'theme_fieldset' implementation, but this function can be overridden
+ * to implement a custom theme just for collapsed text.
+ *
+ * @param $element
+ *   An associative array containing the properties of the element.
+ *   Properties used: attributes, title, value, description, children, collapsible, collapsed
+ * @return
+ *   A themed HTML string representing the collapsed text.
+ *
+ * @ingroup themeable
+ */
+function theme_collapse_text_fieldset($element) {
+  return drupal_render($element);
+}
+
+/**
+ * Converts a string to a suitable html ID attribute.
+ * Copied from zen_id_safe() in the Zen theme.
+ *
+ * http://www.w3.org/TR/html4/struct/global.html#h-7.5.2 specifies what makes a
+ * valid ID attribute in HTML. This function:
+ *
+ * - Ensure an ID starts with an alpha character by optionally adding an 'id'.
+ * - Replaces any character except alphanumeric characters with dashes.
+ * - Converts entire string to lowercase.
+ *
+ * @param $string
+ *   The string
+ * @return
+ *   The converted string
+ */
+function collapse_text_id_safe($string) {
+  // Replace with dashes anything that isn't A-Z, numbers, dashes, or underscores.
+  $string = strtolower(preg_replace('/[^a-zA-Z0-9-]+/', '-', $string));
+  // If the first character is not a-z, add 'id' in front.
+  if (!ctype_lower($string{0})) { // Don't use ctype_alpha since its locale aware.
+    $string = 'id' . $string;
+  }
+  return $string;
+}
