Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 4dd2294

Browse files
committed
Re-enable facebook and twitter embedding without oEmbed (#301)
* fix moving caret in Embeds.convertBadEmbed() * re-enable twitter and facebook emebed without oEmbed * add tests
1 parent 2c26e73 commit 4dd2294

File tree

2 files changed

+60
-15
lines changed

2 files changed

+60
-15
lines changed

spec/embeds.spec.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,46 @@ describe('Embeds addon', function () {
298298
expect(this.$el.find('.medium-insert-embeds-input').length).toEqual(0);
299299
});
300300

301+
it('support embedding twitter', function () {
302+
var $event = $.Event('keydown');
303+
304+
$event.which = 13;
305+
$('#fixtures').html('<div class="editable"><p class="medium-insert-embeds-input medium-insert-embeds-active">https://twitter.com/medium_editor/status/694987296379125760</p></div>');
306+
this.$el = $('.editable');
307+
this.$el.mediumInsert({
308+
addons: {
309+
embeds: {
310+
oembedProxy: false
311+
}
312+
}
313+
});
314+
this.$el.trigger($event);
315+
316+
expect(this.$el.find('.medium-insert-embeds').length).toEqual(1);
317+
expect(this.$el.find('.medium-insert-embeds .twitter-tweet').length).toEqual(1);
318+
expect(this.$el.find('.medium-insert-embeds-input').length).toEqual(0);
319+
});
320+
321+
it('support embedding facebook', function () {
322+
var $event = $.Event('keydown');
323+
324+
$event.which = 13;
325+
$('#fixtures').html('<div class="editable"><p class="medium-insert-embeds-input medium-insert-embeds-active">https://www.facebook.com/cneistat/videos/vb.210351389002863/922328184471843/?type=2&theater</p></div>');
326+
this.$el = $('.editable');
327+
this.$el.mediumInsert({
328+
addons: {
329+
embeds: {
330+
oembedProxy: false
331+
}
332+
}
333+
});
334+
this.$el.trigger($event);
335+
336+
expect(this.$el.find('.medium-insert-embeds').length).toEqual(1);
337+
expect(this.$el.find('.medium-insert-embeds .fb-post').length).toEqual(1);
338+
expect(this.$el.find('.medium-insert-embeds-input').length).toEqual(0);
339+
});
340+
301341
it('converts bad embed into text', function () {
302342
var $event = $.Event('keydown');
303343

src/js/embeds.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -337,15 +337,6 @@
337337
return;
338338
}
339339

340-
if (html && html.indexOf('</script>') > -1) {
341-
// Store embed code with <script> tag inside wrapper attribute value.
342-
// Make nice attribute value escaping using jQuery.
343-
var $div = $('<div>')
344-
.attr('data-embed-code', html)
345-
.html(html);
346-
html = $('<div>').append($div).html();
347-
}
348-
349340
if (pasted) {
350341
$.proxy(that, 'embed', html, url)();
351342
}
@@ -382,23 +373,28 @@
382373
Embeds.prototype.parseUrl = function (url, pasted) {
383374
var html;
384375

385-
if (!(new RegExp(['youtube', 'youtu.be', 'vimeo', 'instagram'].join('|')).test(url))) {
376+
if (!(new RegExp(['youtube', 'youtu.be', 'vimeo', 'instagram', 'twitter', 'facebook'].join('|')).test(url))) {
386377
$.proxy(this, 'convertBadEmbed', url)();
387378
return false;
388379
}
389380

390381
html = url.replace(/\n?/g, '')
391382
.replace(/^((http(s)?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\/(watch\?v=|v\/)?)([a-zA-Z0-9\-_]+)(.*)?$/, '<div class="video video-youtube"><iframe width="420" height="315" src="//www.youtube.com/embed/$7" frameborder="0" allowfullscreen></iframe></div>')
392383
.replace(/^https?:\/\/vimeo\.com(\/.+)?\/([0-9]+)$/, '<div class="video video-vimeo"><iframe src="//player.vimeo.com/video/$2" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>')
393-
//.replace(/^https:\/\/twitter\.com\/(\w+)\/status\/(\d+)\/?$/, '<blockquote class="twitter-tweet" align="center" lang="en"><a href="https://twitter.com/$1/statuses/$2"></a></blockquote><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>')
394-
//.replace(/^https:\/\/www\.facebook\.com\/(video.php|photo.php)\?v=(\d+).+$/, '<div class="fb-post" data-href="https://www.facebook.com/photo.php?v=$2"><div class="fb-xfbml-parse-ignore"><a href="https://www.facebook.com/photo.php?v=$2">Post</a></div></div>')
384+
.replace(/^https:\/\/twitter\.com\/(\w+)\/status\/(\d+)\/?$/, '<blockquote class="twitter-tweet" align="center" lang="en"><a href="https://twitter.com/$1/statuses/$2"></a></blockquote><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>')
385+
.replace(/^(https:\/\/www\.facebook\.com\/(.*))$/, '<script src="//connect.facebook.net/en_US/sdk.js#xfbml=1&amp;version=v2.2" async></script><div class="fb-post" data-href="$1"><div class="fb-xfbml-parse-ignore"><a href="$1">Loading Facebook post...</a></div></div>')
395386
.replace(/^https?:\/\/instagram\.com\/p\/(.+)\/?$/, '<span class="instagram"><iframe src="//instagram.com/p/$1/embed/" width="612" height="710" frameborder="0" scrolling="no" allowtransparency="true"></iframe></span>');
396387

388+
if ((/<("[^"]*"|'[^']*'|[^'">])*>/).test(html) === false) {
389+
$.proxy(this, 'convertBadEmbed', url)();
390+
return false;
391+
}
392+
397393
if (pasted) {
398-
this.embed((/<("[^"]*"|'[^']*'|[^'">])*>/).test(html) ? html : false, url);
394+
this.embed(html, url);
399395
}
400396
else {
401-
this.embed((/<("[^"]*"|'[^']*'|[^'">])*>/).test(html) ? html : false);
397+
this.embed(html);
402398
}
403399
};
404400

@@ -417,6 +413,15 @@
417413
alert('Incorrect URL format specified');
418414
return false;
419415
} else {
416+
if (html.indexOf('</script>') > -1) {
417+
// Store embed code with <script> tag inside wrapper attribute value.
418+
// Make nice attribute value escaping using jQuery.
419+
var $div = $('<div>')
420+
.attr('data-embed-code', html)
421+
.html(html);
422+
html = $('<div>').append($div).html();
423+
}
424+
420425
if (pastedUrl) {
421426
// Get the element with the pasted url
422427
// place the embed template and remove the pasted text
@@ -477,7 +482,7 @@
477482

478483
this.core.triggerInput();
479484

480-
this.core.moveCaret($place);
485+
this.core.moveCaret($empty);
481486
};
482487

483488
/**

0 commit comments

Comments
 (0)