From 2ec51c420491441bfbe67cd4afaa94572ddf7906 Mon Sep 17 00:00:00 2001
From: Bob Vincent <bobvin@pillars.net>
Date: Mon, 17 Oct 2011 17:27:17 -0400
Subject: [PATCH] Issue #93854 by moonray, becw, Dave Reid, pounard,
 das-peter, Steven Jones, c960657: Allow autocompletion to
 contain slashes.

---
 includes/form.inc                |    3 ++-
 misc/autocomplete.js             |    5 +++--
 modules/taxonomy/taxonomy.module |    6 ++++--
 modules/taxonomy/taxonomy.test   |   25 +++++++++++++++++++++++++
 4 files changed, 34 insertions(+), 5 deletions(-)

diff --git a/includes/form.inc b/includes/form.inc
index 8c3b14ab4b2329035b4e3a5114d62f69e20c1f3c..ed9f5e8febe3301b3dc17af2627c33fae025a87f 100644
--- a/includes/form.inc
+++ b/includes/form.inc
@@ -3686,7 +3686,8 @@ function theme_textfield($variables) {
   _form_set_class($element, array('form-text'));
 
   $extra = '';
-  if ($element['#autocomplete_path'] && drupal_valid_path($element['#autocomplete_path'])) {
+  // We append '/placeholder' to represent an autocomplete search term.
+  if ($element['#autocomplete_path'] && drupal_valid_path($element['#autocomplete_path'] . '/placeholder')) {
     drupal_add_library('system', 'drupal.autocomplete');
     $element['#attributes']['class'][] = 'form-autocomplete';
 
diff --git a/misc/autocomplete.js b/misc/autocomplete.js
index 5e85be44fc5f372e1d4a6ccc4cef2454e3be7376..f91d58e0118a6a2e7adf1b14c90b9aa91240624f 100644
--- a/misc/autocomplete.js
+++ b/misc/autocomplete.js
@@ -287,10 +287,11 @@ Drupal.ACDB.prototype.search = function (searchString) {
   this.timer = setTimeout(function () {
     db.owner.setStatus('begin');
 
-    // Ajax GET request for autocompletion.
+    // Ajax GET request for autocompletion. We use Drupal.encodePath instead of
+    // encodeURIComponent to allow autocomplete search terms to contain slashes.
     $.ajax({
       type: 'GET',
-      url: db.uri + '/' + encodeURIComponent(searchString),
+      url: db.uri + '/' + Drupal.encodePath(searchString),
       dataType: 'json',
       success: function (matches) {
         if (typeof matches.status == 'undefined' || matches.status != 0) {
diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module
index 7921b797cce5150e60562c2f8474bcf30e67be84..ce3ece29391a7975b714f722334c47eb42eece11 100644
--- a/modules/taxonomy/taxonomy.module
+++ b/modules/taxonomy/taxonomy.module
@@ -312,14 +312,16 @@ function taxonomy_menu() {
     'type' => MENU_CALLBACK,
     'file' => 'taxonomy.pages.inc',
   );
-  $items['taxonomy/autocomplete'] = array(
+  // We append /%/%menu_tail to allow autocompletion terms to contain slashes.
+  $items['taxonomy/autocomplete/%/%menu_tail'] = array(
     'title' => 'Autocomplete taxonomy',
     'page callback' => 'taxonomy_autocomplete',
+    'page arguments' => array(2, 3),
     'access arguments' => array('access content'),
     'type' => MENU_CALLBACK,
     'file' => 'taxonomy.pages.inc',
+    'load arguments' => array('%map', '%index'),
   );
-
   $items['admin/structure/taxonomy/%taxonomy_vocabulary_machine_name'] = array(
     'title callback' => 'taxonomy_admin_vocabulary_title_callback',
     'title arguments' => array(3),
diff --git a/modules/taxonomy/taxonomy.test b/modules/taxonomy/taxonomy.test
index 9a89b9c98ff63924d7d592c7408efe70cdb756d4..254cb3e755aeac09feccccb28a233b9d0af0e22d 100644
--- a/modules/taxonomy/taxonomy.test
+++ b/modules/taxonomy/taxonomy.test
@@ -641,6 +641,31 @@ class TaxonomyTermTestCase extends TaxonomyWebTestCase {
   }
 
   /**
+   * Test term autocompletion edge cases.
+   */
+  function testTermAutocompletion() {
+    $base_len = 5;
+    $base = $this->randomName($base_len);
+    // Add a term with a slash in the name.
+    $first_term = $this->createTerm($this->vocabulary);
+    $first_term->name = $base . '/' . $this->randomName();
+    taxonomy_term_save($first_term);
+    // Add another term that differs after the slash character.
+    $search_term = $this->createTerm($this->vocabulary);
+    $search_term->name = $base . '/' . $this->randomName();
+    taxonomy_term_save($search_term);
+
+    // Try to autocomplete a term name that contains a slash.
+    // We should only get a single term returned.
+    $input = substr($search_term->name, 0, $base_len + 3);
+    $url = 'taxonomy/autocomplete/taxonomy_';
+    $url .= $this->vocabulary->machine_name . '/' . $input;
+    $this->drupalGet($url);
+    $target = array($search_term->name => check_plain($search_term->name));
+    $this->assertRaw(drupal_json_encode($target), t('Autocomplete returns term %term_name after typing the first %num letters, including a slash in the name.', array('%term_name' => $search_term->name, '%num' => $base_len + 3)));
+  }
+
+  /**
    * Save, edit and delete a term using the user interface.
    */
   function testTermInterface() {
-- 
1.7.5.4

