Added the condition to removeItems, that the to be removedItem be direct child of the menus HTML element.
Change-Id: Ife934e340ab30ed5d497a40659e6a48f8d3922ff
diff --git a/dev/js/spec/menuSpec.js b/dev/js/spec/menuSpec.js
index e1c6fc8..883a71d 100644
--- a/dev/js/spec/menuSpec.js
+++ b/dev/js/spec/menuSpec.js
@@ -410,6 +410,21 @@
menu = KorAP.HintMenu.create("cnx/", list);
expect(menu.element().menu).toEqual(menu);
});
+
+ it('should only remove direct descendants with removeItems', function () {
+ var menu = KorAP.HintMenu.create("cnx/", list);
+ var newUL = document.createElement("ul");
+ var newLI = document.createElement("li");
+ newUL.appendChild(newLI);
+ //This is a very constructed example, but this actually happens within containerMenu
+ menu.element().appendChild(newUL);
+ expect(menu.element().childNodes[3].nodeName).toEqual("UL");
+ expect(menu.element().childNodes[3].childNodes[0]).toEqual(newLI);
+ menu.prefix("a"); //to call show
+ expect(menu.element().childNodes[3].nodeName).toEqual("UL");
+ expect(menu.element().childNodes[3].childNodes[0]).toEqual(newLI);
+
+ });
it('should be visible', function () {
diff --git a/dev/js/src/menu.js b/dev/js/src/menu.js
index 6aff7fa..4e93390 100644
--- a/dev/js/src/menu.js
+++ b/dev/js/src/menu.js
@@ -637,10 +637,12 @@
* Delete all visible items from the menu element
*/
- removeItems : function () {
+ removeItems : function () {
const liElements=this._el.getElementsByTagName("LI");
- while (liElements.length>0){
- this._el.removeChild(liElements[0]);
+ for (let ii = liElements.length-1; ii >= 0; ii-- ) {
+ if (liElements[ii].parentNode === this._el){
+ this._el.removeChild(liElements[ii]);
+ };
};
},