Some cleanup and a working test

This commit is contained in:
Andy Miller
2016-10-06 19:01:48 -06:00
parent ce2b7d7175
commit 8d84b94bc7
2 changed files with 36 additions and 5 deletions

View File

@@ -25,7 +25,19 @@ class Excerpts
public static function processImageHtml($html, $page)
{
$excerpt = static::getExcerptFromHtml($html, 'img');
$original_src = $excerpt['element']['attributes']['src'];
$excerpt['element']['attributes']['href'] = $original_src;
$excerpt = static::processLinkExcerpt($excerpt, $page, 'image');
$excerpt['element']['attributes']['src'] = $excerpt['element']['attributes']['href'];
unset ($excerpt['element']['attributes']['href']);
$excerpt = static::processImageExcerpt($excerpt, $page);
$excerpt['element']['attributes']['data-src'] = $original_src;
$html = static::getHtmlFromExcerpt($excerpt);
return $html;
@@ -61,6 +73,12 @@ class Excerpts
return $excerpt;
}
/**
* Rebuild HTML tag from an excerpt array
*
* @param $excerpt
* @return string
*/
public static function getHtmlFromExcerpt($excerpt)
{
$element = $excerpt['element'];
@@ -86,6 +104,14 @@ class Excerpts
return $html;
}
/**
* Process a Link excerpt
*
* @param $excerpt
* @param $page
* @param string $type
* @return mixed
*/
public static function processLinkExcerpt($excerpt, $page, $type = 'link')
{
$url = $excerpt['element']['attributes']['href'];
@@ -151,6 +177,13 @@ class Excerpts
return $excerpt;
}
/**
* Process an image excerpt
*
* @param $excerpt
* @param $page
* @return mixed
*/
public static function processImageExcerpt($excerpt, $page)
{
$url = $excerpt['element']['attributes']['src'];
@@ -208,8 +241,8 @@ class Excerpts
$medium->urlHash($url_parts['fragment']);
}
$alt = $excerpt['element']['attributes']['alt'] ?: '';
$title = $excerpt['element']['attributes']['title'] ?: '';
$alt = isset($excerpt['element']['attributes']['alt']) ? $excerpt['element']['attributes']['alt'] : '';
$title = isset($excerpt['element']['attributes']['title']) ? $excerpt['element']['attributes']['title'] : '';
$class = isset($excerpt['element']['attributes']['class']) ? $excerpt['element']['attributes']['class'] : '';
$id = isset($excerpt['element']['attributes']['id']) ? $excerpt['element']['attributes']['id'] : '';

View File

@@ -76,9 +76,7 @@ class ExcerptsTest extends \Codeception\TestCase\Test
public function testProcessImageHtml()
{
$this->assertSame('<img src="blah" />',
$this->assertRegexp('|<img alt="Sample Image" src="\/images\/.*-sample-image.jpe?g\" data-src="sample-image\.jpg\?cropZoom=300,300" \/>|',
Excerpts::processImageHtml('<img src="sample-image.jpg?cropZoom=300,300" alt="Sample Image" />', $this->page));
}