Skip to content

Commit

Permalink
Merge pull request #170 from adonisfigueroa/master
Browse files Browse the repository at this point in the history
Transform embed code object tags
  • Loading branch information
Sigurður Guðbrandsson committed Mar 14, 2017
2 parents 476cae4 + 37482c8 commit bdf10c4
Show file tree
Hide file tree
Showing 11 changed files with 507 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/AMP.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class AMP

// The StandardScanPass should be first after all transform passes
// The StandardFixPass should be after StandardScanPass
// The ObjectVideoTagTransformPass should be after all Object transform passes
public $passes = [
'Lullabot\AMP\Pass\PreliminaryPass', // Removes user blacklisted tags
'Lullabot\AMP\Pass\ImgTagTransformPass',
Expand All @@ -63,6 +64,9 @@ class AMP
'Lullabot\AMP\Pass\PinterestTagTransformPass',
'Lullabot\AMP\Pass\FacebookNonIframeTransformPass',
'Lullabot\AMP\Pass\TwitterTransformPass',
'Lullabot\AMP\Pass\ObjectYouTubeTagTransformPass',
'Lullabot\AMP\Pass\ObjectVimeoTagTransformPass',
'Lullabot\AMP\Pass\ObjectVideoTagTransformPass',
'Lullabot\AMP\Pass\StandardScanPass',
'Lullabot\AMP\Pass\StandardFixPass',
'Lullabot\AMP\Pass\AmpImgFixPass',
Expand Down
112 changes: 112 additions & 0 deletions src/Pass/ObjectVideoTagTransformPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/*
* Copyright 2016 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


namespace Lullabot\AMP\Pass;

use QueryPath\DOMQuery;

use Lullabot\AMP\Utility\ActionTakenLine;
use Lullabot\AMP\Utility\ActionTakenType;

/**
* Class ObjectVideoTagTransformPass
* @package Lullabot\AMP\Pass
*
* Transform all Video embed code <object> tags
*
* This is what a video embed looks like:
* <object width="480" height="270">
* <param name="movie" value="http://video.golem.de/player/videoplayer.swf?id=2883&autoPl=false">
* <param name="allowFullScreen" value="true">
* <param name="AllowScriptAccess" value="always">
* <embed src="http://video.golem.de/player/videoplayer.swf?id=2883&autoPl=false" type="application/x-shockwave-flash" allowfullscreen="true" AllowScriptAccess="always" width="480" height="270"></embed>
* </object>
*/
class ObjectVideoTagTransformPass extends BasePass
{
function pass()
{
$all_objects = $this->q->find('object:not(noscript object)');
/** @var DOMQuery $el */
foreach ($all_objects as $el) {
/** @var \DOMElement $dom_el */
$dom_el = $el->get(0);
$lineno = $this->getLineNo($dom_el);
$context_string = $this->getContextString($dom_el);

$actionTakenType = '';

if ($this->isVideoObject($el)) {
$video_url = $this->getVideoUrl($el);

if (empty($video_url)) {
continue;
}

$video_url = htmlspecialchars($video_url, ENT_QUOTES);
$el->after("<a target=\"_blank\" href=\"{$video_url}\">{$video_url}</a>");
$new_dom_el = $el->next()->get(0);

$actionTakenType = ActionTakenType::OBJECT_CONVERTED_TO_A;
} else {
continue;
}

// Remove the object and its children
$el->removeChildren()->remove();
$this->addActionTaken(new ActionTakenLine('object', $actionTakenType, $lineno, $context_string));
$this->context->addLineAssociation($new_dom_el, $lineno);

}

return $this->transformations;
}

protected function isVideoObject(DOMQuery $el)
{
$params = $el->find('param');
foreach ($params as $param) {
if ($param->attr('name') == 'movie') {
return true;
}
}
return false;
}

/**
*
* Get the Video Url
*
* @param DOMQuery $el
* @return string
*/
protected function getVideoUrl(DOMQuery $el)
{
$matches = [];
$video_url = '';
$params = $el->find('param');

foreach ($params as $param) {
if ($param->attr('name') == 'movie') {
return $param->attr('value');
}
}

return $video_url;
}
}
127 changes: 127 additions & 0 deletions src/Pass/ObjectVimeoTagTransformPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
/*
* Copyright 2016 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


namespace Lullabot\AMP\Pass;

use QueryPath\DOMQuery;

use Lullabot\AMP\Utility\ActionTakenLine;
use Lullabot\AMP\Utility\ActionTakenType;

/**
* Class ObjectVimeoTagTransformPass
* @package Lullabot\AMP\Pass
*
* Transform all Vimeo embed code <object> tags which don't have noscript as an ancestor to <amp-vimeo> tags
*
* This is what a vimeo embed looks like:
* <object width="550" height="309">
* <param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=12223465&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" />
* <param name="allowfullscreen" value="true" />
* <param name="allowscriptaccess" value="always" />
* <embed src="http://vimeo.com/moogaloop.swf?clip_id=12223465&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="550" height="309"></embed>
* </object>
*
* @see https://github.com/ampproject/amphtml/blob/master/extensions/amp-vimeo/amp-vimeo.md
*
*/
class ObjectVimeoTagTransformPass extends BasePass
{
function pass()
{
$all_objects = $this->q->find('object:not(noscript object)');
/** @var DOMQuery $el */
foreach ($all_objects as $el) {
/** @var \DOMElement $dom_el */
$dom_el = $el->get(0);
$lineno = $this->getLineNo($dom_el);
$context_string = $this->getContextString($dom_el);

$actionTakenType = '';

if ($this->isVimeoObject($el)) {
$vimeo_code = $this->getVimeoCode($el);

// If we couldnt find a vimeo videoid then we abort
if (empty($vimeo_code)) {
continue;
}

$el->after('<amp-vimeo width="16" height="9" layout="responsive" data-videoid="' . $vimeo_code . '"></amp-vimeo>');
$new_dom_el = $el->next()->get(0);

$actionTakenType = ActionTakenType::VIMEO_OBJECT_CONVERTED;

} else {
continue;
}

// Remove the object and its children
$el->removeChildren()->remove();
$this->addActionTaken(new ActionTakenLine('object', $actionTakenType, $lineno, $context_string));
$this->context->addLineAssociation($new_dom_el, $lineno);

}

return $this->transformations;
}

protected function isVimeoObject(DOMQuery $el)
{
$params = $el->find('param');
foreach ($params as $param) {
if ($param->attr('name') == 'movie') {
$param_value = $param->attr('value');
if (preg_match('&(*UTF8)(vimeo\.com)&i', $param_value)) {
return true;
}
}
}
return false;
}

/**
*
* Get the Vimeo Code
*
* @param DOMQuery $el
* @return string
*/
protected function getVimeoCode(DOMQuery $el)
{
$matches = [];
$vimeo_code = '';
$params = $el->find('param');

foreach ($params as $param) {
if ($param->attr('name') == 'movie') {
$param_value = $param->attr('value');

$pattern = '#http://(?:\w+.)?vimeo.com/(?:video/|moogaloop\.swf\?clip_id=)(\w+)#i';
if (preg_match($pattern, $param_value, $matches)) {
if (!empty($matches[1])) {
$vimeo_code = $matches[1];
return $vimeo_code;
}
}
}
}

return $vimeo_code;
}
}
Loading

0 comments on commit bdf10c4

Please sign in to comment.