Index: chrome/browser/resources/md_bookmarks/command_manager.js |
diff --git a/chrome/browser/resources/md_bookmarks/command_manager.js b/chrome/browser/resources/md_bookmarks/command_manager.js |
index 9c5e564f818e49a55d772090ca5eca27c29fd571..a21c9c471868ec0e2bdcb6484d27bb132b9670f8 100644 |
--- a/chrome/browser/resources/md_bookmarks/command_manager.js |
+++ b/chrome/browser/resources/md_bookmarks/command_manager.js |
@@ -103,21 +103,41 @@ Polymer({ |
}); |
break; |
case Command.DELETE: |
- // TODO(tsergeant): Filter IDs so we don't try to delete children of |
- // something else already being deleted. |
chrome.bookmarkManagerPrivate.removeTrees( |
- Array.from(itemIds), function() { |
+ Array.from(this.minimizeDeletionSet_(itemIds)), function() { |
// TODO(jiaxi): Add toast later. |
}); |
break; |
} |
- |
}, |
//////////////////////////////////////////////////////////////////////////// |
// Private functions: |
/** |
+ * Minimize the set of |itemIds| by removing any node which has an ancestor |
+ * node already in the set. This ensures that instead of trying to delete both |
+ * a node and its descendant, we will only try to delete the topmost node, |
+ * preventing an error in the bookmarkManagerPrivate.removeTrees API call. |
+ * @param {!Set<string>} itemIds |
+ * @return {!Set<string>} |
+ */ |
+ minimizeDeletionSet_: function(itemIds) { |
+ var minimizedSet = new Set(); |
+ var nodes = this.getState().nodes; |
+ itemIds.forEach(function(itemId) { |
+ var currentId = itemId; |
+ while (currentId != ROOT_NODE_ID) { |
+ currentId = assert(nodes[currentId].parentId); |
+ if (itemIds.has(currentId)) |
+ return; |
+ } |
+ minimizedSet.add(itemId); |
+ }); |
+ return minimizedSet; |
+ }, |
+ |
+ /** |
* @param {!Set<string>} itemIds |
* @param {function(BookmarkNode):boolean} predicate |
* @return {boolean} True if any node in |itemIds| returns true for |