Allow before function for selection.insert.

Similar to selection.select(function), the before argument to selection.insert
can now be specified as a function, so that the caller can specify the before
node dynamically. Supersedes #1071.