1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| function LinkReplace(txt)
{
/*
* //Exemple d'utilisation :
* var obj = new LinkReplace($('#test').html());
* $('#test').html(obj.exec());
*/
this.setTexte = function(txt)
{
this.texte = txt + ' ';
this.length = this.texte.length;
}
this.replace = function(debut, fin, str)
{
//On met à jour texte :
this.texte = this.texte.substr(0, debut) + str + this.texte.substr(fin + 1, (this.length - 1) - (fin));
//On met à jour length :
//this.length = this.length + (str.length - (fin - debut));
this.length = this.texte.length;
}
this.isCaracFin = function(carac)
{
for(var i = 0 ; i < this.caracFin.length ; i++)
{
if(this.caracFin[i] == carac)
return true;
}
return false;
}
this.isCaracFinIdVideo = function(carac)
{
for(var i = 0 ; i < this.caracFinIdVideo.length ; i++)
{
if(this.caracFinIdVideo[i] == carac)
return true;
}
return false;
}
this.suite = function(debut, str)
{
for(var i = 0 ; i < str.length ; i++)
{
if(this.texte.charAt(i + debut) != str.charAt(i))
return false;
}
return true;
}
this.isImage = function(lien)
{
var extension = lien.substr(lien.length - 4, 4);
if(extension == ".jpg" || extension == ".png" || extension == ".gif" || extension == "jpeg")
return true;
return false;
}
this.isImageV2 = function(lien)
{
return /\.(jpe?g|png|gif)$/.test(lien);
}
this.isYoutubeLink = function(lien)
{
return lien.indexOf("http://www.youtube.com/watch?v=", 0) == 0;
}
this.isDailymotionLink = function(lien)
{
return lien.indexOf("http://www.dailymotion.com/video/", 0) == 0;
}
this.createIframeYoutube = function(lien)
{
var id = '';
//Tant que ce n'est pas la fin du lien et que ce n'est pas la fin de l'id, on ajoute les caractères :
for(var i = 31 ; i < lien.length && !this.isCaracFinIdVideo(lien.charAt(i)) ; i++)
id += lien.charAt(i);
return '<iframe width="500" height="350" src="http://www.youtube.com/embed/' + id + '" frameborder="0" allowfullscreen></iframe>';
}
this.createIframeDailymotion = function(lien)
{
var id = '';
for(var i = 33 ; i < lien.length && !this.isCaracFinIdVideo(lien.charAt(i)) && lien.charAt(i) != '_' ; i++)
id += lien.charAt(i);
return '<iframe frameborder="0" width="500" height="350" src="http://www.dailymotion.com/embed/video/' + id + '"></iframe>';
}
this.exec = function()
{
for(var i = 0 ; i < this.length ; i++)
{
var carac = this.texte.charAt(i);
if(carac == 'h')
{
//On prend l'index de debut :
var debut = i;
//Si on a du http, on met à jour i :
if(this.suite(i + 1, "ttp://"))
i = i + 7;
//Sinon c'est une autre mise à jour :
else if(this.suite(i + 1, "ttps://"))
i = i + 8;
//Si i a changé, c'est qu'on a bien un lien :
if(i != debut)
{
while(!this.isCaracFin(this.texte.charAt(i)))
i++;
var fin = i - 1;
var lien = this.texte.substr(debut, fin - debut + 1);
var nom = lien;
if(lien.length > 70)
nom = lien.substr(0, 35) + "..." + lien.substr(lien.length - 20, 20)
var replace = '';
if(this.isImageV2(lien))
replace = '<img alt="user image" src="' + lien + '"/>';
else if(this.isYoutubeLink(lien))
replace = this.createIframeYoutube(lien);
else if(this.isDailymotionLink(lien))
replace = this.createIframeDailymotion(lien);
else
replace = '<a href="' + lien + '">' + nom + '</a>';
i = debut + replace.length;
this.replace(debut, fin, replace);
}
}
else if(carac == 'w')
{
var debut = i;
if(this.suite(i + 1, "ww."))
i = i + 4;
if(i != debut)
{
while(!this.isCaracFin(this.texte.charAt(i)))
i++;
var fin = i - 1;
var lien = "http://" + this.texte.substr(debut, fin - debut + 1);
var nom = lien;
if(lien.length > 70)
nom = lien.substr(0, 40) + "..." + lien.substr(lien.length - 17, 17)
var replace = '';
if(this.isImage(lien))
replace = '<img alt="user image" src="' + lien + '"/>';
else
replace = '<a href="' + lien + '">' + nom + '</a>';
i = debut + replace.length;
this.replace(debut, fin, replace);
}
}
}
this.texte = this.texte.substr(0, this.texte.length - 1);
return this.texte;
}
//On met un espace à la fin pour éviter les effets de bord :
this.texte = txt + ' ';
this.caracFin = [ /*'<', '>',*/ '"', "'", '\n', '\r', '\t', ' ' ];
this.caracFinIdVideo = [ '&' ];
this.length = this.texte.length;
} |