Loading html_title.module +2 −6 Original line number Diff line number Diff line Loading @@ -50,12 +50,8 @@ function html_title_preprocess_search_result(&$variables) { */ function html_title_preprocess_breadcrumb(&$vars) { foreach ($vars['breadcrumb'] as $key => $link) { $link_text = $link['text']; if (is_array($link_text)) { $link_text = $link['text']['#markup']; } $vars['breadcrumb'][$key]['text'] = \Drupal::service('html_title.filter') ->decodeToMarkup($link_text); ->decodeToMarkup($link['text']); } } Loading Loading @@ -102,6 +98,6 @@ function html_title_theme_registry_alter(&$theme_registry) { */ function html_title_node_view(array &$build, EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) { if (isset($build['title'][0]['#context']['value'])) { $build['title'][0]['#context']['value'] = \Drupal::service('html_title.filter')->decodeToMarkup($build['title'][0]['#context']['value']); $build['title'][0]['#context']['value'] = \Drupal::service('html_title.filter')->decodeToMarkup($build['title']); } } html_title.services.yml +1 −1 Original line number Diff line number Diff line services: html_title.filter: class: Drupal\html_title\HtmlTitleFilter arguments: ['@config.factory'] arguments: ['@config.factory', '@renderer'] src/HtmlTitleFilter.php +15 −1 Original line number Diff line number Diff line Loading @@ -6,6 +6,7 @@ use Drupal\Component\Utility\Xss; use Drupal\Component\Utility\Html; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Render\Markup; use Drupal\Core\Render\RendererInterface; /** * Drupal\html_titleHtmlTitleFilter. Loading @@ -19,14 +20,24 @@ class HtmlTitleFilter { */ protected $configFactory; /** * The renderer. * * @var \Drupal\Core\Render\RendererInterface */ protected $renderer; /** * HtmlTitleFilter constructor. * * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory * The config factory. * @param \Drupal\Core\Render\RendererInterface $renderer * The renderer. */ public function __construct(ConfigFactoryInterface $configFactory) { public function __construct(ConfigFactoryInterface $configFactory, RendererInterface $renderer) { $this->configFactory = $configFactory; $this->renderer = $renderer; } /** Loading Loading @@ -59,6 +70,9 @@ class HtmlTitleFilter { * Filte string with allow html tags. */ public function decodeToText($str) { if (is_array($str)) { $str = $this->renderer->renderPlain($str); } return $this->filterXss(Html::decodeEntities((string) $str)); } Loading tests/src/Unit/HtmlTitleFilterTest.php +16 −11 Original line number Diff line number Diff line Loading @@ -6,14 +6,14 @@ use Drupal\Core\Config\Config; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Render\Markup; use Drupal\html_title\HtmlTitleFilter; use Drupal\Tests\UnitTestCase; use Drupal\Tests\Core\Render\RendererTestBase; /** * @coversDefaultClass \Drupal\html_title\HtmlTitleFilter * * @group html_title */ class HtmlTitleFilterTest extends UnitTestCase { class HtmlTitleFilterTest extends RendererTestBase { /** * The availability manager. Loading @@ -33,14 +33,14 @@ class HtmlTitleFilterTest extends UnitTestCase { $config_factory->method('get')->willReturn($config); $config->method('get')->willReturn('<br> <sub> <sup>'); $this->htmlTitleFilter = new HtmlTitleFilter($config_factory); $this->htmlTitleFilter = new HtmlTitleFilter($config_factory, $this->renderer); } /** * Tests HtmlTitleFilter::decodeToText(). * * @param string $string * The string passed to decodeToText(). * @param string|array $string * The input passed to decodeToText(). * @param string $expected * The expected result from calling the function. * Loading @@ -48,8 +48,8 @@ class HtmlTitleFilterTest extends UnitTestCase { * * @dataProvider providerDecodeToText */ public function testDecodeToText(string $string, string $expected) { $this->assertEquals($expected, $this->htmlTitleFilter->decodeToText($string)); public function testDecodeToText($input, string $expected) { $this->assertEquals($expected, $this->htmlTitleFilter->decodeToText($input)); } /** Loading @@ -67,14 +67,16 @@ class HtmlTitleFilterTest extends UnitTestCase { ['test <sub>sub</sub>-tag', 'test <sub>sub</sub>-tag'], ['test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag and <br> br-tag', 'test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag and <br> br-tag'], ['test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag and <br> br-tag <p>p</p>-tag', 'test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag and <br> br-tag p-tag'], // The html title filter service should also works with renderable arrays. [['#markup' => '<p>Test renderable <sub>array</sub></p>'], 'Test renderable <sub>array</sub>'], ]; } /** * Tests HtmlTitleFilter::decodeToText(). * * @param string $string * The Markp passed to decodeToText(). * @param string|array $string * The input passed to decodeToText(). * @param \Drupal\Core\Render\Markup $expected * The expected result from calling the function. * Loading @@ -82,8 +84,8 @@ class HtmlTitleFilterTest extends UnitTestCase { * * @dataProvider providerDecodeToMarkup */ public function testDecodeToMarkup(string $string, Markup $expected) { $this->assertEquals($expected, $this->htmlTitleFilter->decodeToMarkup($string)); public function testDecodeToMarkup($input, Markup $expected) { $this->assertEquals($expected, $this->htmlTitleFilter->decodeToMarkup($input)); } /** Loading @@ -101,6 +103,9 @@ class HtmlTitleFilterTest extends UnitTestCase { ['test <sub>sub</sub>-tag', Markup::create('test <sub>sub</sub>-tag')], ['test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag and <br> br-tag', Markup::create('test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag and <br> br-tag')], ['test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag, <br> br-tag and <p>p</p>-tag', Markup::create('test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag, <br> br-tag and p-tag')], // The html title filter service should also works with renderable arrays. [['#markup' => '<p>Test renderable <sub>array</sub></p>'], Markup::create('Test renderable <sub>array</sub>')], ]; } Loading Loading
html_title.module +2 −6 Original line number Diff line number Diff line Loading @@ -50,12 +50,8 @@ function html_title_preprocess_search_result(&$variables) { */ function html_title_preprocess_breadcrumb(&$vars) { foreach ($vars['breadcrumb'] as $key => $link) { $link_text = $link['text']; if (is_array($link_text)) { $link_text = $link['text']['#markup']; } $vars['breadcrumb'][$key]['text'] = \Drupal::service('html_title.filter') ->decodeToMarkup($link_text); ->decodeToMarkup($link['text']); } } Loading Loading @@ -102,6 +98,6 @@ function html_title_theme_registry_alter(&$theme_registry) { */ function html_title_node_view(array &$build, EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) { if (isset($build['title'][0]['#context']['value'])) { $build['title'][0]['#context']['value'] = \Drupal::service('html_title.filter')->decodeToMarkup($build['title'][0]['#context']['value']); $build['title'][0]['#context']['value'] = \Drupal::service('html_title.filter')->decodeToMarkup($build['title']); } }
html_title.services.yml +1 −1 Original line number Diff line number Diff line services: html_title.filter: class: Drupal\html_title\HtmlTitleFilter arguments: ['@config.factory'] arguments: ['@config.factory', '@renderer']
src/HtmlTitleFilter.php +15 −1 Original line number Diff line number Diff line Loading @@ -6,6 +6,7 @@ use Drupal\Component\Utility\Xss; use Drupal\Component\Utility\Html; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Render\Markup; use Drupal\Core\Render\RendererInterface; /** * Drupal\html_titleHtmlTitleFilter. Loading @@ -19,14 +20,24 @@ class HtmlTitleFilter { */ protected $configFactory; /** * The renderer. * * @var \Drupal\Core\Render\RendererInterface */ protected $renderer; /** * HtmlTitleFilter constructor. * * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory * The config factory. * @param \Drupal\Core\Render\RendererInterface $renderer * The renderer. */ public function __construct(ConfigFactoryInterface $configFactory) { public function __construct(ConfigFactoryInterface $configFactory, RendererInterface $renderer) { $this->configFactory = $configFactory; $this->renderer = $renderer; } /** Loading Loading @@ -59,6 +70,9 @@ class HtmlTitleFilter { * Filte string with allow html tags. */ public function decodeToText($str) { if (is_array($str)) { $str = $this->renderer->renderPlain($str); } return $this->filterXss(Html::decodeEntities((string) $str)); } Loading
tests/src/Unit/HtmlTitleFilterTest.php +16 −11 Original line number Diff line number Diff line Loading @@ -6,14 +6,14 @@ use Drupal\Core\Config\Config; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Render\Markup; use Drupal\html_title\HtmlTitleFilter; use Drupal\Tests\UnitTestCase; use Drupal\Tests\Core\Render\RendererTestBase; /** * @coversDefaultClass \Drupal\html_title\HtmlTitleFilter * * @group html_title */ class HtmlTitleFilterTest extends UnitTestCase { class HtmlTitleFilterTest extends RendererTestBase { /** * The availability manager. Loading @@ -33,14 +33,14 @@ class HtmlTitleFilterTest extends UnitTestCase { $config_factory->method('get')->willReturn($config); $config->method('get')->willReturn('<br> <sub> <sup>'); $this->htmlTitleFilter = new HtmlTitleFilter($config_factory); $this->htmlTitleFilter = new HtmlTitleFilter($config_factory, $this->renderer); } /** * Tests HtmlTitleFilter::decodeToText(). * * @param string $string * The string passed to decodeToText(). * @param string|array $string * The input passed to decodeToText(). * @param string $expected * The expected result from calling the function. * Loading @@ -48,8 +48,8 @@ class HtmlTitleFilterTest extends UnitTestCase { * * @dataProvider providerDecodeToText */ public function testDecodeToText(string $string, string $expected) { $this->assertEquals($expected, $this->htmlTitleFilter->decodeToText($string)); public function testDecodeToText($input, string $expected) { $this->assertEquals($expected, $this->htmlTitleFilter->decodeToText($input)); } /** Loading @@ -67,14 +67,16 @@ class HtmlTitleFilterTest extends UnitTestCase { ['test <sub>sub</sub>-tag', 'test <sub>sub</sub>-tag'], ['test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag and <br> br-tag', 'test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag and <br> br-tag'], ['test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag and <br> br-tag <p>p</p>-tag', 'test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag and <br> br-tag p-tag'], // The html title filter service should also works with renderable arrays. [['#markup' => '<p>Test renderable <sub>array</sub></p>'], 'Test renderable <sub>array</sub>'], ]; } /** * Tests HtmlTitleFilter::decodeToText(). * * @param string $string * The Markp passed to decodeToText(). * @param string|array $string * The input passed to decodeToText(). * @param \Drupal\Core\Render\Markup $expected * The expected result from calling the function. * Loading @@ -82,8 +84,8 @@ class HtmlTitleFilterTest extends UnitTestCase { * * @dataProvider providerDecodeToMarkup */ public function testDecodeToMarkup(string $string, Markup $expected) { $this->assertEquals($expected, $this->htmlTitleFilter->decodeToMarkup($string)); public function testDecodeToMarkup($input, Markup $expected) { $this->assertEquals($expected, $this->htmlTitleFilter->decodeToMarkup($input)); } /** Loading @@ -101,6 +103,9 @@ class HtmlTitleFilterTest extends UnitTestCase { ['test <sub>sub</sub>-tag', Markup::create('test <sub>sub</sub>-tag')], ['test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag and <br> br-tag', Markup::create('test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag and <br> br-tag')], ['test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag, <br> br-tag and <p>p</p>-tag', Markup::create('test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag, <br> br-tag and p-tag')], // The html title filter service should also works with renderable arrays. [['#markup' => '<p>Test renderable <sub>array</sub></p>'], Markup::create('Test renderable <sub>array</sub>')], ]; } Loading