Skip to content

Commit 37e3f7a

Browse files
committedJul 25, 2016
Fix(textAngularSetup): We now block javascript for 'insertLink', 'insertVideo', and 'insertImage' issue: #1189
1 parent 3accda4 commit 37e3f7a

File tree

1 file changed

+53
-34
lines changed

1 file changed

+53
-34
lines changed
 

‎src/textAngularSetup.js

+53-34
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,13 @@ angular.module('textAngularSetup', [])
723723
}
724724
});
725725

726+
/* istanbul ignore next: if it's javascript don't worry - though probably should show some kind of error message */
727+
var blockJavascript = function (link) {
728+
if (link.toLowerCase().indexOf('javascript')!==-1) {
729+
return true;
730+
}
731+
return false;
732+
};
726733

727734
taRegisterTool('insertImage', {
728735
iconclass: 'fa fa-picture-o',
@@ -732,22 +739,25 @@ angular.module('textAngularSetup', [])
732739
imageLink = $window.prompt(taTranslations.insertImage.dialogPrompt, 'http://');
733740
if(imageLink && imageLink !== '' && imageLink !== 'http://'){
734741
/* istanbul ignore next: don't know how to test this... since it needs a dialogPrompt */
735-
if (taSelection.getSelectionElement().tagName.toLowerCase() === 'a') {
736-
// due to differences in implementation between FireFox and Chrome, we must move the
737-
// insertion point past the <a> element, otherwise FireFox inserts inside the <a>
738-
// With this change, both FireFox and Chrome behave the same way!
739-
taSelection.setSelectionAfterElement(taSelection.getSelectionElement());
742+
// block javascript here
743+
if (!blockJavascript(imageLink)) {
744+
if (taSelection.getSelectionElement().tagName.toLowerCase() === 'a') {
745+
// due to differences in implementation between FireFox and Chrome, we must move the
746+
// insertion point past the <a> element, otherwise FireFox inserts inside the <a>
747+
// With this change, both FireFox and Chrome behave the same way!
748+
taSelection.setSelectionAfterElement(taSelection.getSelectionElement());
749+
}
750+
// In the past we used the simple statement:
751+
//return this.$editor().wrapSelection('insertImage', imageLink, true);
752+
//
753+
// However on Firefox only, when the content is empty this is a problem
754+
// See Issue #1201
755+
// Investigation reveals that Firefox only inserts a <p> only!!!!
756+
// So now we use insertHTML here and all is fine.
757+
// NOTE: this is what 'insertImage' is supposed to do anyway!
758+
var embed = '<img src="' + imageLink + '">';
759+
return this.$editor().wrapSelection('insertHTML', embed, true);
740760
}
741-
// In the past we used the simple statement:
742-
//return this.$editor().wrapSelection('insertImage', imageLink, true);
743-
//
744-
// However on Firefox only, when the content is empty this is a problem
745-
// See Issue #1201
746-
// Investigation reveals that Firefox only inserts a <p> only!!!!
747-
// So now we use insertHTML here and all is fine.
748-
// NOTE: this is what 'insertImage' is supposed to do anyway!
749-
var embed = '<img src="' + imageLink + '">';
750-
return this.$editor().wrapSelection('insertHTML', embed, true);
751761
}
752762
},
753763
onElementSelect: {
@@ -761,27 +771,32 @@ angular.module('textAngularSetup', [])
761771
action: function(){
762772
var urlPrompt;
763773
urlPrompt = $window.prompt(taTranslations.insertVideo.dialogPrompt, 'https://');
764-
if (urlPrompt && urlPrompt !== '' && urlPrompt !== 'https://') {
774+
// block javascript here
775+
/* istanbul ignore else: if it's javascript don't worry - though probably should show some kind of error message */
776+
if (!blockJavascript(urlPrompt)) {
765777

766-
videoId = taToolFunctions.extractYoutubeVideoId(urlPrompt);
778+
if (urlPrompt && urlPrompt !== '' && urlPrompt !== 'https://') {
767779

768-
/* istanbul ignore else: if it's invalid don't worry - though probably should show some kind of error message */
769-
if(videoId){
770-
// create the embed link
771-
var urlLink = "https://www.youtube.com/embed/" + videoId;
772-
// create the HTML
773-
// for all options see: http://stackoverflow.com/questions/2068344/how-do-i-get-a-youtube-video-thumbnail-from-the-youtube-api
774-
// maxresdefault.jpg seems to be undefined on some.
775-
var embed = '<img class="ta-insert-video" src="https://img.youtube.com/vi/' + videoId + '/hqdefault.jpg" ta-insert-video="' + urlLink + '" contenteditable="false" allowfullscreen="true" frameborder="0" />';
776-
/* istanbul ignore next: don't know how to test this... since it needs a dialogPrompt */
777-
if (taSelection.getSelectionElement().tagName.toLowerCase() === 'a') {
778-
// due to differences in implementation between FireFox and Chrome, we must move the
779-
// insertion point past the <a> element, otherwise FireFox inserts inside the <a>
780-
// With this change, both FireFox and Chrome behave the same way!
781-
taSelection.setSelectionAfterElement(taSelection.getSelectionElement());
780+
videoId = taToolFunctions.extractYoutubeVideoId(urlPrompt);
781+
782+
/* istanbul ignore else: if it's invalid don't worry - though probably should show some kind of error message */
783+
if (videoId) {
784+
// create the embed link
785+
var urlLink = "https://www.youtube.com/embed/" + videoId;
786+
// create the HTML
787+
// for all options see: http://stackoverflow.com/questions/2068344/how-do-i-get-a-youtube-video-thumbnail-from-the-youtube-api
788+
// maxresdefault.jpg seems to be undefined on some.
789+
var embed = '<img class="ta-insert-video" src="https://img.youtube.com/vi/' + videoId + '/hqdefault.jpg" ta-insert-video="' + urlLink + '" contenteditable="false" allowfullscreen="true" frameborder="0" />';
790+
/* istanbul ignore next: don't know how to test this... since it needs a dialogPrompt */
791+
if (taSelection.getSelectionElement().tagName.toLowerCase() === 'a') {
792+
// due to differences in implementation between FireFox and Chrome, we must move the
793+
// insertion point past the <a> element, otherwise FireFox inserts inside the <a>
794+
// With this change, both FireFox and Chrome behave the same way!
795+
taSelection.setSelectionAfterElement(taSelection.getSelectionElement());
796+
}
797+
// insert
798+
return this.$editor().wrapSelection('insertHTML', embed, true);
782799
}
783-
// insert
784-
return this.$editor().wrapSelection('insertHTML', embed, true);
785800
}
786801
}
787802
},
@@ -798,7 +813,11 @@ angular.module('textAngularSetup', [])
798813
var urlLink;
799814
urlLink = $window.prompt(taTranslations.insertLink.dialogPrompt, 'http://');
800815
if(urlLink && urlLink !== '' && urlLink !== 'http://'){
801-
return this.$editor().wrapSelection('createLink', urlLink, true);
816+
// block javascript here
817+
/* istanbul ignore else: if it's javascript don't worry - though probably should show some kind of error message */
818+
if (!blockJavascript(urlLink)) {
819+
return this.$editor().wrapSelection('createLink', urlLink, true);
820+
}
802821
}
803822
},
804823
activeState: function(commonElement){

0 commit comments

Comments
 (0)