functionstringToFragment (templateString) { var frag = document.createDocumentFragment() var tagMatch = templateString.match(tagRE) var entityMatch = entityRE.test(templateString) var commentMatch = commentRE.test(templateString)
if (!tagMatch && !entityMatch && !commentMatch) { // text only, return a single text node. frag.appendChild( document.createTextNode(templateString) ) } else { var node = document.createElement('div') node.innerHTML = templateString frag.appendChild(node) } return frag }
functioninit (el, template) { var origin = el el = transcludeTemplate(el, template) replace(origin, el) return el }
functionreplace (target, el) { var parent = target.parentNode if (parent) { parent.replaceChild(el, target) } }
functiontranscludeTemplate (el, template) { var frag = stringToFragment(template) var replacer = frag.firstChild var attrs = el.attributes var i = attrs.length var name, value while (i--) { name = attrs[i].name value = attrs[i].value if (!replacer.hasAttribute(name)) { replacer.setAttribute(name, value) } } return replacer }