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

Fixes for JSX support #67

Open
wants to merge 2 commits 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
37 changes: 28 additions & 9 deletions src/hyperscript/h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { isPrimitive, isString } from '../helpers'

export const h: HyperscriptFn = function(): VNode {
const tagName: string | ComponentFn = arguments[0] // required
const childrenOrText: HyperscriptChildren = arguments[2] // optional
const childrenOrText: HyperscriptChildren = slice(2, arguments) // optional

let props: VNodeProps<Element> = {}
let children: ArrayLike<VNode> | undefined
let props: VNodeProps = {}
let children: Array<VNode> | undefined
let text: string | undefined

if (childrenOrText) {
props = arguments[1]
props = arguments[1] || {}

if (isArrayLike(childrenOrText)) children = flattenArrayLike(childrenOrText) as Array<VNode>
else if (isPrimitive(childrenOrText)) text = String(childrenOrText)
Expand All @@ -21,11 +21,20 @@ export const h: HyperscriptFn = function(): VNode {
if (isArrayLike(childrenOrTextOrProps))
children = flattenArrayLike(childrenOrTextOrProps) as Array<VNode>
else if (isPrimitive(childrenOrTextOrProps)) text = String(childrenOrTextOrProps)
else props = childrenOrTextOrProps
else props = childrenOrTextOrProps || {}
}

if (typeof tagName === 'function') {
return tagName(props, Array.isArray(children) ? children : text ? [ text ] : [])
const childVNodes = Array.isArray(children)
? children
: text ? [ MostlyVNode.createText(text) ] : []
const computedVNode = tagName(props, childVNodes)

if (Array.isArray(computedVNode.children)) {
computedVNode.children = sanitizeChildren(computedVNode.children, computedVNode)
}

return computedVNode
}

const isSvg = tagName === 'svg'
Expand All @@ -41,6 +50,15 @@ export const h: HyperscriptFn = function(): VNode {
return vNode
}

function slice<A>(from: number, arrLike: ArrayLike<A>): Array<A> | null {
const arr = [] as Array<A>
for (let i = from; i < arrLike.length; ++i) arr.push(arrLike[i])

if (arr.length === 0) return null

return arr
}

function isArrayLike<T>(x: any): x is ArrayLike<T> {
const typeOf = typeof x

Expand All @@ -61,20 +79,21 @@ function forEach<A>(fn: (value: A) => void, list: ArrayLike<A>): void {
}

function sanitizeChildren(childrenOrText: Array<VNode>, parent: VNode): Array<VNode> {
childrenOrText = childrenOrText.filter(Boolean) // remove possible null values
childrenOrText = childrenOrText.filter((x) => x !== null) // remove possible null values
const childCount: number = childrenOrText.length

const children: Array<VNode> = Array(childCount)

for (let i = 0; i < childCount; ++i) {
const vNodeOrText = childrenOrText[i]

if (isString(vNodeOrText)) children[i] = MostlyVNode.createText(vNodeOrText)
if (isString(vNodeOrText) || typeof vNodeOrText === 'number')
children[i] = MostlyVNode.createText(String(vNodeOrText))
else children[i] = vNodeOrText

if (parent.scope && !children[i].scope) children[i].scope = parent.scope

children[i].parent = parent as VNode<Element>
children[i].parent = parent
}

return children
Expand Down