--- includes/menu.inc	2010-12-09 12:57:18.000000000 +0100
+++ includes/menu.inc	2011-02-20 22:57:56.000000000 +0100
@@ -899,8 +899,22 @@ function menu_tree_page_data($menu_name 
 
           if (empty($parents)) {
             // If no link exists, we may be on a local task that's not in the links.
-            // TODO: Handle the case like a local task on a specific node in the menu.
             $parents = db_fetch_array(db_query("SELECT p1, p2, p3, p4, p5, p6, p7, p8 FROM {menu_links} WHERE menu_name = '%s' AND link_path = '%s'", $menu_name, $item['tab_root']));
+
+            if (empty($parents) && strpos($item['tab_root'], '%') !== FALSE) {
+              // Handle cases like a local task on a specific node in the menu.
+              // Make a guess that stripping last argument from path will give
+              // a reasonable menu entry. (TODO: is there a more general solution?)
+              // Example: for a node edit page such as "node/123/edit",
+              // $item['tab_root'] is "node/%node" and there is usually
+              // no such menu_link. Here we will search for menu_link="node/123"
+              // which is the menu for viewing the node that is being edited.
+              // This means the menu for the node remains expanded when edited.
+              $parent_path = explode('/', $_GET['q']);
+              array_pop($parent_path); 
+              $parent_path = join('/', $parent_path);
+              $parents = db_fetch_array(db_query("SELECT p1, p2, p3, p4, p5, p6, p7, p8 FROM {menu_links} WHERE menu_name = '%s' AND link_path = '%s'", $menu_name, $parent_path));
+            }
           }
           // We always want all the top-level links with plid == 0.
           $parents[] = '0';
