var _ = require('../util') var Cache = require('../cache') var templateCache = new Cache(1000) var idSelectorCache = new Cache(1000) var map = { _default : [0, '', ''], legend : [1, '
', '
'], tr : [2, '', '
'], col : [ 2, '', '
' ] } map.td = map.th = [ 3, '', '
' ] map.option = map.optgroup = [ 1, '' ] map.thead = map.tbody = map.colgroup = map.caption = map.tfoot = [1, '', '
'] map.g = map.defs = map.symbol = map.use = map.image = map.text = map.circle = map.ellipse = map.line = map.path = map.polygon = map.polyline = map.rect = [ 1, '', '' ] var TAG_RE = /<([\w:]+)/ /** * Convert a string template to a DocumentFragment. * Determines correct wrapping by tag types. Wrapping * strategy found in jQuery & component/domify. * * @param {String} templateString * @return {DocumentFragment} */ function stringToFragment (templateString) { // try a cache hit first var hit = templateCache.get(templateString) if (hit) { return hit } var frag = document.createDocumentFragment() var tagMatch = TAG_RE.exec(templateString) if (!tagMatch) { // text only, return a single text node. frag.appendChild( document.createTextNode(templateString) ) } else { var tag = tagMatch[1] var wrap = map[tag] || map._default var depth = wrap[0] var prefix = wrap[1] var suffix = wrap[2] var node = document.createElement('div') node.innerHTML = prefix + templateString.trim() + suffix while (depth--) { node = node.lastChild } var child /* jshint boss:true */ while (child = node.firstChild) { frag.appendChild(child) } } templateCache.put(templateString, frag) return frag } /** * Convert a template node to a DocumentFragment. * * @param {Node} node * @return {DocumentFragment} */ function nodeToFragment (node) { var tag = node.tagName // if its a template tag and the browser supports it, // its content is already a document fragment. if ( tag === 'TEMPLATE' && node.content instanceof DocumentFragment ) { return node.content } return tag === 'SCRIPT' ? stringToFragment(node.textContent) : stringToFragment(node.innerHTML) } // Test for the presence of the Safari template cloning bug // https://bugs.webkit.org/show_bug.cgi?id=137755 var hasBrokenTemplate = _.inBrowser ? (function () { var a = document.createElement('div') a.innerHTML = '' return !a.cloneNode(true).firstChild.innerHTML })() : false // Test for IE10/11 textarea placeholder clone bug var hasTextareaCloneBug = _.inBrowser ? (function () { var t = document.createElement('textarea') t.placeholder = 't' return t.cloneNode(true).value === 't' })() : false /** * 1. Deal with Safari cloning nested