diff --git a/core/lib/Drupal/Component/Utility/Tags.php b/core/lib/Drupal/Component/Utility/Tags.php
index 075eaec..db9fca0 100644
--- a/core/lib/Drupal/Component/Utility/Tags.php
+++ b/core/lib/Drupal/Component/Utility/Tags.php
@@ -43,6 +43,22 @@ public static function explode($tags) {
   }
 
   /**
+   * Encodes a tag string, taking care of special cases like commas and quotes.
+   *
+   * @param string $tag
+   *   A tag string.
+   *
+   * @return string
+   *   The encoded string.
+   */
+  public static function encode($tag) {
+    if (strpos($tag, ',') !== FALSE || strpos($tag, '"') !== FALSE) {
+      return '"' . str_replace('"', '""', $tag) . '"';
+    }
+    return $tag;
+  }
+
+  /**
    * Implodes an array of tags into a string.
    *
    * @param array $tags
@@ -54,12 +70,7 @@ public static function explode($tags) {
   public static function implode($tags) {
     $encoded_tags = array();
     foreach ($tags as $tag) {
-      // Commas and quotes in tag names are special cases, so encode them.
-      if (strpos($tag, ',') !== FALSE || strpos($tag, '"') !== FALSE) {
-        $tag = '"' . str_replace('"', '""', $tag) . '"';
-      }
-
-      $encoded_tags[] = $tag;
+      $encoded_tags[] = self::encode($tag);
     }
     return implode(', ', $encoded_tags);
   }
diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/EntityReferenceAutocomplete.php b/core/modules/entity_reference/lib/Drupal/entity_reference/EntityReferenceAutocomplete.php
index e49373d..c1d6114 100644
--- a/core/modules/entity_reference/lib/Drupal/entity_reference/EntityReferenceAutocomplete.php
+++ b/core/modules/entity_reference/lib/Drupal/entity_reference/EntityReferenceAutocomplete.php
@@ -7,6 +7,7 @@
 
 namespace Drupal\entity_reference;
 
+use Drupal\Component\Utility\Tags;
 use Drupal\Core\Entity\EntityManagerInterface;
 use Drupal\entity_reference\Plugin\Type\SelectionPluginManager;
 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
@@ -97,9 +98,7 @@ public function getMatches($field, $instance, $entity_type, $entity_id = '', $pr
           // tags.
           $key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(decode_entities(strip_tags($key)))));
           // Names containing commas or quotes must be wrapped in quotes.
-          if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
-            $key = '"' . str_replace('"', '""', $key) . '"';
-          }
+          $key = Tags::encode($key);
           $matches[] = array('value' => $prefix . $key, 'label' => $label);
         }
       }
diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Field/FieldWidget/AutocompleteWidgetBase.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Field/FieldWidget/AutocompleteWidgetBase.php
index 2ec39ff..bdcf5a7 100644
--- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Field/FieldWidget/AutocompleteWidgetBase.php
+++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Field/FieldWidget/AutocompleteWidgetBase.php
@@ -7,6 +7,7 @@
 
 namespace Drupal\entity_reference\Plugin\Field\FieldWidget;
 
+use Drupal\Component\Utility\Tags;
 use Drupal\Core\Field\FieldItemListInterface;
 use Drupal\Core\Field\WidgetBase;
 use Drupal\user\EntityOwnerInterface;
@@ -130,9 +131,7 @@ protected function getLabels(FieldItemListInterface $items, $delta) {
       $label = $entity_item->label();
       $key = "$label ($entity_id)";
       // Labels containing commas or quotes must be wrapped in quotes.
-      if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
-        $key = '"' . str_replace('"', '""', $key) . '"';
-      }
+      $key = Tags::encode($key);
       $entity_labels[] = $key;
     }
     return $entity_labels;
diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutocompleteTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutocompleteTest.php
index 9895368..987919d 100644
--- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutocompleteTest.php
+++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutocompleteTest.php
@@ -7,6 +7,7 @@
 
 namespace Drupal\entity_reference\Tests;
 
+use Drupal\Component\Utility\Tags;
 use Drupal\entity_reference\EntityReferenceController;
 use Drupal\system\Tests\Entity\EntityUnitTestBase;
 
@@ -101,11 +102,9 @@ function testEntityReferenceAutocompletion() {
     // Try to autocomplete a entity label with both a comma and a slash.
     $input = '"label with, and / t';
     $data = $this->getAutocompleteResult('single', $input);
-    $n = $entity_3->name->value;
+    $n = $entity_3->name->value . ' (3)';
     // Entity labels containing commas or quotes must be wrapped in quotes.
-    if (strpos($entity_3->name->value, ',') !== FALSE || strpos($entity_3->name->value, '"') !== FALSE) {
-      $n = '"' . str_replace('"', '""', $entity_3->name->value) .  ' (3)"';
-    }
+    $n = Tags::encode($n);
     $target = array(
       'value' => $n,
       'label' => check_plain($entity_3->name->value),
diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Controller/TermAutocompleteController.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Controller/TermAutocompleteController.php
index 9d76424..b360883 100644
--- a/core/modules/taxonomy/lib/Drupal/taxonomy/Controller/TermAutocompleteController.php
+++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Controller/TermAutocompleteController.php
@@ -195,11 +195,8 @@ protected function getMatchingTerms($tags_typed, array $vids, $tag_last) {
     if (!empty($tids)) {
       $terms = $this->termStorage->loadMultiple(array_keys($tids));
       foreach ($terms as $term) {
-        $name = $term->label();
         // Term names containing commas or quotes must be wrapped in quotes.
-        if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
-          $name = '"' . str_replace('"', '""', $name) . '"';
-        }
+        $name = Tags::encode($term->label());
         $matches[] = array('value' => $prefix . $name, 'label' => String::checkPlain($term->label()));
       }
       return $matches;
diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php
index 83c42c6..ea316af 100644
--- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php
+++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php
@@ -7,6 +7,7 @@
 
 namespace Drupal\taxonomy\Tests;
 
+use Drupal\Component\Utility\Tags;
 use Drupal\Core\Field\FieldDefinitionInterface;
 
 /**
@@ -279,11 +280,8 @@ function testTermAutocompletion() {
     $input = '"term with, comma and / a';
     $path = 'taxonomy/autocomplete/node/taxonomy_' . $this->vocabulary->id();
     $this->drupalGet($path, array('query' => array('q' => $input)));
-    $n = $third_term->label();
     // Term names containing commas or quotes must be wrapped in quotes.
-    if (strpos($third_term->label(), ',') !== FALSE || strpos($third_term->label(), '"') !== FALSE) {
-      $n = '"' . str_replace('"', '""', $third_term->label()) . '"';
-    }
+    $n = Tags::encode($third_term->label());
     $target = array(array(
       'value' => $n,
       'label' => check_plain($third_term->label()),
diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module
index f2fe6fe..1f94ca8 100644
--- a/core/modules/taxonomy/taxonomy.module
+++ b/core/modules/taxonomy/taxonomy.module
@@ -5,6 +5,7 @@
  * Enables the organization of content into categories.
  */
 
+use Drupal\Component\Utility\Tags;
 use Drupal\Core\Entity\FieldableDatabaseStorageController;
 use Drupal\Core\Entity\EntityInterface;
 use Drupal\file\FileInterface;
@@ -779,12 +780,7 @@ function taxonomy_implode_tags($tags, $vid = NULL) {
       // Make sure we have a completed loaded taxonomy term.
       if ($tag instanceof EntityInterface && $label = $tag->label()) {
         // Commas and quotes in tag names are special cases, so encode 'em.
-        if (strpos($label, ',') !== FALSE || strpos($label, '"') !== FALSE) {
-          $typed_tags[] = '"' . str_replace('"', '""', $label) . '"';
-        }
-        else {
-          $typed_tags[] = $label;
-        }
+        $typed_tags[] = Tags::encode($label);
       }
     }
   }
