Skip to content

Commit bdf10c4

Browse files
author
Sigurður Guðbrandsson
authored
Merge pull request #170 from adonisfigueroa/master
Transform embed code object tags
2 parents 476cae4 + 37482c8 commit bdf10c4

11 files changed

+507
-0
lines changed

src/AMP.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class AMP
4747

4848
// The StandardScanPass should be first after all transform passes
4949
// The StandardFixPass should be after StandardScanPass
50+
// The ObjectVideoTagTransformPass should be after all Object transform passes
5051
public $passes = [
5152
'Lullabot\AMP\Pass\PreliminaryPass', // Removes user blacklisted tags
5253
'Lullabot\AMP\Pass\ImgTagTransformPass',
@@ -63,6 +64,9 @@ class AMP
6364
'Lullabot\AMP\Pass\PinterestTagTransformPass',
6465
'Lullabot\AMP\Pass\FacebookNonIframeTransformPass',
6566
'Lullabot\AMP\Pass\TwitterTransformPass',
67+
'Lullabot\AMP\Pass\ObjectYouTubeTagTransformPass',
68+
'Lullabot\AMP\Pass\ObjectVimeoTagTransformPass',
69+
'Lullabot\AMP\Pass\ObjectVideoTagTransformPass',
6670
'Lullabot\AMP\Pass\StandardScanPass',
6771
'Lullabot\AMP\Pass\StandardFixPass',
6872
'Lullabot\AMP\Pass\AmpImgFixPass',
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
/*
3+
* Copyright 2016 Google
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
19+
namespace Lullabot\AMP\Pass;
20+
21+
use QueryPath\DOMQuery;
22+
23+
use Lullabot\AMP\Utility\ActionTakenLine;
24+
use Lullabot\AMP\Utility\ActionTakenType;
25+
26+
/**
27+
* Class ObjectVideoTagTransformPass
28+
* @package Lullabot\AMP\Pass
29+
*
30+
* Transform all Video embed code <object> tags
31+
*
32+
* This is what a video embed looks like:
33+
* <object width="480" height="270">
34+
* <param name="movie" value="http://video.golem.de/player/videoplayer.swf?id=2883&autoPl=false">
35+
* <param name="allowFullScreen" value="true">
36+
* <param name="AllowScriptAccess" value="always">
37+
* <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>
38+
* </object>
39+
*/
40+
class ObjectVideoTagTransformPass extends BasePass
41+
{
42+
function pass()
43+
{
44+
$all_objects = $this->q->find('object:not(noscript object)');
45+
/** @var DOMQuery $el */
46+
foreach ($all_objects as $el) {
47+
/** @var \DOMElement $dom_el */
48+
$dom_el = $el->get(0);
49+
$lineno = $this->getLineNo($dom_el);
50+
$context_string = $this->getContextString($dom_el);
51+
52+
$actionTakenType = '';
53+
54+
if ($this->isVideoObject($el)) {
55+
$video_url = $this->getVideoUrl($el);
56+
57+
if (empty($video_url)) {
58+
continue;
59+
}
60+
61+
$video_url = htmlspecialchars($video_url, ENT_QUOTES);
62+
$el->after("<a target=\"_blank\" href=\"{$video_url}\">{$video_url}</a>");
63+
$new_dom_el = $el->next()->get(0);
64+
65+
$actionTakenType = ActionTakenType::OBJECT_CONVERTED_TO_A;
66+
} else {
67+
continue;
68+
}
69+
70+
// Remove the object and its children
71+
$el->removeChildren()->remove();
72+
$this->addActionTaken(new ActionTakenLine('object', $actionTakenType, $lineno, $context_string));
73+
$this->context->addLineAssociation($new_dom_el, $lineno);
74+
75+
}
76+
77+
return $this->transformations;
78+
}
79+
80+
protected function isVideoObject(DOMQuery $el)
81+
{
82+
$params = $el->find('param');
83+
foreach ($params as $param) {
84+
if ($param->attr('name') == 'movie') {
85+
return true;
86+
}
87+
}
88+
return false;
89+
}
90+
91+
/**
92+
*
93+
* Get the Video Url
94+
*
95+
* @param DOMQuery $el
96+
* @return string
97+
*/
98+
protected function getVideoUrl(DOMQuery $el)
99+
{
100+
$matches = [];
101+
$video_url = '';
102+
$params = $el->find('param');
103+
104+
foreach ($params as $param) {
105+
if ($param->attr('name') == 'movie') {
106+
return $param->attr('value');
107+
}
108+
}
109+
110+
return $video_url;
111+
}
112+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
/*
3+
* Copyright 2016 Google
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
19+
namespace Lullabot\AMP\Pass;
20+
21+
use QueryPath\DOMQuery;
22+
23+
use Lullabot\AMP\Utility\ActionTakenLine;
24+
use Lullabot\AMP\Utility\ActionTakenType;
25+
26+
/**
27+
* Class ObjectVimeoTagTransformPass
28+
* @package Lullabot\AMP\Pass
29+
*
30+
* Transform all Vimeo embed code <object> tags which don't have noscript as an ancestor to <amp-vimeo> tags
31+
*
32+
* This is what a vimeo embed looks like:
33+
* <object width="550" height="309">
34+
* <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" />
35+
* <param name="allowfullscreen" value="true" />
36+
* <param name="allowscriptaccess" value="always" />
37+
* <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>
38+
* </object>
39+
*
40+
* @see https://github.com/ampproject/amphtml/blob/master/extensions/amp-vimeo/amp-vimeo.md
41+
*
42+
*/
43+
class ObjectVimeoTagTransformPass extends BasePass
44+
{
45+
function pass()
46+
{
47+
$all_objects = $this->q->find('object:not(noscript object)');
48+
/** @var DOMQuery $el */
49+
foreach ($all_objects as $el) {
50+
/** @var \DOMElement $dom_el */
51+
$dom_el = $el->get(0);
52+
$lineno = $this->getLineNo($dom_el);
53+
$context_string = $this->getContextString($dom_el);
54+
55+
$actionTakenType = '';
56+
57+
if ($this->isVimeoObject($el)) {
58+
$vimeo_code = $this->getVimeoCode($el);
59+
60+
// If we couldnt find a vimeo videoid then we abort
61+
if (empty($vimeo_code)) {
62+
continue;
63+
}
64+
65+
$el->after('<amp-vimeo width="16" height="9" layout="responsive" data-videoid="' . $vimeo_code . '"></amp-vimeo>');
66+
$new_dom_el = $el->next()->get(0);
67+
68+
$actionTakenType = ActionTakenType::VIMEO_OBJECT_CONVERTED;
69+
70+
} else {
71+
continue;
72+
}
73+
74+
// Remove the object and its children
75+
$el->removeChildren()->remove();
76+
$this->addActionTaken(new ActionTakenLine('object', $actionTakenType, $lineno, $context_string));
77+
$this->context->addLineAssociation($new_dom_el, $lineno);
78+
79+
}
80+
81+
return $this->transformations;
82+
}
83+
84+
protected function isVimeoObject(DOMQuery $el)
85+
{
86+
$params = $el->find('param');
87+
foreach ($params as $param) {
88+
if ($param->attr('name') == 'movie') {
89+
$param_value = $param->attr('value');
90+
if (preg_match('&(*UTF8)(vimeo\.com)&i', $param_value)) {
91+
return true;
92+
}
93+
}
94+
}
95+
return false;
96+
}
97+
98+
/**
99+
*
100+
* Get the Vimeo Code
101+
*
102+
* @param DOMQuery $el
103+
* @return string
104+
*/
105+
protected function getVimeoCode(DOMQuery $el)
106+
{
107+
$matches = [];
108+
$vimeo_code = '';
109+
$params = $el->find('param');
110+
111+
foreach ($params as $param) {
112+
if ($param->attr('name') == 'movie') {
113+
$param_value = $param->attr('value');
114+
115+
$pattern = '#http://(?:\w+.)?vimeo.com/(?:video/|moogaloop\.swf\?clip_id=)(\w+)#i';
116+
if (preg_match($pattern, $param_value, $matches)) {
117+
if (!empty($matches[1])) {
118+
$vimeo_code = $matches[1];
119+
return $vimeo_code;
120+
}
121+
}
122+
}
123+
}
124+
125+
return $vimeo_code;
126+
}
127+
}

0 commit comments

Comments
 (0)