Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit ac7ea78

Browse files
committed
fixup: use gkalpak's fix, streamline tests
1 parent ceccefe commit ac7ea78

File tree

2 files changed

+33
-53
lines changed

2 files changed

+33
-53
lines changed

src/ngMessages/messages.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ angular.module('ngMessages', [])
451451
function findPreviousMessage(parent, comment) {
452452
var prevNode = comment;
453453
var parentLookup = [];
454+
454455
while (prevNode && prevNode !== parent) {
455456
var prevKey = prevNode.$$ngMessageNode;
456457
if (prevKey && prevKey.length) {
@@ -459,14 +460,14 @@ angular.module('ngMessages', [])
459460

460461
// dive deeper into the DOM and examine its children for any ngMessage
461462
// comments that may be in an element that appears deeper in the list
462-
if (prevNode.childNodes.length &&
463-
prevNode !== comment.parentNode &&
464-
parentLookup.indexOf(prevNode) == -1
465-
) {
463+
if (prevNode.childNodes.length && parentLookup.indexOf(prevNode) == -1) {
466464
parentLookup.push(prevNode);
467465
prevNode = prevNode.childNodes[prevNode.childNodes.length - 1];
466+
} else if (prevNode.previousSibling) {
467+
prevNode = prevNode.previousSibling;
468468
} else {
469-
prevNode = prevNode.previousSibling || prevNode.parentNode;
469+
prevNode = prevNode.parentNode;
470+
parentLookup.push(prevNode);
470471
}
471472
}
472473
}

test/ngMessages/messagesSpec.js

+27-48
Original file line numberDiff line numberDiff line change
@@ -430,20 +430,20 @@ describe('ngMessages', function() {
430430
inject(function($rootScope, $compile) {
431431

432432
element = $compile('<div><div ng-if="show"><div message-wrap col="col">' +
433-
' <div ng-message="required">Fill in the text field.</div>' +
434-
' <div ng-message="extra">Extra error message.</div>' +
433+
' <div ng-message="a">A</div>' +
434+
' <div ng-message="b">B</div>' +
435435
'</div></div></div>')($rootScope);
436436

437437
$rootScope.$apply(function() {
438438
$rootScope.show = true;
439439
$rootScope.col = {
440-
required: true,
441-
extra: true
440+
a: true,
441+
b: true
442442
};
443443
});
444444

445445
expect(messageChildren(element).length).toBe(1);
446-
expect(trim(element.text())).toEqual('Fill in the text field.');
446+
expect(trim(element.text())).toEqual('A');
447447

448448
$rootScope.$apply('show = false');
449449

@@ -453,91 +453,70 @@ describe('ngMessages', function() {
453453

454454

455455
it('should not crash when the first of two nested messages is removed', function() {
456-
457-
module(function($compileProvider) {
458-
$compileProvider.directive('removeMe', function() {
459-
return {
460-
link: function(scope, element) {
461-
scope.$watch('hide', function(newVal) {
462-
if (newVal === true) element.remove();
463-
});
464-
}
465-
};
466-
});
467-
});
468-
469456
inject(function($rootScope, $compile) {
470457

471458
element = $compile(
472459
'<div ng-messages="col">' +
473460
'<div class="wrapper">' +
474-
'<div remove-me ng-message="required">Fill in the text field.</div>' +
475-
'<div ng-message="extra">Extra error message.</div>' +
461+
'<div ng-message="a">A</div>' +
462+
'<div ng-message="b">B</div>' +
476463
'</div>' +
477464
'</div>'
478465
)($rootScope);
479466

480467
$rootScope.$apply(function() {
481468
$rootScope.col = {
482-
required: true,
483-
extra: false
469+
a: true,
470+
b: false
484471
};
485472
});
486473

487474
expect(messageChildren(element).length).toBe(1);
488-
expect(trim(element.text())).toEqual('Fill in the text field.');
475+
expect(trim(element.text())).toEqual('A');
489476

490-
$rootScope.$apply('hide = true');
477+
var nodeA = element[0].querySelector('[ng-message="a"]');
478+
nodeA.remove();
491479

492480
expect(messageChildren(element).length).toBe(0);
493481
});
494482
});
495483

496484

497-
xit('should not crash when a deeply nested messages is removed', function() {
498-
499-
module(function($compileProvider) {
500-
$compileProvider.directive('removeMe', function() {
501-
return {
502-
link: function(scope, element) {
503-
scope.$watch('hide', function(newVal) {
504-
if (newVal === true) element.remove();
505-
});
506-
}
507-
};
508-
});
509-
});
510-
485+
it('should show deeply nested messages correctly after a message has been removed', function() {
511486
inject(function($rootScope, $compile) {
512487

513488
element = $compile(
514-
'<div ng-messages="col">' +
489+
'<div ng-messages="col" ng-messages-multiple>' +
515490
'<div class="another-wrapper">' +
491+
'<div ng-message="a">A</div>' +
516492
'<div class="wrapper">' +
517-
'<div remove-me ng-message="required">Fill in the text field.</div>' +
518-
'<div ng-message="extra">Extra error message.</div>' +
493+
'<div ng-message="b">B</div>' +
494+
'<div ng-message="c">C</div>' +
519495
'</div>' +
520-
'<div ng-message="different">Different error message.</div>' +
496+
'<div ng-message="d">D</div>' +
521497
'</div>' +
522498
'</div>'
523499
)($rootScope);
524500

525501
$rootScope.$apply(function() {
526502
$rootScope.col = {
527-
required: true,
528-
extra: false
503+
a: true,
504+
b: true
529505
};
530506
});
531507

532-
expect(messageChildren(element).length).toBe(1);
533-
expect(trim(element.text())).toEqual('Fill in the text field.');
508+
expect(messageChildren(element).length).toBe(2);
509+
expect(trim(element.text())).toEqual('AB');
534510

535-
$rootScope.$apply('hide = true');
511+
var nodeB = element[0].querySelector('[ng-message="b"]');
512+
nodeB.remove();
536513

537-
expect(messageChildren(element).length).toBe(0);
514+
expect(messageChildren(element).length).toBe(1);
515+
expect(trim(element.text())).toEqual('A');
538516
});
539517
});
540518

519+
541520
// issue #12856
542521
it('should only detach the message object that is associated with the message node being removed',
543522
inject(function($rootScope, $compile, $animate) {

0 commit comments

Comments
 (0)