From eb3951ce12730fed0ac3c684dc0116aef3b559a1 Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Sat, 6 Dec 2014 14:29:59 -0500 Subject: [PATCH] #379 fix equality checks --- mithril.js | 124 ++++++++++++++++++++++----------------------- tests/e2e/tests.js | 6 +++ 2 files changed, 68 insertions(+), 62 deletions(-) diff --git a/mithril.js b/mithril.js index 9e8505a7..b5b29143 100644 --- a/mithril.js +++ b/mithril.js @@ -33,17 +33,17 @@ var m = (function app(window, undefined) { */ function m() { var args = [].slice.call(arguments); - var hasAttrs = args[1] != null && type.call(args[1]) == OBJECT && !("tag" in args[1]) && !("subtree" in args[1]); + var hasAttrs = args[1] != null && type.call(args[1]) === OBJECT && !("tag" in args[1]) && !("subtree" in args[1]); var attrs = hasAttrs ? args[1] : {}; var classAttrName = "class" in attrs ? "class" : "className"; var cell = {tag: "div", attrs: {}}; var match, classes = []; if (type.call(args[0]) != STRING) throw new Error("selector in m(selector, attrs, children) should be a string") while (match = parser.exec(args[0])) { - if (match[1] == "" && match[2]) cell.tag = match[2]; - else if (match[1] == "#") cell.attrs.id = match[2]; - else if (match[1] == ".") classes.push(match[2]); - else if (match[3][0] == "[") { + if (match[1] === "" && match[2]) cell.tag = match[2]; + else if (match[1] === "#") cell.attrs.id = match[2]; + else if (match[1] === ".") classes.push(match[2]); + else if (match[3][0] === "[") { var pair = attrParser.exec(match[3]); cell.attrs[pair[1]] = pair[3] || (pair[2] ? "" :true) } @@ -52,7 +52,7 @@ var m = (function app(window, undefined) { var children = hasAttrs ? args[2] : args[1]; - if (type.call(children) == ARRAY) { + if (type.call(children) === ARRAY) { cell.children = children } else { @@ -60,7 +60,7 @@ var m = (function app(window, undefined) { } for (var attrName in attrs) { - if (attrName == classAttrName) cell.attrs[attrName] = (cell.attrs[attrName] || "") + " " + attrs[attrName]; + if (attrName === classAttrName) cell.attrs[attrName] = (cell.attrs[attrName] || "") + " " + attrs[attrName]; else cell.attrs[attrName] = attrs[attrName] } return cell @@ -94,11 +94,11 @@ var m = (function app(window, undefined) { if (data == null) data = ""; if (data.subtree === "retain") return cached; var cachedType = type.call(cached), dataType = type.call(data); - if (cached == null || cachedType != dataType) { + if (cached == null || cachedType !== dataType) { if (cached != null) { if (parentCache && parentCache.nodes) { var offset = index - parentIndex; - var end = offset + (dataType == ARRAY ? data : cached.nodes).length; + var end = offset + (dataType === ARRAY ? data : cached.nodes).length; clear(parentCache.nodes.slice(offset, end), parentCache.slice(offset, end)) } else if (cached.nodes) clear(cached.nodes, cached) @@ -108,10 +108,10 @@ var m = (function app(window, undefined) { cached.nodes = [] } - if (dataType == ARRAY) { + if (dataType === ARRAY) { //recursively flatten array for (var i = 0; i < data.length; i++) { - if (type.call(data[i]) == ARRAY) { + if (type.call(data[i]) === ARRAY) { data = data.concat.apply([], data); i-- //check current index again and flatten until there are no more nested arrays at that index } @@ -154,18 +154,18 @@ var m = (function app(window, undefined) { var newCached = cached.slice(); for (var i = 0, change; change = changes[i]; i++) { - if (change.action == DELETION) { + if (change.action === DELETION) { clear(cached[change.index].nodes, cached[change.index]); newCached.splice(change.index, 1) } - if (change.action == INSERTION) { + if (change.action === INSERTION) { var dummy = $document.createElement("div"); dummy.key = data[change.index].attrs.key; parentElement.insertBefore(dummy, parentElement.childNodes[change.index] || null); newCached.splice(change.index, 0, {attrs: {key: data[change.index].attrs.key}, nodes: [dummy]}) } - if (change.action == MOVE) { + if (change.action === MOVE) { if (parentElement.childNodes[change.index] !== change.element && change.element !== null) { parentElement.insertBefore(change.element, parentElement.childNodes[change.index] || null) } @@ -194,7 +194,7 @@ var m = (function app(window, undefined) { //the second clause (after the pipe) matches text nodes subArrayCount += (item.match(/<[^\/]|\>\s*[^<]/g) || []).length } - else subArrayCount += type.call(item) == ARRAY ? item.length : 1; + else subArrayCount += type.call(item) === ARRAY ? item.length : 1; cached[cacheCount++] = item } if (!intact) { @@ -213,7 +213,7 @@ var m = (function app(window, undefined) { cached.nodes = nodes } } - else if (data != null && dataType == OBJECT) { + else if (data != null && dataType === OBJECT) { if (!data.attrs) data.attrs = {}; if (!cached.attrs) cached.attrs = {}; @@ -222,7 +222,7 @@ var m = (function app(window, undefined) { //if an element is different enough from the one in cache, recreate it if (data.tag != cached.tag || dataAttrKeys.join() != Object.keys(cached.attrs).join() || data.attrs.id != cached.attrs.id) { if (cached.nodes.length) clear(cached.nodes); - if (cached.configContext && typeof cached.configContext.onunload == FUNCTION) cached.configContext.onunload() + if (cached.configContext && typeof cached.configContext.onunload === FUNCTION) cached.configContext.onunload() } if (type.call(data.tag) != STRING) return; @@ -244,7 +244,7 @@ var m = (function app(window, undefined) { }; if (cached.children && !cached.children.nodes) cached.children.nodes = []; //edge case: setting value on