@@ 736-803 (lines=68) @@ | ||
733 | </file> |
|
734 | </example> |
|
735 | */ |
|
736 | angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) { |
|
737 | var LINKY_URL_REGEXP = |
|
738 | /((ftp|https?):\/\/|(www\.)|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>"\u201d\u2019]/i, |
|
739 | MAILTO_REGEXP = /^mailto:/i; |
|
740 | ||
741 | var linkyMinErr = angular.$$minErr('linky'); |
|
742 | var isDefined = angular.isDefined; |
|
743 | var isFunction = angular.isFunction; |
|
744 | var isObject = angular.isObject; |
|
745 | var isString = angular.isString; |
|
746 | ||
747 | return function(text, target, attributes) { |
|
748 | if (text == null || text === '') return text; |
|
749 | if (!isString(text)) throw linkyMinErr('notstring', 'Expected string but received: {0}', text); |
|
750 | ||
751 | var attributesFn = |
|
752 | isFunction(attributes) ? attributes : |
|
753 | isObject(attributes) ? function getAttributesObject() {return attributes;} : |
|
754 | function getEmptyAttributesObject() {return {};}; |
|
755 | ||
756 | var match; |
|
757 | var raw = text; |
|
758 | var html = []; |
|
759 | var url; |
|
760 | var i; |
|
761 | while ((match = raw.match(LINKY_URL_REGEXP))) { |
|
762 | // We can not end in these as they are sometimes found at the end of the sentence |
|
763 | url = match[0]; |
|
764 | // if we did not match ftp/http/www/mailto then assume mailto |
|
765 | if (!match[2] && !match[4]) { |
|
766 | url = (match[3] ? 'http://' : 'mailto:') + url; |
|
767 | } |
|
768 | i = match.index; |
|
769 | addText(raw.substr(0, i)); |
|
770 | addLink(url, match[0].replace(MAILTO_REGEXP, '')); |
|
771 | raw = raw.substring(i + match[0].length); |
|
772 | } |
|
773 | addText(raw); |
|
774 | return $sanitize(html.join('')); |
|
775 | ||
776 | function addText(text) { |
|
777 | if (!text) { |
|
778 | return; |
|
779 | } |
|
780 | html.push(sanitizeText(text)); |
|
781 | } |
|
782 | ||
783 | function addLink(url, text) { |
|
784 | var key, linkAttributes = attributesFn(url); |
|
785 | html.push('<a '); |
|
786 | ||
787 | for (key in linkAttributes) { |
|
788 | html.push(key + '="' + linkAttributes[key] + '" '); |
|
789 | } |
|
790 | ||
791 | if (isDefined(target) && !('target' in linkAttributes)) { |
|
792 | html.push('target="', |
|
793 | target, |
|
794 | '" '); |
|
795 | } |
|
796 | html.push('href="', |
|
797 | url.replace(/"/g, '"'), |
|
798 | '">'); |
|
799 | addText(text); |
|
800 | html.push('</a>'); |
|
801 | } |
|
802 | }; |
|
803 | }]); |
|
804 | ||
805 | ||
806 | })(window, window.angular); |
@@ 686-753 (lines=68) @@ | ||
683 | </file> |
|
684 | </example> |
|
685 | */ |
|
686 | angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) { |
|
687 | var LINKY_URL_REGEXP = |
|
688 | /((ftp|https?):\/\/|(www\.)|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>"\u201d\u2019]/i, |
|
689 | MAILTO_REGEXP = /^mailto:/i; |
|
690 | ||
691 | var linkyMinErr = angular.$$minErr('linky'); |
|
692 | var isDefined = angular.isDefined; |
|
693 | var isFunction = angular.isFunction; |
|
694 | var isObject = angular.isObject; |
|
695 | var isString = angular.isString; |
|
696 | ||
697 | return function(text, target, attributes) { |
|
698 | if (text == null || text === '') return text; |
|
699 | if (!isString(text)) throw linkyMinErr('notstring', 'Expected string but received: {0}', text); |
|
700 | ||
701 | var attributesFn = |
|
702 | isFunction(attributes) ? attributes : |
|
703 | isObject(attributes) ? function getAttributesObject() {return attributes;} : |
|
704 | function getEmptyAttributesObject() {return {};}; |
|
705 | ||
706 | var match; |
|
707 | var raw = text; |
|
708 | var html = []; |
|
709 | var url; |
|
710 | var i; |
|
711 | while ((match = raw.match(LINKY_URL_REGEXP))) { |
|
712 | // We can not end in these as they are sometimes found at the end of the sentence |
|
713 | url = match[0]; |
|
714 | // if we did not match ftp/http/www/mailto then assume mailto |
|
715 | if (!match[2] && !match[4]) { |
|
716 | url = (match[3] ? 'http://' : 'mailto:') + url; |
|
717 | } |
|
718 | i = match.index; |
|
719 | addText(raw.substr(0, i)); |
|
720 | addLink(url, match[0].replace(MAILTO_REGEXP, '')); |
|
721 | raw = raw.substring(i + match[0].length); |
|
722 | } |
|
723 | addText(raw); |
|
724 | return $sanitize(html.join('')); |
|
725 | ||
726 | function addText(text) { |
|
727 | if (!text) { |
|
728 | return; |
|
729 | } |
|
730 | html.push(sanitizeText(text)); |
|
731 | } |
|
732 | ||
733 | function addLink(url, text) { |
|
734 | var key, linkAttributes = attributesFn(url); |
|
735 | html.push('<a '); |
|
736 | ||
737 | for (key in linkAttributes) { |
|
738 | html.push(key + '="' + linkAttributes[key] + '" '); |
|
739 | } |
|
740 | ||
741 | if (isDefined(target) && !('target' in linkAttributes)) { |
|
742 | html.push('target="', |
|
743 | target, |
|
744 | '" '); |
|
745 | } |
|
746 | html.push('href="', |
|
747 | url.replace(/"/g, '"'), |
|
748 | '">'); |
|
749 | addText(text); |
|
750 | html.push('</a>'); |
|
751 | } |
|
752 | }; |
|
753 | }]); |
|
754 | ||
755 | ||
756 | })(window, window.angular); |