Skip to content

Commit

Permalink
Merge pull request #17 from mundschenk-at/fix-hyphenator-caching
Browse files Browse the repository at this point in the history
Fix hyphenator caching
  • Loading branch information
mundschenk-at authored Aug 26, 2017
2 parents d70afd5 + cfa904c commit dffb74c
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Change History

## 5.0.0. - unreleased
## 5.0.1. - August 26, 2017
* _Bugfix_: Hyphenator caching was not really working.

## 5.0.0. - August 13, 2017

* _Feature_: Use Composer for dependencies.
* _Change_: API refactoring:
Expand Down
2 changes: 1 addition & 1 deletion src/class-dom.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static function get_ancestors( \DOMNode $node ) {
* @param \DOMNode $tag An element or textnode.
* @param string|array $classnames A single classname or an array of classnames.
*
* @return boolean True if the element has any of the given class(es).
* @return bool True if the element has any of the given class(es).
*/
public static function has_class( \DOMNode $tag, $classnames ) {
if ( $tag instanceof \DOMText ) {
Expand Down
5 changes: 5 additions & 0 deletions src/class-php-typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,11 @@ public function get_hyphenator_cache() {
*/
public function set_hyphenator_cache( Hyphenator_Cache $cache ) {
$this->hyphenator_cache = $cache;

// Change hyphenator cache for existing node fixes.
if ( null !== $this->process_words_fix ) {
$this->process_words_fix->update_hyphenator_cache( $cache );
}
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/fixes/node-fixes/class-process-words-fix.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
use \PHP_Typography\Text_Parser;
use \PHP_Typography\Settings;
use \PHP_Typography\DOM;
use \PHP_Typography\Hyphenator_Cache;
use \PHP_Typography\Fixes\Token_Fix;
use \PHP_Typography\Fixes\Token_Fixes\Hyphenate_Fix;

/**
* Tokenizes the content of a textnode and process the individual words separately.
Expand Down Expand Up @@ -119,4 +121,17 @@ public function get_text_parser() {
public function register_token_fix( Token_Fix $fix ) {
$this->token_fixes[] = $fix;
}

/**
* Sets the hyphenator cache for all registered token fixes (that require one).
*
* @param Hyphenator_Cache $cache A cache instance.
*/
public function update_hyphenator_cache( Hyphenator_Cache $cache ) {
foreach ( $this->token_fixes as $fix ) {
if ( $fix instanceof Hyphenate_Fix ) {
$fix->set_hyphenator_cache( $cache );
}
}
}
}
19 changes: 19 additions & 0 deletions tests/class-hyphenator-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,25 @@ public function test_hyphenate_invalid() {
$this->assertEquals( $tokens, $hyphenated );
}

/**
* Tests lookup_word_pattern.
*
* @covers ::lookup_word_pattern
*
* @uses PHP_Typography\Hyphenator\Trie_Node
* @uses PHP_Typography\Text_Parser\Token
* @uses PHP_Typography\Strings::functions
* @uses PHP_Typography\Strings::mb_str_split
*/
public function test_lookup_word_pattern_invalid_pattern_trie() {
$string = 'unknown';

// Make pattern trie invalid.
$this->setValue( $this->h, 'pattern_trie', null );

$this->assertSame( [], $this->invokeMethod( $this->h, 'lookup_word_pattern', [ $string, 'strlen', 'str_split' ] ) );
}

/**
* Tests hyphenate.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/fixes/node-fixes/class-process-words-fix-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

use \PHP_Typography\Fixes\Node_Fixes;
use \PHP_Typography\Fixes\Token_Fix;
use \PHP_Typography\Fixes\Token_Fixes\Hyphenate_Fix;
use \PHP_Typography\Hyphenator_Cache;
use \PHP_Typography\Settings;

/**
Expand Down Expand Up @@ -139,4 +141,26 @@ public function test_register_token_fix() {
$this->fix->register_token_fix( $fake_token_fixer );
$this->assertAttributeContains( $fake_token_fixer, 'token_fixes', $this->fix, 'The registered fixer is not present in the $token_fixes array.' );
}

/**
* Tests update_hyphenator_cache.
*
* @covers ::update_hyphenator_cache
*
* @uses ::register_token_fix
* @uses PHP_Typography\Fixes\Token_Fixes\Hyphenate_Fix
* @uses PHP_Typography\Fixes\Token_Fixes\Abstract_Token_Fix::__construct
*/
public function test_update_hyphenator_cache() {
// Create a stub for the Hyphenator_Cache class.
$fake_cache = $this->createMock( Hyphenator_Cache::class );

// Create a stub for the Hyphenate_Fix class.
$token_fixer = new Hyphenate_Fix();

$this->fix->register_token_fix( $token_fixer );
$this->fix->update_hyphenator_cache( $fake_cache );

$this->assertAttributeSame( $fake_cache, 'cache', $token_fixer, 'The hyphenator cache was not update correctly.' );
}
}

0 comments on commit dffb74c

Please sign in to comment.