Skip to content
Closed
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions src/van.debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ const isDomOrPrimitive = v => v instanceof Node || isValidPrimitive(v)

const checkChildValid = child => {
expect(
isDomOrPrimitive(child) || protoOf(child ?? 0) === stateProto && isValidPrimitive(child.val),
"Only DOM Node, string, number, boolean, bigint, and state of string, number, boolean or bigint are valid child of a DOM Node"
child == undefined || isDomOrPrimitive(child) || protoOf(child ?? 0) === stateProto && isValidPrimitive(child.val),
"Only DOM Node, string, number, boolean, bigint, and state of string, number, boolean, bigint, null or undefined are valid child of a DOM Node"
)
expect(!child.isConnected, "You can't add a DOM Node that is already connected to document")
expect(!child || !child.isConnected, "You can't add a DOM Node that is already connected to document")
}

const add = (dom, ...children) => {
Expand Down
2 changes: 1 addition & 1 deletion src/van.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ let state = initVal => ({
let toDom = v => v.nodeType ? v : new Text(v)

let add = (dom, ...children) => (
children.flat(Infinity).forEach(child => dom.appendChild(
children.flat(Infinity).filter(c => c != undefined).forEach(child => dom.appendChild(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!= null is shorter and achieves the same thing.

protoOf(child) === stateProto ? bind(child, v => v) : toDom(child))),
dom)

Expand Down
13 changes: 8 additions & 5 deletions test/van.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,6 @@ const runTests = async (vanObj: VanForTesting, msgDom: Element, {debug}: BundleO

tagsTest_invalidChild: () => {
assertError(/Only.*are valid child of a DOM Node/, () => div(div(), <any>{}, p()))
assertError(/Only.*are valid child of a DOM Node/, () => div(div(), <any>null, p()))
assertError(/Only.*are valid child of a DOM Node/, () => div(div(), <any>undefined, p()))
assertError(/Only.*are valid child of a DOM Node/, () => div(div(), <any>((x: number) => x * 2), p()))

assertError(/Only.*are valid child of a DOM Node/, () => div(div(), state(<any>{}), p()))
Expand All @@ -608,8 +606,6 @@ const runTests = async (vanObj: VanForTesting, msgDom: Element, {debug}: BundleO
const dom = div()

assertError(/Only.*are valid child of a DOM Node/, () => add(dom, div(), <any>{}, p()))
assertError(/Only.*are valid child of a DOM Node/, () => add(dom, div(), <any>null, p()))
assertError(/Only.*are valid child of a DOM Node/, () => add(dom, div(), <any>undefined, p()))
assertError(/Only.*are valid child of a DOM Node/, () => add(dom, div(), <any>((x: number) => x * 2), p()))

assertError(/Only.*are valid child of a DOM Node/, () => add(dom, div(), state(<any>{}), p()))
Expand Down Expand Up @@ -744,7 +740,14 @@ const runTests = async (vanObj: VanForTesting, msgDom: Element, {debug}: BundleO
await sleep(waitMsOnDomUpdates)
assertEq((<Element>hiddenDom.firstChild).querySelector("div")!.innerText, "❤️: 1")
}),

nullChildren: () => {
const List = () => div("one", undefined, null)
assertEq(List().outerHTML, "<div>one</div>")
},
childrenWith0: () => {
const List = () => div([div(0), null])
assertEq(List().outerHTML, "<div><div>0</div></div>")
},
bulletList: () => {
const List = ({items}) => ul(items.map(it => li(it)))
assertEq(List({items: ["Item 1", "Item 2", "Item 3"]}).outerHTML, "<ul><li>Item 1</li><li>Item 2</li><li>Item 3</li></ul>")
Expand Down