diff --git a/includes/common.inc b/includes/common.inc index b6ea297..6bab69c 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -608,16 +608,43 @@ function drupal_parse_url($url) { /** * Encodes a Drupal path for use in a URL. * - * For aesthetic reasons slashes are not escaped. + * For aesthetic reasons slashes are not escaped. Additional characters can be + * passed as an (optional) array. * * Note that url() takes care of calling this function, so a path passed to that * function should not be encoded in advance. * + * The example use case for optional unencoded characters is redirects using + * drupal_goto() to taxonomy pages which are using commas (,) and pluses (+) as + * the operators in the URL. + * + * For example: + * @code + * $path = 'taxonomy/term/123,456'; + * $preserve = array('+', ','); + * $default = drupal_encode_path($path); + * $preserved = drupal_encode_path($path, $preserve); + * // $default now contains string "taxonomy/term/123%2C456" + * // $preserved now contains string "taxonomy/term/123,456" + * @endcode + * * @param $path * The Drupal path to encode. + * + * @param $no_encode_chars + * An optional array of additional characters which should be left unencoded. + * Note that slash (/) will be always unencoded. + * + * @return + * The encoded Drupal path. + * */ -function drupal_encode_path($path) { - return str_replace('%2F', '/', rawurlencode($path)); +function drupal_encode_path($path, $no_encode_chars = array()) { + $path = str_replace('%2F', '/', rawurlencode($path)); + if (!empty($no_encode_chars)) { + $path = str_replace(array_map('rawurlencode', $no_encode_chars), $no_encode_chars, $path); + } + return $path; } /** @@ -2059,6 +2086,8 @@ function format_username($account) { * url() is invoked by entity_uri(). * - 'entity': The entity object (such as a node) for which the URL is being * generated. Only set if url() is invoked by entity_uri(). + * - 'no_encode_chars': An array of characters which should not be encoded if + * the 'path' is going to be encoded using drupal_encode_path(). * * @return * A string containing a URL to the given path. @@ -2070,7 +2099,8 @@ function url($path = NULL, array $options = array()) { 'query' => array(), 'absolute' => FALSE, 'alias' => FALSE, - 'prefix' => '' + 'prefix' => '', + 'no_encode_chars' => array(), ); if (!isset($options['external'])) { @@ -2154,7 +2184,7 @@ function url($path = NULL, array $options = array()) { // With Clean URLs. if (!empty($GLOBALS['conf']['clean_url'])) { - $path = drupal_encode_path($prefix . $path); + $path = drupal_encode_path($prefix . $path, $options['no_encode_chars']); if ($options['query']) { return $base . $path . '?' . drupal_http_build_query($options['query']) . $options['fragment']; }