function selfAndChildren(elements, unique, maxDepth) {
  var result = [],
      processedChildren = [];

  eachElement(elements, function(element, i, depth) {
    add(result, element, unique);

    var children = element.children;

    // max traversal depth not reached yet
    if (maxDepth === -1 || depth < maxDepth) {

      // children exist && children not yet processed
      if (children && add(processedChildren, children, unique)) {
        return children;
      }
    }
  });

  return result;
}
Collects self + child elements up to a given depth from a list of elements.

@param {Element|Element[]} elements the elements to select the children from
@param {boolean} unique whether to return a unique result set (no duplicates)
@param {number} maxDepth the depth to search through or -1 for infinite

@return {Element[]} found elements