Permalink
Browse files

func ref now invoked with args, can return all sorts of things

  • Loading branch information...
Stuart Malin
Stuart Malin committed Aug 2, 2011
1 parent d16248a commit 96c8be81db7c8508ed1075a45119f9e02c566ba7
Showing with 119 additions and 32 deletions.
  1. +36 −13 devel/debug.js
  2. +17 −6 src/doml.js
  3. +66 −13 test/the-tests.js
View
@@ -1,5 +1,6 @@
var body, Doml, doml, elem, global, report, run, sink, startSink, theDocument,
Doml = require('../src/doml.js'),
+ eyes = require('eyes'),
jsdom = require('jsdom'),
sink = require('sink-test');
@@ -15,23 +16,45 @@ window = jsdom.createWindow();
body = document.getElementsByTagName('BODY')[0];
doml = new Doml(document);
-var elem1 = doml.create('span', 'a span');
-
-elem = doml.create('p', "a para", elem1);
-
-console.log('elem children: ' + elem.children.length)
-
+elem = doml.create('div', function (args) {
+ var elems = [];
+ elems.push(doml.create('p', args.text1));
+ elems.push(doml.create('span', args.text2));
+ return elems;
+}, {text1: 'text1', text2: 'text2'});
+
+console.log('elem.children.length: ' + elem.children.length)
+console.log('elem.childNodes.length: ' + elem.childNodes.length)
+elem = elem.firstChild;
+console.log('child: elem.nodeType: ' + elem.nodeType);
+elem = elem.nextSibling;
+console.log('child: elem.nodeType: ' + elem.nodeType);
+
+console.log('END');
sink('Doml', function(test, ok, before, after) {
- test('node cloning', 2, function() {
- var newElem;
- elem = document.getElementById('the-span');
- newElem = doml.create(elem);
- ok(newElem !== elem, 'cloned element is not original element');
- ok(newElem.nodeName === 'SPAN', 'cloned element has proper nodeName');
- });
+ test('element creation: function invocation returns an element', 11, function() {
+ elem = doml.create('div', 'hello', function (args) {
+ return doml.create('p', args.text, {className: args.className, "id": args.id});
+ }, {className: "myClass", "id": "myID", text: 'hello'}, 'final');
+ ok(elem.nodeName === 'DIV', 'element has proper tag');
+ ok(elem.children.length === 2, 'element has proper number of (element) children');
+ ok(elem.childNodes.length === 3, 'element has proper number of (elements & text) children');
+ elem = elem.firstChild;
+ ok(elem.nodeType === 3, '1st child is proper type');
+ ok(elem.nodeValue === 'hello', '1st child has proper text');
+ elem = elem.nextSibling;
+ ok(elem.nodeName === 'P', 'function generated child element has proper tag');
+ ok(elem.innerHTML === 'hello', 'function generated child has proper text value');
+ ok(elem.getAttribute('id') === 'myID', 'function generated child has proper ID');
+ ok(elem.className === 'myClass', 'function generated child has proper class');
+ elem = elem.nextSibling;
+console.log('elem.nodeName=' + elem.nodeName);
+ ok(elem.nodeType === 3, '3rd child is proper type');
+ ok(elem.nodeValue === 'final', '3rd child has proper text');
+ });
});
View
@@ -50,6 +50,7 @@
this.document = doc;
this.element = null; // the HTML element
this.allowTextNodes = false; // true if the tag can have text
+ this.pendingFunc = null; // holds a ref to a func that is pending execution awaiting its argument
};
Element.prototype = (function () {
@@ -95,6 +96,14 @@
procArg = function (arg) {
var i, n, node, subArg, t;
+
+ if ((t = this.pendingFunc)) {
+ // inoke the pending function with this argument and process the results
+ // the result is processed as if it were the present arg
+ this.pendingFunc = null; // clear pending state
+ arg = t.call(this, arg);
+ }
+
t = isArray(arg) ? 'array' : typeof arg;
if (t === 'object' && isElementNode(arg)) {
t = 'node';
@@ -131,19 +140,20 @@
}
}
} else {
- // treat as an argument of the current element
- procArg.call(this, subArg);
+ // treat array elements as further arguments of the current element
+ n = arg.length;
+ for (i = 0; i < n; i += 1) {
+ procArg.call(this, arg[i]);
+ }
}
break;
case 'object':
// attributes
handleAttrs.call(this,arg);
break;
case 'function':
- t = arg.call(this);
- if (t) {
- procArg.call(this, t);
- }
+ // the next arg is passed to the function, so for now, just capture the function
+ this.pendingFunc = arg;
break;
}
};
@@ -187,6 +197,7 @@
doc = env.global.window.document;
}
this.document = doc;
+ this.verbose = false;
};
Doml.prototype = {
View
@@ -127,7 +127,7 @@
ok(elem.nodeName === 'SPAN', '4th child has proper tag');
});
- test('element creation: array argument for child', 9, function() {
+ test('element creation: array arguments for children', 9, function() {
elem = doml.create('div', 'text1', ['p', 'p-text'], 'text2', ['span', 'span-text']);
ok(elem.nodeName === 'DIV', 'element has proper tag');
ok(elem.children.length === 2, 'element has proper number of (element) children');
@@ -144,6 +144,31 @@
ok(elem.nodeName === 'SPAN', '4th child has proper tag');
});
+ test('element creation: array of nodes', 7, function() {
+ elem = doml.create('div', [doml.create('p', 'p-text'), doml.create('span', 'span-text')]);
+ ok(elem.nodeName === 'DIV', 'element has proper tag');
+ ok(elem.children.length === 2, 'element has proper number of (element) children');
+ ok(elem.childNodes.length === 2, 'element has proper number of (elements & text) children');
+ elem = elem.firstChild;
+ ok(elem.nodeName === 'P', '1st child has proper tag');
+ ok(elem.innerHTML === 'p-text', '1st child has proper text');
+ elem = elem.nextSibling;
+ ok(elem.nodeName === 'SPAN', '2nd child has proper tag');
+ ok(elem.innerHTML === 'span-text', '2nd child has proper text');
+ });
+
+ test('element creation: array of mixed arguments', 7, function() {
+ elem = doml.create('div', [doml.create('p', 'p-text'), 'hello', {"id": "myID"}]);
+ ok(elem.nodeName === 'DIV', 'element has proper tag');
+ ok(elem.getAttribute('id') === 'myID', 'element has proper ID');
+ ok(elem.children.length === 1, 'element has proper number of (element) children');
+ ok(elem.childNodes.length === 2, 'element has proper number of (elements & text) children');
+ ok(elem.lastChild.nodeValue === 'hello', 'element has proper text');
+ elem = elem.firstChild;
+ ok(elem.nodeName === 'P', '1st child has proper tag');
+ ok(elem.innerHTML === 'p-text', '1st child has proper text');
+ });
+
test('element creation: array of arrays argument for children', 9, function() {
elem = doml.create('div', 'text1', [['p', 'p-text'], 'text2', ['span', 'span-text']]);
ok(elem.nodeName === 'DIV', 'element has proper tag');
@@ -161,21 +186,49 @@
ok(elem.nodeName === 'SPAN', '4th child has proper tag');
});
- test('element creation: function invocation', 8, function() {
- elem = doml.create('div', 'hello', {className: "myClass", "id": "myID", myAttr: 3}, function (ctxt) {
- console.log('==> anon function invoked');
- return {checked: 1, selected: true};
- });
+ test('element creation: function invocation returns nothing', 3, function() {
+ elem = doml.create('div', 'hello', function (args) {}, {text: 'hello'}, 'final');
ok(elem.nodeName === 'DIV', 'element has proper tag');
- ok(elem.innerHTML === 'hello', 'has proper text value');
- ok(elem.getAttribute('id') === 'myID', 'has ID');
- ok(elem.getAttribute('myAttr') === "3", 'has attribute');
- ok(!elem.getAttribute('type'), 'has no type attribute');
- ok(elem.className === 'myClass', 'has class');
- ok(elem.checked, 'is checked');
- ok(elem.selected, 'is selected');
+ ok(elem.children.length === 0, 'element has proper number of (element) children');
+ ok(elem.childNodes.length === 2, 'element has proper number of (elements & text) children');
});
+ test('element creation: function invocation returns an element', 11, function() {
+ elem = doml.create('div', 'hello', function (args) {
+ return doml.create('p', args.text, {className: args.className, "id": args.id});
+ }, {className: "myClass", "id": "myID", text: 'hello'}, 'final');
+ ok(elem.nodeName === 'DIV', 'element has proper tag');
+ ok(elem.children.length === 1, 'element has proper number of (element) children');
+ ok(elem.childNodes.length === 3, 'element has proper number of (elements & text) children');
+ elem = elem.firstChild;
+ ok(elem.nodeType === 3, '1st child is proper type');
+ ok(elem.nodeValue === 'hello', '1st child has proper text');
+ elem = elem.nextSibling;
+ ok(elem.nodeName === 'P', 'function generated child element has proper tag');
+ ok(elem.innerHTML === 'hello', 'function generated child has proper text value');
+ ok(elem.getAttribute('id') === 'myID', 'function generated child has proper ID');
+ ok(elem.className === 'myClass', 'function generated child has proper class');
+ elem = elem.nextSibling;
+ ok(elem.nodeType === 3, '3rd child is proper type');
+ ok(elem.nodeValue === 'final', '3rd child has proper text');
+ });
+
+ test('element creation: function invocation returns array of elements', 6, function() {
+ elem = doml.create('div', function (args) {
+ var elems = [];
+ elems.push(doml.create('p', args.text1));
+ elems.push(doml.create('span', args.text2));
+ return elems;
+ }, {text1: 'text1', text2: 'text2'});
+ ok(elem.children.length === 2, 'element has proper number of (element) children');
+ ok(elem.childNodes.length === 2, 'element has proper number of (elements & text) children');
+ elem = elem.firstChild;
+ ok(elem.nodeName === 'P', '1st child element has proper tag');
+ ok(elem.innerHTML === 'text1', '1st child has proper text value');
+ elem = elem.nextSibling;
+ ok(elem.nodeName === 'SPAN', '2nd child element has proper tag');
+ ok(elem.innerHTML === 'text2', '2nd child has proper text value');
+ });
});

0 comments on commit 96c8be8

Please sign in to comment.