github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/vue-1.0.24/src/compiler/resolve-slots.js (about)

     1  import { parseTemplate } from '../parsers/template'
     2  import {
     3    isTemplate,
     4    toArray,
     5    getBindAttr,
     6    warn
     7  } from '../util/index'
     8  
     9  /**
    10   * Scan and determine slot content distribution.
    11   * We do this during transclusion instead at compile time so that
    12   * the distribution is decoupled from the compilation order of
    13   * the slots.
    14   *
    15   * @param {Element|DocumentFragment} template
    16   * @param {Element} content
    17   * @param {Vue} vm
    18   */
    19  
    20  export function resolveSlots (vm, content) {
    21    if (!content) {
    22      return
    23    }
    24    var contents = vm._slotContents = Object.create(null)
    25    var el, name
    26    for (var i = 0, l = content.children.length; i < l; i++) {
    27      el = content.children[i]
    28      /* eslint-disable no-cond-assign */
    29      if (name = el.getAttribute('slot')) {
    30        (contents[name] || (contents[name] = [])).push(el)
    31      }
    32      /* eslint-enable no-cond-assign */
    33      if (process.env.NODE_ENV !== 'production' && getBindAttr(el, 'slot')) {
    34        warn('The "slot" attribute must be static.', vm.$parent)
    35      }
    36    }
    37    for (name in contents) {
    38      contents[name] = extractFragment(contents[name], content)
    39    }
    40    if (content.hasChildNodes()) {
    41      const nodes = content.childNodes
    42      if (
    43        nodes.length === 1 &&
    44        nodes[0].nodeType === 3 &&
    45        !nodes[0].data.trim()
    46      ) {
    47        return
    48      }
    49      contents['default'] = extractFragment(content.childNodes, content)
    50    }
    51  }
    52  
    53  /**
    54   * Extract qualified content nodes from a node list.
    55   *
    56   * @param {NodeList} nodes
    57   * @return {DocumentFragment}
    58   */
    59  
    60  function extractFragment (nodes, parent) {
    61    var frag = document.createDocumentFragment()
    62    nodes = toArray(nodes)
    63    for (var i = 0, l = nodes.length; i < l; i++) {
    64      var node = nodes[i]
    65      if (
    66        isTemplate(node) &&
    67        !node.hasAttribute('v-if') &&
    68        !node.hasAttribute('v-for')
    69      ) {
    70        parent.removeChild(node)
    71        node = parseTemplate(node, true)
    72      }
    73      frag.appendChild(node)
    74    }
    75    return frag
    76  }