diff --git a/markup.js b/markup.js
index df50ec1ed..c5228b83d 100644
--- a/markup.js
+++ b/markup.js
@@ -168,11 +168,10 @@ class Markup {
}
static formatHTML (text = '', entities = []) {
- const chars = [...text]
const available = [...entities]
const opened = []
const result = []
- for (let offset = 0; offset < chars.length; offset++) {
+ for (let offset = 0; offset < text.length; offset++) {
while (true) {
const index = available.findIndex((entity) => entity.offset === offset)
if (index === -1) {
@@ -213,7 +212,7 @@ class Markup {
available.splice(index, 1)
}
- result.push(chars[offset])
+ result.push(escapeHTML(text[offset]))
while (true) {
const index = opened.findIndex((entity) => entity.offset + entity.length - 1 === offset)
@@ -256,6 +255,18 @@ class Markup {
}
}
+const escapedChars = {
+ '"': '"',
+ '&': '&',
+ '<': '<',
+ '>': '>'
+}
+
+function escapeHTML (string) {
+ const chars = [...string]
+ return chars.map(char => escapedChars[char] || char).join('')
+}
+
function buildKeyboard (buttons, options) {
const result = []
if (!Array.isArray(buttons)) {
diff --git a/test/markup.js b/test/markup.js
index 3304fad59..cbc71eb04 100644
--- a/test/markup.js
+++ b/test/markup.js
@@ -222,7 +222,7 @@ test('should generate nested multi markup', (t) => {
t.deepEqual(markup, 'strikeboldunder')
})
-test.only('should generate nested multi markup 2', (t) => {
+test('should generate nested multi markup 2', (t) => {
const markup = Markup.formatHTML('×11 22 333× ×С123456× ×1 22 333×', [
{
offset: 1,
@@ -252,3 +252,35 @@ test.only('should generate nested multi markup 2', (t) => {
])
t.deepEqual(markup, '×11 22 333× ×С123456× ×1 22 333×')
})
+
+test('should generate correct markup with emojis', (t) => {
+ const markup = Markup.formatHTML('bold🙂👨👩👧👧 italic', [
+ {
+ offset: 0,
+ length: 6,
+ type: 'bold'
+ },
+ {
+ offset: 18,
+ length: 6,
+ type: 'italic'
+ }
+ ])
+ t.deepEqual(markup, 'bold🙂👨👩👧👧 italic')
+})
+
+test('should generate correct markup with HTML tags', (t) => {
+ const markup = Markup.formatHTML('bold italic', [
+ {
+ offset: 3,
+ length: 4,
+ type: 'bold'
+ },
+ {
+ offset: 15,
+ length: 6,
+ type: 'italic'
+ }
+ ])
+ t.deepEqual(markup, '<b>bold</b> <i>italic</i>')
+})
\ No newline at end of file