1/** 2 * @file oembed.js 3 * @brief javascript code for oembed addon 4 * @author XEHub ([email protected]) 5 */ 6(function($){ 7 var protocol_re = '(https?|ftp|news|telnet|irc|mms)://'; 8 var domain_re = '(?:[\\w\\-]+\\.)+(?:[a-z]+)'; 9 var max_255_re = '(?:1[0-9]{2}|2[0-4][0-9]|25[0-5]|[1-9]?[0-9])'; 10 var ip_re = '(?:'+max_255_re+'\\.){3}'+max_255_re; 11 var port_re = '(?::([0-9]+))?'; 12 var user_re = '(?:/~[\\w-]+)?'; 13 var path_re = '((?:/[\\w!"$-/:-@]+)*)'; 14 var hash_re = '(?:#([\\w!-@]+))?'; 15 16 var url_regex = new RegExp('('+protocol_re+'('+domain_re+'|'+ip_re+'|localhost'+')'+port_re+user_re+path_re+hash_re+')', 'ig'); 17 18 var OEmbed = xe.createPlugin("OEmbed", { 19 targets : [], 20 init : function() { 21 this.targets = []; 22 this.enableAutoLink = false; 23 this.castedOembedA = false; 24 this.embedSetting = []; 25 // this.embedSetting.embedMethod = 'replace'; 26 }, 27 API_ONREADY : function() { 28 var thisPlugin = this; 29 30 // extract target text nodes 31 this.extractTargets($('.xe_content')); 32 33 if(this.oApp.getPlugin('autolink').length) 34 { 35 this.enableAutoLink = true; 36 } 37 thisPlugin.cast('OEMBEDA'); 38 39 if(!this.enableAutoLink) 40 { 41 $(this.targets).each(function(){ 42 thisPlugin.cast('OEMBED', [this]); 43 }); 44 } 45 }, 46 API_BEFORE_AUTOLINK : function(oSender, params) { 47 var thisPlugin = this; 48 thisPlugin.cast('OEMBEDA'); 49 }, 50 API_AFTER_AUTOLINK : function(oSender, params) { 51 this.oembed(params[0]); 52 }, 53 API_OEMBED : function(oSender, params) { 54 if(this.enableAutoLink) return; 55 var thisPlugin = this; 56 57 var textNode = params[0]; 58 if(!$(textNode).parent().length || $(textNode).parent().get(0).nodeName.toLowerCase() == 'a') 59 { 60 this.oembed($(textNode)); 61 return; 62 } 63 var content = textNode.nodeValue; 64 var dummy = $('<span>'); 65 66 content = content.replace(/</g, '<').replace(/>/g, '>'); 67 content = content.replace(url_regex, '<a href="$1" target="_blank">$1</a>'); 68 69 $(textNode).before(dummy); 70 $(textNode).replaceWith(content); 71 params[0] = dummy.next('a'); 72 dummy.remove(); 73 74 this.oembed(params[0]); 75 }, 76 API_OEMBEDA : function(oSender, params) { 77 if(this.castedOembedA) return; 78 var thisPlugin = this; 79 80 this.castedOembedA = true; 81 82 $('.read_body a').not('_oembed').each(function(){ 83 thisPlugin.oembed($(this)); 84 }); 85 }, 86 oembed : function(target) { 87 var thisPlugin = this; 88 89 if(!this.embedSetting.maxWidth) 90 { 91 this.embedSetting.maxWidth = $('.xe_content').width(); 92 } 93 94 if(!target) return; 95 if(target && target.nodeType == 3) target = $(target); 96 97 target.oembed(null, thisPlugin.embedSetting).addClass('_oembed'); 98 }, 99 extractTargets : function(obj) { 100 var thisPlugin = this; 101 var wrap = $('.xe_content', obj); 102 if(wrap.length) { 103 this.extractTargets(wrap); 104 return; 105 } 106 107 $(obj) 108 .contents() 109 .each(function(){ 110 var node_name = this.nodeName.toLowerCase(); 111 if($.inArray(node_name, ['a', 'pre', 'xml', 'textarea', 'input', 'select', 'option', 'code', 'script', 'style', 'iframe', 'button', 'img', 'embed', 'object', 'ins']) != -1) return; 112 113 // FIX ME : When this meanless code wasn't executed, url_regex do not run correctly. why? 114 url_regex.exec(''); 115 116 if(this.nodeType == 3) { // text node 117 var content = this.nodeValue; 118 119 if(content.length < 5) return; 120 121 if(!/(http|https|ftp|news|telnet|irc|mms):\/\//i.test(content)) return; 122 123 thisPlugin.targets.push(this); 124 } else { 125 thisPlugin.extractTargets(this); 126 } 127 }); 128 } 129 }); 130 131 xe.registerPlugin(new OEmbed()); 132})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.