/ and & problems

wrunt - September 6, 2006 - 06:44
Project:Active Select
Version:5.x-1.0
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:active
Description

If one of the options you select in an activeselector contains an & then the callback function doesn't receive the full option string. It is encoded as %26, but this doesn't seem to help. The following change fixes the problem for me by doubly encoding the & as %2526. The problem may be related to clean urls, which I have enabled.

-      var optionString = this.input.options[i].value.replace(/\|/g, '|') +'|'+ this.input.options[i].text.replace(/\|/g, '|');
+      var optionString = encodeURIComponent(this.input.options[i].value.replace(/\|/g, '|') +'|'+ this.input.options[i].text.replace(/\|/g, '|'));

#1

wrunt - September 7, 2006 - 06:18
Title:& character in sources» / and & problems

I've got a fix that works for sources with both & and / characters in them, but it only works if you have clean urls enabled. It seems that the rewrite rules for clean urls do some decoding of escaped characters, so you need to encode them twice, then decode them once in activeselect_explode_values.

Here's the fix:

--- tmp/activeselect/activeselect.js    2006-04-15 03:02:40.000000000 +1000
+++ activeselect/activeselect.js        2006-09-07 16:14:02.000000000 +1000
@@ -72,7 +72,7 @@ function jsAS(input, db, targets, extra)
   var maxWidth = 0;
   for (var i = 0; i < this.input.options.length; i++) {
     if (this.input.options[i].selected) {
-      var optionString = this.input.options[i].value.replace(/\|/g, '|') +'|'+ this.input.options[i].text.replace(/\|/g, '|');
+      var optionString = encodeURIComponent(encodeURIComponent(this.input.options[i].value.replace(/\|/g, '|') +'|'+ this.input.options[i].text.replace(/\|/g, '|')));
       selectedOptions.push(optionString);
     }
     if (this.input.options[i].text.length > maxWidth) {

--- tmp/activeselect/activeselect.module        2006-04-17 19:01:31.000000000 +1000
+++ activeselect/activeselect.module    2006-09-07 16:13:50.000000000 +1000
@@ -39,7 +39,7 @@ function activeselect_elements() {
  *   An array of values, in the form $key => $value.
  */
function activeselect_explode_values($string) {
-  $array = explode('||', $string);
+  $array = explode('||', urldecode($string));
   foreach ($array as $key => $value) {
     $match = explode('|', $value);
     $array[$match[0]] = html_entity_decode($match[1]);

#2

wrunt - September 25, 2006 - 00:44

It looks like there's already a function to work around these issues:

<?php
/**
* Wrapper around urlencode() which avoids Apache quirks.
*
* Should be used when placing arbitrary data in an URL. Note that Drupal paths
* are urlencoded() when passed through url() and do not require urlencoding()
* of individual components.
*
* Notes:
* - For esthetic reasons, we do not escape slashes. This also avoids a 'feature'
*   in Apache where it 404s on any path containing '%2F'.
* - mod_rewrite's unescapes %-encoded ampersands and hashes when clean URLs
*   are used, which are interpreted as delimiters by PHP. These characters are
*   double escaped so PHP will still see the encoded version.
*
* @param $text
*   String to encode
*/
function drupal_urlencode($text) {
  if (
variable_get('clean_url', '0')) {
    return
str_replace(array('%2F', '%26', '%23'),
                       array(
'/', '%2526', '%2523'),
                      
urlencode($text));
  }
  else {
    return
str_replace('%2F', '/', urlencode($text));
  }
}
?>

#3

geodaniel - February 2, 2007 - 14:24

subscribing

#4

nterbogt - June 25, 2007 - 06:23
Version:4.7.x-1.x-dev» 5.x-1.0

This is still an issue for me too.
Does anyone have any thoughts about doing the encoding at a PHP level instead of at the javascript level when returning a value?

 
 

Drupal is a registered trademark of Dries Buytaert.