From da19fd0d1aa526a1136af4389feb8f862952329d Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Wed, 15 Oct 2014 14:42:14 +0100 Subject: Add unsanitizedLinky filter to fix links in formatted messages. This filter is identical to ngSanitize's linky but instead of sanitizing text which isn't linkified in the addText function, it doesn't. --- webclient/app-filter.js | 59 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 4 deletions(-) (limited to 'webclient/app-filter.js') diff --git a/webclient/app-filter.js b/webclient/app-filter.js index fc16492ef3..654783cb60 100644 --- a/webclient/app-filter.js +++ b/webclient/app-filter.js @@ -1,12 +1,12 @@ /* Copyright 2014 OpenMarket Ltd - + Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - + http://www.apache.org/licenses/LICENSE-2.0 - + Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -80,4 +80,55 @@ angular.module('matrixWebClient') return function(text) { return $sce.trustAsHtml(text); }; -}]); \ No newline at end of file +}]) +// Exactly the same as ngSanitize's linky but instead of pushing sanitized +// text in the addText function, we just push the raw text. This is ONLY SAFE +// IF THIS IS USED IN CONJUNCTION WITH NG-BIND-HTML which sweeps with $sanitize +// already. +.filter('unsanitizedLinky', ['$sanitize', function($sanitize) { + var LINKY_URL_REGEXP = + /((ftp|https?):\/\/|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>"]/, + MAILTO_REGEXP = /^mailto:/; + + return function(text, target) { + if (!text) return text; + var match; + var raw = text; + var html = []; + var url; + var i; + while ((match = raw.match(LINKY_URL_REGEXP))) { + // We can not end in these as they are sometimes found at the end of the sentence + url = match[0]; + // if we did not match ftp/http/mailto then assume mailto + if (match[2] == match[3]) url = 'mailto:' + url; + i = match.index; + addText(raw.substr(0, i)); + addLink(url, match[0].replace(MAILTO_REGEXP, '')); + raw = raw.substring(i + match[0].length); + } + addText(raw); + return $sanitize(html.join('')); + + function addText(text) { + if (!text) { + return; + } + html.push(text); + } + + function addLink(url, text) { + html.push(''); + addText(text); + html.push(''); + } + }; +}]); -- cgit 1.5.1 From 79bd6e77b8901b47539996c72d22b75d69585383 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Wed, 15 Oct 2014 14:45:38 +0100 Subject: Remove warning since the end result is still $sanitize'd --- webclient/app-filter.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'webclient/app-filter.js') diff --git a/webclient/app-filter.js b/webclient/app-filter.js index 654783cb60..39ea1d637d 100644 --- a/webclient/app-filter.js +++ b/webclient/app-filter.js @@ -82,9 +82,7 @@ angular.module('matrixWebClient') }; }]) // Exactly the same as ngSanitize's linky but instead of pushing sanitized -// text in the addText function, we just push the raw text. This is ONLY SAFE -// IF THIS IS USED IN CONJUNCTION WITH NG-BIND-HTML which sweeps with $sanitize -// already. +// text in the addText function, we just push the raw text. .filter('unsanitizedLinky', ['$sanitize', function($sanitize) { var LINKY_URL_REGEXP = /((ftp|https?):\/\/|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>"]/, -- cgit 1.5.1