Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: block nesting $if #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,32 @@ export function isDLPtr(arr) {
return isobj(arr) && USE_COMPUTED in arr
}

/* DEV.START */
function isDLIfNested(obj) {
if (!isobj(obj)) return false

if (IF in obj) return true

return (
obj instanceof Array &&
obj.reduce((partial, child) => {
//fragment checking
if (partial) return true
return isDLIfNested(child) // check if $if or if fragment -> recurse
}, false)
)
}
/* DEV.END */

export function $if(condition, then, otherwise) {
otherwise ??= doc.createTextNode('')
if (!isDLPtr(condition)) return condition ? then : otherwise

assert(
!isDLIfNested(then) && !isDLIfNested(otherwise),
'Elements in $if cannot also be an $if macro'
)

return { [IF]: condition, then, otherwise }
}

Expand Down