-
Notifications
You must be signed in to change notification settings - Fork 454
/
MessageText.vue
178 lines (170 loc) · 5.75 KB
/
MessageText.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<template>
<div
v-if="message.text && (message.type === 'human' || message.type === 'feedback')"
class="message-text"
>
<span class="sr-only">I say: </span>{{ message.text }}
</div>
<div
v-else-if="altHtmlMessage && AllowSuperDangerousHTMLInMessage"
v-html="altHtmlMessage"
class="message-text"
></div>
<div
v-else-if="message.text && shouldRenderAsHtml"
v-html="botMessageAsHtml"
class="message-text"
></div>
<div
v-else-if="message.text && (message.type === 'bot' || message.type === 'agent')"
class="message-text bot-message-plain"
>
<span class="sr-only">{{ message.type }} says: </span>{{ (shouldStripTags) ? stripTagsFromMessage(message.text) : message.text }}
</div>
</template>
<script>
/*
Copyright 2017-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Amazon Software License (the "License"). You may not use this file
except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/asl/
or in the "license" file accompanying this file. This file is distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied. See the
License for the specific language governing permissions and limitations under the License.
*/
const marked = require('marked');
const renderer = {};
renderer.link = function link(href, title, text) {
return `<a href="${href}" title="${title}" target="_blank">${text}</a>`;
};
marked.use({renderer});
export default {
name: 'message-text',
props: ['message'],
computed: {
shouldConvertUrlToLinks() {
return this.$store.state.config.ui.convertUrlToLinksInBotMessages;
},
shouldStripTags() {
return this.$store.state.config.ui.stripTagsFromBotMessages;
},
AllowSuperDangerousHTMLInMessage() {
return this.$store.state.config.ui.AllowSuperDangerousHTMLInMessage;
},
altHtmlMessage() {
let out = false;
if (this.message.alts) {
if (this.message.alts.html) {
out = this.message.alts.html;
} else if (this.message.alts.markdown) {
out = marked.parse(this.message.alts.markdown);
}
}
if (out) out = this.prependBotScreenReader(out);
return out;
},
shouldRenderAsHtml() {
return (['bot', 'agent'].includes(this.message.type) && this.shouldConvertUrlToLinks);
},
botMessageAsHtml() {
// Security Note: Make sure that the content is escaped according
// to context (e.g. URL, HTML). This is rendered as HTML
const messageText = this.stripTagsFromMessage(this.message.text);
const messageWithLinks = this.botMessageWithLinks(messageText);
const messageWithSR = this.prependBotScreenReader(messageWithLinks);
return messageWithSR;
},
},
methods: {
encodeAsHtml(value) {
return value
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>');
},
botMessageWithLinks(messageText) {
const linkReplacers = [
// The regex in the objects of linkReplacers should return a single
// reference (from parenthesis) with the whole address
// The replace function takes a matched url and returns the
// hyperlink that will be replaced in the message
{
type: 'web',
regex: new RegExp(
'\\b((?:https?://\\w{1}|www\\.)(?:[\\w-.]){2,256}' +
'(?:[\\w._~:/?#@!$&()*+,;=[\'\\]-]){0,256})',
'im',
),
replace: (item) => {
const url = (!/^https?:\/\//.test(item)) ? `http://${item}` : item;
return '<a target="_blank" ' +
`href="${encodeURI(url)}">${this.encodeAsHtml(item)}</a>`;
},
},
];
// TODO avoid double HTML encoding when there's more than 1 linkReplacer
return linkReplacers
.reduce(
(message, replacer) =>
// splits the message into an array containing content chunks
// and links. Content chunks will be the even indexed items in the
// array (or empty string when applicable).
// Links (if any) will be the odd members of the array since the
// regex keeps references.
message.split(replacer.regex)
.reduce(
(messageAccum, item, index, array) => {
let messageResult = '';
if ((index % 2) === 0) {
const urlItem = ((index + 1) === array.length) ?
'' : replacer.replace(array[index + 1]);
messageResult = `${this.encodeAsHtml(item)}${urlItem}`;
}
return messageAccum + messageResult;
},
'',
),
messageText,
);
},
// used for stripping SSML (and other) tags from bot responses
stripTagsFromMessage(messageText) {
const doc = document.implementation.createHTMLDocument('').body;
doc.innerHTML = messageText;
return doc.textContent || doc.innerText || '';
},
prependBotScreenReader(messageText) {
return `<span class="sr-only">bot says: </span>${messageText}`;
},
},
};
</script>
<style scoped>
.message-text {
hyphens: auto;
overflow-wrap: break-word;
padding: 0.8em;
white-space: normal;
word-break: break-word;
width: 100%;
}
.message-text :deep(p) {
margin-bottom: 16px;
}
</style>
<style>
.sr-only {
position: absolute !important;
width: 1px !important;
height: 1px !important;
padding: 0 !important;
margin: -1px !important;
overflow: hidden !important;
clip: rect(1px, 1px, 1px, 1px) !important;
clip-path: inset(50%) !important;
white-space: nowrap !important;
border: 0 !important;
}
</style>