diff --git a/includes/common.inc b/includes/common.inc index 00b2e71..a203dfc 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -3626,10 +3626,20 @@ function drupal_delete_file_if_stale($uri) { * @return * The cleaned identifier. */ -function drupal_clean_css_identifier($identifier, $filter = array(' ' => '-', '_' => '-', '/' => '-', '[' => '-', ']' => '')) { - // By default, we filter using Drupal's coding standards. +function drupal_clean_css_identifier($identifier, $filter = array(' ' => '-')) { + // replace whitespaces $identifier = strtr($identifier, $filter); + // CSS class identifiers must not start with + // - a digit + // - two hyphens + // - hyphen followed by a digit + $prev = ''; + while ($prev != $identifier) { + $prev = $identifier; + $identifier = preg_replace(array('/^\d*/u', '/^\x{002D}*(?=\x{002D})/u', '/^\x{002D}\d/u'),'', $identifier); + } + // Valid characters in a CSS identifier are: // - the hyphen (U+002D) // - a-z (U+0030 - U+0039) @@ -3638,6 +3648,8 @@ function drupal_clean_css_identifier($identifier, $filter = array(' ' => '-', '_ // - 0-9 (U+0061 - U+007A) // - ISO 10646 characters U+00A1 and higher // We strip out any character not in the above list. + // as opposed to the spec we do not allow U+00A0 + // as opposed to the spec we only allow code points up to U+FFFF $identifier = preg_replace('/[^\x{002D}\x{0030}-\x{0039}\x{0041}-\x{005A}\x{005F}\x{0061}-\x{007A}\x{00A1}-\x{FFFF}]/u', '', $identifier); return $identifier; diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test index 4664f04..d20bf82 100644 --- a/modules/simpletest/tests/common.test +++ b/modules/simpletest/tests/common.test @@ -812,6 +812,22 @@ 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', t('Strip invalid characters.')); + + $identifier = array( + '-foo' => '----foo', + '-bar' => '-bar', + 'foo' => 'foo', + 'bar' => '1bar', + 'foobar' => '1111foobar', + '-foofoo' => '1111---foofoo', + '-barbar' => '-1--barbar', + '-_foo-bar' => '---11---_foo bar', + 'foo_bar' => 'foo_bar', + '☺' => '☺' + ); + foreach ($identifier as $expected => $given) { + $this->assertIdentical(drupal_clean_css_identifier($given), $expected, t('Strip invalid trailing characters.')); + } } /**