1- define ( [ ] , function ( ) {
2- // this makes recursing a lot simpler
1+ ( function ( ) {
2+ var Hyperjson = { } ;
33 var isArray = function ( A ) {
44 return Object . prototype . toString . call ( A ) === '[object Array]' ;
55 } ;
66
7- var callOnHyperJSON = function ( hj , cb ) {
7+ var isTruthy = function ( x ) {
8+ return x ;
9+ } ;
10+
11+ var callOnHyperJSON = Hyperjson . callOn = function ( hj , cb ) {
812 var children ;
913
1014 if ( hj && hj [ 2 ] ) {
@@ -27,11 +31,7 @@ define([], function () {
2731 return cb ( hj [ 0 ] , hj [ 1 ] , children ) ;
2832 } ;
2933
30- var isTruthy = function ( x ) {
31- return x ;
32- } ;
33-
34- var DOM2HyperJSON = function ( el , predicate , filter ) {
34+ var DOM2HyperJSON = Hyperjson . fromDOM = function ( el , predicate , filter ) {
3535 if ( ! el . tagName && el . nodeType === Node . TEXT_NODE ) {
3636 return el . textContent ;
3737 }
@@ -60,16 +60,7 @@ define([], function () {
6060
6161 // get the element type, id, and classes of the element
6262 // and push them to the result array
63- var sel = el . tagName ;
64-
65- if ( attributes . id ) {
66- // we don't have to do much to validate IDs because the browser
67- // will only permit one id to exist
68- // unless we come across a strange browser in the wild
69- sel = sel + '#' + attributes . id ;
70- delete attributes . id ;
71- }
72- result . push ( sel ) ;
63+ result . push ( el . tagName ) ;
7364
7465 // second element of the array is the element attributes
7566 result . push ( attributes ) ;
@@ -91,8 +82,22 @@ define([], function () {
9182 }
9283 } ;
9384
94- return {
95- fromDOM : DOM2HyperJSON ,
96- callOn : callOnHyperJSON
85+ var H = Hyperjson . toDOM = function ( hj ) {
86+ if ( typeof ( hj ) === 'string' ) { return document . createTextNode ( hj ) ; }
87+ var e = document . createElement ( hj [ 0 ] ) ;
88+ for ( var x in hj [ 1 ] ) { e . setAttribute ( x , hj [ 1 ] [ x ] ) ; }
89+ for ( var i = 0 ; i < hj [ 2 ] . length ; i ++ ) { e . appendChild ( H ( hj [ 2 ] [ i ] ) ) ; }
90+ return e ;
9791 } ;
98- } ) ;
92+
93+ if ( typeof ( module ) !== 'undefined' && module . exports ) {
94+ module . exports = Hyperjson ;
95+ } else if ( ( typeof ( define ) !== 'undefined' && define !== null ) && ( define . amd !== null ) ) {
96+ define ( function ( ) {
97+ return Hyperjson ;
98+ } ) ;
99+ } else {
100+ window . Hyperjson = Hyperjson ;
101+ }
102+ } ( ) ) ;
103+
0 commit comments