Unverified Commit 51be5437 authored by Janez Urevc's avatar Janez Urevc Committed by Mark Halliwell
Browse files

Issue #3114383 by slashrsm: Use random suffixes for dropbutton items HTML ID to prevent collisions

parent b935d5bc
Loading
Loading
Loading
Loading
+6 −5
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\bootstrap\Plugin\Preprocess;

use Drupal\bootstrap\Utility\Crypt;
use Drupal\bootstrap\Utility\Element;
use Drupal\bootstrap\Utility\Unicode;
use Drupal\bootstrap\Utility\Variables;
@@ -117,17 +118,17 @@ class BootstrapDropdown extends PreprocessBase implements PreprocessInterface {
          $child->setAttribute('formnovalidate', 'formnovalidate');
        }

        // Generate the current timestamp to use with identifiers. This helps
        // Generate the unique suffix to use with identifiers. This helps
        // eliminate any render cache issues when dealing with multiple
        // dropdown elements on the same page, as in a listing.
        // @see https://www.drupal.org/project/bootstrap/issues/2939166
        $current_time = \Drupal::time()->getCurrentTime();
        $suffix = Crypt::randomBytesBase64(8);

        // The first item is always the "primary link".
        if ($i === 0) {
          // Must generate an ID for this child because the toggle will use it.
          if (!$child->getAttribute('id')) {
            $child->setAttribute('id', $child->getProperty('id', Html::getUniqueId("dropdown-item-$current_time")));
            $child->setAttribute('id', $child->getProperty('id', Html::getUniqueId("dropdown-item-$suffix")));
          }
          $primary_action = $child->addClass('hidden');
        }
@@ -141,7 +142,7 @@ class BootstrapDropdown extends PreprocessBase implements PreprocessInterface {
          // events to the "dropdown-target" (the original element).
          $id = $child->getAttribute('id');
          if (!$id) {
            $id = $child->getProperty('id', Html::getUniqueId("dropdown-item-$current_time"));
            $id = $child->getProperty('id', Html::getUniqueId("dropdown-item-$suffix"));
            $child->setAttribute('id', $id);
          }

@@ -164,7 +165,7 @@ class BootstrapDropdown extends PreprocessBase implements PreprocessInterface {

        // If no HTML ID was found, automatically create one.
        if ($child->hasProperty('ajax') && !$child->hasProperty('ajax_processed') && !$child->hasProperty('id')) {
          $child->setProperty('id', $child->getAttribute('id', Html::getUniqueId("ajax-link-$current_time")));
          $child->setProperty('id', $child->getAttribute('id', Html::getUniqueId("ajax-link-$suffix")));
        }

        $items->$key = $child->getArrayCopy();