From a621674bcd36c0a7c75dfe261634ce1917022ca2 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Mon, 3 Jun 2024 12:44:14 +0200 Subject: [PATCH] Update escaping function (#665) * update escaping function * simplify check to know if the text is already escaped. --- includes/create-theme/theme-locale.php | 4 +- tests/CbtThemeLocale/escapeString.php | 10 ++--- .../escapeTextContentOfBlocks.php | 32 ++++++++-------- tests/test-theme-templates.php | 38 +++++++++---------- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/includes/create-theme/theme-locale.php b/includes/create-theme/theme-locale.php index 5705df3b..343b855e 100644 --- a/includes/create-theme/theme-locale.php +++ b/includes/create-theme/theme-locale.php @@ -17,12 +17,12 @@ public static function escape_string( $string ) { } // Check if the text is already escaped. - if ( str_starts_with( $string, 'get( 'TextDomain' ) . "');?>"; + return "get( 'TextDomain' ) . "');?>"; } /** diff --git a/tests/CbtThemeLocale/escapeString.php b/tests/CbtThemeLocale/escapeString.php index 4ffcaf9a..b9ff7361 100644 --- a/tests/CbtThemeLocale/escapeString.php +++ b/tests/CbtThemeLocale/escapeString.php @@ -13,29 +13,29 @@ class CBT_Theme_Locale_EscapeString extends CBT_Theme_Locale_UnitTestCase { public function test_escape_string() { $string = 'This is a test text.'; $escaped_string = CBT_Theme_Locale::escape_string( $string ); - $this->assertEquals( "", $escaped_string ); + $this->assertEquals( "", $escaped_string ); } public function test_escape_string_with_single_quote() { $string = "This is a test text with a single quote '"; $escaped_string = CBT_Theme_Locale::escape_string( $string ); - $this->assertEquals( "", $escaped_string ); + $this->assertEquals( "", $escaped_string ); } public function test_escape_string_with_double_quote() { $string = 'This is a test text with a double quote "'; $escaped_string = CBT_Theme_Locale::escape_string( $string ); - $this->assertEquals( "", $escaped_string ); + $this->assertEquals( "", $escaped_string ); } public function test_escape_string_with_html() { $string = '

This is a test text with HTML.

'; $escaped_string = CBT_Theme_Locale::escape_string( $string ); - $this->assertEquals( "This is a test text with HTML.

', 'test-locale-theme');?>", $escaped_string ); + $this->assertEquals( "This is a test text with HTML.

', 'test-locale-theme');?>", $escaped_string ); } public function test_escape_string_with_already_escaped_string() { - $string = ""; + $string = ""; $escaped_string = CBT_Theme_Locale::escape_string( $string ); $this->assertEquals( $string, $escaped_string ); } diff --git a/tests/CbtThemeLocale/escapeTextContentOfBlocks.php b/tests/CbtThemeLocale/escapeTextContentOfBlocks.php index c138a24a..7df7b62f 100644 --- a/tests/CbtThemeLocale/escapeTextContentOfBlocks.php +++ b/tests/CbtThemeLocale/escapeTextContentOfBlocks.php @@ -30,7 +30,7 @@ public function data_test_escape_text_content_of_blocks() { 'paragraph' => array( 'block_markup' => '

This is a test text.

', - 'expected_markup' => '

', + 'expected_markup' => '

', ), 'paragraph on nested groups' => array( @@ -46,7 +46,7 @@ public function data_test_escape_text_content_of_blocks() { '
-

+

', @@ -59,7 +59,7 @@ public function data_test_escape_text_content_of_blocks() { ', 'expected_markup' => ' -

+

', ), @@ -70,7 +70,7 @@ public function data_test_escape_text_content_of_blocks() { ', 'expected_markup' => ' -

+

', ), @@ -90,13 +90,13 @@ public function data_test_escape_text_content_of_blocks() { 'expected_markup' => ' ', ), @@ -108,7 +108,7 @@ public function data_test_escape_text_content_of_blocks() { ', 'expected_markup' => ' -
El polvo elemental que nos ignora
y que fue el rojo Adán y que es ahora
todos los hombres, y que no veremos.\', \'test-locale-theme\');?>
+
El polvo elemental que nos ignora
y que fue el rojo Adán y que es ahora
todos los hombres, y que no veremos.\', \'test-locale-theme\');?>
', ), @@ -119,7 +119,7 @@ public function data_test_escape_text_content_of_blocks() { ', 'expected_markup' => ' -
+
', ), @@ -130,7 +130,7 @@ public function data_test_escape_text_content_of_blocks() { ', 'expected_markup' => ' -
<?php echo __(\'Windows of a building in Nuremberg, Germany\', \'test-locale-theme\');?>
+
<?php esc_html_e(\'Windows of a building in Nuremberg, Germany\', \'test-locale-theme\');?>
', ), @@ -143,8 +143,8 @@ public function data_test_escape_text_content_of_blocks() { ', 'expected_markup' => ' -
<?php echo __(\'Alternative text for cover image\', \'test-locale-theme\');?>
-

+
<?php esc_html_e(\'Alternative text for cover image\', \'test-locale-theme\');?>
+

', ), @@ -158,8 +158,8 @@ public function data_test_escape_text_content_of_blocks() { ', 'expected_markup' => ' -
<?php echo __(\'This is alt text\', \'test-locale-theme\');?>
-

+
<?php esc_html_e(\'This is alt text\', \'test-locale-theme\');?>
+

', ), @@ -171,7 +171,7 @@ public function data_test_escape_text_content_of_blocks() { ', 'expected_markup' => ' -

+

', ), @@ -182,7 +182,7 @@ public function data_test_escape_text_content_of_blocks() { ', 'expected_markup' => ' -
+
', ), diff --git a/tests/test-theme-templates.php b/tests/test-theme-templates.php index faabb623..8ed6f10b 100644 --- a/tests/test-theme-templates.php +++ b/tests/test-theme-templates.php @@ -12,7 +12,7 @@ public function test_paragraphs_are_localized() { $template = new stdClass(); $template->content = '

This is text to localize

'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "

", $new_template->content ); + $this->assertStringContainsString( "

", $new_template->content ); $this->assertStringNotContainsString( '

This is text to localize

', $new_template->content ); } @@ -136,7 +136,7 @@ public function test_properly_encode_quotes_and_doublequotes() { $escaped_template = CBT_Theme_Templates::escape_text_in_template( $template ); /* That looks like a mess, but what it should look like for REAL is */ - $this->assertStringContainsString( '', $escaped_template->content ); + $this->assertStringContainsString( '', $escaped_template->content ); } public function test_properly_encode_lessthan_and_greaterthan() { @@ -146,7 +146,7 @@ public function test_properly_encode_lessthan_and_greaterthan() { '; $escaped_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( ' is a <test>\', \'\');?>', $escaped_template->content ); + $this->assertStringContainsString( ' is a <test>\', \'\');?>', $escaped_template->content ); } public function test_properly_encode_html_markup() { @@ -156,7 +156,7 @@ public function test_properly_encode_html_markup() { '; $escaped_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( 'Bold text has feelings <> TOO\', \'\');?>', $escaped_template->content ); + $this->assertStringContainsString( 'Bold text has feelings <> TOO\', \'\');?>', $escaped_template->content ); } public function test_localize_alt_text_from_image() { @@ -167,7 +167,7 @@ public function test_localize_alt_text_from_image() { '; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( 'alt=""', $new_template->content ); + $this->assertStringContainsString( 'alt=""', $new_template->content ); } public function test_localize_alt_text_from_cover() { @@ -187,7 +187,7 @@ public function test_localize_alt_text_from_cover() { '; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); // Check the markup attribute - $this->assertStringContainsString( 'alt=""', $new_template->content ); + $this->assertStringContainsString( 'alt=""', $new_template->content ); } public function test_localize_quote() { @@ -201,8 +201,8 @@ public function test_localize_quote() { '; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "", $new_template->content ); - $this->assertStringContainsString( "", $new_template->content ); + $this->assertStringContainsString( "", $new_template->content ); + $this->assertStringContainsString( "", $new_template->content ); } public function test_localize_pullquote() { @@ -216,8 +216,8 @@ public function test_localize_pullquote() { '; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "", $new_template->content ); - $this->assertStringContainsString( "", $new_template->content ); + $this->assertStringContainsString( "", $new_template->content ); + $this->assertStringContainsString( "", $new_template->content ); } public function test_localize_list() { @@ -234,7 +234,7 @@ public function test_localize_list() { '; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "
  • ", $new_template->content ); + $this->assertStringContainsString( "
  • ", $new_template->content ); } public function test_localize_verse() { @@ -243,7 +243,7 @@ public function test_localize_verse() {
    Here is some verse to localize
    '; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "verse to localize', '');?>", $new_template->content ); + $this->assertStringContainsString( "verse to localize', '');?>", $new_template->content ); } public function test_localize_table() { @@ -274,10 +274,10 @@ public function test_localize_table() { '; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "", $new_template->content ); - $this->assertStringContainsString( "", $new_template->content ); - $this->assertStringContainsString( "", $new_template->content ); - $this->assertStringContainsString( "", $new_template->content ); + $this->assertStringContainsString( "", $new_template->content ); + $this->assertStringContainsString( "", $new_template->content ); + $this->assertStringContainsString( "", $new_template->content ); + $this->assertStringContainsString( "", $new_template->content ); } public function test_localize_media_text() { @@ -295,8 +295,8 @@ public function test_localize_media_text() {
    '; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "", $new_template->content ); - $this->assertStringContainsString( "", $new_template->content ); + $this->assertStringContainsString( "", $new_template->content ); + $this->assertStringContainsString( "", $new_template->content ); } public function test_localize_cover_block_children() { @@ -314,7 +314,7 @@ public function test_localize_cover_block_children() { '; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( '

    ', $new_template->content ); + $this->assertStringContainsString( '

    ', $new_template->content ); } public function test_localize_nested_cover_block_children() {