From 94a2df8fdc8efa7a335f72744a7ff49f0a80a3cd Mon Sep 17 00:00:00 2001 From: mundschenk_at Date: Thu, 13 Jun 2024 20:03:14 +0200 Subject: [PATCH] Fully initialize Settings even when $use_defaults is set to false --- src/class-settings.php | 14 ++++++++------ src/settings/class-dash-style.php | 13 +++++++++++++ src/settings/class-quote-style.php | 5 +++++ tests/class-settings-test.php | 21 +++++++++++++-------- tests/class-testcase.php | 5 +++++ 5 files changed, 44 insertions(+), 14 deletions(-) diff --git a/src/class-settings.php b/src/class-settings.php index c77a7a9..1d1dd42 100644 --- a/src/class-settings.php +++ b/src/class-settings.php @@ -619,12 +619,8 @@ class Settings { /** * Sets up a new Settings object. * - * @since 6.0.0 If $set_defaults is `false`, the settings object is not fully - * initialized unless `set_smart_quotes_primary`, - * `set_smart_quotes_secondary`, `set_smart_dashes_style` and - * `set_true_no_break_narrow_space` are called explicitly. - * @since 6.5.0 A (partial) character mapping can be given to remap certain - * characters. + * @since 6.5.0 A (partial) character mapping can be given to remap certain characters. + * @since 7.0.0 The object is no fully initialized again even when `$set_defaults` is `false`. * * @param bool $set_defaults Optional. If true, set default values for various properties. Default true. * @param string[] $mapping Optional. Unicode characters to remap. The default maps the narrow no-break space to the normal NO-BREAK SPACE and the apostrophe to the RIGHT SINGLE QUOTATION MARK. @@ -646,6 +642,12 @@ public function __construct( bool $set_defaults = true, array $mapping = [ U::NO $this->set_defaults(); } else { $this->data[ self::CUSTOM_UNITS ] = ''; + + $null_quotes = Quote_Style::get_styled_quotes( Quote_Style::NONE ); + $this->data[ self::SMART_QUOTES_PRIMARY_STYLE ] = $null_quotes; + $this->data[ self::SMART_QUOTES_SECONDARY_STYLE ] = $null_quotes; + + $this->data[ self::SMART_DASHES_STYLE ] = Dash_Style::get_styled_dashes( Dash_Style::NONE ); } // Merge default character mapping with given mapping. diff --git a/src/settings/class-dash-style.php b/src/settings/class-dash-style.php index d764683..20a1faf 100644 --- a/src/settings/class-dash-style.php +++ b/src/settings/class-dash-style.php @@ -53,6 +53,13 @@ abstract class Dash_Style { */ const INTERNATIONAL_NO_HAIR_SPACES = 'internationalNoHairSpaces'; + /** + * Empty dash style. + * + * @since 7.0.0 + */ + const NONE = 'noneAtAll'; + /** * Available dash styles. * @@ -79,6 +86,12 @@ abstract class Dash_Style { self::INTERVAL => U::EN_DASH, self::INTERVAL_SPACE => '', ], + self::NONE => [ + self::PARENTHETICAL => '', + self::PARENTHETICAL_SPACE => '', + self::INTERVAL => '', + self::INTERVAL_SPACE => '', + ], ]; /** diff --git a/src/settings/class-quote-style.php b/src/settings/class-quote-style.php index 686fdcd..3c9e509 100644 --- a/src/settings/class-quote-style.php +++ b/src/settings/class-quote-style.php @@ -54,6 +54,7 @@ abstract class Quote_Style { const SINGLE_GUILLEMETS_REVERSED = 'singleGuillemetsReversed'; const CORNER_BRACKETS = 'cornerBrackets'; const WHITE_CORNER_BRACKETS = 'whiteCornerBracket'; + const NONE = 'noneAtAll'; /** * Available quote styles. @@ -123,6 +124,10 @@ abstract class Quote_Style { self::OPEN => U::LEFT_WHITE_CORNER_BRACKET, self::CLOSE => U::RIGHT_WHITE_CORNER_BRACKET, ], + self::NONE => [ + self::OPEN => '', + self::CLOSE => '', + ], ]; /** diff --git a/tests/class-settings-test.php b/tests/class-settings-test.php index 0ca613f..a79b793 100644 --- a/tests/class-settings-test.php +++ b/tests/class-settings-test.php @@ -75,7 +75,7 @@ protected function set_up() { */ public function test_set_defaults() { $second_settings = new \PHP_Typography\Settings( false ); - $this->assert_attribute_count( 2, 'data', $second_settings ); + $this->assert_attribute_count( 5, 'data', $second_settings ); $second_settings->set_defaults(); $this->assert_attribute_count( 55, 'data', $second_settings ); } @@ -94,11 +94,11 @@ public function test_initialization() { $s = $this->settings; // No defaults. - $this->assert_attribute_count( 2, 'data', $s ); + $this->assert_attribute_count( 5, 'data', $s ); // After set_defaults(). $s->set_defaults(); - $this->assert_attribute_not_count( 2, 'data', $s ); + $this->assert_attribute_count( 55, 'data', $s ); $second_settings = new \PHP_Typography\Settings( true ); $this->assert_attribute_count( 55, 'data', $second_settings ); @@ -340,6 +340,7 @@ public function test_set_smart_quotes_primary() { 'singleGuillemetsReversed', 'cornerBrackets', 'whiteCornerBracket', + 'noneAtAll', ]; foreach ( $quote_styles as $style ) { @@ -410,6 +411,7 @@ public function test_set_smart_quotes_secondary() { 'singleGuillemetsReversed', 'cornerBrackets', 'whiteCornerBracket', + 'noneAtAll', ]; foreach ( $quote_styles as $style ) { @@ -520,6 +522,14 @@ public function test_set_smart_dashes_style() { $this->assertSame( U::EN_DASH, $dashes->interval_dash() ); $this->assertSame( ' ', $dashes->parenthetical_space() ); $this->assertSame( '', $dashes->interval_space() ); + + $s->set_smart_dashes_style( 'noneAtAll' ); + $dashes = $s->dash_style; + + $this->assertSame( '', $dashes->parenthetical_dash() ); + $this->assertSame( '', $dashes->interval_dash() ); + $this->assertSame( '', $dashes->parenthetical_space() ); + $this->assertSame( '', $dashes->interval_space() ); } /** @@ -1436,11 +1446,6 @@ public function test_set_hyphenation_exceptions() { public function test_get_hash() { $s = $this->settings; - // Finish initialization. - $s->set_smart_quotes_primary(); - $s->set_smart_quotes_secondary(); - $s->set_smart_dashes_style(); - $s->set_smart_quotes( true ); $hash1 = $s->get_hash( 10 ); $this->assertEquals( 10, strlen( $hash1 ) ); diff --git a/tests/class-testcase.php b/tests/class-testcase.php index fec4b30..af698bc 100644 --- a/tests/class-testcase.php +++ b/tests/class-testcase.php @@ -242,6 +242,11 @@ protected function assert_smart_quotes_style( $style, $open, $close ) { $this->assertSame( U::RIGHT_WHITE_CORNER_BRACKET, $close, "Closing quote $close did not match quote style $style." ); break; + case 'noneAtAll': + $this->assertSame( '', $open, "Opening quote $open did not match quote style $style." ); + $this->assertSame( '', $close, "Closing quote $close did not match quote style $style." ); + break; + default: $this->assertTrue( false, "Invalid quote style $style." ); }