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

Commit fe002ea

Browse files
committed
Merge pull request #277 from itteco/master
Fix JavaScript-based embeds
2 parents 947bfef + 16c9eb7 commit fe002ea

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

spec/embeds.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,4 +336,24 @@ describe('Embeds addon', function () {
336336
expect($serialized.find('.medium-insert-embeds').attr('contenteditable')).toBeUndefined();
337337
expect($serialized.find('.medium-insert-embeds-overlay').length).toEqual(0);
338338
});
339+
340+
it('uses data-embed-code as container html for javascript-based embeds', function() {
341+
var html = '<div class="medium-insert-embeds"><figure class="medium-insert-embed"><div data-embed-code="<div>good-value</div>">bad-value</div></figure></div>',
342+
editor, $serialized;
343+
344+
$('#fixtures').html('<div class="editable">' + html + '</div>');
345+
this.$el = $('.editable');
346+
347+
editor = new MediumEditor(this.$el.get(0));
348+
349+
this.$el.mediumInsert({
350+
editor: editor,
351+
addons: {
352+
embeds: {}
353+
}
354+
});
355+
356+
$serialized = $(editor.serialize()['element-0'].value);
357+
expect($serialized.find('[data-embed-code]').html()).toEqual('<div>good-value</div>');
358+
});
339359
});

src/js/core.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@
136136

137137
$data.find('.medium-insert-buttons').remove();
138138

139+
// Restore original embed code from embed wrapper attribute value.
140+
$data.find('[data-embed-code]').each(function() {
141+
var $this = $(this);
142+
$this.html($this.attr('data-embed-code'));
143+
});
144+
139145
data[key].value = $data.html();
140146
});
141147

src/js/embeds.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,25 @@
298298
success: function(data) {
299299
var html = data && data.html;
300300

301-
if (data && !data.html && data.type === 'photo' && data.url) {
301+
if (data && !html && data.type === 'photo' && data.url) {
302302
html = '<img src="' + data.url + '" alt="">';
303303
}
304304

305+
if (!html) {
306+
// Prevent render empty embed.
307+
$.proxy(that, 'convertBadEmbed', url)();
308+
return;
309+
}
310+
311+
if (html && html.indexOf('</script>') > -1) {
312+
// Store embed code with <script> tag inside wrapper attribute value.
313+
// Make nice attribute value escaping using jQuery.
314+
var $div = $('<div>')
315+
.attr('data-embed-code', html)
316+
.html(html);
317+
html = $('<div>').append($div).html();
318+
}
319+
305320
$.proxy(that, 'embed', html)();
306321
},
307322
error: function(jqXHR, textStatus, errorThrown) {

0 commit comments

Comments
 (0)