-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace Settings::apply_character_remapping with Unicode_Remapping_Fi…
…x in post-processing
- Loading branch information
1 parent
99a7a7e
commit f14c4ab
Showing
7 changed files
with
199 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/** | ||
* This file is part of PHP-Typography. | ||
* | ||
* Copyright 2024 Peter Putzer. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* | ||
* *** | ||
* | ||
* @package mundschenk-at/php-typography | ||
* @license http://www.gnu.org/licenses/gpl-2.0.html | ||
*/ | ||
|
||
namespace PHP_Typography\Fixes\Node_Fixes; | ||
|
||
use PHP_Typography\Settings; | ||
|
||
/** | ||
* Remaps Unicode characters. | ||
* | ||
* @author Peter Putzer <github@mundschenk.at> | ||
* | ||
* @since 7.0.0 | ||
*/ | ||
class Unicode_Remapping_Fix extends Abstract_Node_Fix { | ||
|
||
/** | ||
* Creates a new node fix. | ||
*/ | ||
public function __construct() { | ||
parent::__construct( true ); | ||
} | ||
|
||
/** | ||
* Apply the fix to a given textnode. | ||
* | ||
* @param \DOMText $textnode The DOM node. | ||
* @param Settings $settings The settings to apply. | ||
* @param bool $is_title Indicates if the processed tokens occur in a title/heading context. | ||
* | ||
* @return void | ||
*/ | ||
public function apply( \DOMText $textnode, Settings $settings, $is_title ) { | ||
if ( empty( $settings->unicode_character_mapping ) ) { | ||
return; | ||
} | ||
|
||
$textnode->data = \strtr( $textnode->data, $settings->unicode_character_mapping ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
tests/fixes/node-fixes/class-unicode-remapping-fix-test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
/** | ||
* This file is part of PHP-Typography. | ||
* | ||
* Copyright 2024 Peter Putzer. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* of the License, or ( at your option ) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* | ||
* @package mundschenk-at/php-typography/tests | ||
* @license http://www.gnu.org/licenses/gpl-2.0.html | ||
*/ | ||
|
||
namespace PHP_Typography\Tests\Fixes\Node_Fixes; | ||
|
||
use PHP_Typography\Fixes\Node_Fixes; | ||
use PHP_Typography\Settings; | ||
|
||
/** | ||
* Unicode_Remapping_Fix unit test. | ||
* | ||
* @coversDefaultClass \PHP_Typography\Fixes\Node_Fixes\Unicode_Remapping_Fix | ||
* @usesDefaultClass \PHP_Typography\Fixes\Node_Fixes\Unicode_Remapping_Fix | ||
* | ||
* @uses ::__construct | ||
* @uses PHP_Typography\Fixes\Node_Fixes\Abstract_Node_Fix::__construct | ||
* @uses PHP_Typography\DOM | ||
* @uses PHP_Typography\Settings | ||
* @uses PHP_Typography\Settings\Dash_Style | ||
* @uses PHP_Typography\Settings\Quote_Style | ||
* @uses PHP_Typography\Settings\Simple_Dashes | ||
* @uses PHP_Typography\Settings\Simple_Quotes | ||
* @uses PHP_Typography\Strings | ||
*/ | ||
class Unicode_Remapping_Fix_Test extends Node_Fix_Testcase { | ||
|
||
/** | ||
* Sets up the fixture, for example, opens a network connection. | ||
* This method is called before a test is executed. | ||
*/ | ||
protected function set_up() { | ||
parent::set_up(); | ||
|
||
$this->fix = new Node_Fixes\Unicode_Remapping_Fix(); | ||
} | ||
|
||
/** | ||
* Provides data for testing apply. | ||
* | ||
* @return array | ||
*/ | ||
public function provide_character_mapping_data() { | ||
return [ | ||
[ | ||
'foobar', | ||
[ | ||
'a' => 'A', | ||
'r' => 'z', | ||
], | ||
'foobAz', | ||
], | ||
[ | ||
'', | ||
[ | ||
'a' => 'A', | ||
'r' => 'z', | ||
], | ||
'', | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Test apply. | ||
* | ||
* @covers ::apply | ||
* @covers ::__construct | ||
* | ||
* @uses PHP_Typography\Fixes\Node_Fixes\Simple_Regex_Replacement_Fix::apply | ||
* | ||
* @dataProvider provide_character_mapping_data | ||
* | ||
* @param string $input HTML input. | ||
* @param string[] $mapping The character remapping to apply. | ||
* @param string $result Expected result. | ||
*/ | ||
public function test_apply( $input, array $mapping, $result ) { | ||
$this->s = new Settings( false, $mapping ); | ||
|
||
$this->assertFixResultSame( $input, $result ); | ||
} | ||
|
||
/** | ||
* Test apply. | ||
* | ||
* @covers ::apply | ||
* @covers ::__construct | ||
* | ||
* @uses PHP_Typography\Fixes\Node_Fixes\Simple_Regex_Replacement_Fix::apply | ||
* | ||
* @dataProvider provide_character_mapping_data | ||
* | ||
* @param string $input HTML input. | ||
*/ | ||
public function test_apply_off( $input ) { | ||
$this->s = new Settings( false, [] ); | ||
|
||
$this->assertFixResultSame( $input, $input ); | ||
} | ||
} |