(function (root, factory) { if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. define(['exports'], factory); } else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') { // CommonJS factory(exports); } else { // Browser globals factory(root.maquette = {}); } }(this, function (exports) { 'use strict'; ; ; ; ; var NAMESPACE_W3 = 'http://www.w3.org/'; var NAMESPACE_SVG = NAMESPACE_W3 + '2000/svg'; var NAMESPACE_XLINK = NAMESPACE_W3 + '1999/xlink'; // Utilities var emptyArray = []; var extend = function (base, overrides) { var result = {}; Object.keys(base).forEach(function (key) { result[key] = base[key]; }); if (overrides) { Object.keys(overrides).forEach(function (key) { result[key] = overrides[key]; }); } return result; }; // Hyperscript helper functions var same = function (vnode1, vnode2) { if (vnode1.vnodeSelector !== vnode2.vnodeSelector) { return false; } if (vnode1.properties && vnode2.properties) { if (vnode1.properties.key !== vnode2.properties.key) { return false; } return vnode1.properties.bind === vnode2.properties.bind; } return !vnode1.properties && !vnode2.properties; }; var toTextVNode = function (data) { return { vnodeSelector: '', properties: undefined, children: undefined, text: data.toString(), domNode: null }; }; var appendChildren = function (parentSelector, insertions, main) { for (var i = 0; i < insertions.length; i++) { var item = insertions[i]; if (Array.isArray(item)) { appendChildren(parentSelector, item, main); } else { if (item !== null && item !== undefined) { if (!item.hasOwnProperty('vnodeSelector')) { item = toTextVNode(item); } main.push(item); } } } }; // Render helper functions var missingTransition = function () { throw new Error('Provide a transitions object to the projectionOptions to do animations'); }; var DEFAULT_PROJECTION_OPTIONS = { namespace: undefined, eventHandlerInterceptor: undefined, styleApplyer: function (domNode, styleName, value) { // Provides a hook to add vendor prefixes for browsers that still need it. domNode.style[styleName] = value; }, transitions: { enter: missingTransition, exit: missingTransition } }; var applyDefaultProjectionOptions = function (projectorOptions) { return extend(DEFAULT_PROJECTION_OPTIONS, projectorOptions); }; var checkStyleValue = function (styleValue) { if (typeof styleValue !== 'string') { throw new Error('Style values must be strings'); } }; var setProperties = function (domNode, properties, projectionOptions) { if (!properties) { return; } var eventHandlerInterceptor = projectionOptions.eventHandlerInterceptor; var propNames = Object.keys(properties); var propCount = propNames.length; for (var i = 0; i < propCount; i++) { var propName = propNames[i]; /* tslint:disable:no-var-keyword: edge case */ var propValue = properties[propName]; /* tslint:enable:no-var-keyword */ if (propName === 'className') { throw new Error('Property "className" is not supported, use "class".'); } else if (propName === 'class') { if (domNode.className) { // May happen if classes is specified before class domNode.className += ' ' + propValue; } else { domNode.className = propValue; } } else if (propName === 'classes') { // object with string keys and boolean values var classNames = Object.keys(propValue); var classNameCount = classNames.length; for (var j = 0; j < classNameCount; j++) { var className = classNames[j]; if (propValue[className]) { domNode.classList.add(className); } } } else if (propName === 'styles') { // object with string keys and string (!) values var styleNames = Object.keys(propValue); var styleCount = styleNames.length; for (var j = 0; j < styleCount; j++) { var styleName = styleNames[j]; var styleValue = propValue[styleName]; if (styleValue) { checkStyleValue(styleValue); projectionOptions.styleApplyer(domNode, styleName, styleValue); } } } else if (propName === 'key') { continue; } else if (propValue === null || propValue === undefined) { continue; } else { var type = typeof propValue; if (type === 'function') { if (propName.lastIndexOf('on', 0) === 0) { if (eventHandlerInterceptor) { propValue = eventHandlerInterceptor(propName, propValue, domNode, properties); // intercept eventhandlers } if (propName === 'oninput') { (function () { // record the evt.target.value, because IE and Edge sometimes do a requestAnimationFrame between changing value and running oninput var oldPropValue = propValue; propValue = function (evt) { evt.target['oninput-value'] = evt.target.value; // may be HTMLTextAreaElement as well oldPropValue.apply(this, [evt]); }; }()); } domNode[propName] = propValue; } } else if (type === 'string' && propName !== 'value' && propName !== 'innerHTML') { if (projectionOptions.namespace === NAMESPACE_SVG && propName === 'href') { domNode.setAttributeNS(NAMESPACE_XLINK, propName, propValue); } else { domNode.setAttribute(propName, propValue); } } else { domNode[propName] = propValue; } } } }; var updateProperties = function (domNode, previousProperties, properties, projectionOptions) { if (!properties) { return; } var propertiesUpdated = false; var propNames = Object.keys(properties); var propCount = propNames.length; for (var i = 0; i < propCount; i++) { var propName = propNames[i]; // assuming that properties will be nullified instead of missing is by design var propValue = properties[propName]; var previousValue = previousProperties[propName]; if (propName === 'class') { if (previousValue !== propValue) { throw new Error('"class" property may not be updated. Use the "classes" property for conditional css classes.'); } } else if (propName === 'classes') { var classList = domNode.classList; var classNames = Object.keys(propValue); var classNameCount = classNames.length; for (var j = 0; j < classNameCount; j++) { var className = classNames[j]; var on = !!propValue[className]; var previousOn = !!previousValue[className]; if (on === previousOn) { continue; } propertiesUpdated = true; if (on) { classList.add(className); } else { classList.remove(className); } } } else if (propName === 'styles') { var styleNames = Object.keys(propValue); var styleCount = styleNames.length; for (var j = 0; j < styleCount; j++) { var styleName = styleNames[j]; var newStyleValue = propValue[styleName]; var oldStyleValue = previousValue[styleName]; if (newStyleValue === oldStyleValue) { continue; } propertiesUpdated = true; if (newStyleValue) { checkStyleValue(newStyleValue); projectionOptions.styleApplyer(domNode, styleName, newStyleValue); } else { projectionOptions.styleApplyer(domNode, styleName, ''); } } } else { if (!propValue && typeof previousValue === 'string') { propValue = ''; } if (propName === 'value') { if (domNode[propName] !== propValue && domNode['oninput-value'] !== propValue) { domNode[propName] = propValue; // Reset the value, even if the virtual DOM did not change domNode['oninput-value'] = undefined; } // else do not update the domNode, otherwise the cursor position would be changed if (propValue !== previousValue) { propertiesUpdated = true; } } else if (propValue !== previousValue) { var type = typeof propValue; if (type === 'function') { throw new Error('Functions may not be updated on subsequent renders (property: ' + propName + '). Hint: declare event handler functions outside the render() function.'); } if (type === 'string' && propName !== 'innerHTML') { if (projectionOptions.namespace === NAMESPACE_SVG && propName === 'href') { domNode.setAttributeNS(NAMESPACE_XLINK, propName, propValue); } else { domNode.setAttribute(propName, propValue); } } else { if (domNode[propName] !== propValue) { domNode[propName] = propValue; } } propertiesUpdated = true; } } } return propertiesUpdated; }; var findIndexOfChild = function (children, sameAs, start) { if (sameAs.vnodeSelector !== '') { // Never scan for text-nodes for (var i = start; i < children.length; i++) { if (same(children[i], sameAs)) { return i; } } } return -1; }; var nodeAdded = function (vNode, transitions) { if (vNode.properties) { var enterAnimation = vNode.properties.enterAnimation; if (enterAnimation) { if (typeof enterAnimation === 'function') { enterAnimation(vNode.domNode, vNode.properties); } else { transitions.enter(vNode.domNode, vNode.properties, enterAnimation); } } } }; var nodeToRemove = function (vNode, transitions) { var domNode = vNode.domNode; if (vNode.properties) { var exitAnimation = vNode.properties.exitAnimation; if (exitAnimation) { domNode.style.pointerEvents = 'none'; var removeDomNode = function () { if (domNode.parentNode) { domNode.parentNode.removeChild(domNode); } }; if (typeof exitAnimation === 'function') { exitAnimation(domNode, removeDomNode, vNode.properties); return; } else { transitions.exit(vNode.domNode, vNode.properties, exitAnimation, removeDomNode); return; } } } if (domNode.parentNode) { domNode.parentNode.removeChild(domNode); } }; var checkDistinguishable = function (childNodes, indexToCheck, parentVNode, operation) { var childNode = childNodes[indexToCheck]; if (childNode.vnodeSelector === '') { return; // Text nodes need not be distinguishable } var properties = childNode.properties; var key = properties ? properties.key === undefined ? properties.bind : properties.key : undefined; if (!key) { for (var i = 0; i < childNodes.length; i++) { if (i !== indexToCheck) { var node = childNodes[i]; if (same(node, childNode)) { if (operation === 'added') { throw new Error(parentVNode.vnodeSelector + ' had a ' + childNode.vnodeSelector + ' child ' + 'added, but there is now more than one. You must add unique key properties to make them distinguishable.'); } else { throw new Error(parentVNode.vnodeSelector + ' had a ' + childNode.vnodeSelector + ' child ' + 'removed, but there were more than one. You must add unique key properties to make them distinguishable.'); } } } } } }; var createDom; var updateDom; var updateChildren = function (vnode, domNode, oldChildren, newChildren, projectionOptions) { if (oldChildren === newChildren) { return false; } oldChildren = oldChildren || emptyArray; newChildren = newChildren || emptyArray; var oldChildrenLength = oldChildren.length; var newChildrenLength = newChildren.length; var transitions = projectionOptions.transitions; var oldIndex = 0; var newIndex = 0; var i; var textUpdated = false; while (newIndex < newChildrenLength) { var oldChild = oldIndex < oldChildrenLength ? oldChildren[oldIndex] : undefined; var newChild = newChildren[newIndex]; if (oldChild !== undefined && same(oldChild, newChild)) { textUpdated = updateDom(oldChild, newChild, projectionOptions) || textUpdated; oldIndex++; } else { var findOldIndex = findIndexOfChild(oldChildren, newChild, oldIndex + 1); if (findOldIndex >= 0) { // Remove preceding missing children for (i = oldIndex; i < findOldIndex; i++) { nodeToRemove(oldChildren[i], transitions); checkDistinguishable(oldChildren, i, vnode, 'removed'); } textUpdated = updateDom(oldChildren[findOldIndex], newChild, projectionOptions) || textUpdated; oldIndex = findOldIndex + 1; } else { // New child createDom(newChild, domNode, oldIndex < oldChildrenLength ? oldChildren[oldIndex].domNode : undefined, projectionOptions); nodeAdded(newChild, transitions); checkDistinguishable(newChildren, newIndex, vnode, 'added'); } } newIndex++; } if (oldChildrenLength > oldIndex) { // Remove child fragments for (i = oldIndex; i < oldChildrenLength; i++) { nodeToRemove(oldChildren[i], transitions); checkDistinguishable(oldChildren, i, vnode, 'removed'); } } return textUpdated; }; var addChildren = function (domNode, children, projectionOptions) { if (!children) { return; } for (var i = 0; i < children.length; i++) { createDom(children[i], domNode, undefined, projectionOptions); } }; var initPropertiesAndChildren = function (domNode, vnode, projectionOptions) { addChildren(domNode, vnode.children, projectionOptions); // children before properties, needed for value property of