/ Published in: jQuery
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>
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>
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/* --------------------------------------------------------------- * 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; };