Skip to content

Commit

Permalink
amp-twitter: support urls with 'statuses'
Browse files Browse the repository at this point in the history
* update regex to match twitter id
* add test

Fixes #137
  • Loading branch information
sebastianbenz committed Sep 7, 2016
1 parent bfa5763 commit fd76239
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Pass/TwitterTransformPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ protected function getTweetId(DOMQuery $el)
foreach ($links as $link) {
$href = $link->attr('href');
$matches = [];
if (preg_match('&(*UTF8)twitter.com/.*/status/([^/]+)&i', $href, $matches)) {
if (preg_match('&(*UTF8)twitter.com/.*/status(?:es)?/([^/]+)&i', $href, $matches)) {
if (!empty($matches[1])) {
$tweet_id = $matches[1];
break;
Expand Down
7 changes: 7 additions & 0 deletions tests/test-data/fragment-html/twitter-fragment.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<!-- normal twitter embed using 'status' -->
<blockquote data-cards="hidden" class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">Lorraine and I had an amazing time at the <a
href="https://twitter.com/hashtag/Oscars?src=hash">#Oscars</a>! Congrats to all of the nominees on a job
well-done! <a href="https://t.co/xyrO7kQBzH">pic.twitter.com/xyrO7kQBzH</a></p>&mdash; Andy Serkis (@andyserkis) <a
href="https://twitter.com/andyserkis/status/704420904437043200">February 29, 2016</a></blockquote>
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
<!-- normal twitter embed using 'statuses' -->
<blockquote data-cards="hidden" class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">Lorraine and I had an amazing time at the <a
href="https://twitter.com/hashtag/Oscars?src=hash">#Oscars</a>! Congrats to all of the nominees on a job
well-done! <a href="https://t.co/xyrO7kQBzH">pic.twitter.com/xyrO7kQBzH</a></p>&mdash; Andy Serkis (@andyserkis) <a
href="https://twitter.com/andyserkis/statuses/704420904437043200">February 29, 2016</a></blockquote>
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
28 changes: 21 additions & 7 deletions tests/test-data/fragment-html/twitter-fragment.html.out
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
<!-- normal twitter embed using 'status' -->
<amp-twitter data-cards="hidden" data-lang="en" height="223" width="486" layout="responsive" data-tweetid="704420904437043200"></amp-twitter>

<!-- normal twitter embed using 'statuses' -->
<amp-twitter data-cards="hidden" data-lang="en" height="223" width="486" layout="responsive" data-tweetid="704420904437043200"></amp-twitter>



ORIGINAL HTML
---------------
Line 1: <blockquote data-cards="hidden" class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">Lorraine and I had an amazing time at the <a
Line 2: href="https://twitter.com/hashtag/Oscars?src=hash">#Oscars</a>! Congrats to all of the nominees on a job
Line 3: well-done! <a href="https://t.co/xyrO7kQBzH">pic.twitter.com/xyrO7kQBzH</a></p>&mdash; Andy Serkis (@andyserkis) <a
Line 4: href="https://twitter.com/andyserkis/status/704420904437043200">February 29, 2016</a></blockquote>
Line 5: <script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
Line 6:
Line 1: <!-- normal twitter embed using 'status' -->
Line 2: <blockquote data-cards="hidden" class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">Lorraine and I had an amazing time at the <a
Line 3: href="https://twitter.com/hashtag/Oscars?src=hash">#Oscars</a>! Congrats to all of the nominees on a job
Line 4: well-done! <a href="https://t.co/xyrO7kQBzH">pic.twitter.com/xyrO7kQBzH</a></p>&mdash; Andy Serkis (@andyserkis) <a
Line 5: href="https://twitter.com/andyserkis/status/704420904437043200">February 29, 2016</a></blockquote>
Line 6: <script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
Line 7: <!-- normal twitter embed using 'statuses' -->
Line 8: <blockquote data-cards="hidden" class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">Lorraine and I had an amazing time at the <a
Line 9: href="https://twitter.com/hashtag/Oscars?src=hash">#Oscars</a>! Congrats to all of the nominees on a job
Line 10: well-done! <a href="https://t.co/xyrO7kQBzH">pic.twitter.com/xyrO7kQBzH</a></p>&mdash; Andy Serkis (@andyserkis) <a
Line 11: href="https://twitter.com/andyserkis/statuses/704420904437043200">February 29, 2016</a></blockquote>
Line 12: <script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
Line 13:


Transformations made from HTML tags to AMP custom tags
-------------------------------------------------------

<blockquote data-cards="hidden" class="twitter-tweet" data-lang="en"> at line 1
<blockquote data-cards="hidden" class="twitter-tweet" data-lang="en"> at line 2
ACTION TAKEN: blockquote.twitter-tweet (with twitter script tag) twitter embed code was converted to the amp-twitter tag.

<blockquote data-cards="hidden" class="twitter-tweet" data-lang="en"> at line 8
ACTION TAKEN: blockquote.twitter-tweet (with twitter script tag) twitter embed code was converted to the amp-twitter tag.


Expand Down

0 comments on commit fd76239

Please sign in to comment.