Skip to content

Commit 090b459

Browse files
committed
Never and ever pollute Array/String prototype (fixes ritz078#42).
1 parent 36713c3 commit 090b459

4 files changed

Lines changed: 52 additions & 47 deletions

File tree

dist/jquery.embed.js

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,10 @@
214214

215215
/* UTILITIES - FUNCTIONS */
216216

217-
String.prototype.trunc = function (n, useWordBoundary) {
218-
var toLong = this.length > n, s_ = toLong ? this.substr(0, n - 1) : this;
217+
var utils = {};
218+
219+
utils.trunc = function (string, n, useWordBoundary) {
220+
var toLong = string.length > n, s_ = toLong ? string.substr(0, n - 1) : string;
219221
s_ = useWordBoundary && toLong ? s_.substr(0, s_.lastIndexOf(' ')) : s_;
220222
return toLong ? s_ + '...' : s_;
221223
};
@@ -225,24 +227,24 @@
225227
*
226228
* @returns {Array}
227229
*/
228-
Array.prototype.getUnique = function () {
230+
utils.getUnique = function (array) {
229231
var u = {}, a = [];
230-
for (var i = 0, l = this.length; i < l; ++i) {
231-
if (u.hasOwnProperty(this[i])) {
232+
for (var i = 0, l = array.length; i < l; ++i) {
233+
if (u.hasOwnProperty(array[i])) {
232234
continue;
233235
}
234-
a.push(this[i]);
235-
u[this[i]] = 1;
236+
a.push(array[i]);
237+
u[array[i]] = 1;
236238
}
237239
return a;
238240
};
239241

240-
String.prototype.toUrl = function () {
242+
utils.toUrl = function (string) {
241243
var url;
242-
if (this.indexOf('//') == -1) {
243-
url = '//' + this;
244+
if (string.indexOf('//') == -1) {
245+
url = '//' + string;
244246
} else {
245-
url = this;
247+
url = string;
246248
}
247249
return url;
248250
};
@@ -292,7 +294,7 @@
292294
return str.replace(urlRegex, function (match) {
293295
var extension = match.split('.')[match.split('.').length - 1];
294296
if (($.inArray(extension, opts.linkExclude) === -1)) {
295-
return '<a href="' + match.toUrl() + '" target="' + opts.linkTarget + '">' + match + '</a>';
297+
return '<a href="' + utils.toUrl(match) + '" target="' + opts.linkTarget + '">' + match + '</a>';
296298
}
297299
return match;
298300
});
@@ -374,7 +376,7 @@
374376
video.host = 'youtube';
375377
video.title = ytData.snippet.title;
376378
video.thumbnail = ytData.snippet.thumbnails.medium.url;
377-
video.description = (ytData.snippet.description.trunc(150, true)).replace(/\n/g, ' ').replace(/&#10;/g, ' ');
379+
video.description = (utils.trunc(ytData.snippet.description, 150, true)).replace(/\n/g, ' ').replace(/&#10;/g, ' ');
378380
video.rawDescription = ytData.snippet.description;
379381
video.views = ytData.statistics.viewCount;
380382
video.likes = ytData.statistics.likeCount;
@@ -393,7 +395,7 @@
393395
video.host = 'vimeo';
394396
video.title = d[0].title;
395397
video.rawDescription = (d[0].description).replace(/\n/g, '<br/>').replace(/&#10;/g, '<br/>');
396-
video.description = (d[0].description).replace(/((<|&lt;)br\s*\/*(>|&gt;)\r\n)/g, ' ').trunc(150, true);
398+
video.description = utils.trunc((d[0].description).replace(/((<|&lt;)br\s*\/*(>|&gt;)\r\n)/g, ' '), 150, true);
397399
video.thumbnail = d[0].thumbnail_medium;
398400
video.views = d[0].stats_number_of_plays;
399401
video.likes = d[0].stats_number_of_likes;
@@ -443,7 +445,7 @@
443445

444446
//Remove duplicate urls and save to the variable removedDuplicates
445447

446-
matchArray = matchArray.getUnique();
448+
matchArray = utils.getUnique(matchArray);
447449

448450
var _this = this;
449451

@@ -572,7 +574,7 @@
572574
var matches;
573575
while ((matches = docRegex.exec(rawStr)) !== null) {
574576

575-
var template = '<div class="ejs-doc"><div class="ejs-doc-preview"><div class="ejs-doc-icon"><i class="fa fa-file-o"></i></div><div class="ejs-doc-detail" ><div class="ejs-doc-title"> <a href="">' + matches[0].toUrl() + '</a></div> <div class="ejs-doc-view"> <a href="' + matches[0].toUrl() + '" target="_blank"><button>' + opts.docOptions.downloadText + '</button></a> <button class="ejs-doc-view-active">' + opts.docOptions.viewText + '</button></div> </div> </div></div>';
577+
var template = '<div class="ejs-doc"><div class="ejs-doc-preview"><div class="ejs-doc-icon"><i class="fa fa-file-o"></i></div><div class="ejs-doc-detail" ><div class="ejs-doc-title"> <a href="">' + utils.toUrl(matches[0]) + '</a></div> <div class="ejs-doc-view"> <a href="' + utils.toUrl(matches[0]) + '" target="_blank"><button>' + opts.docOptions.downloadText + '</button></a> <button class="ejs-doc-view-active">' + opts.docOptions.viewText + '</button></div> </div> </div></div>';
576578
embedArray.push(createObject(matches.index, template));
577579

578580
}
@@ -588,7 +590,7 @@
588590

589591
var docParent = $(self).closest('.ejs-doc');
590592
var docUrl = $(docParent).find('a')[1].href;
591-
var docViewTemplate = ' <div class="ejs-doc-viewer"><iframe src="http://docs.google.com/viewer?embedded=true&url=' + docUrl.toUrl() + '" frameBorder="0" style="border: none;margin : 0 auto; display : block;"></iframe></div>';
593+
var docViewTemplate = ' <div class="ejs-doc-viewer"><iframe src="http://docs.google.com/viewer?embedded=true&url=' + utils.toUrl(docUrl) + '" frameBorder="0" style="border: none;margin : 0 auto; display : block;"></iframe></div>';
592594
docParent.html(docViewTemplate);
593595

594596
//calling the function after the document is shown.
@@ -693,7 +695,7 @@
693695
var matches;
694696
while ((matches = flickrRegex.exec(rawStr)) !== null) {
695697

696-
var template = '<div class="ejs-embed"><div class="ne-image-wrapper"><iframe src="' + matches[0].toUrl() + '/player/" width="' + dimensions.width + '" height="' + dimensions.height + '"></iframe></div></div>';
698+
var template = '<div class="ejs-embed"><div class="ne-image-wrapper"><iframe src="' + utils.toUrl(matches[0]) + '/player/" width="' + dimensions.width + '" height="' + dimensions.height + '"></iframe></div></div>';
697699
embedArray.push(createObject(matches.index, template));
698700

699701
}
@@ -705,7 +707,7 @@
705707
var matches;
706708
while ((matches = instagramRegex.exec(rawStr)) !== null) {
707709

708-
var template = '<div class="ejs-embed"><iframe src="' + matches[0].toUrl() + '/embed/" width="' + dimensions.width + '" height="' + dimensions.height + '"></iframe></div>';
710+
var template = '<div class="ejs-embed"><iframe src="' + utils.toUrl(matches[0]) + '/embed/" width="' + dimensions.width + '" height="' + dimensions.height + '"></iframe></div>';
709711
embedArray.push(createObject(matches.index, template));
710712

711713
}
@@ -782,7 +784,7 @@
782784

783785
getMatches: function (str) {
784786
var tweetRegex = /https:\/\/twitter\.com\/\w+\/\w+\/\d+/gi;
785-
var matches = str.match(tweetRegex) ? (str.match(tweetRegex)).getUnique() : null;
787+
var matches = str.match(tweetRegex) ? utils.getUnique(str.match(tweetRegex)) : null;
786788
return matches;
787789

788790
},
@@ -883,7 +885,7 @@
883885
if (rawStr.match(ggRegex)) {
884886
while ((matches = ggRegex.exec(rawStr)) !== null) {
885887
var m = matches;
886-
var match = 'https:' + matches[0].toUrl();
888+
var match = 'https:' + utils.toUrl(matches[0]);
887889
var url = 'https://noembed.com/embed?nowrap=on&url=' + match;
888890
var template = '<div class="ejs-embed ejs-github-gist" data-url="' + url + '"></div>';
889891
embedArray.push(createObject(m.index, template));
@@ -972,7 +974,7 @@
972974
$.each(embedArray, function (index, value) {
973975
embedCodeArray.push(value.embedCode);
974976
});
975-
str = str + embedCodeArray.getUnique().join(' ');
977+
str = str + utils.getUnique(embedCodeArray).join(' ');
976978
embedArray = [];
977979
embedCodeArray = [];
978980
return str;

dist/jquery.embed.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/jquery.embed.min.js

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/jquery.embed.js

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,10 @@
206206

207207
/* UTILITIES - FUNCTIONS */
208208

209-
String.prototype.trunc = function (n, useWordBoundary) {
210-
var toLong = this.length > n, s_ = toLong ? this.substr(0, n - 1) : this;
209+
var utils = {};
210+
211+
utils.trunc = function (string, n, useWordBoundary) {
212+
var toLong = string.length > n, s_ = toLong ? string.substr(0, n - 1) : string;
211213
s_ = useWordBoundary && toLong ? s_.substr(0, s_.lastIndexOf(' ')) : s_;
212214
return toLong ? s_ + '...' : s_;
213215
};
@@ -217,24 +219,24 @@
217219
*
218220
* @returns {Array}
219221
*/
220-
Array.prototype.getUnique = function () {
222+
utils.getUnique = function (array) {
221223
var u = {}, a = [];
222-
for (var i = 0, l = this.length; i < l; ++i) {
223-
if (u.hasOwnProperty(this[i])) {
224+
for (var i = 0, l = array.length; i < l; ++i) {
225+
if (u.hasOwnProperty(array[i])) {
224226
continue;
225227
}
226-
a.push(this[i]);
227-
u[this[i]] = 1;
228+
a.push(array[i]);
229+
u[array[i]] = 1;
228230
}
229231
return a;
230232
};
231233

232-
String.prototype.toUrl = function () {
234+
utils.toUrl = function (string) {
233235
var url;
234-
if (this.indexOf('//') == -1) {
235-
url = '//' + this;
236+
if (string.indexOf('//') == -1) {
237+
url = '//' + string;
236238
} else {
237-
url = this;
239+
url = string;
238240
}
239241
return url;
240242
};
@@ -284,7 +286,7 @@
284286
return str.replace(urlRegex, function (match) {
285287
var extension = match.split('.')[match.split('.').length - 1];
286288
if (($.inArray(extension, opts.linkExclude) === -1)) {
287-
return '<a href="' + match.toUrl() + '" target="' + opts.linkTarget + '">' + match + '</a>';
289+
return '<a href="' + utils.toUrl(match) + '" target="' + opts.linkTarget + '">' + match + '</a>';
288290
}
289291
return match;
290292
});
@@ -366,7 +368,7 @@
366368
video.host = 'youtube';
367369
video.title = ytData.snippet.title;
368370
video.thumbnail = ytData.snippet.thumbnails.medium.url;
369-
video.description = (ytData.snippet.description.trunc(150, true)).replace(/\n/g, ' ').replace(/&#10;/g, ' ');
371+
video.description = (utils.trunc(ytData.snippet.description, 150, true)).replace(/\n/g, ' ').replace(/&#10;/g, ' ');
370372
video.rawDescription = ytData.snippet.description;
371373
video.views = ytData.statistics.viewCount;
372374
video.likes = ytData.statistics.likeCount;
@@ -385,7 +387,7 @@
385387
video.host = 'vimeo';
386388
video.title = d[0].title;
387389
video.rawDescription = (d[0].description).replace(/\n/g, '<br/>').replace(/&#10;/g, '<br/>');
388-
video.description = (d[0].description).replace(/((<|&lt;)br\s*\/*(>|&gt;)\r\n)/g, ' ').trunc(150, true);
390+
video.description = utils.trunc((d[0].description).replace(/((<|&lt;)br\s*\/*(>|&gt;)\r\n)/g, ' '), 150, true);
389391
video.thumbnail = d[0].thumbnail_medium;
390392
video.views = d[0].stats_number_of_plays;
391393
video.likes = d[0].stats_number_of_likes;
@@ -435,7 +437,7 @@
435437

436438
//Remove duplicate urls and save to the variable removedDuplicates
437439

438-
matchArray = matchArray.getUnique();
440+
matchArray = utils.getUnique(matchArray);
439441

440442
var _this = this;
441443

@@ -564,7 +566,7 @@
564566
var matches;
565567
while ((matches = docRegex.exec(rawStr)) !== null) {
566568

567-
var template = '<div class="ejs-doc"><div class="ejs-doc-preview"><div class="ejs-doc-icon"><i class="fa fa-file-o"></i></div><div class="ejs-doc-detail" ><div class="ejs-doc-title"> <a href="">' + matches[0].toUrl() + '</a></div> <div class="ejs-doc-view"> <a href="' + matches[0].toUrl() + '" target="_blank"><button>' + opts.docOptions.downloadText + '</button></a> <button class="ejs-doc-view-active">' + opts.docOptions.viewText + '</button></div> </div> </div></div>';
569+
var template = '<div class="ejs-doc"><div class="ejs-doc-preview"><div class="ejs-doc-icon"><i class="fa fa-file-o"></i></div><div class="ejs-doc-detail" ><div class="ejs-doc-title"> <a href="">' + utils.toUrl(matches[0]) + '</a></div> <div class="ejs-doc-view"> <a href="' + utils.toUrl(matches[0]) + '" target="_blank"><button>' + opts.docOptions.downloadText + '</button></a> <button class="ejs-doc-view-active">' + opts.docOptions.viewText + '</button></div> </div> </div></div>';
568570
embedArray.push(createObject(matches.index, template));
569571

570572
}
@@ -580,7 +582,7 @@
580582

581583
var docParent = $(self).closest('.ejs-doc');
582584
var docUrl = $(docParent).find('a')[1].href;
583-
var docViewTemplate = ' <div class="ejs-doc-viewer"><iframe src="http://docs.google.com/viewer?embedded=true&url=' + docUrl.toUrl() + '" frameBorder="0" style="border: none;margin : 0 auto; display : block;"></iframe></div>';
585+
var docViewTemplate = ' <div class="ejs-doc-viewer"><iframe src="http://docs.google.com/viewer?embedded=true&url=' + utils.toUrl(docUrl) + '" frameBorder="0" style="border: none;margin : 0 auto; display : block;"></iframe></div>';
584586
docParent.html(docViewTemplate);
585587

586588
//calling the function after the document is shown.
@@ -685,7 +687,7 @@
685687
var matches;
686688
while ((matches = flickrRegex.exec(rawStr)) !== null) {
687689

688-
var template = '<div class="ejs-embed"><div class="ne-image-wrapper"><iframe src="' + matches[0].toUrl() + '/player/" width="' + dimensions.width + '" height="' + dimensions.height + '"></iframe></div></div>';
690+
var template = '<div class="ejs-embed"><div class="ne-image-wrapper"><iframe src="' + utils.toUrl(matches[0]) + '/player/" width="' + dimensions.width + '" height="' + dimensions.height + '"></iframe></div></div>';
689691
embedArray.push(createObject(matches.index, template));
690692

691693
}
@@ -697,7 +699,7 @@
697699
var matches;
698700
while ((matches = instagramRegex.exec(rawStr)) !== null) {
699701

700-
var template = '<div class="ejs-embed"><iframe src="' + matches[0].toUrl() + '/embed/" width="' + dimensions.width + '" height="' + dimensions.height + '"></iframe></div>';
702+
var template = '<div class="ejs-embed"><iframe src="' + utils.toUrl(matches[0]) + '/embed/" width="' + dimensions.width + '" height="' + dimensions.height + '"></iframe></div>';
701703
embedArray.push(createObject(matches.index, template));
702704

703705
}
@@ -774,7 +776,7 @@
774776

775777
getMatches: function (str) {
776778
var tweetRegex = /https:\/\/twitter\.com\/\w+\/\w+\/\d+/gi;
777-
var matches = str.match(tweetRegex) ? (str.match(tweetRegex)).getUnique() : null;
779+
var matches = str.match(tweetRegex) ? utils.getUnique(str.match(tweetRegex)) : null;
778780
return matches;
779781

780782
},
@@ -875,7 +877,7 @@
875877
if (rawStr.match(ggRegex)) {
876878
while ((matches = ggRegex.exec(rawStr)) !== null) {
877879
var m = matches;
878-
var match = 'https:' + matches[0].toUrl();
880+
var match = 'https:' + utils.toUrl(matches[0]);
879881
var url = 'https://noembed.com/embed?nowrap=on&url=' + match;
880882
var template = '<div class="ejs-embed ejs-github-gist" data-url="' + url + '"></div>';
881883
embedArray.push(createObject(m.index, template));
@@ -964,7 +966,7 @@
964966
$.each(embedArray, function (index, value) {
965967
embedCodeArray.push(value.embedCode);
966968
});
967-
str = str + embedCodeArray.getUnique().join(' ');
969+
str = str + utils.getUnique(embedCodeArray).join(' ');
968970
embedArray = [];
969971
embedCodeArray = [];
970972
return str;

0 commit comments

Comments
 (0)