1- //Copyright (c) 2016 Crystalline Technologies
1+ //Copyright (c) 2018 Crystalline Technologies
22//
33// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'),
44// to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
@@ -135,14 +135,61 @@ var json2html = {
135135
136136 switch ( key ) {
137137
138- //LEGACY support for tag
138+ //DEPRECATED (use <> instead)
139139 case 'tag' :
140+
141+ //HTML element to render
140142 case '<>' :
141143 //Do nothing as we have already created the element
142144 break ;
145+
146+ //Encode as text
147+ case 'text' :
148+ //Get the transform value associated with this key
149+ var _transform = transform [ key ] ;
150+
151+ //Determine what kind of object this is
152+ // array => NOT SUPPORTED
153+ // other => text
154+ if ( json2html . _isArray ( _transform ) ) {
155+ //NOT Supported
156+ } else if ( typeof _transform === 'function' ) {
157+
158+ //Get the result from the function
159+ var temp = _transform . call ( obj , obj , index ) ;
160+
161+ //Don't allow arrays as return objects from functions
162+ if ( ! json2html . _isArray ( temp ) ) {
163+
164+ //Determine what type of object was returned
165+ switch ( typeof temp ) {
166+
167+ //Not supported for text
168+ case 'function' :
169+ case 'undefined' :
170+ case 'object' :
171+ break ;
172+
173+ //Append as text
174+ // string, number, boolean
175+ default :
176+ //Insure we encode as text first
177+ children . html += json2html . toText ( temp ) ;
178+ break ;
179+ }
180+ }
181+ } else {
182+
183+ //Get the encoded text associated with this element
184+ html = json2html . toText ( json2html . _getValue ( obj , transform , key , index ) ) ;
185+ }
186+ break ;
143187
144- //LEGACY support for children
188+ //DEPRECATED (use HTML instead)
145189 case 'children' :
190+
191+ //Encode as HTML
192+ // accepts Array of children, functions, string, number, boolean
146193 case 'html' :
147194
148195 //Get the transform value associated with this key
@@ -160,31 +207,35 @@ var json2html = {
160207
161208 //Get the result from the function
162209 var temp = _transform . call ( obj , obj , index ) ;
163-
164- //Determine what type of object was returned
165- switch ( typeof temp ) {
166-
167- //Only returned by json2html.transform or $.json2html calls
168- case 'object' :
169- //make sure this object is a valid json2html response object
170- // we ignore all other objects (since we don't know how to represent them in html)
171- if ( temp . html !== undefined && temp . events !== undefined ) children = json2html . _append ( children , temp ) ;
172- break ;
173-
174- //Not supported
175- case 'function' :
176- case 'undefined' :
177- break ;
178-
179- //Append to html
180- // string, number, boolean
181- default :
182- children . html += temp ;
183- break ;
184- }
210+
211+ //Don't allow arrays as return objects from functions
212+ if ( ! json2html . _isArray ( temp ) ) {
213+
214+ //Determine what type of object was returned
215+ switch ( typeof temp ) {
216+
217+ //Only returned by json2html.transform or $.json2html calls
218+ case 'object' :
219+ //make sure this object is a valid json2html response object
220+ // we ignore all other objects (since we don't know how to represent them in html)
221+ if ( temp . html !== undefined && temp . events !== undefined ) children = json2html . _append ( children , temp ) ;
222+ break ;
223+
224+ //Not supported
225+ case 'function' :
226+ case 'undefined' :
227+ break ;
228+
229+ //Append to html
230+ // string, number, boolean
231+ default :
232+ children . html += temp ;
233+ break ;
234+ }
235+ }
185236 } else {
186237
187- //Create the html attribute for this element
238+ //Get the HTML associated with this element
188239 html = json2html . _getValue ( obj , transform , key , index ) ;
189240 }
190241 break ;
@@ -319,6 +370,17 @@ var json2html = {
319370
320371 return ( out ) ;
321372 } ,
373+
374+ //Encode the html to text
375+ 'toText' :function ( html ) {
376+ return html
377+ . replace ( / & / g, '&' )
378+ . replace ( / < / g, '<' )
379+ . replace ( / > / g, '>' )
380+ . replace ( / \" / g, '"' )
381+ . replace ( / \' / g, ''' )
382+ . replace ( / \/ / g, '/' ) ;
383+ } ,
322384
323385 //Tokenizer
324386 '_tokenizer' :function ( tokenizers , doBuild ) {
0 commit comments