diff --git a/core/lib/Drupal/Component/Render/EscapedMarkup.php b/core/lib/Drupal/Component/Render/EscapableString.php similarity index 83% rename from core/lib/Drupal/Component/Render/EscapedMarkup.php rename to core/lib/Drupal/Component/Render/EscapableString.php index a6fd863..eb44f16 100644 --- a/core/lib/Drupal/Component/Render/EscapedMarkup.php +++ b/core/lib/Drupal/Component/Render/EscapableString.php @@ -2,7 +2,7 @@ /** * @file - * Contains \Drupal\Component\Render\EscapedMarkup. + * Contains \Drupal\Component\Render\EscapableString. */ namespace Drupal\Component\Render; @@ -15,7 +15,7 @@ * * @ingroup sanitization */ -class EscapedMarkup implements EscapedMarkupInterface { +class EscapableString implements EscapableStringInterface { /** * The string to escape. @@ -25,7 +25,7 @@ class EscapedMarkup implements EscapedMarkupInterface { protected $string; /** - * Constructs an EscapedMarkup object. + * Constructs an EscapableString object. * * @param $string * The string to escape. This value will be cast to a string. diff --git a/core/lib/Drupal/Component/Render/EscapedMarkupInterface.php b/core/lib/Drupal/Component/Render/EscapableStringInterface.php similarity index 55% rename from core/lib/Drupal/Component/Render/EscapedMarkupInterface.php rename to core/lib/Drupal/Component/Render/EscapableStringInterface.php index 7efc291..eb1d63f 100644 --- a/core/lib/Drupal/Component/Render/EscapedMarkupInterface.php +++ b/core/lib/Drupal/Component/Render/EscapableStringInterface.php @@ -2,25 +2,25 @@ /** * @file - * Contains \Drupal\Component\Render\EscapedMarkupInterface. + * Contains \Drupal\Component\Render\EscapableStringInterface. */ namespace Drupal\Component\Render; /** - * Marks an object's __toString() method as returning escaped markup. + * Marks an object's __toString() method as returning an escaped string. * - * This interface should only be used on objects that emit escaped markup from + * This interface should only be used on objects that emit escaped strings from * their __toString() method. If the output of __toString() contains unescaped * markup, it must not be used. * * @ingroup sanitization * */ -interface EscapedMarkupInterface extends MarkupInterface, \Countable { +interface EscapableStringInterface extends MarkupInterface, \Countable { /** - * Returns escaped markup. + * Returns an escaped string. */ public function __toString(); diff --git a/core/lib/Drupal/Component/Utility/SafeMarkup.php b/core/lib/Drupal/Component/Utility/SafeMarkup.php index 2a96258..a64ac28 100644 --- a/core/lib/Drupal/Component/Utility/SafeMarkup.php +++ b/core/lib/Drupal/Component/Utility/SafeMarkup.php @@ -7,7 +7,7 @@ namespace Drupal\Component\Utility; -use Drupal\Component\Render\EscapedMarkup; +use Drupal\Component\Render\EscapableString; use Drupal\Component\Render\FormattableMarkup; use Drupal\Component\Render\MarkupInterface; @@ -68,8 +68,8 @@ public static function isSafe($string, $strategy = 'html') { * @param string $text * The text to be checked or processed. * - * @return \Drupal\Component\Render\EscapedMarkupInterface - * An EscapedMarkup object that escapes when rendered to string. + * @return \Drupal\Component\Render\EscapableStringInterface + * An EscapableString object that escapes when rendered to string. * * @ingroup sanitization * @@ -83,7 +83,7 @@ public static function isSafe($string, $strategy = 'html') { * @see drupal_validate_utf8() */ public static function checkPlain($text) { - return new EscapedMarkup($text); + return new EscapableString($text); } /** diff --git a/core/lib/Drupal/Core/Utility/Token.php b/core/lib/Drupal/Core/Utility/Token.php index 8215837..f6a9aed 100644 --- a/core/lib/Drupal/Core/Utility/Token.php +++ b/core/lib/Drupal/Core/Utility/Token.php @@ -7,9 +7,8 @@ namespace Drupal\Core\Utility; -use Drupal\Component\Render\EscapedMarkup; +use Drupal\Component\Render\EscapableString; use Drupal\Component\Render\MarkupInterface; -use Drupal\Component\Utility\Html; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableDependencyInterface; use Drupal\Core\Cache\CacheBackendInterface; @@ -208,7 +207,7 @@ public function replace($text, array $data = array(), array $options = array(), // Escape the tokens, unless they are explicitly markup. foreach ($replacements as $token => $value) { - $replacements[$token] = $value instanceof MarkupInterface ? $value : new EscapedMarkup($value); + $replacements[$token] = $value instanceof MarkupInterface ? $value : new EscapableString($value); } // Optionally alter the list of replacement values. diff --git a/core/tests/Drupal/Tests/Component/Render/EscapableStringTest.php b/core/tests/Drupal/Tests/Component/Render/EscapableStringTest.php new file mode 100644 index 0000000..00757d1 --- /dev/null +++ b/core/tests/Drupal/Tests/Component/Render/EscapableStringTest.php @@ -0,0 +1,69 @@ +assertEquals($expected, (string) $escapeable_string, $message); + $this->assertEquals($expected, $escapeable_string->jsonSerialize()); + } + + /** + * Data provider for testToString(). + * + * @see testToString() + */ + function providerToString() { + // Checks that invalid multi-byte sequences are escaped. + $tests[] = array("Foo\xC0barbaz", 'Foo�barbaz', 'Escapes invalid sequence "Foo\xC0barbaz"'); + $tests[] = array("\xc2\"", '�"', 'Escapes invalid sequence "\xc2\""'); + $tests[] = array("Fooÿñ", "Fooÿñ", 'Does not escape valid sequence "Fooÿñ"'); + + // Checks that special characters are escaped. + $script_tag = $this->prophesize(MarkupInterface::class); + $script_tag->__toString()->willReturn('