var d3 = require('d3-selection')

module.exports = function (selection, tree, transitions) {

  // Add the node contents
  function field (id, f) {
    var n = tree.nodes[id]
    return n ? (n[f] || '') : ''
  }

  selection.each(function (data) {
    var node = d3.select(this)
      , contents = node.selectAll('.node-contents')
                       .data(function (d) {
                         return [d]
                       })
      , enter = contents.enter()
                        .append('div')
                          .attr('class', 'node-contents')
                          .attr('style', function (d) {
                            var x = transitions ? (d.parent ? d.parent._x : 0) : d._x
                            return tree.prefix + 'transform:' + 'translate3d(' + x + 'px,0px,0px)'
                          })

    // Add the toggler
    enter.append('div')
           .attr('class', function (d) {
             return 'toggler ' + (d.children ? 'expanded' : d._allChildren && d._allChildren.length ? 'collapsed' : 'leaf')
           })
           .on('click', tree._onToggle.bind(tree))
           .append('svg')
             .append('use')
               .attr('xlink:href', '#icon-collapsed')

    enter.append('svg')
         .attr('class', function (d) {
           return 'icon ' + field(d.id, tree.options.accessors.color) + ' '
                          + field(d.id, tree.options.accessors.icon) + ' '
         })
         .append('use')
         .attr('xlink:href', function (d) {
           return '#icon-' + field(d.id, tree.options.accessors.icon)
         })

    enter.append('div')
         .attr('class', 'label')
         .call(tree.options.label.bind(tree))

    contents.select('svg.icon')
            .attr('class', function (d) {
              return 'icon ' + (tree.nodes[d.id][tree.options.accessors.color] || '') + ' '
                             + (tree.nodes[d.id][tree.options.accessors.icon] || '')
            })
            .select('use')
            .attr('xlink:href', function (d) {
              return '#icon-' + tree.nodes[d.id][tree.options.accessors.icon]
            })

    // change the state of the toggle icon by adjusting its class
    contents.select('.toggler')
            .attr('class', function (d) {
              return 'toggler ' + (d.children ? 'expanded' : d._allChildren && d._allChildren.length ? 'collapsed' : 'leaf')
            })

    // Perhaps the name changed
    contents.select('div.label')
            .call(tree.options.label.bind(tree))

    // Now the label mask
    node.selectAll('.label-mask')
        .data(function (d) {
          return [d]
        })
        .enter()
        .append('div')
          .attr('class', function (d) {
            return (tree.options.indicator ? 'label-mask indicator ' : 'label-mask ') + field(d.id, tree.options.accessors.color)
          })
  })
}

@@ -27,16 +27,337 @@
 tion')%0A%0A
+// Nodes are expandable if they have some children in _allChildren that aren't marked as %60visible: false%60%0Afunction expandable (node) %7B%0A  return node._allChildren &&%0A         node._allChildren.length &&%0A         node._allChildren.filter(function (c) %7B%0A           return c.visible !== false%0A         %7D)%0A         .length%0A%7D%0A%0A
 module.e
@@ -1304,47 +1304,21 @@
 ' : 
-d._allChildren && d._allChildren.length
+expandable(d)
  ? '
@@ -2632,47 +2632,21 @@
 ' : 
-d._allChildren && d._allChildren.length
+expandable(d)
  ? '

Check not just for _allChildren, but make sure there are visible nodes in that array.