YQL2DOM jQuery Plugin


/ Published in: jQuery
Save to your folder(s)

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>


Copy this code and paste it in your HTML
  1. /* ---------------------------------------------------------------
  2.  * YQL2DOM Plugin
  3.  *
  4.  * Convert YQL Jason results to a complete HTML Dom
  5.  * Note: unfortunatly the YQL JSON format is lossy
  6.  * Info: http://developer.yahoo.com/yql/guide/output-xml-json-conversion.html
  7.  *
  8.  * Warning: this plugin doesn't work in the right manner
  9.  * The problem is that there's no way to extract the attributes in the right order
  10.  * So for example: <p>just a <strong>test</strong></p> may arbitrary become <p><strong>test</strong>just a </p>
  11.  *
  12.  * @return: dom object
  13.  * -------------------------------------------------------------*/
  14.  
  15. // in some cases we can only distinguish between tags and attributes by the name
  16. var YQL2DOM_CONTENT_TAGS = ['a','p','h1','h2','h3','h4','h5','h6','div','span','td', 'strong','em','i','b']
  17.  
  18. $.YQL2DOM = function(jsonResults, tag) {
  19.  
  20. var htmlResult = "";
  21.  
  22. // just one content
  23. if (typeof(jsonResults) == 'string'){
  24.  
  25. return '<' + tag + '>' + jsonResults + '</' + tag + '>';
  26.  
  27. // array of the same tag
  28. }else if($.isArray(jsonResults)){
  29.  
  30. for (var i = 0; i < jsonResults.length; i++){
  31. htmlResult += $.YQL2DOM(jsonResults[i], tag);
  32. }
  33.  
  34.  
  35. // just one tag
  36. }else if (typeof(jsonResults) == 'object'){
  37.  
  38. var childrenHTML = "";
  39. var attributes = [];
  40. var attrID = 0;
  41.  
  42. for (index in jsonResults){
  43.  
  44. if(tag == 'p') console.log('index: ' + index + ' | tag: ' + tag);
  45. // content
  46. if(index == 'content'){
  47. childrenHTML += jsonResults[index];
  48. if(tag == 'p') console.log('content ' + jsonResults[index]);
  49.  
  50. // attribute or simple child
  51. }else if(typeof(jsonResults[index]) !== 'object' ){
  52.  
  53. if(tag == 'p') console.log('whonows ' + index);
  54.  
  55. // test if it has a tag name
  56. if($.inArray(index, YQL2DOM_CONTENT_TAGS) != -1)
  57. // simple tag
  58. childrenHTML += '<' + index + '>' + jsonResults[index] + '</' + index + '>';
  59. else
  60. // attribute
  61. attributes[attrID++] = index + "=\"" + jsonResults[index] + "\"";
  62.  
  63. // child
  64. }else{
  65. childrenHTML += $.YQL2DOM(jsonResults[index], index);
  66. }
  67. }
  68.  
  69. if(tag === undefined){
  70. // root
  71. htmlResult = childrenHTML;
  72. }else{
  73. attributes = (attrID > 0)?' ' + attributes.join(' '):'';
  74. htmlResult = "<" + tag + attributes + ">" + childrenHTML + "</" + tag + ">";
  75. }
  76.  
  77. }
  78.  
  79. return htmlResult;
  80. };

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.