From 811828c0b82bb702ff01eabc741e9b64eb6d3881 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9rome=20Maes?= Date: Mon, 5 Oct 2015 18:20:46 +0200 Subject: [PATCH] [FIX] mail : clickable url in mail.message from channel, chat and chatter html_escape_keep_url is now included in plaintext2html method (since the opposite method html2plaintext transform link tags into simple url text). Force the posted message to be plaintext (adding directly html is not safe), and trim the posted message before submitting it. The url are now clickable again in chat message, but also in chatter messages ! --- addons/mail/static/src/js/chat_manager.js | 6 ++-- addons/mail/static/src/js/chatter.js | 2 +- openerp/tools/mail.py | 34 ++++++++++------------- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/addons/mail/static/src/js/chat_manager.js b/addons/mail/static/src/js/chat_manager.js index 2f2542e7e3f..245f0af69c9 100644 --- a/addons/mail/static/src/js/chat_manager.js +++ b/addons/mail/static/src/js/chat_manager.js @@ -119,9 +119,9 @@ function add_channel_to_message (message, channel_id) { function post_channel_message (data) { return ChannelModel.call('message_post', [data.channel_id], { message_type: 'comment', - content_subtype: 'html', + content_subtype: 'plaintext', partner_ids: data.partner_ids, - body: data.content, + body: _.str.trim(data.content), subtype: 'mail.mt_comment', attachment_ids: data.attachment_ids, }); @@ -130,7 +130,7 @@ function post_channel_message (data) { function post_document_message (model_name, res_id, data) { var values = { attachment_ids: data.attachment_ids, - body: data.content, + body: _.str.trim(data.content), content_subtype: data.content_subtype, message_type: data.message_type, partner_ids: data.partner_ids, diff --git a/addons/mail/static/src/js/chatter.js b/addons/mail/static/src/js/chatter.js index 3415789917d..c0d7d8afba6 100644 --- a/addons/mail/static/src/js/chatter.js +++ b/addons/mail/static/src/js/chatter.js @@ -484,7 +484,7 @@ var ChatterComposer = ChatComposer.extend({ subtype_id: false, subtype: 'mail.mt_comment', message_type: 'comment', - content_subtype: 'html', + content_subtype: 'plaintext', }); // Subtype diff --git a/openerp/tools/mail.py b/openerp/tools/mail.py index 45b16dd811f..09010efe0e7 100644 --- a/openerp/tools/mail.py +++ b/openerp/tools/mail.py @@ -525,29 +525,12 @@ def html2plaintext(html, body_id=None, encoding='utf-8'): return html -def html_escape_keep_url(self, message): - """ Escape the message and transform the url into clickable link - :param string message: the message to escape url and transform them into clickable link - :returns the escaped message - :rtype : string - """ - safe_message = "" - first = 0 - last = 0 - for m in re.finditer('(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?', message): - last = m.start() - safe_message += cgi.escape(message[first:last]) - safe_message += '%s' % (cgi.escape(m.group(0)), m.group(0)) - first = m.end() - last = m.end() - safe_message += cgi.escape(message[last:]) - return safe_message - def plaintext2html(text, container_tag=False): """ Convert plaintext into html. Content of the text is escaped to manage html entities, using cgi.escape(). - all \n,\r are replaced by
- enclose content into

+ - convert url into clickable link - 2 or more consecutive
are considered as paragraph breaks :param string container_tag: container of the html; by default the @@ -559,7 +542,18 @@ def plaintext2html(text, container_tag=False): text = text.replace('\n', '
') text = text.replace('\r', '
') - # 2-3: form paragraphs + # 2. clickable links + idx = 0 + final = '' + link_tags = re.compile(r'(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?') + for item in re.finditer(link_tags, text): + final += text[idx:item.start()] + final += '%s' % (item.group(0), item.group(0)) + idx = item.end() + final += text[idx:] + text = final + + # 3-4: form paragraphs idx = 0 final = '

' br_tags = re.compile(r'(([<]\s*[bB][rR]\s*\/?[>]\s*){2,})') @@ -568,7 +562,7 @@ def plaintext2html(text, container_tag=False): idx = item.end() final += text[idx:] + '

' - # 4. container + # 5. container if container_tag: final = '<%s>%s' % (container_tag, final, container_tag) return ustr(final)