From 3466a19397a6096ebdee3c6963a7528b39a8878c Mon Sep 17 00:00:00 2001 From: zadam Date: Sat, 2 May 2020 12:16:48 +0200 Subject: [PATCH] protection against recursive expansion of search notes --- src/public/app/widgets/note_tree.js | 42 +++++++++++++++++++---------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/public/app/widgets/note_tree.js b/src/public/app/widgets/note_tree.js index a0b7ba235..e8c0cd7b4 100644 --- a/src/public/app/widgets/note_tree.js +++ b/src/public/app/widgets/note_tree.js @@ -314,7 +314,17 @@ export default class NoteTreeWidget extends TabAwareWidget { } }, lazyLoad: (event, data) => { - const noteId = data.node.data.noteId; + const {noteId, noteType} = data.node.data; + + if (noteType === 'search') { + const notePath = treeService.getNotePath(data.node.getParent()); + + // this is a search cycle (search note is a descendant of its own search result) + if (notePath.includes(noteId)) { + data.result = []; + return; + } + } data.result = treeCache.getNote(noteId).then(note => this.prepareChildren(note)); }, @@ -452,17 +462,12 @@ export default class NoteTreeWidget extends TabAwareWidget { extraClasses: this.getExtraClasses(note), icon: this.getIcon(note), refKey: note.noteId, + lazy: true, + folder: await this.isFolder(note), expanded: branch.isExpanded || hoistedNoteId === note.noteId, key: utils.randomString(12) // this should prevent some "duplicate key" errors }; - const childBranches = await this.getChildBranches(note); - - node.folder = childBranches.length > 0 - || note.type === 'search' - - node.lazy = node.folder && !node.expanded; - if (node.folder && node.expanded) { node.children = await this.prepareChildren(note); } @@ -470,6 +475,17 @@ export default class NoteTreeWidget extends TabAwareWidget { return node; } + async isFolder(note) { + if (note.type === 'search') { + return true; + } + else { + const childBranches = await this.getChildBranches(note); + + return childBranches.length > 0; + } + } + async prepareNormalNoteChildren(parentNote) { utils.assertArguments(parentNote); @@ -680,7 +696,6 @@ export default class NoteTreeWidget extends TabAwareWidget { let foundChildNode = this.findChildNode(parentNode, childNoteId); if (!foundChildNode) { // note might be recently created so we'll force reload and try again - parentNode.lazy = true; await parentNode.load(true); foundChildNode = this.findChildNode(parentNode, childNoteId); @@ -723,8 +738,7 @@ export default class NoteTreeWidget extends TabAwareWidget { node.data.isProtected = note.isProtected; node.data.noteType = note.type; - node.folder = (await this.getChildBranches(note)).length > 0 - || note.type === 'search'; + node.folder = await this.isFolder(note); node.icon = this.getIcon(note); node.extraClasses = this.getExtraClasses(note); node.title = (branch.prefix ? (branch.prefix + " - ") : "") + note.title; @@ -783,7 +797,6 @@ export default class NoteTreeWidget extends TabAwareWidget { async refreshSearch() { const activeNode = this.getActiveNode(); - activeNode.lazy = true; activeNode.load(true); activeNode.setExpanded(true); @@ -847,7 +860,9 @@ export default class NoteTreeWidget extends TabAwareWidget { } } - node.remove(); + if (node.getParent()) { + node.remove(); + } noteIdsToUpdate.add(branch.parentNoteId); } @@ -877,7 +892,6 @@ export default class NoteTreeWidget extends TabAwareWidget { for (const noteId of noteIdsToReload) { for (const node of this.getNodesByNoteId(noteId)) { - node.lazy = true; await node.load(true); this.updateNode(node);