Browse Source

文本消息,禁止解析 emoji shortcodes

imndx 2 years ago
parent
commit
c6da3a85d3

+ 2 - 2
emoji/emoji.js

@@ -1,5 +1,5 @@
 import anchorme from "anchorme";
-import uEmojiParser from './universal-emoji-parser'
+import twemoji from "twemoji";
 
 const emoji = [];
 
@@ -25,7 +25,7 @@ function parser(text) {
     //     text = decodeText = text.split(`${e}`).join(`<a target="_blank" class="${className}"></a>`);
     // });
 
-    let emoji = uEmojiParser.parse(decodeText);
+    let emoji = twemoji.parse(decodeText);
     // emoji = emoji.replace(/src="https:\/\/twemoji\.maxcdn\.com\/v\/[0-9.]+\//g, 'src="' + twemoji_base_url)
     return emoji;
 }

+ 2 - 2
pages/conversation/ConversationPage.vue

@@ -700,11 +700,11 @@ export default {
     computed: {
         conversationTitle() {
             let info = this.sharedConversationState.currentConversationInfo;
-            return info.conversation._target._displayName;
+            return !info ? '' : info.conversation._target._displayName;
         },
         targetUserOnlineStateDesc() {
             let info = this.sharedConversationState.currentConversationInfo;
-            return info.conversation._targetOnlineStateDesc;
+            return info ? info.conversation._targetOnlineStateDesc : null;
         },
         loadingIdentifier() {
             let conversation = this.sharedConversationState.currentConversationInfo.conversation;

+ 21 - 16
pages/conversation/message/content/TextMessageContentView.vue

@@ -7,7 +7,7 @@
 
 <script>
 import Message from "@/wfc/messages/message";
-import {parser} from "@/emoji/emoji";
+import {parser as emojiParse} from "@/emoji/emoji";
 
 export default {
     name: "TextMessageContentView",
@@ -26,27 +26,32 @@ export default {
     },
 
     methods: {
+        escapeHtml(text) {
+            return text.replace(/&/g, '&amp;')
+                .replace(/</g, '&lt;')
+                .replace(/>/g, '&gt;')
+                .replace(/ /g, '&nbsp;')
+                .replace(/<script/gi, "&lt;script")
+                .replace(/<iframe/gi, "&lt;iframe");
+        }
     },
 
     computed: {
         textContent() {
-            try {
-                let tmp = this.message.messageContent.digest(this.message);
-                // let tmp = this.message.messageContent.digest(this.message);
-                // pls refer to https://stackoverflow.com/questions/4522124/replace-leading-spaces-with-nbsp-in-javascript
-                tmp = tmp.replace(/^[ \t]+/gm, function (x) {
-                    return new Array(x.length + 1).join('&nbsp;')
-                })
-                tmp = tmp.replace(/<script/gi, "&lt;script");
-                tmp = tmp.replace(/<iframe/gi, "&lt;iframe");
-                if (tmp.indexOf('<img') >= 0) {
-                    tmp = tmp.replace(/<img/g, '<img style="max-width:400px;"')
-                    return tmp;
+            let content = this.message.messageContent.digest(this.message);
+            let lines = content.split('\n');
+            if (lines.length > 1) {
+                content = lines.map(line => `<span>${this.escapeHtml(line)}</span>\n`).reduce((total, cv, ci, arr) => total + cv, '');
+            } else {
+               content = this.escapeHtml(content)
                 }
-                return tmp;
-            }catch (e) {
-				return this.message.messageContent.digest(this.message);
+            content = emojiParse(content);
+            // tmp = marked.parse(tmp);
+            if (content.indexOf('<img') >= 0) {
+                content = content.replace(/<img/g, '<img style="max-width:400px;"')
+                return content;
             }
+            return content;
         }
     }
 }

+ 1 - 1
pages/conversationList/ConversationListPage.vue

@@ -159,7 +159,7 @@ export default {
                     break;
 
             }
-        },
+        }
     },
     activated() {
         this.scrollActiveElementCenter();

+ 1 - 1
pages/search/SearchPortalPage.vue

@@ -72,4 +72,4 @@ export default {
     color: grey;
 }
 
-</style>
+</style>

+ 0 - 1
store.js

@@ -1180,7 +1180,6 @@ let store = {
         // TODO
         // _from
         // _showTime
-        console.log('__patchMessage', m)
         if (m.conversation.type === ConversationType.Single) {
             m._from = userInfoMap ? userInfoMap.get(m.from) : wfc.getUserInfo(m.from, false, '');
         }