You are here

function menu_breadcrumb_html_id in Menu Breadcrumb 7

Same name and namespace in other branches
  1. 6 menu_breadcrumb.module \menu_breadcrumb_html_id()

Prepare a string for use as a valid HTML ID and guarantee uniqueness.

This is adapted from Drupal 7's drupal_html_id().

Parameters

$id: The ID to clean.

Return value

string The cleaned ID.

1 call to menu_breadcrumb_html_id()
menu_breadcrumb_admin_settings_form in ./menu_breadcrumb.module
Menu breadcrumb admin settings form.

File

./menu_breadcrumb.module, line 644
The main file for the menu_breadcrumb module.

Code

function menu_breadcrumb_html_id($id) {
  static $seen_ids = array();
  $id = strtr(drupal_strtolower($id), array(
    ' ' => '-',
    '_' => '-',
    '[' => '-',
    ']' => '',
  ));

  // As defined in http://www.w3.org/TR/html4/types.html#type-name, HTML IDs can
  // only contain letters, digits ([0-9]), hyphens ("-"), underscores ("_"),
  // colons (":"), and periods ("."). We strip out any character not in that
  // list. Note that the CSS spec doesn't allow colons or periods in identifiers
  // (http://www.w3.org/TR/CSS21/syndata.html#characters), so we strip those two
  // characters as well.
  $id = preg_replace('/[^A-Za-z0-9\\-_]/', '', $id);

  // Ensure IDs are unique. The first occurrence is held but left alone.
  // Subsequent occurrences get a number appended to them. This incrementing
  // will almost certainly break code that relies on explicit HTML IDs in forms
  // that appear more than once on the page, but the alternative is outputting
  // duplicate IDs, which would break JS code and XHTML validity anyways. For
  // now, it's an acceptable stopgap solution.
  if (isset($seen_ids[$id])) {
    $id = $id . '-' . ++$seen_ids[$id];
  }
  else {
    $seen_ids[$id] = 1;
  }
  return $id;
}