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

Support array for multiple roots? #148

Open
gilbert opened this issue Apr 29, 2019 · 0 comments
Open

Support array for multiple roots? #148

gilbert opened this issue Apr 29, 2019 · 0 comments

Comments

@gilbert
Copy link

gilbert commented Apr 29, 2019

Here's a case when multiple roots throws an error:

var html = require('nanohtml')

var nums = [10,20,30]

var el = html`
  <main>
    <h1>Hello planet</h1>
  </main>
  ${nums.map(n =>
    html`<div>${n}</div>`
  )}
`
document.body.appendChild(el)

Updating nanohtml's createFragment to look something like this solves the problem:

function createFragment (nodes) {
  var fragment = document.createDocumentFragment()
  for (var i = 0; i < nodes.length; i++) {
    if (typeof nodes[i] === 'string') nodes[i] = document.createTextNode(nodes[i])

    if (Array.isArray(nodes[i])) {
      for (var n=0; n < nodes[i].length; n++) {
        fragment.appendChild(nodes[i][n])
      }
    }
    else {
      fragment.appendChild(nodes[i])
    }
  }
  return fragment
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant