diff --git a/spec/images.spec.js b/spec/images.spec.js
index 1d1364da7..5f00b919a 100644
--- a/spec/images.spec.js
+++ b/spec/images.spec.js
@@ -307,6 +307,22 @@ describe('Images addon', function () {
expect($('.medium-insert-images-toolbar').length).toEqual(0);
});
+ it('only the selected image is removed', function () {
+ var $p = this.$el.find('p'),
+ $event = $.Event('keydown');
+
+ $event.which = 8;
+
+ $p.before('
');
+ $p.before('');
+
+ this.$el.find('img').first().addClass('medium-insert-image-active');
+ this.$el.trigger($event);
+
+ expect(this.$el.find('.medium-insert-images').length).toEqual(1);
+ expect(this.$el.find('img').first().attr('src')).toEqual('delete-image2.jpg');
+ });
+
it('fires deleteFile function even when images isn\'t selected but backspace is pressed in text', function () {
var $p = this.$el.find('p'),
$event = $.Event('keydown');
diff --git a/src/js/images.js b/src/js/images.js
index 4de62ebc3..c152860cf 100644
--- a/src/js/images.js
+++ b/src/js/images.js
@@ -515,34 +515,34 @@
if (e.which === 8 || e.which === 46) {
if ($selectedImage.length) {
images.push($selectedImage);
- }
-
- // Remove image even if it's not selected, but backspace/del is pressed in text
- selection = window.getSelection();
- if (selection && selection.rangeCount) {
- range = selection.getRangeAt(0);
- current = range.commonAncestorContainer;
- $current = current.nodeName === '#text' || current.nodeName === 'BR' ? $(current).parent() : $(current);
- caretPosition = MediumEditor.selection.getCaretOffsets(current).left;
-
- // Is backspace pressed and caret is at the beginning of a paragraph, get previous element
- if (e.which === 8 && caretPosition === 0) {
- $sibling = $current.prev();
- // Is del pressed and caret is at the end of a paragraph, get next element
- } else if (e.which === 46 && caretPosition === $current.text().length) {
- $sibling = $current.next();
- }
+ } else {
+ // Remove image even if it's not selected, but backspace/del is pressed in text
+ selection = window.getSelection();
+ if (selection && selection.rangeCount) {
+ range = selection.getRangeAt(0);
+ current = range.commonAncestorContainer;
+ $current = current.nodeName === '#text' || current.nodeName === 'BR' ? $(current).parent() : $(current);
+ caretPosition = MediumEditor.selection.getCaretOffsets(current).left;
+
+ // Is backspace pressed and caret is at the beginning of a paragraph, get previous element
+ if (e.which === 8 && caretPosition === 0) {
+ $sibling = $current.prev();
+ // Is del pressed and caret is at the end of a paragraph, get next element
+ } else if (e.which === 46 && caretPosition === $current.text().length) {
+ $sibling = $current.next();
+ }
- if ($sibling && $sibling.hasClass('medium-insert-images')) {
- images.push($sibling.find('img'));
- }
+ if ($sibling && $sibling.hasClass('medium-insert-images')) {
+ images.push($sibling.find('img'));
+ }
- // If text is selected, find images in the selection
- selectedHtml = MediumEditor.selection.getSelectionHtml(document);
- if (selectedHtml) {
- $('').html(selectedHtml).find('.medium-insert-images img').each(function () {
- images.push($(this));
- });
+ // If text is selected, find images in the selection
+ selectedHtml = MediumEditor.selection.getSelectionHtml(document);
+ if (selectedHtml) {
+ $('').html(selectedHtml).find('.medium-insert-images img').each(function () {
+ images.push($(this));
+ });
+ }
}
}