define("morph", ["./morph/morph","./morph/dom-helper","exports"], function(__dependency1__, __dependency2__, __exports__) { "use strict"; var Morph = __dependency1__["default"]; var Morph; __exports__.Morph = Morph; var DOMHelper = __dependency2__["default"]; var DOMHelper; __exports__.DOMHelper = DOMHelper; }); define("morph/dom-helper", ["../morph/morph","./dom-helper/build-html-dom","exports"], function(__dependency1__, __dependency2__, __exports__) { "use strict"; var Morph = __dependency1__["default"]; var buildHTMLDOM = __dependency2__.buildHTMLDOM; var svgNamespace = __dependency2__.svgNamespace; var svgHTMLIntegrationPoints = __dependency2__.svgHTMLIntegrationPoints; var deletesBlankTextNodes = (function(){ var element = document.createElement('div'); element.appendChild( document.createTextNode('') ); var clonedElement = element.cloneNode(true); return clonedElement.childNodes.length === 0; })(); var ignoresCheckedAttribute = (function(){ var element = document.createElement('input'); element.setAttribute('checked', 'checked'); var clonedElement = element.cloneNode(false); return !clonedElement.checked; })(); function isSVG(ns){ return ns === svgNamespace; } // This is not the namespace of the element, but of // the elements inside that elements. function interiorNamespace(element){ if ( element && element.namespaceURI === svgNamespace && !svgHTMLIntegrationPoints[element.tagName] ) { return svgNamespace; } else { return null; } } // The HTML spec allows for "omitted start tags". These tags are optional // when their intended child is the first thing in the parent tag. For // example, this is a tbody start tag: // // // // // // The tbody may be omitted, and the browser will accept and render: // //
// // // However, the omitted start tag will still be added to the DOM. Here // we test the string and context to see if the browser is about to // perform this cleanup. // // http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html#optional-tags // describes which tags are omittable. The spec for tbody and colgroup // explains this behavior: // // http://www.whatwg.org/specs/web-apps/current-work/multipage/tables.html#the-tbody-element // http://www.whatwg.org/specs/web-apps/current-work/multipage/tables.html#the-colgroup-element // var omittedStartTagChildTest = /<([\w:]+)/; function detectOmittedStartTag(string, contextualElement){ // Omitted start tags are only inside table tags. if (contextualElement.tagName === 'TABLE') { var omittedStartTagChildMatch = omittedStartTagChildTest.exec(string); if (omittedStartTagChildMatch) { var omittedStartTagChild = omittedStartTagChildMatch[1]; // It is already asserted that the contextual element is a table // and not the proper start tag. Just see if a tag was omitted. return omittedStartTagChild === 'tr' || omittedStartTagChild === 'col'; } } } function buildSVGDOM(html, dom){ var div = dom.document.createElement('div'); div.innerHTML = ''+html+''; return div.firstChild.childNodes; } /* * A class wrapping DOM functions to address environment compatibility, * namespaces, contextual elements for morph un-escaped content * insertion. * * When entering a template, a DOMHelper should be passed: * * template(context, { hooks: hooks, dom: new DOMHelper() }); * * TODO: support foreignObject as a passed contextual element. It has * a namespace (svg) that does not match its internal namespace * (xhtml). * * @class DOMHelper * @constructor * @param {HTMLDocument} _document The document DOM methods are proxied to */ function DOMHelper(_document){ this.document = _document || window.document; this.namespace = null; } var prototype = DOMHelper.prototype; prototype.constructor = DOMHelper; prototype.insertBefore = function(element, childElement, referenceChild) { return element.insertBefore(childElement, referenceChild); }; prototype.appendChild = function(element, childElement) { return element.appendChild(childElement); }; prototype.appendText = function(element, text) { return element.appendChild(this.document.createTextNode(text)); }; prototype.setAttribute = function(element, name, value) { element.setAttribute(name, value); }; if (document.createElementNS) { // Only opt into namespace detection if a contextualElement // is passed. prototype.createElement = function(tagName, contextualElement) { var namespace = this.namespace; if (contextualElement) { if (tagName === 'svg') { namespace = svgNamespace; } else { namespace = interiorNamespace(contextualElement); } } if (namespace) { return this.document.createElementNS(namespace, tagName); } else { return this.document.createElement(tagName); } }; } else { prototype.createElement = function(tagName) { return this.document.createElement(tagName); }; } prototype.setNamespace = function(ns) { this.namespace = ns; }; prototype.detectNamespace = function(element) { this.namespace = interiorNamespace(element); }; prototype.createDocumentFragment = function(){ return this.document.createDocumentFragment(); }; prototype.createTextNode = function(text){ return this.document.createTextNode(text); }; prototype.repairClonedNode = function(element, blankChildTextNodes, isChecked){ if (deletesBlankTextNodes && blankChildTextNodes.length > 0) { for (var i=0, len=blankChildTextNodes.length;i]*)>", 'i'))[0]; var endTag = ''; var wrappedHTML = [startTag, html, endTag]; var i = wrappingTags.length; var wrappedDepth = 1 + i; while(i--) { wrappedHTML.unshift('<'+wrappingTags[i]+'>'); wrappedHTML.push(''); } var wrapper = document.createElement('div'); scriptSafeInnerHTML(wrapper, wrappedHTML.join('')); var element = wrapper; while (wrappedDepth--) { element = element.firstChild; while (element && element.nodeType !== 1) { element = element.nextSibling; } } while (element && element.tagName !== tagName) { element = element.nextSibling; } return element ? element.childNodes : []; } var buildDOM; if (needsShy) { buildDOM = function buildDOM(html, contextualElement, dom){ contextualElement = dom.cloneNode(contextualElement, false); scriptSafeInnerHTML(contextualElement, html); return contextualElement.childNodes; }; } else { buildDOM = function buildDOM(html, contextualElement, dom){ contextualElement = dom.cloneNode(contextualElement, false); contextualElement.innerHTML = html; return contextualElement.childNodes; }; } var buildIESafeDOM; if (tagNamesRequiringInnerHTMLFix.length > 0 || movesWhitespace) { buildIESafeDOM = function buildIESafeDOM(html, contextualElement, dom) { // Make a list of the leading text on script nodes. Include // script tags without any whitespace for easier processing later. var spacesBefore = []; var spacesAfter = []; html = html.replace(/(\s*)()(\s*)/g, function(match, tag, spaces) { spacesAfter.push(spaces); return tag; }); // Fetch nodes var nodes; if (tagNamesRequiringInnerHTMLFix[contextualElement.tagName.toLowerCase()]) { // buildDOMWithFix uses string wrappers for problematic innerHTML. nodes = buildDOMWithFix(html, contextualElement); } else { nodes = buildDOM(html, contextualElement, dom); } // Build a list of script tags, the nodes themselves will be // mutated as we add test nodes. var i, j, node, nodeScriptNodes; var scriptNodes = []; for (i=0;node=nodes[i];i++) { if (node.nodeType !== 1) { continue; } if (node.tagName === 'SCRIPT') { scriptNodes.push(node); } else { nodeScriptNodes = node.getElementsByTagName('script'); for (j=0;j 0) { textNode = dom.document.createTextNode(spaceBefore); scriptNode.parentNode.insertBefore(textNode, scriptNode); } spaceAfter = spacesAfter[i]; if (spaceAfter && spaceAfter.length > 0) { textNode = dom.document.createTextNode(spaceAfter); scriptNode.parentNode.insertBefore(textNode, scriptNode.nextSibling); } } return nodes; }; } else { buildIESafeDOM = buildDOM; } var buildHTMLDOM; if (needsIntegrationPointFix) { buildHTMLDOM = function buildHTMLDOM(html, contextualElement, dom){ if (svgHTMLIntegrationPoints[contextualElement.tagName]) { return buildIESafeDOM(html, document.createElement('div'), dom); } else { return buildIESafeDOM(html, contextualElement, dom); } }; } else { buildHTMLDOM = buildIESafeDOM; } __exports__.buildHTMLDOM = buildHTMLDOM; }); define("morph/morph", ["exports"], function(__exports__) { "use strict"; var splice = Array.prototype.splice; function ensureStartEnd(start, end) { if (start === null || end === null) { throw new Error('a fragment parent must have boundary nodes in order to detect insertion'); } } function ensureContext(contextualElement) { if (!contextualElement || contextualElement.nodeType !== 1) { throw new Error('An element node must be provided for a contextualElement, you provided ' + (contextualElement ? 'nodeType ' + contextualElement.nodeType : 'nothing')); } } // TODO: this is an internal API, this should be an assert function Morph(parent, start, end, domHelper, contextualElement) { if (parent.nodeType === 11) { ensureStartEnd(start, end); this.element = null; } else { this.element = parent; } this._parent = parent; this.start = start; this.end = end; this.domHelper = domHelper; ensureContext(contextualElement); this.contextualElement = contextualElement; this.reset(); } Morph.prototype.reset = function() { this.text = null; this.owner = null; this.morphs = null; this.before = null; this.after = null; this.escaped = true; }; Morph.prototype.parent = function () { if (!this.element) { var parent = this.start.parentNode; if (this._parent !== parent) { this.element = this._parent = parent; } } return this._parent; }; Morph.prototype.destroy = function () { if (this.owner) { this.owner.removeMorph(this); } else { clear(this.element || this.parent(), this.start, this.end); } }; Morph.prototype.removeMorph = function (morph) { var morphs = this.morphs; for (var i=0, l=morphs.length; i 0 ? morphs[index-1] : null; var after = index < morphs.length ? morphs[index] : null; var start = before === null ? this.start : (before.end === null ? parent.lastChild : before.end.previousSibling); var end = after === null ? this.end : (after.start === null ? parent.firstChild : after.start.nextSibling); var morph = new Morph(parent, start, end, this.domHelper, this.contextualElement); morph.owner = this; morph._update(parent, node); if (before !== null) { morph.before = before; before.end = start.nextSibling; before.after = morph; } if (after !== null) { morph.after = after; after.before = morph; after.start = end.previousSibling; } this.morphs.splice(index, 0, morph); return morph; }; Morph.prototype.replace = function (index, removedLength, addedNodes) { if (this.morphs === null) this.morphs = []; var parent = this.element || this.parent(); var morphs = this.morphs; var before = index > 0 ? morphs[index-1] : null; var after = index+removedLength < morphs.length ? morphs[index+removedLength] : null; var start = before === null ? this.start : (before.end === null ? parent.lastChild : before.end.previousSibling); var end = after === null ? this.end : (after.start === null ? parent.firstChild : after.start.nextSibling); var addedLength = addedNodes === undefined ? 0 : addedNodes.length; var args, i, current; if (removedLength > 0) { clear(parent, start, end); } if (addedLength === 0) { if (before !== null) { before.after = after; before.end = end; } if (after !== null) { after.before = before; after.start = start; } morphs.splice(index, removedLength); return; } args = new Array(addedLength+2); if (addedLength > 0) { for (i=0; i