Skip to content

Commit

Permalink
Fully initialize Settings even when $use_defaults is set to false
Browse files Browse the repository at this point in the history
  • Loading branch information
mundschenk-at committed Jun 13, 2024
1 parent b887952 commit 94a2df8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
14 changes: 8 additions & 6 deletions src/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions src/settings/class-dash-style.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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 => '',
],
];

/**
Expand Down
5 changes: 5 additions & 0 deletions src/settings/class-quote-style.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 => '',
],
];

/**
Expand Down
21 changes: 13 additions & 8 deletions tests/class-settings-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand All @@ -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 );
Expand Down Expand Up @@ -340,6 +340,7 @@ public function test_set_smart_quotes_primary() {
'singleGuillemetsReversed',
'cornerBrackets',
'whiteCornerBracket',
'noneAtAll',
];

foreach ( $quote_styles as $style ) {
Expand Down Expand Up @@ -410,6 +411,7 @@ public function test_set_smart_quotes_secondary() {
'singleGuillemetsReversed',
'cornerBrackets',
'whiteCornerBracket',
'noneAtAll',
];

foreach ( $quote_styles as $style ) {
Expand Down Expand Up @@ -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() );
}

/**
Expand Down Expand Up @@ -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 ) );
Expand Down
5 changes: 5 additions & 0 deletions tests/class-testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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." );
}
Expand Down

0 comments on commit 94a2df8

Please sign in to comment.