From 2eabff3ab983edeedd830fbe7aa68f4ee94326b1 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Fri, 19 Aug 2022 13:13:17 +0300 Subject: [PATCH] fixed slot content... Signed-off-by: Alex A. Naanou --- pwiki/page.js | 70 +++++++++++++++++++++++++++++++------------------- pwiki2-test.js | 17 ++++++++++++ pwiki2.js | 6 ++--- 3 files changed, 64 insertions(+), 29 deletions(-) diff --git a/pwiki/page.js b/pwiki/page.js index b867610..7d70e60 100755 --- a/pwiki/page.js +++ b/pwiki/page.js @@ -853,6 +853,11 @@ object.Constructor('Page', BasePage, { // all other slots with will replace its content, unless // explicit shown/hidden arguments are given. // NOTE: hidden has precedence over shown if both are given. + // NOTE: slots are handled in order of occurrence of opening tags + // in text and not by hierarchy, i.e. the later slot overrides + // the former and the most nested overrides the parent. + // This also works for cases where slots override slots they + // are contained in, this will not lead to recursion. // // XXX revise the use of hidden/shown use mechanic and if it's // needed... @@ -886,32 +891,40 @@ object.Constructor('Page', BasePage, { // show first instance... : name in slots) - // NOTE: we prioritize the nested slots over the current... - var parent_slot = slots[name] + var stack = [] + slots[name] + && stack.push(slots[name]) delete slots[name] var slot = await this.__parser__.expand(this, text, state) - parent_slot = slot[name] == null ? - parent_slot - : slot + slots[name] + && stack.unshift(slot) slot = slots[name] = slots[name] ?? slot // handle ... - // XXX BUG: nested slots are not handled correctly here... - for(var i in slot){ - if(typeof(slot[i]) != 'string' - && slot[i].name == 'content'){ - break } - i = null } - i != null - && parent_slot - && (slot[i] = parent_slot) + for(prev of stack){ + // get the first + for(var i in slot){ + if(typeof(slot[i]) != 'string' + && slot[i].name == 'content'){ + break } + i = null } + i != null + && slot.splice(i, 1, + ...prev + // remove nested slot handlers... + .filter(function(e){ + return typeof(e) != 'function' + || e.slot != name }) ) } return hidden ? '' - : function(state){ - return state.slots[name] } }), + : Object.assign( + function(state){ + return state.slots[name] }, + {slot: name}) }), + //*/ 'content': ['slot'], // @@ -1504,18 +1517,23 @@ module.System = { C - ` }, - test_nested_slots: { - text: object.doc` - Nested content: A B C: - - A - + +

+ Mixed content: X A B C Z: + + X + + + A + B - - C - + + C + + + + Z ` }, } diff --git a/pwiki2-test.js b/pwiki2-test.js index 362bd51..bdac2b4 100755 --- a/pwiki2-test.js +++ b/pwiki2-test.js @@ -207,6 +207,23 @@ pwiki.pwiki C
+ +

+ Mixed content: X A B C Z: + + X + + + A + + B + + C + + + + + Z `, }) .update({ location: '/test/nesting', diff --git a/pwiki2.js b/pwiki2.js index 9b015a3..b9c954d 100755 --- a/pwiki2.js +++ b/pwiki2.js @@ -1,7 +1,6 @@ /********************************************************************** * * -* XXX slot does not work in nested slots... * XXX GENERATOR make pattern path parsing a generator... * ...experiment with a controllable iterator/range thing... * This would require: @@ -19,7 +18,7 @@ * 2) all the macros that can source pages to produce generators: * @include(..) -- DONE * @source(..) -- DONE -* @quote(..) -- +* @quote(..) -- DONE * @macro(..) -- * 3) experiment with back-drivable generators... * this can be implemented/tested in parallel and integrated into @@ -171,7 +170,8 @@ * mistakes, bugs and inconsistencies. * - types of recursion * (see: pwiki/page.js: Page.macros.include(..) notes) -* - +* - slot order -- +* (see: page.js: Page's .macros.slot(..) notes) * * * XXX weaknesses to review: