From c980fd9b667c83ac704fd283fa03039a1afee70d Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Fri, 31 Oct 2014 09:31:01 -0400 Subject: [PATCH] fix order issue when trusted content is mixed with other siblings, fix js error when child is a random object make home demo more minimalist add explanation about modules in guide --- Gruntfile.js | 1 + docs/getting-started.md | 16 +++++++++++++++- docs/layout/index.html | 5 ++--- mithril.js | 10 +++++++++- tests/e2e/tests.js | 12 ++++++++++++ tests/mithril-tests.js | 5 +++++ tests/test.js | 11 +++++++---- 7 files changed, 51 insertions(+), 9 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index acf9e8b0..f7e3c0ad 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -204,6 +204,7 @@ module.exports = function(grunt) { grunt.loadNpmTasks('grunt-saucelabs'); grunt.registerTask("build", ["test", "uglify", "zip", "md2html", "replace", "copy", "clean"]); + grunt.registerTask("testall", ["test", "teste2e"]); grunt.registerTask("test", ["concat", "execute"]); grunt.registerTask('teste2e', ['connect', 'qunit']); grunt.registerTask("default", ["build"]); diff --git a/docs/getting-started.md b/docs/getting-started.md index e6d2b8c1..4384622f 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -37,6 +37,20 @@ In Mithril, an application typically lives in a namespace and contains modules. For simplicity, our application will have only one module, and we're going to use it as the namespace for our application. +In Mithril, a *module* is an object that contains two functions: `controller` and `view`. + +``` +//an empty Mithril module +var myModule = { + controller: function() {}, + view: function() {} +} +``` + +In addition to holding a controller and a view, a module is typically also used to store data that pertains to it. + +Let's create a module. + ```markup ``` -This object will namespace our two Model classes: +Typically, model entities are reusable and live outside of modules (e.g. `var User = ...`). In our example, since the whole application lives in one module, we're going to use the module as a namespace for our model entities. ```javascript var todo = {}; diff --git a/docs/layout/index.html b/docs/layout/index.html index 92ae83b4..db5ac699 100644 --- a/docs/layout/index.html +++ b/docs/layout/index.html @@ -77,14 +77,13 @@

Sample code

//namespace
-var app = {};
-
 //model
-app.PageList = function() {
+var PageList = function() {
 	return m.request({method: "GET", url: "pages.json"});
 };
 
 //controller
+var app = {};
 app.controller = function() {
 	this.pages = app.PageList();
 	
diff --git a/mithril.js b/mithril.js
index f7afece5..4f5ffa35 100644
--- a/mithril.js
+++ b/mithril.js
@@ -170,7 +170,13 @@ Mithril = m = new function app(window, undefined) {
 				var item = build(parentElement, parentTag, cached, index, data[i], cached[cacheCount], shouldReattach, index + subArrayCount || subArrayCount, editable, namespace, configs)
 				if (item === undefined) continue
 				if (!item.nodes.intact) intact = false
-				subArrayCount += isArr(item) ? item.length : 1
+				if (item.$trusted) {
+					//fix offset of next element if item was a trusted string w/ more than one html element
+					//the first clause in the regexp matches elements
+					//the second clause (after the pipe) matches text nodes
+					subArrayCount += (item.match(/<[^\/]|\>\s*[^<]/g) || []).length
+				}
+				else subArrayCount += isArr(item) ? item.length : 1
 				cached[cacheCount++] = item
 			}
 			if (!intact) {
@@ -194,6 +200,8 @@ Mithril = m = new function app(window, undefined) {
 			}
 		}
 		else if (data != null && dataType == sObj) {
+			if (!data.attrs) data.attrs = {}
+			if (!cached.attrs) cached.attrs = {}
 			//if an element is different enough from the one in cache, recreate it
 			if (data.tag != cached.tag || Object.keys(data.attrs).join() != Object.keys(cached.attrs).join() || data.attrs.id != cached.attrs.id) {
 				clear(cached.nodes)
diff --git a/tests/e2e/tests.js b/tests/e2e/tests.js
index 593dfe02..e0a16250 100644
--- a/tests/e2e/tests.js
+++ b/tests/e2e/tests.js
@@ -396,3 +396,15 @@ test('issue278 regression', function() {
 
 	equal(selected, 2)
 })
+test("mixing trusted content", function() {
+	m.render(dummyEl, [m.trust("

1

2

"), m("i", "foo")]) + equal(dummyEl.childNodes[2].nodeName, "I") +}) +test("mixing trusted content w/ text nodes", function() { + m.render(dummyEl, [m.trust("

1

123

2

"), m("i", "foo")]) + equal(dummyEl.childNodes[3].nodeName, "I") +}) +test("mixing trusted content w/ td", function() { + m.render(dummyEl, [m.trust("12"), m("i", "foo")]) + equal(dummyEl.childNodes[1].nodeName, "I") +}) diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index e1055730..494d1e15 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -775,6 +775,11 @@ function testMithril(mock) { m.render(root, new Field()) return root.childNodes.length == 1 }) + test(function() { + var root = mock.document.createElement("div") + m.render(root, {foo: 123}) + return root.childNodes.length == 0 + }) //end m.render //m.redraw diff --git a/tests/test.js b/tests/test.js index 1a4b68da..dc35ee54 100644 --- a/tests/test.js +++ b/tests/test.js @@ -1,3 +1,6 @@ +//make "use strict" and nodejs happy +var window = this + //test reporting for saucelabs if (typeof window != "undefined") { window.global_test_results = { @@ -19,7 +22,7 @@ function test(condition) { var result = true test.total++ - if (this.performance != null && performance.now) { + if (typeof performance != "undefined" && performance.now) { start = performance.now() } try { @@ -30,11 +33,11 @@ function test(condition) { console.error(e) test.failures.push(condition) } - if (this.performance != null && performance.now) { + if (typeof performance != "undefined" && performance.now) { duration = performance.now() - start } - test_obj = { + window.test_obj = { name: "" + test.total, result: result, duration: duration @@ -42,7 +45,7 @@ function test(condition) { if (typeof window != "undefined") { if (!result) { - window.global_test_results.tests.push(test_obj) + window.global_test_results.tests.push(window.test_obj) } window.global_test_results.duration += duration