diff --git a/includes/common.inc b/includes/common.inc
index 717f568..c23c80b 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -3893,6 +3893,21 @@ function drupal_delete_file_if_stale($uri) {
  *   The cleaned identifier.
  */
 function drupal_clean_css_identifier($identifier, $filter = array(' ' => '-', '_' => '-', '/' => '-', '[' => '-', ']' => '')) {
+  // Use the advanced drupal_static() pattern, since this is called very often.
+  static $drupal_static_fast;
+  if (!isset($drupal_static_fast)) {
+    $drupal_static_fast['allow_css_double_underscores'] = &drupal_static(__FUNCTION__ . ':allow_css_double_underscores');
+  }
+  $allow_css_double_underscores = &$drupal_static_fast['allow_css_double_underscores'];
+  if (!isset($allow_css_double_underscores)) {
+    $allow_css_double_underscores = variable_get('allow_css_double_underscores');
+  }
+
+  // Preserve BEM-style double-underscores depending on custom setting.
+  if ($allow_css_double_underscores) {
+    $filter['__'] = '__';
+  }
+
   // By default, we filter using Drupal's coding standards.
   $identifier = strtr($identifier, $filter);
 
diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test
index 92aefe4..61f4912 100644
--- a/modules/simpletest/tests/common.test
+++ b/modules/simpletest/tests/common.test
@@ -947,6 +947,21 @@ class DrupalHTMLIdentifierTestCase extends DrupalUnitTestCase {
 
     // Verify that invalid characters (including non-breaking space) are stripped from the identifier.
     $this->assertIdentical(drupal_clean_css_identifier('invalid !"#$%&\'()*+,./:;<=>?@[\\]^`{|}~ identifier', array()), 'invalididentifier', 'Strip invalid characters.');
+
+    // Verify that double underscores are replaced in the identifier by default.
+    $identifier = 'css__identifier__with__double__underscores';
+    $expected = 'css--identifier--with--double--underscores';
+    $this->assertIdentical(drupal_clean_css_identifier($identifier), $expected, 'Verify double underscores are replaced with double hyphens by default.');
+
+    // Mock a variable_set of allow_css_double_underscores to TRUE.
+    $original = isset($GLOBALS['conf']['allow_css_double_underscores']) ? $GLOBALS['conf']['allow_css_double_underscores'] : NULL;
+    $GLOBALS['conf']['allow_css_double_underscores'] = TRUE;
+
+    // Now, verify that double underscores are preserved in the identifier.
+    $this->assertIdentical(drupal_clean_css_identifier($identifier), $identifier, 'Verify double underscores are preserved.');
+
+    // Revert the overridden value above.
+    $GLOBALS['conf']['allow_css_double_underscores'] = $original;
   }
 
   /**
diff --git a/sites/default/default.settings.php b/sites/default/default.settings.php
index 7e36a4a..53401bc 100644
--- a/sites/default/default.settings.php
+++ b/sites/default/default.settings.php
@@ -584,3 +584,13 @@ $conf['404_fast_html'] = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN"
  * Remove the leading hash sign to enable.
  */
 # $conf['theme_debug'] = TRUE;
+
+/**
+ * CSS identifier double underscores allowance:
+ *
+ * To allow CSS identifiers to contain double underscores (.example__selector)
+ * for BEM-style naming standards, uncomment the line below.
+ *
+ * @see drupal_clean_css_identifier()
+ */
+# $conf['allow_css_double_underscores'] = TRUE;
