Skip to content

Commit

Permalink
fix unit tests and lint erros + new build
Browse files Browse the repository at this point in the history
  • Loading branch information
Havunen committed Apr 7, 2016
1 parent ede702f commit 801dba3
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 41 deletions.
122 changes: 91 additions & 31 deletions packages/inferno-dom/dist/inferno-dom.es2015.js
Expand Up @@ -65,25 +65,6 @@ Lifecycle.prototype = {
}
};

function diffChildrenWithTemplate(lastNode, nextNode, lastChildrenType, nextChildrenType, dom, namespace, lifecycle, context, instance, staticCheck) {
var nextChildren = nextNode.children;
var lastChildren = lastNode.children;

if (lastChildrenType === 3) {
if (nextChildrenType === 3) {
patchKeyedChildren(lastChildren, nextChildren, dom, namespace, lifecycle, context, instance);
}
} else if (lastChildrenType === 2) {
if (nextChildrenType === 2) {
patchNode(lastChildren, nextChildren, dom, namespace, lifecycle, context, instance, staticCheck);
}
} else if (lastChildrenType === 1) {
if (nextChildrenType === 1) {
updateTextNode(dom, lastChildren, nextChildren);
}
}
}

function diffChildren(lastNode, nextNode, dom, namespace, lifecycle, context, instance, staticCheck) {
var nextChildren = nextNode.children;
var lastChildren = lastNode.children;
Expand Down Expand Up @@ -216,8 +197,11 @@ function diffAttributes(lastNode, nextNode, dom, instance) {
function diffNodesWithTemplate(lastNode, nextNode, lastTpl, nextTpl, parentDom, namespace, lifecycle, context, instance, deepCheck) {
var nextHooks = void 0;

if (nextNode.hasHooks === true && (nextHooks = nextNode.hooks && !isNullOrUndefined(nextHooks.willUpdate))) {
nextHooks.willUpdate(lastNode.dom);
if (nextNode.hasHooks === true) {
/* eslint no-cond-assign:0 */
if (nextHooks = nextNode.hooks && !isNullOrUndefined(nextHooks.willUpdate)) {
nextHooks.willUpdate(lastNode.dom);
}
}
var nextTag = nextNode.tag || deepCheck && lastTpl.tag;
var lastTag = lastNode.tag || deepCheck && nextTpl.tag;
Expand All @@ -243,14 +227,31 @@ function diffNodesWithTemplate(lastNode, nextNode, lastTpl, nextTpl, parentDom,
if (nextTpl.isComponent === true) {
nextNode.instance = lastNode.instance;
nextNode.dom = lastNode.dom;
patchComponent(nextNode, nextNode.tag, nextNode.instance, lastNode.attrs || {}, nextNode.attrs || {}, nextNode.hooks, nextNode.children, parentDom, lifecycle, context);
patchComponentWithTemplate(nextNode, nextNode.tag, lastTpl, nextTpl, nextNode.instance, lastNode.attrs || {}, nextNode.attrs || {}, nextNode.hooks, nextNode.children, parentDom, lifecycle, context);
}
} else {
var dom = lastNode.dom;
var lastChildrenType = lastTpl.childrenType;
nextNode.dom = dom;

if (lastTpl.childrenType > 0) {
diffChildrenWithTemplate(lastNode, nextNode, lastTpl.childrenType, nextTpl.childrenType, dom, namespace, lifecycle, context, instance, deepCheck);
if (lastChildrenType > 0) {
var nextChildrenType = nextTpl.childrenType;

if (lastChildrenType === 4) {
if (nextChildrenType === 4) {
patchKeyedChildren(lastNode.children, nextNode.children, dom, namespace, lifecycle, context, instance);
}
} else if (lastChildrenType === 2) {
if (nextChildrenType === 2) {
patchNode(lastNode.children, nextNode.children, dom, namespace, lifecycle, context, instance, deepCheck);
}
} else if (lastChildrenType === 1) {
if (nextChildrenType === 1) {
updateTextNode(dom, lastNode.children, nextNode.children);
}
} else {
diffChildren(lastNode, nextNode, dom, namespace, lifecycle, context, instance, deepCheck);
}
}
if (lastTpl.hasAttrs === true) {
diffAttributes(lastNode, nextNode, dom, instance);
Expand Down Expand Up @@ -494,6 +495,55 @@ function patchAttribute(attrName, nextAttrValue, dom) {
}
}

function patchComponentWithTemplate(lastNode, Component, lastTpl, nextTpl, instance, lastProps, nextProps, nextHooks, nextChildren, parentDom, lifecycle, context) {
nextProps = addChildrenToProps(nextChildren, nextProps);

if (isStatefulComponent(Component)) {
var prevProps = instance.props;
var prevState = instance.state;
var nextState = instance.state;

var childContext = instance.getChildContext();
if (!isNullOrUndefined(childContext)) {
context = babelHelpers.extends({}, context, childContext);
}
instance.context = context;
var nextNode = instance._updateComponent(prevState, nextState, prevProps, nextProps);

if (!isNullOrUndefined(nextNode)) {
diffNodes(lastNode, nextNode, parentDom, null, lifecycle, context, instance, true);
lastNode.dom = nextNode.dom;
instance._lastNode = nextNode;
}
} else {
var shouldUpdate = true;

if (nextTpl.hasHooks === true) {
if (!isNullOrUndefined(nextHooks.componentShouldUpdate)) {
shouldUpdate = nextHooks.componentShouldUpdate(lastNode.dom, lastProps, nextProps);
}
}
if (shouldUpdate !== false) {
if (nextTpl.hasHooks === true) {
if (!isNullOrUndefined(nextHooks.componentWillUpdate)) {
nextHooks.componentWillUpdate(lastNode.dom, lastProps, nextProps);
}
}
var _nextNode = Component(nextProps);
var dom = lastNode.dom;
_nextNode.dom = dom;

diffNodes(instance, _nextNode, dom, null, lifecycle, context, null, true);
lastNode.instance = _nextNode;
if (nextTpl.hasHooks === true) {
if (!isNullOrUndefined(nextHooks.componentDidUpdate)) {
nextHooks.componentDidUpdate(lastNode.dom, lastProps, nextProps);
}
}
}
}
}

function patchComponent(lastNode, Component, instance, lastProps, nextProps, nextHooks, nextChildren, parentDom, lifecycle, context) {
nextProps = addChildrenToProps(nextChildren, nextProps);

Expand Down Expand Up @@ -525,12 +575,12 @@ function patchComponent(lastNode, Component, instance, lastProps, nextProps, nex
if (nextHooksDefined && !isNullOrUndefined(nextHooks.componentWillUpdate)) {
nextHooks.componentWillUpdate(lastNode.dom, lastProps, nextProps);
}
var _nextNode = Component(nextProps);
var _nextNode2 = Component(nextProps);
var dom = lastNode.dom;
_nextNode.dom = dom;
_nextNode2.dom = dom;

diffNodes(instance, _nextNode, dom, null, lifecycle, context, null, true);
lastNode.instance = _nextNode;
diffNodes(instance, _nextNode2, dom, null, lifecycle, context, null, true);
lastNode.instance = _nextNode2;
if (nextHooksDefined && !isNullOrUndefined(nextHooks.componentDidUpdate)) {
nextHooks.componentDidUpdate(lastNode.dom, lastProps, nextProps);
}
Expand Down Expand Up @@ -1086,7 +1136,7 @@ function createVirtualFragment() {
}

function isKeyed(lastChildren, nextChildren) {
return nextChildren.length && !isNullOrUndefined(nextChildren[0]) && !isNullOrUndefined(nextChildren[0].key) || lastChildren.length && !isNullOrUndefined(lastChildren[0]) && !isNullOrUndefined(lastChildren[0].key);
return nextChildren.length && !isNullOrUndefined(nextChildren[0]) && nextChildren[0].key !== undefined || lastChildren.length && !isNullOrUndefined(lastChildren[0]) && lastChildren[0].key !== undefined;
}

function selectOptionValueIfNeeded(vdom, values) {
Expand Down Expand Up @@ -1389,7 +1439,7 @@ function appendNodeWithTemplate(node, tpl, parentDom, namespace, lifecycle, cont
if (tpl.isComponent === true) {
return mountComponent(node, tag, node.attrs || {}, node.hooks, node.children, parentDom, lifecycle, context);
}
var dom = tpl.dom.cloneNode(true);
var dom = tpl.dom.cloneNode(false);

node.dom = dom;
if (tpl.hasHooks === true) {
Expand All @@ -1400,7 +1450,8 @@ function appendNodeWithTemplate(node, tpl, parentDom, namespace, lifecycle, cont
// 1: text node
// 2: single child
// 3: multiple children
// 4: variable children (defaults to no optimisation)
// 4: multiple children (keyed)
// 5: variable children (defaults to no optimisation)

switch (tpl.childrenType) {
case 1:
Expand All @@ -1413,6 +1464,9 @@ function appendNodeWithTemplate(node, tpl, parentDom, namespace, lifecycle, cont
mountArrayChildren(node, node.children, dom, namespace, lifecycle, context, instance);
break;
case 4:
mountArrayChildrenWithKeys(node.children, dom, namespace, lifecycle, context, instance);
break;
case 5:
mountChildren(node, node.children, dom, namespace, lifecycle, context, instance);
break;
default:
Expand Down Expand Up @@ -1497,6 +1551,12 @@ function appendPromise(child, parentDom, domChildren, namespace, lifecycle, cont
parentDom.appendChild(placeholder);
}

function mountArrayChildrenWithKeys(children, parentDom, namespace, lifecycle, context, instance) {
for (var i = 0; i < children.length; i++) {
mountNode(children[i], parentDom, namespace, lifecycle, context, instance);
}
}

function mountArrayChildren(node, children, parentDom, namespace, lifecycle, context, instance) {
var domChildren = null;
var isNonKeyed = false;
Expand Down
5 changes: 3 additions & 2 deletions packages/inferno/dist/inferno-dom.js
Expand Up @@ -204,6 +204,7 @@
var nextHooks = void 0;

if (nextNode.hasHooks === true) {
/* eslint no-cond-assign:0 */
if (nextHooks = nextNode.hooks && !isNullOrUndefined(nextHooks.willUpdate)) {
nextHooks.willUpdate(lastNode.dom);
}
Expand Down Expand Up @@ -248,14 +249,14 @@
}
} else if (lastChildrenType === 2) {
if (nextChildrenType === 2) {
patchNode(lastNode.children, nextNode.children, dom, namespace, lifecycle, context, instance, staticCheck);
patchNode(lastNode.children, nextNode.children, dom, namespace, lifecycle, context, instance, deepCheck);
}
} else if (lastChildrenType === 1) {
if (nextChildrenType === 1) {
updateTextNode(dom, lastNode.children, nextNode.children);
}
} else {
diffChildren(lastNode, nextNode, dom, namespace, lifecycle, context, instance, staticCheck);
diffChildren(lastNode, nextNode, dom, namespace, lifecycle, context, instance, deepCheck);
}
}
if (lastTpl.hasAttrs === true) {
Expand Down
2 changes: 1 addition & 1 deletion packages/inferno/dist/inferno-dom.min.js

Large diffs are not rendered by default.

99 changes: 97 additions & 2 deletions packages/inferno/dist/inferno.es2015.js
Expand Up @@ -313,15 +313,15 @@ function createChild(_ref) {
if (tag === undefined && !isNullOrUndefined(attrs) && !attrs.tpl && !isNullOrUndefined(children) && children.length === 0) {
return null;
}
var key = !isNullOrUndefined(attrs) && !isNullOrUndefined(attrs.key) ? attrs.key : null;
var key = !isNullOrUndefined(attrs) && !isNullOrUndefined(attrs.key) ? attrs.key : undefined;

if (!isNullOrUndefined(children) && children.length === 0) {
children = null;
} else if (!isInvalidNode(children)) {
children = isArray(children) && children.length === 1 ? createChildren(children[0]) : createChildren(children);
}

if (key !== null) {
if (key !== undefined) {
delete attrs.key;
}
var attrsAndEvents = createAttrsAndEvents(attrs, tag);
Expand Down Expand Up @@ -409,8 +409,103 @@ function createStaticAttributes(attrs, dom) {
}
}

function InfernoVNode(tpl) {
this.tpl = tpl;
this.dom = null;
this.instance = null;
this.tag = null;
this.children = null;
this.style = null;
this.className = null;
this.attrs = null;
this.events = null;
this.hooks = null;
this.key = null;
}

function createTemplate(shape, childrenType) {
var tag = shape.tag || null;
var tagIsDynamic = tag && tag.arg !== undefined ? true : false;

var children = !isNullOrUndefined(shape.children) ? shape.children : null;
var childrenIsDynamic = children && children.arg !== undefined ? true : false;

var attrs = shape.attrs || null;
var attrsIsDynamic = attrs && attrs.arg !== undefined ? true : false;

var hooks = shape.hooks || null;
var hooksIsDynamic = hooks && hooks.arg !== undefined ? true : false;

var events = shape.events || null;
var eventsIsDynamic = events && events.arg !== undefined ? true : false;

var key = shape.key !== undefined ? shape.key : null;
var keyIsDynamic = !isNullOrUndefined(key) && !isNullOrUndefined(key.arg);

var style = shape.style || null;
var styleIsDynamic = style && style.arg !== undefined ? true : false;

var className = shape.className !== undefined ? shape.className : null;
var classNameIsDynamic = className && className.arg !== undefined ? true : false;

var dom = null;

if (typeof tag === 'string') {
var newAttrs = Object.assign({}, className ? { className: className } : {}, shape.attrs || {});
dom = createUniversalElement(tag, newAttrs);
}

var tpl = {
dom: dom,
pools: {
keyed: {},
nonKeyed: []
},
tag: !tagIsDynamic ? tag : null,
isComponent: tagIsDynamic,
hasAttrs: attrsIsDynamic,
hasHooks: hooksIsDynamic,
hasEvents: eventsIsDynamic,
hasStyle: styleIsDynamic,
hasClassName: classNameIsDynamic,
childrenType: childrenType === undefined ? children ? 5 : 0 : childrenType
};

return function () {
var vNode = new InfernoVNode(tpl);

if (tagIsDynamic === true) {
vNode.tag = arguments[tag.arg];
}
if (childrenIsDynamic === true) {
vNode.children = arguments[children.arg];
}
if (attrsIsDynamic === true) {
vNode.attrs = arguments[attrs.arg];
}
if (hooksIsDynamic === true) {
vNode.hooks = arguments[hooks.arg];
}
if (eventsIsDynamic === true) {
vNode.events = arguments[events.arg];
}
if (keyIsDynamic === true) {
vNode.key = arguments[key.arg];
}
if (styleIsDynamic === true) {
vNode.style = arguments[style.arg];
}
if (classNameIsDynamic === true) {
vNode.className = arguments[className.arg];
}

return vNode;
};
}

var index = {
createElement: createElement,
createTemplate: createTemplate,
universal: {
createElement: createUniversalElement
}
Expand Down
4 changes: 2 additions & 2 deletions packages/inferno/dist/inferno.js
Expand Up @@ -319,15 +319,15 @@
if (tag === undefined && !isNullOrUndefined(attrs) && !attrs.tpl && !isNullOrUndefined(children) && children.length === 0) {
return null;
}
var key = !isNullOrUndefined(attrs) && !isNullOrUndefined(attrs.key) ? attrs.key : null;
var key = !isNullOrUndefined(attrs) && !isNullOrUndefined(attrs.key) ? attrs.key : undefined;

if (!isNullOrUndefined(children) && children.length === 0) {
children = null;
} else if (!isInvalidNode(children)) {
children = isArray(children) && children.length === 1 ? createChildren(children[0]) : createChildren(children);
}

if (key !== null) {
if (key !== undefined) {
delete attrs.key;
}
var attrsAndEvents = createAttrsAndEvents(attrs, tag);
Expand Down

0 comments on commit 801dba3

Please sign in to comment.