Skip to content

Commit f14c4ab

Browse files
committed
Replace Settings::apply_character_remapping with Unicode_Remapping_Fix in post-processing
1 parent 99a7a7e commit f14c4ab

7 files changed

+199
-66
lines changed

src/class-php-typography.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ private function process_textnodes_internal( \DOMDocument $dom, \DOMNode $body_n
227227

228228
// Replace original node (if anything was changed).
229229
if ( $new !== $original ) {
230-
$this->replace_node_with_html( $textnode, $settings->apply_character_mapping( $new ) );
230+
$this->replace_node_with_html( $textnode, $new );
231231
}
232232
}
233233
}

src/class-settings.php

+9-29
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
* - `$unicode_mapping`
5353
*
5454
* Additional removed methods:
55+
* - `apply_character_mapping`
5556
* - `array_map_assoc` (previously deprecated)
5657
* - custom_unit
5758
* - dash_style
@@ -133,6 +134,9 @@
133134
* @property-read bool $ignore_parser_errors Whether lenient parser error handling (output "best guess" HTML) is enabled.
134135
* @property-read ?callable $parser_errors_handler An optional handler for parser errors. The callable takes an array of error strings as its parameter.
135136
*
137+
* Post-processing:
138+
* @property-read array<string,string> $unicode_character_mapping The Unicode character mapping (as some characters still have compatibility issues).
139+
*
136140
* Setters for general attributes:
137141
* @method void set_classes_to_ignore( string[] $classes = ['vcard','noTypo'] ) Sets classes for which the typography of their children will be left untouched.
138142
* @method void set_ids_to_ignore( string[] $ids = [] ) Sets IDs for which the typography of their children will be left untouched.
@@ -262,7 +266,7 @@ class Settings {
262266
const PARSER_ERRORS_IGNORE = 'parserErrorsIgnore';
263267
const PARSER_ERRORS_HANDLER = 'parserErrorsHandler';
264268

265-
// Unicode character remapping (some characters still have compatibility issues).
269+
// Post-processing.
266270
const UNICODE_CHARACTER_MAPPING = 'unicodeCharacterMapping';
267271

268272
/**
@@ -596,6 +600,10 @@ class Settings {
596600
'default' => true,
597601
'verify' => 'is_bool',
598602
],
603+
[
604+
'property' => self::UNICODE_CHARACTER_MAPPING,
605+
'name' => 'unicode_character_mapping',
606+
],
599607
];
600608

601609
/**
@@ -759,34 +767,6 @@ public function remap_character( string $char, string $new_char ): void {
759767
}
760768
}
761769

762-
/**
763-
* Remaps one or more strings.
764-
*
765-
* @since 6.5.0
766-
*
767-
* @template T of string|string[]
768-
*
769-
* @param T $input The input string(s).
770-
*
771-
* @return T
772-
*/
773-
public function apply_character_mapping( $input ) {
774-
775-
// Nothing for us to do.
776-
if ( empty( $input ) || empty( $this->data[ self::UNICODE_CHARACTER_MAPPING ] ) ) {
777-
return $input;
778-
}
779-
780-
$native_array = \is_array( $input );
781-
$data = (array) $input;
782-
783-
foreach ( $data as $key => $string ) {
784-
$data[ $key ] = \strtr( $string, $this->data[ self::UNICODE_CHARACTER_MAPPING ] );
785-
}
786-
787-
return $native_array ? $data : $data[0]; // @phpstan-ignore-line -- Ignore generics/array clash
788-
}
789-
790770
/**
791771
* (Re)set various options to their default values.
792772
*/

src/fixes/class-default-registry.php

+4
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ protected static function get_default_node_fixes(): array {
188188
'classes' => [ 'push-single', 'push-double', 'pull-single', 'pull-double' ],
189189
],
190190
],
191+
192+
self::POST_PROCESSING => [
193+
Node_Fixes\Unicode_Remapping_Fix::class => [],
194+
],
191195
];
192196
}
193197

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* This file is part of PHP-Typography.
4+
*
5+
* Copyright 2024 Peter Putzer.
6+
*
7+
* This program is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License along
18+
* with this program; if not, write to the Free Software Foundation, Inc.,
19+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20+
*
21+
* ***
22+
*
23+
* @package mundschenk-at/php-typography
24+
* @license http://www.gnu.org/licenses/gpl-2.0.html
25+
*/
26+
27+
namespace PHP_Typography\Fixes\Node_Fixes;
28+
29+
use PHP_Typography\Settings;
30+
31+
/**
32+
* Remaps Unicode characters.
33+
*
34+
* @author Peter Putzer <[email protected]>
35+
*
36+
* @since 7.0.0
37+
*/
38+
class Unicode_Remapping_Fix extends Abstract_Node_Fix {
39+
40+
/**
41+
* Creates a new node fix.
42+
*/
43+
public function __construct() {
44+
parent::__construct( true );
45+
}
46+
47+
/**
48+
* Apply the fix to a given textnode.
49+
*
50+
* @param \DOMText $textnode The DOM node.
51+
* @param Settings $settings The settings to apply.
52+
* @param bool $is_title Indicates if the processed tokens occur in a title/heading context.
53+
*
54+
* @return void
55+
*/
56+
public function apply( \DOMText $textnode, Settings $settings, $is_title ) {
57+
if ( empty( $settings->unicode_character_mapping ) ) {
58+
return;
59+
}
60+
61+
$textnode->data = \strtr( $textnode->data, $settings->unicode_character_mapping );
62+
}
63+
}

tests/class-php-typography-test.php

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
* @uses PHP_Typography\Fixes\Node_Fixes\Style_Ampersands_Fix
9595
* @uses PHP_Typography\Fixes\Node_Fixes\Style_Caps_Fix
9696
* @uses PHP_Typography\Fixes\Node_Fixes\Style_Numbers_Fix
97+
* @uses PHP_Typography\Fixes\Node_Fixes\Unicode_Remapping_Fix
9798
* @uses PHP_Typography\Fixes\Node_Fixes\Unit_Spacing_Fix
9899
*/
99100
class PHP_Typography_Test extends Testcase {

tests/class-settings-test.php

-36
Original file line numberDiff line numberDiff line change
@@ -1508,40 +1508,4 @@ public function test_remap_character() {
15081508
$this->assertCount( 2, $data[ Settings::UNICODE_CHARACTER_MAPPING ] );
15091509
$this->assertContains( 'x', $data[ Settings::UNICODE_CHARACTER_MAPPING ] );
15101510
}
1511-
1512-
1513-
/**
1514-
* Provides data for testing apply_character_mapping.
1515-
*
1516-
* @return array
1517-
*/
1518-
public function provide_apply_character_mapping_data() {
1519-
return [
1520-
[ 'foobar', 'foobAz' ],
1521-
[ [ 'foobar' ], [ 'foobAz' ] ],
1522-
[ [ 'foobar', 'fugazi' ], [ 'foobAz', 'fugAzi' ] ],
1523-
[ '', '' ],
1524-
];
1525-
}
1526-
1527-
/**
1528-
* Tests apply_character_mapping.
1529-
*
1530-
* @covers ::apply_character_mapping
1531-
*
1532-
* @dataProvider provide_apply_character_mapping_data
1533-
*
1534-
* @param string|string[] $input The input.
1535-
* @param string|string[] $result The expected result.
1536-
*/
1537-
public function test_apply_character_mapping( $input, $result ) {
1538-
$mapping = [
1539-
'a' => 'A',
1540-
'r' => 'z',
1541-
];
1542-
1543-
$s = new Settings( false, $mapping );
1544-
1545-
$this->assertSame( $result, $s->apply_character_mapping( $input ) );
1546-
}
15471511
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
/**
3+
* This file is part of PHP-Typography.
4+
*
5+
* Copyright 2024 Peter Putzer.
6+
*
7+
* This program is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* of the License, or ( at your option ) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License along
18+
* with this program; if not, write to the Free Software Foundation, Inc.,
19+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20+
*
21+
* @package mundschenk-at/php-typography/tests
22+
* @license http://www.gnu.org/licenses/gpl-2.0.html
23+
*/
24+
25+
namespace PHP_Typography\Tests\Fixes\Node_Fixes;
26+
27+
use PHP_Typography\Fixes\Node_Fixes;
28+
use PHP_Typography\Settings;
29+
30+
/**
31+
* Unicode_Remapping_Fix unit test.
32+
*
33+
* @coversDefaultClass \PHP_Typography\Fixes\Node_Fixes\Unicode_Remapping_Fix
34+
* @usesDefaultClass \PHP_Typography\Fixes\Node_Fixes\Unicode_Remapping_Fix
35+
*
36+
* @uses ::__construct
37+
* @uses PHP_Typography\Fixes\Node_Fixes\Abstract_Node_Fix::__construct
38+
* @uses PHP_Typography\DOM
39+
* @uses PHP_Typography\Settings
40+
* @uses PHP_Typography\Settings\Dash_Style
41+
* @uses PHP_Typography\Settings\Quote_Style
42+
* @uses PHP_Typography\Settings\Simple_Dashes
43+
* @uses PHP_Typography\Settings\Simple_Quotes
44+
* @uses PHP_Typography\Strings
45+
*/
46+
class Unicode_Remapping_Fix_Test extends Node_Fix_Testcase {
47+
48+
/**
49+
* Sets up the fixture, for example, opens a network connection.
50+
* This method is called before a test is executed.
51+
*/
52+
protected function set_up() {
53+
parent::set_up();
54+
55+
$this->fix = new Node_Fixes\Unicode_Remapping_Fix();
56+
}
57+
58+
/**
59+
* Provides data for testing apply.
60+
*
61+
* @return array
62+
*/
63+
public function provide_character_mapping_data() {
64+
return [
65+
[
66+
'foobar',
67+
[
68+
'a' => 'A',
69+
'r' => 'z',
70+
],
71+
'foobAz',
72+
],
73+
[
74+
'',
75+
[
76+
'a' => 'A',
77+
'r' => 'z',
78+
],
79+
'',
80+
],
81+
];
82+
}
83+
84+
/**
85+
* Test apply.
86+
*
87+
* @covers ::apply
88+
* @covers ::__construct
89+
*
90+
* @uses PHP_Typography\Fixes\Node_Fixes\Simple_Regex_Replacement_Fix::apply
91+
*
92+
* @dataProvider provide_character_mapping_data
93+
*
94+
* @param string $input HTML input.
95+
* @param string[] $mapping The character remapping to apply.
96+
* @param string $result Expected result.
97+
*/
98+
public function test_apply( $input, array $mapping, $result ) {
99+
$this->s = new Settings( false, $mapping );
100+
101+
$this->assertFixResultSame( $input, $result );
102+
}
103+
104+
/**
105+
* Test apply.
106+
*
107+
* @covers ::apply
108+
* @covers ::__construct
109+
*
110+
* @uses PHP_Typography\Fixes\Node_Fixes\Simple_Regex_Replacement_Fix::apply
111+
*
112+
* @dataProvider provide_character_mapping_data
113+
*
114+
* @param string $input HTML input.
115+
*/
116+
public function test_apply_off( $input ) {
117+
$this->s = new Settings( false, [] );
118+
119+
$this->assertFixResultSame( $input, $input );
120+
}
121+
}

0 commit comments

Comments
 (0)