Skip to content
This repository was archived by the owner on May 25, 2019. It is now read-only.

Commit 7dfcb07

Browse files
committed
fix: element not removed when the element gets replaced
1 parent 28813c7 commit 7dfcb07

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed

src/ui-codemirror.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ angular.module('ui.codemirror', [])
3232
} else {
3333
iElement.html('');
3434
codeMirror = new window.CodeMirror(function(cm_el) {
35-
iElement.replaceWith(cm_el);
35+
iElement.append(cm_el);
3636
}, {
3737
value: value
3838
});
@@ -105,7 +105,7 @@ angular.module('ui.codemirror', [])
105105

106106
// Allow access to the CodeMirror instance through a broadcasted event
107107
// eg: $broadcast('CodeMirror', function(cm){...});
108-
scope.$on('CodeMirror', function(event, callback){
108+
scope.$on('CodeMirror', function(event, callback) {
109109
if (angular.isFunction(callback)) {
110110
callback(codeMirror);
111111
} else {

test/codemirror.spec.js

+57-14
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@ describe('uiCodemirror', function () {
44
// declare these up here to be global to all tests
55
var scope, $compile, $timeout, uiConfig;
66

7-
beforeEach(module('ui.codemirror'));
8-
beforeEach(inject(function (uiCodemirrorConfig) {
9-
uiConfig = uiCodemirrorConfig;
10-
}));
11-
12-
// inject in angular constructs. Injector knows about leading/trailing underscores and does the right thing
13-
// otherwise, you would need to inject these into each test
14-
beforeEach(inject(function (_$rootScope_, _$compile_, _$timeout_) {
15-
scope = _$rootScope_.$new();
16-
$compile = _$compile_;
17-
$timeout = _$timeout_;
18-
}));
7+
beforeEach(function(){
8+
module('ui.codemirror');
9+
10+
inject(function (_$rootScope_, _$compile_, _$timeout_, uiCodemirrorConfig) {
11+
scope = _$rootScope_.$new();
12+
$compile = _$compile_;
13+
$timeout = _$timeout_;
14+
uiConfig = uiCodemirrorConfig;
15+
});
16+
17+
});
1918

2019
afterEach(function () {
2120
uiConfig = {};
@@ -34,6 +33,50 @@ describe('uiCodemirror', function () {
3433
window.CodeMirror = _CodeMirror;
3534
});
3635

36+
describe('destruction', function () {
37+
38+
var parentElement;
39+
40+
beforeEach(function () {
41+
parentElement = angular.element('<div></div>');
42+
angular.element(document.body).prepend(parentElement);
43+
});
44+
45+
afterEach(function () {
46+
parentElement.remove();
47+
});
48+
49+
function shouldDestroyTest(elementType, template) {
50+
it('should destroy the directive of ' + elementType, function () {
51+
var element = angular.element(template);
52+
parentElement.append(element);
53+
54+
$compile(element)(scope);
55+
scope.$digest();
56+
57+
expect(parentElement.children().length).toBe(1);
58+
element.remove();
59+
scope.$digest();
60+
expect(parentElement.children().length).toBe(0);
61+
});
62+
}
63+
64+
shouldDestroyTest('an element', '<ui-codemirror></ui-codemirror>');
65+
shouldDestroyTest('an attribute', '<div ui-codemirror=""></div>');
66+
shouldDestroyTest('an attribute of a textearea', '<textarea ui-codemirror=""></textarea>');
67+
68+
});
69+
70+
it('should not throw an error when window.CodeMirror is defined', function () {
71+
function compile() {
72+
$compile('<div ui-codemirror></div>')(scope);
73+
}
74+
75+
expect(window.CodeMirror).toBeDefined();
76+
expect(compile).not.toThrow();
77+
});
78+
79+
3780
it('should not throw an error when window.CodeMirror is defined', function () {
3881
function compile() {
3982
$compile('<div ui-codemirror></div>')(scope);
@@ -86,9 +129,9 @@ describe('uiCodemirror', function () {
86129
expect(codemirror).toBeDefined();
87130
});
88131

89-
it('should replace the element with a div.CodeMirror', function () {
132+
it('should have a child element with a div.CodeMirror', function () {
90133
// Explicit a parent node to support the directive.
91-
var element = $compile('<div><div ui-codemirror></div></div>')(scope).children();
134+
var element = $compile('<div ui-codemirror></div>')(scope).children();
92135

93136
expect(element).toBeDefined();
94137
expect(element.prop('tagName')).toBe('DIV');

0 commit comments

Comments
 (0)