2 * Copyright 2010-2013 Ben Birch
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this software except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 var joey = this.joey = function joey(elementDef, parentNode) {
19 return createNode( elementDef, parentNode, parentNode ? parentNode.ownerDocument : this.document );
22 var shortcuts = joey.shortcuts = {
23 "text" : "textContent",
27 var plugins = joey.plugins = [
28 function( obj, context ) {
29 if( typeof obj === 'string' ) {
30 return context.createTextNode( obj );
33 function( obj, context ) {
35 var el = context.createElement( obj.tag );
36 for( var attr in obj ) {
37 addAttr( el, attr, obj[ attr ], context );
44 function addAttr( el, attr, value, context ) {
45 attr = shortcuts[attr] || attr;
46 if( attr === 'children' ) {
47 for( var i = 0; i < value.length; i++) {
48 createNode( value[i], el, context );
50 } else if( attr === 'style' || attr === 'dataset' ) {
51 for( var prop in value ) {
52 el[ attr ][ prop ] = value[ prop ];
54 } else if( attr.indexOf("on") === 0 ) {
55 el.addEventListener( attr.substr(2), value, false );
56 } else if( value !== undefined ) {
61 function createNode( obj, parent, context ) {
64 plugins.some( function( plug ) {
65 return ( el = plug( obj, context ) );
67 parent && parent.appendChild( el );