From a5bfd122e1b6dd284e54cb2ce9fd42cccf8db5a5 Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole Date: Mon, 17 Aug 2015 08:34:08 +0100 Subject: [PATCH] Issue #2550945 by alexpott, joelpittet, xjm, Wim Leers, stefan.r: Add Html::escape() --- core/lib/Drupal/Component/Utility/Html.php | 40 +++++++++++++++ .../Drupal/Component/Utility/SafeMarkup.php | 2 +- .../Drupal/Core/Render/Element/HtmlTag.php | 3 +- core/lib/Drupal/Core/Template/Attribute.php | 4 +- .../Drupal/Core/Template/AttributeArray.php | 4 +- .../Drupal/Core/Template/AttributeBoolean.php | 4 +- .../Drupal/Core/Template/AttributeString.php | 4 +- .../Core/Template/AttributeValueBase.php | 3 +- .../src/Tests/AggregatorTestBase.php | 3 +- .../system/src/Tests/Menu/LocalActionTest.php | 5 +- .../system/src/Tests/Menu/LocalTasksTest.php | 7 +-- .../Tests/Component/Utility/HtmlTest.php | 50 +++++++++++++++++++ core/themes/engines/twig/twig.engine | 9 ++-- 13 files changed, 121 insertions(+), 17 deletions(-) diff --git a/core/lib/Drupal/Component/Utility/Html.php b/core/lib/Drupal/Component/Utility/Html.php index d4bf13e8207..892f0691ae9 100644 --- a/core/lib/Drupal/Component/Utility/Html.php +++ b/core/lib/Drupal/Component/Utility/Html.php @@ -338,14 +338,54 @@ EOD; * "<", not "<"). Be careful when using this function, as it will revert * previous sanitization efforts (<script> will become ", ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8')], - [Url::fromRoute('menu_test.local_action4'), htmlspecialchars("", ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8')], + [Url::fromRoute('menu_test.local_action4'), Html::escape("")], + [Url::fromRoute('menu_test.local_action4'), Html::escape("")], [Url::fromRoute('menu_test.local_action2'), 'My hook_menu action'], [Url::fromRoute('menu_test.local_action3'), 'My YAML discovery action'], [Url::fromRoute('menu_test.local_action5'), 'Title override'], diff --git a/core/modules/system/src/Tests/Menu/LocalTasksTest.php b/core/modules/system/src/Tests/Menu/LocalTasksTest.php index cb6b529fbf8..1a0ce81a86a 100644 --- a/core/modules/system/src/Tests/Menu/LocalTasksTest.php +++ b/core/modules/system/src/Tests/Menu/LocalTasksTest.php @@ -7,6 +7,7 @@ namespace Drupal\system\Tests\Menu; +use Drupal\Component\Utility\Html; use Drupal\Core\Url; use Drupal\simpletest\WebTestBase; @@ -78,9 +79,9 @@ class LocalTasksTest extends WebTestBase { ]); // Verify that script tags are escaped on output. - $title = htmlspecialchars("Task 1 ", ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); + $title = Html::escape("Task 1 "); $this->assertLocalTaskAppers($title); - $title = htmlspecialchars("", ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); + $title = Html::escape(""); $this->assertLocalTaskAppers($title); // Verify that local tasks appear as defined in the router. @@ -92,7 +93,7 @@ class LocalTasksTest extends WebTestBase { ['menu_test.local_task_test_tasks_settings_dynamic', []], ]); - $title = htmlspecialchars("", ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); + $title = Html::escape(""); $this->assertLocalTaskAppers($title); // Ensure the view tab is active. diff --git a/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php index 0addf3c3b46..a8f261404c1 100644 --- a/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php +++ b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php @@ -259,4 +259,54 @@ class HtmlTest extends UnitTestCase { ); } + /** + * Tests Html::escape(). + * + * @dataProvider providerEscape + * @covers ::escape + */ + public function testEscape($expected, $text) { + $this->assertEquals($expected, Html::escape($text)); + } + + /** + * Data provider for testEscape(). + * + * @see testCheckPlain() + */ + public function providerEscape() { + return array( + array('Drupal', 'Drupal'), + array('<script>', '