[jQuery] Another Easy DOM creation for jQuery

[jQuery] Another Easy DOM creation for jQuery

Another Easy DOM creation for jQuery
My jQuery plugin.
$.dom = function() {
var ret = [], a = arguments, e, o, i=0, j, jq = $();
a = a[0].constructor == Array ? a[0] : a;
for (; i<a.length; i++) {
if (a[i] && a[i].nodeType) ret[ret.length] = a[i];
else if (a[i].$jquery)
for (j=0; j<a[i].size(); j++)
ret[ret.length] = a[i].get(j);
else if (a[i].constructor == Array) {
o = $.dom(a[i]).cur;
for (j=0; j<o.length; j++) ret[ret.length] = o[j];
} else if (a[i+1] && a[i+1].constructor == Object) {
e = ret[ret.length] = document.createElement(a[i]);
for (j in a[++i]) $.attr(e, j, a[i][j]);
if (a[i+1].constructor == Array) {
o = $.dom(a[++i]).cur;
for (j=0; j<o.length; j++) e.appendChild(o[j]);
}
} else ret[ret.length] = document.createTextNode(a[i]);
}
jq.cur = ret;
return jq;
};
$.tpl = function(json, tpl) {
var ret = [], a, i=0, j, jq = $();
json = json.constructor == Array ? json : [json];
for (; i<json.length; i++) {
a = $.dom($.apply(json[i], tpl)).cur;
for (j=0; j<a.length; j++) ret[ret.length] = a[j];
}
jq.cur = ret;
return jq;
};
$.fn.clone = function(deep) {
var jq = $(), i = 0;
for ( ; i < this.size(); i++ ) { jq.cur[i] =
this.get(i).cloneNode(deep); }
return jq;
};
$.html = function(){ return $.clean(arguments); }
And now you can do thing like this:
<table id="tst" border="1">
<tr><td colspan="2">cloned row 1</td></tr>
<tr><td colspan="2">cloned row 2</td></tr>
</table>
<table id="fill-table" border="1">
<thead><tr><th>Name</th><th>Surname</th></tr></thead>
<tbody></tbody>
</table>
<script type="text/javascript">
// <![CDATA[
var json = [
{'name' : "John", 'surname' : "Smith"},
{'name' : "Sar&amp;ra", 'surname' : "Smith"}
];
var tdTPL = function() { return [ 'td', {}, [this] ]; };
$.tpl(json, function(){
return [
'tr', {}, [ $.dom('td', { 'colspan' : 2 }, [ "dom ",
"function" ]) ],
'tr', {}, [ $.tpl(["another", "template"], tdTPL) ],
'tr', { 'class':"MyTableRow" }, [
'td', { 'class':"MyTableCol1" }, [ this.name ],
'td', { 'class':"MyTableCol2" }, [ this.surname ]],
'tr', {}, $.html('<td colspan="2">html function</td>'),
$('#tst tr').clone(true)
];
}).appendTo($('#fill-table > tbody').get(0));
</script>
PS: I hope it is good enough to implement in jQuery core :)
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/