diff --git a/includes/common.inc b/includes/common.inc
index d0649d7..0af4f0d 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -2269,6 +2269,33 @@ function drupal_attributes(array $attributes = array()) {
 }
 
 /**
+ * Generates either an HTML element open tag or a complete element.
+ *
+ * @param $name
+ *   The string tag name.
+ * @param $attributes
+ *   An associative array of key-value pairs to be converted to attributes.
+ * @param $content
+ *   If boolean false (default), generate only the open tag.
+ *   If boolean true, generate an XML-shorthand empty element.
+ *   If a string, use that as the contents of the element and generate the close tag.
+ *
+ * @return
+ *   The string of the complete open tag or complete element with open tag, content, and close tag.
+ */
+function drupal_element($name, array $attributes = array(), $content = FALSE) {
+  $output = '<' . $name . drupal_attributes($attributes);
+  if ($content === TRUE) {
+    $output .= ' /';
+  }
+  elseif (is_string($content)) {
+    $output .= '>' . $content . '</' . $name;
+  }
+  $output .= '>';
+  return $output;
+}
+
+/**
  * Formats an internal or external URL link as an HTML anchor tag.
  *
  * This function correctly handles aliased paths, and adds an 'active' class
diff --git a/includes/theme.inc b/includes/theme.inc
index 3ae5000..e797e04 100644
--- a/includes/theme.inc
+++ b/includes/theme.inc
@@ -1437,7 +1437,7 @@ function theme_links($variables) {
       $output .= '>' . check_plain($heading['text']) . '</' . $heading['level'] . '>';
     }
 
-    $output .= '<ul' . drupal_attributes($attributes) . '>';
+    $output .= drupal_element('ul', $attributes);
 
     $num_links = count($links);
     $i = 1;
@@ -1456,7 +1456,7 @@ function theme_links($variables) {
           && (empty($link['language']) || $link['language']->language == $language_url->language)) {
         $class[] = 'active';
       }
-      $output .= '<li' . drupal_attributes(array('class' => $class)) . '>';
+      $output .= drupal_element('li', array('class' => $class));
 
       if (isset($link['href'])) {
         // Pass in $link as $options, they share the same keys.
@@ -1467,11 +1467,10 @@ function theme_links($variables) {
         if (empty($link['html'])) {
           $link['title'] = check_plain($link['title']);
         }
-        $span_attributes = '';
-        if (isset($link['attributes'])) {
-          $span_attributes = drupal_attributes($link['attributes']);
+        if (!isset($link['attributes'])) {
+          $link['attributes'] = array();
         }
-        $output .= '<span' . $span_attributes . '>' . $link['title'] . '</span>';
+        $output .= drupal_element('span', $link['attributes'], $link['title']);
       }
 
       $i++;
@@ -1518,7 +1517,7 @@ function theme_image($variables) {
     }
   }
 
-  return '<img' . drupal_attributes($attributes) . ' />';
+  return drupal_element('img', $attributes, true);
 }
 
 /**
@@ -1633,7 +1632,7 @@ function theme_table($variables) {
     $attributes['class'][] = 'sticky-enabled';
   }
 
-  $output = '<table' . drupal_attributes($attributes) . ">\n";
+  $output = drupal_element('table', $attributes) . "\n";
 
   if (isset($caption)) {
     $output .= '<caption>' . $caption . "</caption>\n";
@@ -1661,15 +1660,15 @@ function theme_table($variables) {
 
       // Build colgroup
       if (is_array($cols) && count($cols)) {
-        $output .= ' <colgroup' . drupal_attributes($attributes) . '>';
+        $output .= ' ' . drupal_element('colgroup', $attributes);
         $i = 0;
         foreach ($cols as $col) {
-          $output .= ' <col' . drupal_attributes($col) . ' />';
+          $output .= ' ' . drupal_element('col', $col, true);
         }
         $output .= " </colgroup>\n";
       }
       else {
-        $output .= ' <colgroup' . drupal_attributes($attributes) . " />\n";
+        $output .= ' ' . drupal_element('colgroup', $attributes, true) . "\n";
       }
     }
   }
@@ -1735,7 +1734,7 @@ function theme_table($variables) {
         }
 
         // Build row
-        $output .= ' <tr' . drupal_attributes($attributes) . '>';
+        $output .= ' ' . drupal_element('tr', $attributes);
         $i = 0;
         foreach ($cells as $cell) {
           $cell = tablesort_cell($cell, $header, $ts, $i++);
@@ -1815,7 +1814,7 @@ function theme_item_list($variables) {
   }
 
   if (!empty($items)) {
-    $output .= "<$type" . drupal_attributes($attributes) . '>';
+    $output .= drupal_element($type, $attributes);
     $num_items = count($items);
     foreach ($items as $i => $item) {
       $attributes = array();
@@ -1846,7 +1845,7 @@ function theme_item_list($variables) {
       if ($i == $num_items - 1) {
         $attributes['class'][] = 'last';
       }
-      $output .= '<li' . drupal_attributes($attributes) . '>' . $data . "</li>\n";
+      $output .= drupal_element('li', $attributes, $data) . "\n";
     }
     $output .= "</$type>";
   }
@@ -1902,10 +1901,10 @@ function theme_feed_icon($variables) {
 function theme_html_tag($variables) {
   $element = $variables['element'];
   if (!isset($element['#value'])) {
-    return '<' . $element['#tag'] . drupal_attributes($element['#attributes']) . " />\n";
+    return drupal_element($element['#tag'], $element['#attributes'], true) . "\n";
   }
   else {
-    $output = '<' . $element['#tag'] . drupal_attributes($element['#attributes']) . '>';
+    $output = drupal_element($element['#tag'], $element['#attributes']);
     if (isset($element['#value_prefix'])) {
       $output .= $element['#value_prefix'];
     }
@@ -1959,7 +1958,7 @@ function theme_username($variables) {
     // Modules may have added important attributes so they must be included
     // in the output. Additional classes may be added as array elements like
     // $variables['attributes_array']['class'][] = 'myclass';
-    $output = '<span' . drupal_attributes($variables['attributes_array']) . '>' . $variables['name'] . $variables['extra'] . '</span>';
+    $output = drupal_element('span', $variables['attributes_array'], $variables['name'] . $variables['extra']);
   }
   return $output;
 }
