Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
49b376e
Add fixed layout option by width for images
FuzzyTree Sep 23, 2016
0f71259
Add amp-ad support. Regenerated validator-generated.php file with cus…
FuzzyTree Sep 24, 2016
ab6eea9
Add support for additional Facebook embed url schemas
FuzzyTree Sep 25, 2016
2f9a7ed
Fix height check on setResponsiveImgHeightAndWidth
FuzzyTree Oct 5, 2016
c58b205
Fix unit tests
FuzzyTree Oct 5, 2016
3339a46
Fix for the Youtube code getter
mayrop Dec 13, 2016
262b834
Adding regexp to second youtube getter
mayrop Dec 13, 2016
1dac838
Update README.md
Cashewz Jan 13, 2017
c1169e5
initial step for some hulu support
dawehner Feb 7, 2017
c095c0d
fix typo
dawehner Feb 8, 2017
dda5e38
Update the specfile generated by https://github.com/Lullabot/amphtml/…
dawehner Feb 8, 2017
d10e691
Merge branch 'update-spec-file' into hulu-support
dawehner Feb 8, 2017
3f4990f
Add amp-sidebar component mapping on context.php
Feb 8, 2017
ea94a03
Merge pull request #144 from FuzzyTree/amp-img-fixed-layout
sebastianbenz Feb 8, 2017
86f3254
Merge pull request #145 from FuzzyTree/amp-ad
sebastianbenz Feb 8, 2017
e01d6b0
Merge pull request #146 from FuzzyTree/facebook-photos
sebastianbenz Feb 8, 2017
19fa863
Merge pull request #149 from FuzzyTree/amp-img-height
sebastianbenz Feb 8, 2017
fdf242a
Merge pull request #152 from mayrop/master
sebastianbenz Feb 8, 2017
fbfe225
Merge pull request #154 from Cashewz/patch-1
sebastianbenz Feb 8, 2017
b91b246
Merge pull request #163 from renzit/master
sebastianbenz Feb 8, 2017
ad852d3
Badly formatted youtube URLs cause a parse error
Jan 24, 2017
9bfcb27
fix-image-height-missing
Deuh Jan 26, 2017
514a743
youtube-start-time : Fix IframeYotubeTransformPass::getYouTubeCode()
Deuh Jan 27, 2017
5265d55
Adapt the hulu pass to ensure its just running on amp-hulu
dawehner Feb 13, 2017
30fbd97
Add amp-anim for animated image + remove non-converted img tag in Amp…
Deuh Feb 1, 2017
211b8f4
Fixing builds to pass testing
Feb 9, 2017
0e89046
Allow Travis builds to fail on PHP nightly
Feb 9, 2017
a9bb7fe
Merge pull request #164 from GuideToIceland/upstream-migration
sebastianbenz Feb 13, 2017
5d1e1a7
Merge branch 'master' into hulu-support
dawehner Feb 14, 2017
39d279f
Provide a proper testcase
dawehner Feb 15, 2017
4485bd7
fix pass
dawehner Feb 15, 2017
e87bede
Add inline script
dawehner Feb 15, 2017
a18c8a7
Allow to fetch the attr value from value_casei as well
dawehner Feb 15, 2017
e4190a6
manually fixed generated validator
dawehner Feb 15, 2017
a21e886
fix a lot of failures
dawehner Feb 22, 2017
66ea994
Another small bugfix£
dawehner Feb 22, 2017
bebaf75
fix some failures
dawehner Mar 3, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ before_script:

script:
- vendor/bin/phpunit tests

matrix:
allow_failures:
- php: nightly
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ $ ./amp-console amp:convert sample-html/several_errors.html --full-document
```
Note that you need to provide `--full-document` if you're providing a full html document file for conversion.

Lets see the output output of the first example command above. The first few lines is the AMPized HTML provided by our library. The rest of the headings are self explanatory.
Lets see the output of the first example command above. The first few lines is the AMPized HTML provided by our library. The rest of the headings are self explanatory.

```html
$ cd <amp-php-library-repo-cloned-location>
Expand Down Expand Up @@ -161,18 +161,18 @@ FAIL

<a style="color: red;" href="javascript:run();"> on line 1
- The attribute 'style' may not appear in tag 'a'.
[code: DISALLOWED_ATTR category: DISALLOWED_HTML]
[code: DISALLOWED_ATTR category: DISALLOWED_HTML see: https://www.ampproject.org/docs/reference/spec.html#links]
ACTION TAKEN: a.style attribute was removed due to validation issues.
- Invalid URL protocol 'javascript:' for attribute 'href' in tag 'a'.
[code: INVALID_URL_PROTOCOL category: DISALLOWED_HTML]
[code: INVALID_URL_PROTOCOL category: DISALLOWED_HTML see: https://www.ampproject.org/docs/reference/spec.html#links]
ACTION TAKEN: a.href attribute was removed due to validation issues.

<a style="margin: 2px;" href="http://www.cnn.com" target="_parent"> on line 2
- The attribute 'style' may not appear in tag 'a'.
[code: DISALLOWED_ATTR category: DISALLOWED_HTML]
[code: DISALLOWED_ATTR category: DISALLOWED_HTML see: https://www.ampproject.org/docs/reference/spec.html#links]
ACTION TAKEN: a.style attribute was removed due to validation issues.
- The attribute 'target' in tag 'a' is set to the invalid value '_parent'.
[code: INVALID_ATTR_VALUE category: DISALLOWED_HTML]
[code: INVALID_ATTR_VALUE category: DISALLOWED_HTML see: https://www.ampproject.org/docs/reference/spec.html#html-tags]
ACTION TAKEN: a.target attribute was removed due to validation issues.

<input type="submit" value="submit"> on line 5
Expand All @@ -182,7 +182,7 @@ FAIL

<div onmouseover="hello();"> on line 6
- The attribute 'onmouseover' may not appear in tag 'div'.
[code: DISALLOWED_ATTR category: DISALLOWED_HTML]
[code: DISALLOWED_ATTR category: DISALLOWED_HTML see: https://www.ampproject.org/docs/reference/spec.html#links]
ACTION TAKEN: div.onmouseover attribute was removed due to validation issues.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"> on line 8
Expand Down
12 changes: 12 additions & 0 deletions src/AMP.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class AMP
'Lullabot\AMP\Pass\IframeVineTagTransformPass',
'Lullabot\AMP\Pass\IframeDailymotionTagTransformPass',
'Lullabot\AMP\Pass\IframeYouTubeTagTransformPass',
'Lullabot\AMP\Pass\IframeHuluTagTransformPass',
'Lullabot\AMP\Pass\IframeTagTransformPass',
'Lullabot\AMP\Pass\InstagramTransformPass',
'Lullabot\AMP\Pass\PinterestTagTransformPass',
Expand Down Expand Up @@ -174,6 +175,17 @@ public function loadHtml($html, $options = [])
$options['use_html5_parser'] = true;
}

// By default the convertion of img into amp-anim is disabled (because of ressource cost)
// => they will be converted into amp-img instead
if (!isset($options['use_img_anim_tag'])) {
$options['use_img_anim_tag'] = false;
}

// By default img that can't be converted are kept as it is and not removed
if (!isset($options['remove_non_converted_img_tag'])) {
$options['remove_non_converted_img_tag'] = false;
}

$this->options = $options;
$this->scope = !empty($options['scope']) ? $options['scope'] : Scope::BODY_SCOPE;

Expand Down
11 changes: 11 additions & 0 deletions src/Pass/AmpImgFixPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ function pass()
$error->resolved = false;
}
}
elseif (in_array($error->code, [ValidationErrorCode::MANDATORY_TAG_ANCESTOR_WITH_HINT]) &&
!$error->resolved &&
!empty($error->dom_tag) &&
strtolower($error->dom_tag->tagName) == 'img' &&
!empty($this->options['remove_non_converted_img_tag'])
) {
// Remove the offending tag
$error->dom_tag->parentNode->removeChild($error->dom_tag);
$error->addActionTaken(new ActionTakenLine('img', ActionTakenType::TAG_REMOVED));
$error->resolved = TRUE;
}
}
}
}
49 changes: 49 additions & 0 deletions src/Pass/BaseFacebookPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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;

abstract class BaseFacebookPass extends BasePass
{
/**
* Checks whether the given url is a valid facebook post url
*
* @param string $url
* @return bool
*/
protected function isValidPostUrl($url)
{
return
// e.g. https://www.facebook.com/20531316728/posts/10154009990506729/
preg_match('&(*UTF8)facebook\.com/.*/posts/\d+/?&i', $url)
// e.g. https://www.facebook.com/photos/{photo-id}
// or https://www.facebook.com/SanAntonioVAMC/photos/a.411451129506.185409.351086129506/10154231221264507/?type=3&amp;theater
|| preg_match('&(*UTF8)facebook\.com/.*photos.*&i', $url)
// e.g. https://www.facebook.com/photo.php?fbid={photo-id}
// or https://www.facebook.com/photo.php?v=10153655829445601
|| preg_match('&(*UTF8)facebook\.com/photo\.php.*&i', $url);
}

protected function isValidVideoUrl($url)
{
return
// e.g https://www.facebook.com/facebook/videos/10153231379946729/
preg_match('&(*UTF8)facebook\.com/.*/videos/\d+/?&i', $url)
// e.g https://www.facebook.com/video.php?v=10153231379946729
|| preg_match('&(*UTF8)facebook\.com/video\.php.*&i', $url);
}
}
9 changes: 4 additions & 5 deletions src/Pass/FacebookNonIframeTransformPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* @see https://github.com/ampproject/amphtml/blob/master/extensions/amp-facebook/amp-facebook.md
* @see https://github.com/ampproject/amphtml/blob/master/extensions/amp-facebook/0.1/validator-amp-facebook.protoascii
*/
class FacebookNonIframeTransformPass extends BasePass
class FacebookNonIframeTransformPass extends BaseFacebookPass
{
const DEFAULT_WIDTH = 500;
const DEFAULT_HEIGHT = 281;
Expand Down Expand Up @@ -109,17 +109,16 @@ protected function getFacebookEmbedAttrs(DOMQuery $el)
}

$card = true;
// e.g https://www.facebook.com/facebook/videos/10153231379946729/
if (preg_match('&(*UTF8)facebook\.com/.*/videos/\d+/?&i', $src)) {
if ($this->isValidVideoUrl($src)) {
// A facebook video can be embedded as a post. Doing that enables the video "card" to display
if ($el->attr('data-show-text') !== "false") {
$embed_as = 'post';
$card = true;
} else {
$embed_as = 'video';
}
} // e.g. https://www.facebook.com/20531316728/posts/10154009990506729/
else if (preg_match('&(*UTF8)facebook\.com/.*/posts/\d+/?&i', $src)) {
}
elseif ($this->isValidPostUrl($src)) {
$embed_as = 'post';
} else {
return false;
Expand Down
9 changes: 4 additions & 5 deletions src/Pass/IframeFacebookTagTransformPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
* @see https://github.com/ampproject/amphtml/blob/master/extensions/amp-facebook/amp-facebook.md
* @see https://github.com/ampproject/amphtml/blob/master/extensions/amp-facebook/0.1/validator-amp-facebook.protoascii
*/
class IframeFacebookTagTransformPass extends BasePass
class IframeFacebookTagTransformPass extends BaseFacebookPass
{
const DEFAULT_ASPECT_RATIO = 1.7778;
const DEFAULT_WIDTH = 500;
Expand Down Expand Up @@ -108,16 +108,15 @@ protected function setStandardFacebookParameters(DOMQuery $el)
return false;
}

// e.g https://www.facebook.com/facebook/videos/10153231379946729/
if (preg_match('&(*UTF8)facebook\.com/facebook/videos/\d+/?&i', $query_arr['href'])) {
if ($this->isValidVideoUrl($query_arr['href'])) {
// A facebook video can be embedded as a post. Doing that enables the video "card" to display
if (isset($query_arr['show_text']) && $query_arr['show_text'] !== "false") {
$embed_as = 'post';
} else {
$embed_as = 'video';
}
} // e.g. https://www.facebook.com/20531316728/posts/10154009990506729/
else if (preg_match('&(*UTF8)facebook\.com/.*/posts/\d+/?&i', $query_arr['href'])) {
}
elseif ($this->isValidPostUrl($query_arr['href'])) {
$embed_as = 'post';
} else {
return false;
Expand Down
65 changes: 65 additions & 0 deletions src/Pass/IframeHuluTagTransformPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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 IframeHuluTagTransformPass
* @package Lullabot\AMP\Pass
*
* Sample hulu embed code:
* <iframe width="854" height="480" frameborder="0" allowfullscreen="allowfullscreen" src="//www.hulu.com/embed.html?eid=_hHzwnAcj3RrXMJFDDvkuw"></iframe>
*
* @see https://www.ampproject.org/docs/reference/extended/amp-hulu.html
* @see https://ampbyexample.com/components/amp-hulu/
*/
class IframeHuluTagTransformPass extends BasePass
{
function pass()
{
$all_iframes = $this->q->find('iframe:not(noscript iframe)');
/** @var DOMQuery $el */
foreach ($all_iframes as $el) {
/** @var \DOMElement $dom_el */
$dom_el = $el->get(0);
$lineno = $this->getLineNo($dom_el);

if ($eid = $this->getArtifactId($el, '&(*UTF8)hulu\.com/embed\.html\?eid=(\w+)&i')) {
$width = $el->attr('width') ?: 800;
$height = $el->attr('height') ?: 600;

$context_string = $this->getContextString($dom_el);

// width and height are intended to be aspect ratios here
$el->after('<amp-hulu width="' . $width . '" height="' . $height . '" layout="responsive" data-eid="' . $eid . '"></amp-hulu>');
$new_dom_el = $el->next()->get(0);

$el->removeChildren()->remove();
$this->addActionTaken(new ActionTakenLine('iframe', ActionTakenType::HULU_CONVERTED, $lineno, $context_string));
$this->context->addLineAssociation($new_dom_el, $lineno);
}
}

return $this->transformations;
}
}
28 changes: 8 additions & 20 deletions src/Pass/IframeYouTubeTagTransformPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,37 +113,25 @@ protected function isYouTubeIframe(DOMQuery $el)
*
* @param DOMQuery $el
* @return string
*
*/
protected function getYouTubeCode(DOMQuery $el)
{
$matches = [];
$youtube_code = '';
$href = $el->attr('src');

// @todo there seem to be a lot of ways to embed a youtube video. We probably need to capture all patterns here
// The next one is the embed code that youtube gives you
if (preg_match('&(*UTF8)/embed/([^/?]+)&i', $href, $matches)) {
if (!empty($matches[1])) {
$youtube_code = $matches[1];
return $youtube_code;
}
}

if (preg_match('&(*UTF8)youtu\.be/([^/?]+)&i', $href, $matches)) {
if (!empty($matches[1])) {
$youtube_code = $matches[1];
return $youtube_code;
}
}

if (preg_match('!(*UTF8)watch\?v=([^&]+)!i', $href, $matches)) {
if (!empty($matches[1])) {
// This regex is supposed to catch all possible way to embed youtube video
if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $href, $matches))
{
if (!empty($matches[1]))
{
$youtube_code = $matches[1];
return $youtube_code;
return htmlspecialchars($youtube_code);
}
}

return $youtube_code;
return htmlspecialchars($youtube_code);
}

/**
Expand Down
Loading