@@ -1252,11 +1252,12 @@ expose.renderJsonML = function( jsonml, options ) {
12521252 options = options || { } ;
12531253 // include the root element in the rendered output?
12541254 options . root = options . root || false ;
1255+ options . xhtml = options . xhtml || false ;
12551256
12561257 var content = [ ] ;
12571258
12581259 if ( options . root ) {
1259- content . push ( render_tree ( jsonml ) ) ;
1260+ content . push ( render_tree ( jsonml , options . xhtml ) ) ;
12601261 }
12611262 else {
12621263 jsonml . shift ( ) ; // get rid of the tag
@@ -1265,14 +1266,14 @@ expose.renderJsonML = function( jsonml, options ) {
12651266 }
12661267
12671268 while ( jsonml . length ) {
1268- content . push ( render_tree ( jsonml . shift ( ) ) ) ;
1269+ content . push ( render_tree ( jsonml . shift ( ) , options . xhtml ) ) ;
12691270 }
12701271 }
12711272
1272- return content . join ( "\n" ) . replace ( / \n + $ / , " " ) ;
1273+ return content . join ( "\n\n " ) ;
12731274}
12741275
1275- function render_tree ( jsonml ) {
1276+ function render_tree ( jsonml , xhtml ) {
12761277 // basic case
12771278 if ( typeof jsonml === "string" ) {
12781279 return jsonml . replace ( / & / g, "&" )
@@ -1289,24 +1290,25 @@ function render_tree( jsonml ) {
12891290 }
12901291
12911292 while ( jsonml . length ) {
1292- content . push ( arguments . callee ( jsonml . shift ( ) ) ) ;
1293+ content . push ( arguments . callee ( jsonml . shift ( ) , xhtml ) ) ;
12931294 }
12941295
12951296 var tag_attrs = "" ;
12961297 for ( var a in attributes ) {
12971298 tag_attrs += " " + a + '="' + attributes [ a ] + '"' ;
12981299 }
1299-
1300- var newlinetab = "\n " ,
1301- newline = "\n" ;
1302-
1303- if ( ~ [ "em" , "strong" , "img" , "br" , "a" ] . indexOf ( tag ) ) {
1304- newlinetab = "" ;
1305- newline = "" ;
1306- }
1307-
1300+
1301+ // if xhtml, self-close empty tags
13081302 // be careful about adding whitespace here for inline elements
1309- return "<" + tag + tag_attrs + ">" + newlinetab + content . join ( "" ) . replace ( / \n $ / , "" ) . replace ( / \n / g, "\n " ) + newline + "</" + tag + ">" + newline ;
1303+ var markup = "<" + tag + tag_attrs ;
1304+ var contentstr = content . join ( "" ) ;
1305+ if ( xhtml && contentstr . length == 0 ) {
1306+ markup += " />" ;
1307+ }
1308+ else {
1309+ markup += ">" + contentstr + "</" + tag + ">" ;
1310+ }
1311+ return markup ;
13101312}
13111313
13121314function convert_tree_to_html ( tree , references ) {
0 commit comments