diff --git a/core/lib/Drupal/Component/Transliteration/PhpTransliteration.php b/core/lib/Drupal/Component/Transliteration/PhpTransliteration.php index 642f350..d12347c 100644 --- a/core/lib/Drupal/Component/Transliteration/PhpTransliteration.php +++ b/core/lib/Drupal/Component/Transliteration/PhpTransliteration.php @@ -10,6 +10,7 @@ */ namespace Drupal\Component\Transliteration; +use Drupal\Core\Language\LanguageInterface; /** * Implements transliteration without using the PECL extensions. @@ -78,19 +79,18 @@ public function __construct($data_directory = NULL) { /** * {@inheritdoc} */ - public function replaceDiacritics($string, $langcode = 'en', $unknown_character = '?') { + public function removeDiacritics($string) { $result = ''; foreach (preg_split('//u', $string, 0, PREG_SPLIT_NO_EMPTY) as $character) { $to_add = $character; $code = self::ordUTF8($character); - if ($code == -1) { - // @todo since we know all the characters, we shoudln't need this - $to_add = $unknown_character; - } + // @todo make an exhaustive list of what ascii code numbers we want to replace - elseif ($code > 191 && $code < 384) { - $to_add = $this->replace($code, $langcode, $unknown_character); +// if ($code > 191 && $code < 384) { +// if (($code > 223 && $code < 231) || $code == 257) { + if (($code > 0x00df && $code < 0x00ef) || $code == 0x0101) { + $to_add = $this->replace($code, LanguageInterface::LANGCODE_NOT_SPECIFIED, NULL); } $result .= $to_add; diff --git a/core/lib/Drupal/Component/Transliteration/TransliterationInterface.php b/core/lib/Drupal/Component/Transliteration/TransliterationInterface.php index 25d24dc..2e36271 100644 --- a/core/lib/Drupal/Component/Transliteration/TransliterationInterface.php +++ b/core/lib/Drupal/Component/Transliteration/TransliterationInterface.php @@ -15,23 +15,20 @@ interface TransliterationInterface { /** - * Replaces diacritics from the input string with their nearest US-ASCII - * equivalent. + * Removes diacritics (accents) from certain letters. + * + * This only applies to certain letters: + * - Accented Latin characters like a-with-acute-accent, in the UTF-8 character range of 0xE0 to 0xE6. + * - @todo + * - @todo * * @param string $string * The string holding diacritics. - * @param string $langcode - * (optional) The language code of the language the string is in. Defaults - * to 'en' if not provided. - * @param string $unknown_character - * (optional) The character to substitute for characters in $string without - * transliterated equivalents. Defaults to '?'. * * @return string - * $string with non-US-ASCII characters transliterated to US-ASCII - * characters, and unknown characters replaced with $unknown_character. + * $string with accented letters replaced by their unaccented equivalents. */ - public function replaceDiacritics($string, $langcode = 'en', $unknown_character = '?'); + public function removeDiacritics($string); /** * Transliterates text from Unicode to US-ASCII. diff --git a/core/modules/search/search.module b/core/modules/search/search.module index 50cb6cf..ac27c25 100644 --- a/core/modules/search/search.module +++ b/core/modules/search/search.module @@ -241,9 +241,6 @@ function search_simplify($text, $langcode = NULL) { // Lowercase $text = Unicode::strtolower($text); - // Replace diacitics. - $text = \Drupal::service('transliteration')->replaceDiacritics($text); - // Call an external processor for word handling. search_invoke_preprocess($text, $langcode); @@ -395,8 +392,8 @@ function search_index($type, $sid, $langcode, $text) { $text = str_replace(array('<', '>'), array(' <', '> '), $text); $text = strip_tags($text, '<' . implode('><', array_keys($tags)) . '>'); - // Replace diacitics. - $text = \Drupal::service('transliteration')->replaceDiacritics($text); + // Remove diacitics. + $text = \Drupal::service('transliteration')->removeDiacritics($text); // Split HTML tags from plain text. $split = preg_split('/\s*<([^>]+?)>\s*/', $text, -1, PREG_SPLIT_DELIM_CAPTURE); diff --git a/core/tests/Drupal/Tests/Component/Transliteration/PhpTransliterationTest.php b/core/tests/Drupal/Tests/Component/Transliteration/PhpTransliterationTest.php index f1b3705..92e3a67 100644 --- a/core/tests/Drupal/Tests/Component/Transliteration/PhpTransliterationTest.php +++ b/core/tests/Drupal/Tests/Component/Transliteration/PhpTransliterationTest.php @@ -21,6 +21,34 @@ class PhpTransliterationTest extends UnitTestCase { /** + * Tests the PhpTransliteration::removeDiacritics() function. + * + * @param string $original + * The language code to test. + * @param string $expected + * The expected return from PhpTransliteration::removeDiacritics(). + * + * @dataProvider providerTestPhpTransliterationRemoveDiacritics + */ + public function testRemoveDiacritics($original, $expected) { + $transliterator_class = new PhpTransliteration(); + $this->assertEquals($expected, $transliterator_class->removeDiacritics($original)); + } + + /** + * Provides data for self::testRemoveDiacritics(). + * + * @return array + * An array of arrays, each containing the parameters for + * self::testRemoveDiacritics(). + */ + public function providerTestPhpTransliterationRemoveDiacritics() { + return array( + array('àáâäæãåā', 'aaaaaeaaa'), + ); + } + + /** * Tests the PhpTransliteration class. * * @param string $langcode @@ -116,4 +144,6 @@ public function testTransliterationWithMaxLength() { $this->assertSame($trunc_output, $transliteration->transliterate($input, 'de', '?', 18), 'Truncating to 18 characters works'); } + + }