Return to Snippet

Revision: 12659
at March 24, 2009 16:12 by postream


Updated Code
/* ---------------------------------------------------------------
 * YQL2DOM Plugin
 *
 * Convert YQL Jason results to a complete HTML Dom
 * Note: unfortunatly the YQL JSON format is lossy
 * Info: http://developer.yahoo.com/yql/guide/output-xml-json-conversion.html
 *
 * Warning: this plugin doesn't work in the right manner
 * The problem is that there's no way to extract the attributes in the right order
 * So for example: <p>just a <strong>test</strong></p> may arbitrary become <p><strong>test</strong>just a </p>
 *
 * @return: dom object
 * -------------------------------------------------------------*/  
 
// in some cases we can only distinguish between tags and attributes by the name
var YQL2DOM_CONTENT_TAGS = ['a','p','h1','h2','h3','h4','h5','h6','div','span','td', 'strong','em','i','b']

$.YQL2DOM = function(jsonResults, tag) {

	var htmlResult = "";
	
	// just one content
	if (typeof(jsonResults) == 'string'){
		
		return '<' + tag + '>' + jsonResults + '</' + tag + '>';	
	
	// array of the same tag
	}else if($.isArray(jsonResults)){
		
		for (var i = 0; i < jsonResults.length; i++){  
			htmlResult += $.YQL2DOM(jsonResults[i], tag);		
		}  
	
	
	// just one tag
	}else if (typeof(jsonResults) == 'object'){
		
		var childrenHTML = "";
		var attributes = [];		
		var attrID = 0;
		
		for (index in jsonResults){  
			
			if(tag == 'p') console.log('index: ' + index + ' | tag: ' + tag);
			// content
			if(index == 'content'){
				childrenHTML += jsonResults[index];		
				if(tag == 'p') console.log('content ' + jsonResults[index]);
				
			// attribute or simple child
			}else if(typeof(jsonResults[index]) !== 'object' ){
				
				if(tag == 'p') console.log('whonows ' + index);
				
				// test if it has a tag name
				if($.inArray(index, YQL2DOM_CONTENT_TAGS) != -1)
					// simple tag
					childrenHTML += '<' + index + '>' + jsonResults[index] + '</' + index + '>';
				else
					// attribute
					attributes[attrID++] = index + "=\"" + jsonResults[index] + "\"";
				
			// child
			}else{
				childrenHTML += $.YQL2DOM(jsonResults[index], index);		
			}
		}  	

		if(tag === undefined){
			// root
			htmlResult = childrenHTML; 
		}else{
			attributes = (attrID > 0)?' ' + attributes.join(' '):'';
			htmlResult = "<" + tag + attributes + ">" + childrenHTML + "</" + tag + ">";		
		}

	}
	
	return htmlResult;
};

Revision: 12658
at March 24, 2009 00:43 by postream


Initial Code
/* ---------------------------------------------------------------
 * YQL2DOM Plugin
 *
 * Convert YQL Jason results to a complete HTML Dom
 * Note: unfortunatly the YQL JSON format is lossy
 * Info: http://developer.yahoo.com/yql/guide/output-xml-json-conversion.html
 *
 * Warning: this plugin doesn't work in the right manner
 * The problem is that there's no way to extract the attributes in the right order
 * So for example: <p>just a <strong>test</strong></p> may arbitrary become <p><strong>test</strong>just a </p>
 *
 * @return: dom object
 * -------------------------------------------------------------*/  
 
// in some cases we can only distinguish between tags and attributes by the name
var YQL2DOM_CONTENT_TAGS = ['a','p','h1','h2','h3','h4','h5','h6','div','span','td', 'strong','em','i','b']

$.YQL2DOM = function(jsonResults, tag) {

	var htmlResult = "";
	
	if($.isArray(jsonResults)){
		// array of the same tag
		for (var i = 0; i < jsonResults.length; i++){  
			htmlResult += $.YQL2DOM(jsonResults[i], tag);		
		}  

	}else{
		// just one tag
		var childrenHTML = "";
		var attributes = [];		
		var attrID = 0;
		
		for (index in jsonResults){  
			
			if(tag == 'p') console.log('index: ' + index + ' | tag: ' + tag);
			// content
			if(index == 'content'){
				childrenHTML += jsonResults[index];		
				if(tag == 'p') console.log('content ' + jsonResults[index]);
				
			// attribute or simple child
			}else if(typeof(jsonResults[index]) !== 'object' ){
				
				if(tag == 'p') console.log('whonows ' + index);
				
				// test if it has a tag name
				if($.inArray(index, YQL2DOM_CONTENT_TAGS) != -1)
					// simple tag
					childrenHTML += '<' + index + '>' + jsonResults[index] + '</' + index + '>';
				else
					// attribute
					attributes[attrID++] = index + "=\"" + jsonResults[index] + "\"";
				
			// child
			}else{
				childrenHTML += $.YQL2DOM(jsonResults[index], index);		
			}
		}  	

		if(tag === undefined){
			// root
			htmlResult = childrenHTML; 
		}else{
			attributes = (attrID > 0)?' ' + attributes.join(' '):'';
			htmlResult = "<" + tag + attributes + ">" + childrenHTML + "</" + tag + ">";		
		}
	}
	
	return htmlResult;
};

Initial URL

                                

Initial Description
Convert YQL Jason results to a complete HTML Dom
Note: unfortunatly the YQL JSON format is lossy
Info: http://developer.yahoo.com/yql/guide/output-xml-json-conversion.html
 
Warning: this plugin doesn't work in the right manner!
The problem is that there's no way to extract the attributes in the right order
So for example: <p>just a <strong>test</strong></p> may arbitrary become <p><strong>test</strong>just a </p>

Initial Title
YQL2DOM jQuery Plugin

Initial Tags

                                

Initial Language
jQuery