Skip to content

Commit 5b129be

Browse files
committed
Ensure placeholder extensions cleans up removed elements
1 parent 7930d1e commit 5b129be

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

spec/placeholder.spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,18 @@ describe('MediumEditor.extensions.placeholder TestCase', function () {
172172
expect(this.el.hasAttribute('data-placeholder')).toBe(false);
173173
});
174174

175+
it('should remove the added data-placeholder attribute when elements are removed dynamically from the editor', function () {
176+
var editor = this.newMediumEditor('.editor'),
177+
newEl = this.createElement('div', 'other-element');
178+
179+
expect(newEl.hasAttribute('other-element')).toBe(false);
180+
editor.addElements(newEl);
181+
expect(newEl.getAttribute('data-placeholder')).toBe(MediumEditor.extensions.placeholder.prototype.text);
182+
183+
editor.removeElements('.other-element');
184+
expect(newEl.hasAttribute('data-placeholder')).toBe(false);
185+
});
186+
175187
it('should not remove custom data-placeholder attribute when destroyed', function () {
176188
var placeholderText = 'Custom placeholder';
177189
this.el.setAttribute('data-placeholder', placeholderText);

src/js/extensions/placeholder.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,17 @@
3939
},
4040

4141
destroy: function () {
42-
this.getEditorElements().forEach(function (el) {
43-
if (el.getAttribute('data-placeholder') === this.text) {
44-
el.removeAttribute('data-placeholder');
45-
}
46-
}, this);
42+
this.getEditorElements().forEach(this.cleanupElement, this);
43+
},
44+
45+
handleRemoveElement: function (event, editable) {
46+
this.cleanupElement(editable);
47+
},
48+
49+
cleanupElement: function (el) {
50+
if (el.getAttribute('data-placeholder') === this.text) {
51+
el.removeAttribute('data-placeholder');
52+
}
4753
},
4854

4955
showPlaceholder: function (el) {
@@ -93,8 +99,9 @@
9399
// When the editor loses focus, check if the placeholder should be visible
94100
this.subscribe('blur', this.handleBlur.bind(this));
95101

96-
// Need to know when an element as been added to the editor
102+
// Need to know when elements are added/removed from the editor
97103
this.subscribe('addElement', this.handleAddElement.bind(this));
104+
this.subscribe('removeElement', this.handleRemoveElement.bind(this));
98105
},
99106

100107
handleInput: function (event, element) {

0 commit comments

Comments
 (0)