Skip to content

Commit

Permalink
ref #602 -- added tests for custom more tags
Browse files Browse the repository at this point in the history
  • Loading branch information
jarednova committed Jul 29, 2015
1 parent e215afe commit 79047e7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/timber-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ function get_preview($len = 50, $force = false, $readmore = 'Read More', $strip
$text = $this->post_excerpt;
}
}
if (!strlen($text) && preg_match('/<!--more(.*?)?-->/', $this->post_content, $readmore_matches)) {
if (!strlen($text) && preg_match('/<!--\s?more(.*?)?-->/', $this->post_content, $readmore_matches)) {
$pieces = explode($readmore_matches[0], $this->post_content);
$text = $pieces[0];
if ($force) {
Expand Down Expand Up @@ -253,9 +253,9 @@ function get_preview($len = 50, $force = false, $readmore = 'Read More', $strip
}
}
if ($readmore && (isset($readmore_matches) && !empty($readmore_matches[1]))) {
$text .= ' <a href="' . $this->get_permalink() . '" class="read-more">' . $readmore_matches[1] . '</a>';
$text .= ' <a href="' . $this->get_permalink() . '" class="read-more">' . trim($readmore_matches[1]) . '</a>';
} elseif ($readmore) {
$text .= ' <a href="' . $this->get_permalink() . '" class="read-more">' . $readmore . '</a>';
$text .= ' <a href="' . $this->get_permalink() . '" class="read-more">' . trim($readmore) . '</a>';
}
if (!$strip) {
$text .= '</p>';
Expand Down
12 changes: 12 additions & 0 deletions tests/test-timber-post-preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,16 @@ function testShortcodesInPreviewFromContentWithMoreTag() {
$this->assertEquals('jared Quack! <a href="'.$post->link().'" class="read-more">Read More</a>', $post->get_preview());
}

function testPreviewWithSpaceInMoreTag() {
$pid = $this->factory->post->create( array('post_content' => 'Lauren is a duck<!-- more--> Lauren is not a duck', 'post_excerpt' => '') );
$post = new TimberPost( $pid );
$this->assertEquals('Lauren is a duck <a href="'.$post->link().'" class="read-more">Read More</a>', $post->get_preview());
}

function testPreviewWithCustomMoreTag() {
$pid = $this->factory->post->create( array('post_content' => 'Eric is a polar bear <!-- more But what is Elaina? --> Lauren is not a duck', 'post_excerpt' => '') );
$post = new TimberPost( $pid );
$this->assertEquals('Eric is a polar bear <a href="'.$post->link().'" class="read-more">But what is Elaina?</a>', $post->get_preview());
}

}

0 comments on commit 79047e7

Please sign in to comment.