Repository: primefaces/primefaces (java)

Issue:
Menu: JS recursion error 14.0.1
### Describe the bug

In PF 14.0.1, a js error is thrown in the mouse enter event on a menu item. The error is not present in 14.0.0

### Reproducer

```
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:view>
    <h:head></h:head>
    <h:body>
        <p:commandButton id="dynaButton" value="Show" type="button" icon="pi pi-bars"/>
        <p:menu overlay="true" trigger="dynaButton" my="left top" at="left bottom">
            <p:submenu label="Options">
                <p:menuitem value="Save (Non-Ajax)" action="#{menuView.save}" update="messages" ajax="false"
                            icon="pi pi-save"/>
                <p:menuitem value="Update" action="#{menuView.update}" update="messages" icon="pi pi-refresh"/>
                <p:menuitem value="Delete" action="#{menuView.delete}" update="messages" icon="pi pi-times"/>
            </p:submenu>
        </p:menu>
    </h:body>
</f:view>
</html>
```


### Expected behavior

_No response_

### PrimeFaces edition

Community

### PrimeFaces version

14.0.1

### Theme

_No response_

### JSF implementation

Mojarra

### JSF version

2.3-next-M7

### Java version

21

### Browser(s)

_No response_

Pull request description:
Fix #12117: 14.0.2 Menu fix focus infinite loop
Fix #12117: 14.0.2 Menu fix focus infinite loop

Patch:
diff --git a/pom.xml b/pom.xml
index 5841627510..8a7f1e2175 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
     <groupId>org.primefaces</groupId>
     <artifactId>primefaces-parent</artifactId>
     <packaging>pom</packaging>
-    <version>14.0.1</version>
+    <version>14.0.2-SNAPSHOT</version>
 
     <name>PrimeFaces Parent</name>
     <url>https://www.primefaces.org</url>
diff --git a/primefaces-cli/pom.xml b/primefaces-cli/pom.xml
index fb6aa6141f..64c44c5e77 100644
--- a/primefaces-cli/pom.xml
+++ b/primefaces-cli/pom.xml
@@ -7,7 +7,7 @@
     <parent>
         <groupId>org.primefaces</groupId>
         <artifactId>primefaces-parent</artifactId>
-        <version>14.0.1</version>
+        <version>14.0.2-SNAPSHOT</version>
         <relativePath>../pom.xml</relativePath>
     </parent>
 
diff --git a/primefaces-selenium/pom.xml b/primefaces-selenium/pom.xml
index 6992a90014..0f782274be 100644
--- a/primefaces-selenium/pom.xml
+++ b/primefaces-selenium/pom.xml
@@ -7,7 +7,7 @@
     <parent>
         <groupId>org.primefaces</groupId>
         <artifactId>primefaces-parent</artifactId>
-        <version>14.0.1</version>
+        <version>14.0.2-SNAPSHOT</version>
         <relativePath>../pom.xml</relativePath>
     </parent>
 
diff --git a/primefaces-selenium/primefaces-selenium-components/pom.xml b/primefaces-selenium/primefaces-selenium-components/pom.xml
index ada9dbe0b3..955d9d0c69 100644
--- a/primefaces-selenium/primefaces-selenium-components/pom.xml
+++ b/primefaces-selenium/primefaces-selenium-components/pom.xml
@@ -7,7 +7,7 @@
     <parent>
         <groupId>org.primefaces</groupId>
         <artifactId>primefaces-selenium</artifactId>
-        <version>14.0.1</version>
+        <version>14.0.2-SNAPSHOT</version>
         <relativePath>../pom.xml</relativePath>
     </parent>
 
diff --git a/primefaces-selenium/primefaces-selenium-core/pom.xml b/primefaces-selenium/primefaces-selenium-core/pom.xml
index 351891680d..6a5c51ca1b 100644
--- a/primefaces-selenium/primefaces-selenium-core/pom.xml
+++ b/primefaces-selenium/primefaces-selenium-core/pom.xml
@@ -7,7 +7,7 @@
     <parent>
         <groupId>org.primefaces</groupId>
         <artifactId>primefaces-selenium</artifactId>
-        <version>14.0.1</version>
+        <version>14.0.2-SNAPSHOT</version>
         <relativePath>../pom.xml</relativePath>
     </parent>
 
diff --git a/primefaces-showcase/pom.xml b/primefaces-showcase/pom.xml
index e9f45a7092..2e846939b5 100644
--- a/primefaces-showcase/pom.xml
+++ b/primefaces-showcase/pom.xml
@@ -7,7 +7,7 @@
     <parent>
         <groupId>org.primefaces</groupId>
         <artifactId>primefaces-parent</artifactId>
-        <version>14.0.1</version>
+        <version>14.0.2-SNAPSHOT</version>
         <relativePath>../pom.xml</relativePath>
     </parent>
 
diff --git a/primefaces/pom.xml b/primefaces/pom.xml
index 4091f1bd81..563e50162a 100644
--- a/primefaces/pom.xml
+++ b/primefaces/pom.xml
@@ -7,7 +7,7 @@
     <parent>
         <groupId>org.primefaces</groupId>
         <artifactId>primefaces-parent</artifactId>
-        <version>14.0.1</version>
+        <version>14.0.2-SNAPSHOT</version>
         <relativePath>../pom.xml</relativePath>
     </parent>
 
diff --git a/primefaces/src/main/resources/META-INF/resources/primefaces/menu/menu.plainmenu.js b/primefaces/src/main/resources/META-INF/resources/primefaces/menu/menu.plainmenu.js
index a4e128cbd8..f902e46983 100644
--- a/primefaces/src/main/resources/META-INF/resources/primefaces/menu/menu.plainmenu.js
+++ b/primefaces/src/main/resources/META-INF/resources/primefaces/menu/menu.plainmenu.js
@@ -30,83 +30,61 @@ PrimeFaces.widget.PlainMenu = PrimeFaces.widget.Menu.extend({
 
         this.menuitemLinks = this.jq.find('.ui-menuitem-link:not(.ui-state-disabled)');
 
-        //events
         this.bindEvents();
-
-        if (this.cfg.toggleable) {
-            this.cfg.statefulGlobal = Boolean(this.cfg.statefulGlobal);
-            this.collapsedIds = [];
-            this.createStorageKey();
-            this.restoreState();
-        }
+        this.bindToggleable();
+        this.bindOverlay();
     },
 
     /**
-     * Sets up all event listeners required by this widget.
+     * Binds the necessary events for the menu if it is toggleable. This includes setting up the state management
+     * by initializing the storage key and restoring the state from storage.
      * @private
      */
-    bindEvents: function() {
-        var $this = this;
-
-        // make first focusable
-        this.menuitemLinks.filter(':not([disabled])').first().attr("tabindex", "0");
-        this.resetFocus(true);
-
-        this.menuitemLinks.on("mouseenter.menu click.menu", function(e) {
-            $(this).trigger('focus');
-        })
-            .on("focusin.menu", function(e) {
-                $this.focus($(this));
-            })
-            .on("focusout.menu mouseleave.menu", function(e) {
-                $this.unfocus($(this));
-            })
-            .on('keydown.menu', function(e) {
-                var currentLink = $this.menuitemLinks.filter('.ui-state-hover');
-                switch (e.code) {
-                    case 'ArrowUp':
-                        var prevItem = currentLink.parent().prevAll('.ui-menuitem:first');
-                        if (prevItem.length) {
-                            $this.unfocus(currentLink);
-                            $this.focus(prevItem.children('.ui-menuitem-link'));
-                        }
-
-                        e.preventDefault();
-                        break;
+    bindToggleable: function() {
+        if (!this.cfg.toggleable) return;
 
-                    case 'ArrowDown':
-                        var nextItem = currentLink.parent().nextAll('.ui-menuitem:first');
-                        if (nextItem.length) {
-                            $this.unfocus(currentLink);
-                            $this.focus(nextItem.children('.ui-menuitem-link'));
-                        }
-
-                        e.preventDefault();
-                        break;
-
-                    case 'Space':
-                    case 'Enter':
-                    case 'NumpadEnter':
-                        currentLink.trigger('click');
-                        PrimeFaces.utils.openLink(e, currentLink);
-                        break;
+        this.cfg.statefulGlobal = Boolean(this.cfg.statefulGlobal);
+        this.collapsedIds = [];
+        this.createStorageKey();
+        this.restoreState();
 
-                    case 'Escape':
-                        $this.hide();
+        var $this = this;
+        var headers = this.jq.find('> .ui-menu-list > .ui-widget-header');
+        
+        headers.on('mouseover.menu', function() {
+            $(this).addClass('ui-state-hover');
+        }).on('mouseout.menu', function() {
+            $(this).removeClass('ui-state-hover');
+        }).on('click.menu', function(e) {
+            var header = $(this);
+            var icon = header.find('> h3 > .ui-icon');
+
+            if (icon.hasClass('ui-icon-triangle-1-s')) {
+                $this.collapseSubmenu(header, true);
+            } else {
+                $this.expandSubmenu(header, true);
+            }
 
-                        if ($this.cfg.overlay) {
-                            $this.trigger.trigger('focus');
-                        }
-                        break;
+            PrimeFaces.clearSelection();
+            e.preventDefault();
+        });
+    },
 
-                }
-            });
+    /**
+     * Binds overlay-specific event handlers if the overlay configuration is enabled.
+     * This includes hiding the menu on certain key presses or clicks, and managing focus.
+     * @private
+     */
+    bindOverlay: function() {
+        var $this = this;
 
         if (this.cfg.overlay) {
+            // Hide menu when any menu item link is clicked
             this.menuitemLinks.on("click", function() {
                 $this.hide();
             });
 
+            // Handle keyboard navigation for overlay
             this.trigger.on('keydown.ui-menu', function(e) {
                 switch (e.key) {
                     case 'ArrowDown':
@@ -123,33 +101,75 @@ PrimeFaces.widget.PlainMenu = PrimeFaces.widget.Menu.extend({
                         break;
                 }
             });
-        }
-        else {
+        } else {
+            // Reset focus when focus moves out of the menu and the new focus target is not within the menu
             this.jq.on("focusout.menu", function(e) {
                 if (!$this.jq.has(e.relatedTarget).length) {
                     $this.resetFocus(true);
                 }
             });
         }
+    },
+ 
+    /**
+     * Binds event handlers to menu item links for interaction via mouse and keyboard.
+     * This includes setting the initial focus, handling mouse enter and leave, click events,
+     * and keyboard navigation using arrow keys, space, and enter.
+     * @private
+     */
+    bindEvents: function() {
+        var $this = this;
+
+        // Set the first focusable menu item
+        this.menuitemLinks.filter(':not([disabled])').first().attr("tabindex", "0");
+        this.resetFocus(true);
 
-        if (this.cfg.toggleable) {
-            this.jq.find('> .ui-menu-list > .ui-widget-header').on('mouseover.menu', function() {
-                $(this).addClass('ui-state-hover');
-            })
-                .on('mouseout.menu', function() {
-                    $(this).removeClass('ui-state-hover');
-                })
-                .on('click.menu', function(e) {
-                    var header = $(this);
-
-                    if (header.find('> h3 > .ui-icon').hasClass('ui-icon-triangle-1-s'))
-                        $this.collapseSubmenu(header, true);
-                    else
-                        $this.expandSubmenu(header, true);
-
-                    PrimeFaces.clearSelection();
+        // Bind mouse and click events to focus items
+        this.menuitemLinks.on("mouseenter.menu click.menu", function() {
+            $this.focus($(this));
+        }).on("focusout.menu mouseleave.menu", function() {
+            $this.unfocus($(this));
+        });
+
+        // Bind keyboard navigation events
+        this.menuitemLinks.on('keydown.menu', function(e) {
+            var currentLink = $this.menuitemLinks.filter('.ui-state-hover');
+            switch (e.code) {
+                case 'ArrowUp':
+                    $this.navigateMenu(currentLink, 'prev');
+                    e.preventDefault();
+                    break;
+                case 'ArrowDown':
+                    $this.navigateMenu(currentLink, 'next');
                     e.preventDefault();
-                });
+                    break;
+                case 'Space':
+                case 'Enter':
+                case 'NumpadEnter':
+                    currentLink.trigger('click');
+                    PrimeFaces.utils.openLink(e, currentLink);
+                    break;
+                case 'Escape':
+                    $this.hide();
+                    if ($this.cfg.overlay) {
+                        $this.trigger.trigger('focus');
+                    }
+                    break;
+            }
+        });
+    },
+
+    /**
+     * Navigates the menu items in the specified direction ('prev' or 'next').
+     * @param {JQuery} currentLink The currently focused menu item link.
+     * @param {string} direction The direction to navigate ('prev' or 'next').
+     * @private
+     */
+    navigateMenu: function(currentLink, direction) {
+        var targetItem = currentLink.parent()[direction + 'All']('.ui-menuitem:first');
+        if (targetItem.length) {
+            this.unfocus(currentLink);
+            this.focus(targetItem.children('.ui-menuitem-link'));
         }
     },
 


Test patch:
diff --git a/primefaces-integration-tests-jakarta/pom.xml b/primefaces-integration-tests-jakarta/pom.xml
index 0a924b9c4e..8163e41f32 100644
--- a/primefaces-integration-tests-jakarta/pom.xml
+++ b/primefaces-integration-tests-jakarta/pom.xml
@@ -7,7 +7,7 @@
     <parent>
         <groupId>org.primefaces</groupId>
         <artifactId>primefaces-parent</artifactId>
-        <version>14.0.1</version>
+        <version>14.0.2-SNAPSHOT</version>
         <relativePath>../pom.xml</relativePath>
     </parent>
 
diff --git a/primefaces-integration-tests/pom.xml b/primefaces-integration-tests/pom.xml
index 95a7cc519f..d5a12b4ab9 100644
--- a/primefaces-integration-tests/pom.xml
+++ b/primefaces-integration-tests/pom.xml
@@ -7,7 +7,7 @@
     <parent>
         <groupId>org.primefaces</groupId>
         <artifactId>primefaces-parent</artifactId>
-        <version>14.0.1</version>
+        <version>14.0.2-SNAPSHOT</version>
         <relativePath>../pom.xml</relativePath>
     </parent>