Skip to content

Commit

Permalink
amp-twitter: tighten up fallback logic so it does not pick up random…
Browse files Browse the repository at this point in the history
… numbers. (ampproject#11761)
  • Loading branch information
aghassemi authored and erwinmombay committed Oct 19, 2017
1 parent 9f55e55 commit 1f75853
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
11 changes: 8 additions & 3 deletions 3p/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@ export function cleanupTweetId_(tweetid) {
}
}
// 2)
// Otherwise just return the first number we find.
// Tweet Ids are always a Int64 number.
return tweetid.match(/\d+/)[0];
// Handle malformed ids such as
// 585110598171631616?ref_src
const match = tweetid.match(/^(\d+)\?ref.*/);
if (match) {
return match[1];
}

return tweetid;
};
25 changes: 22 additions & 3 deletions extensions/amp-twitter/0.1/test/test-amp-twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,29 @@ describes.realWin('amp-twitter', {
expect(cleanupTweetId_(good)).to.equal('20');
});

it('cleans up bad tweet id with query string at end', () => {
const bad = '585110598171631616?ref=twsrc%5Etfw';
it('does not pick up random numbers', () => {
const bad1 = '<div>123</div>';
const bad2 = '123123junk123123';
const bad3 = 'https://twitter.com/1cram2force/status?ref=';
const bad4 = '<div>nonumber</div>';
const bad5 = 'aa585110598171631616?ref=twsrc%5etfw';
const bad6 = '';
const bad7 = ' ';

expect(cleanupTweetId_(bad1)).to.equal(bad1);
expect(cleanupTweetId_(bad2)).to.equal(bad2);
expect(cleanupTweetId_(bad3)).to.equal(bad3);
expect(cleanupTweetId_(bad4)).to.equal(bad4);
expect(cleanupTweetId_(bad5)).to.equal(bad5);
expect(cleanupTweetId_(bad6)).to.equal(bad6);
expect(cleanupTweetId_(bad7)).to.equal(bad7);
});

expect(cleanupTweetId_(bad)).to.equal(tweetId);
it('cleans up bad tweet id with ref query string at end', () => {
const bad1 = '585110598171631616?ref=twsrc%5Etfw';
const bad2 = '585110598171631616?ref_src=twsrc%5Etfw';
expect(cleanupTweetId_(bad1)).to.equal(tweetId);
expect(cleanupTweetId_(bad2)).to.equal(tweetId);
});

it('cleans up bad tweet full Url', () => {
Expand Down

0 comments on commit 1f75853

Please sign in to comment.