biel-search 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/biel-search/biel-search.css +1 -0
- package/dist/biel-search/biel-search.esm.js +2 -0
- package/dist/biel-search/biel-search.esm.js.map +1 -0
- package/dist/biel-search/index.esm.js +2 -0
- package/dist/biel-search/index.esm.js.map +1 -0
- package/dist/biel-search/p-1b3c90ca.js +3 -0
- package/dist/biel-search/p-1b3c90ca.js.map +1 -0
- package/dist/biel-search/p-e1255160.js +2 -0
- package/dist/biel-search/p-e1255160.js.map +1 -0
- package/dist/biel-search/p-fe900c87.entry.js +3 -0
- package/dist/biel-search/p-fe900c87.entry.js.map +1 -0
- package/dist/cjs/app-globals-3a1e7e63.js +7 -0
- package/dist/cjs/app-globals-3a1e7e63.js.map +1 -0
- package/dist/cjs/biel-bot_8.cjs.entry.js +63952 -0
- package/dist/cjs/biel-bot_8.cjs.entry.js.map +1 -0
- package/dist/cjs/biel-search.cjs.js +27 -0
- package/dist/cjs/biel-search.cjs.js.map +1 -0
- package/dist/cjs/index-05fc01fd.js +1281 -0
- package/dist/cjs/index-05fc01fd.js.map +1 -0
- package/dist/cjs/index.cjs.js +11 -0
- package/dist/cjs/index.cjs.js.map +1 -0
- package/dist/cjs/loader.cjs.js +17 -0
- package/dist/cjs/loader.cjs.js.map +1 -0
- package/dist/collection/collection-manifest.json +19 -0
- package/dist/collection/components/biel-bot/biel-bot.css +189 -0
- package/dist/collection/components/biel-bot/biel-bot.js +338 -0
- package/dist/collection/components/biel-bot/biel-bot.js.map +1 -0
- package/dist/collection/components/biel-button/biel-button.css +63 -0
- package/dist/collection/components/biel-button/biel-button.js +301 -0
- package/dist/collection/components/biel-button/biel-button.js.map +1 -0
- package/dist/collection/components/biel-chat/biel-chat.css +75 -0
- package/dist/collection/components/biel-chat/biel-chat.js +119 -0
- package/dist/collection/components/biel-chat/biel-chat.js.map +1 -0
- package/dist/collection/components/biel-header/biel-header.css +49 -0
- package/dist/collection/components/biel-header/biel-header.js +165 -0
- package/dist/collection/components/biel-header/biel-header.js.map +1 -0
- package/dist/collection/components/biel-input/biel-input.css +55 -0
- package/dist/collection/components/biel-input/biel-input.js +164 -0
- package/dist/collection/components/biel-input/biel-input.js.map +1 -0
- package/dist/collection/components/biel-message/biel-message.css +38 -0
- package/dist/collection/components/biel-message/biel-message.js +98 -0
- package/dist/collection/components/biel-message/biel-message.js.map +1 -0
- package/dist/collection/components/biel-table/biel-table.css +37 -0
- package/dist/collection/components/biel-table/biel-table.js +43 -0
- package/dist/collection/components/biel-table/biel-table.js.map +1 -0
- package/dist/collection/components/code-snippet/code-snippet.css +162 -0
- package/dist/collection/components/code-snippet/code-snippet.js +101 -0
- package/dist/collection/components/code-snippet/code-snippet.js.map +1 -0
- package/dist/collection/index.js +11 -0
- package/dist/collection/index.js.map +1 -0
- package/dist/collection/services/biel-service.js +33 -0
- package/dist/collection/services/biel-service.js.map +1 -0
- package/dist/collection/services/biel-websocket-service.js +64 -0
- package/dist/collection/services/biel-websocket-service.js.map +1 -0
- package/dist/collection/utils/utils.js +4 -0
- package/dist/collection/utils/utils.js.map +1 -0
- package/dist/components/biel-bot.d.ts +11 -0
- package/dist/components/biel-bot.js +8 -0
- package/dist/components/biel-bot.js.map +1 -0
- package/dist/components/biel-button.d.ts +11 -0
- package/dist/components/biel-button.js +152 -0
- package/dist/components/biel-button.js.map +1 -0
- package/dist/components/biel-chat.d.ts +11 -0
- package/dist/components/biel-chat.js +8 -0
- package/dist/components/biel-chat.js.map +1 -0
- package/dist/components/biel-header.d.ts +11 -0
- package/dist/components/biel-header.js +8 -0
- package/dist/components/biel-header.js.map +1 -0
- package/dist/components/biel-input.d.ts +11 -0
- package/dist/components/biel-input.js +8 -0
- package/dist/components/biel-input.js.map +1 -0
- package/dist/components/biel-message.d.ts +11 -0
- package/dist/components/biel-message.js +8 -0
- package/dist/components/biel-message.js.map +1 -0
- package/dist/components/biel-table.d.ts +11 -0
- package/dist/components/biel-table.js +8 -0
- package/dist/components/biel-table.js.map +1 -0
- package/dist/components/code-snippet.d.ts +11 -0
- package/dist/components/code-snippet.js +8 -0
- package/dist/components/code-snippet.js.map +1 -0
- package/dist/components/index.d.ts +33 -0
- package/dist/components/index.js +9 -0
- package/dist/components/index.js.map +1 -0
- package/dist/components/p-09743aa7.js +88 -0
- package/dist/components/p-09743aa7.js.map +1 -0
- package/dist/components/p-5a2466bd.js +59456 -0
- package/dist/components/p-5a2466bd.js.map +1 -0
- package/dist/components/p-7b8b1ac3.js +1098 -0
- package/dist/components/p-7b8b1ac3.js.map +1 -0
- package/dist/components/p-8e08748d.js +4158 -0
- package/dist/components/p-8e08748d.js.map +1 -0
- package/dist/components/p-b920c67f.js +239 -0
- package/dist/components/p-b920c67f.js.map +1 -0
- package/dist/components/p-c94c3bd6.js +59 -0
- package/dist/components/p-c94c3bd6.js.map +1 -0
- package/dist/components/p-f4016e6d.js +37 -0
- package/dist/components/p-f4016e6d.js.map +1 -0
- package/dist/components/p-fe8bcdeb.js +86 -0
- package/dist/components/p-fe8bcdeb.js.map +1 -0
- package/dist/esm/app-globals-0f993ce5.js +5 -0
- package/dist/esm/app-globals-0f993ce5.js.map +1 -0
- package/dist/esm/biel-bot_8.entry.js +63941 -0
- package/dist/esm/biel-bot_8.entry.js.map +1 -0
- package/dist/esm/biel-search.js +22 -0
- package/dist/esm/biel-search.js.map +1 -0
- package/dist/esm/index-6ad604ca.js +1252 -0
- package/dist/esm/index-6ad604ca.js.map +1 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/loader.js +13 -0
- package/dist/esm/loader.js.map +1 -0
- package/dist/index.cjs.js +1 -0
- package/dist/index.js +1 -0
- package/dist/types/components/biel-bot/biel-bot.d.ts +35 -0
- package/dist/types/components/biel-button/biel-button.d.ts +23 -0
- package/dist/types/components/biel-chat/biel-chat.d.ts +15 -0
- package/dist/types/components/biel-header/biel-header.d.ts +15 -0
- package/dist/types/components/biel-input/biel-input.d.ts +17 -0
- package/dist/types/components/biel-message/biel-message.d.ts +10 -0
- package/dist/types/components/biel-table/biel-table.d.ts +4 -0
- package/dist/types/components/code-snippet/code-snippet.d.ts +10 -0
- package/dist/types/components.d.ts +248 -0
- package/dist/types/index.d.ts +11 -0
- package/dist/types/services/biel-service.d.ts +11 -0
- package/dist/types/services/biel-websocket-service.d.ts +17 -0
- package/dist/types/stencil-public-runtime.d.ts +1674 -0
- package/dist/types/utils/utils.d.ts +1 -0
- package/loader/cdn.js +1 -0
- package/loader/index.cjs.js +1 -0
- package/loader/index.d.ts +24 -0
- package/loader/index.es2017.js +1 -0
- package/loader/index.js +2 -0
- package/loader/package.json +11 -0
- package/package.json +55 -0
- package/readme.md +22 -0
|
@@ -0,0 +1,4158 @@
|
|
|
1
|
+
import { p as proxyCustomElement, H, h } from './p-7b8b1ac3.js';
|
|
2
|
+
import { d as defineCustomElement$2 } from './p-f4016e6d.js';
|
|
3
|
+
import { d as defineCustomElement$1 } from './p-5a2466bd.js';
|
|
4
|
+
|
|
5
|
+
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
6
|
+
|
|
7
|
+
var marked_umd = {exports: {}};
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* marked v14.0.0 - a markdown parser
|
|
11
|
+
* Copyright (c) 2011-2024, Christopher Jeffrey. (MIT Licensed)
|
|
12
|
+
* https://github.com/markedjs/marked
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
(function (module, exports) {
|
|
16
|
+
/**
|
|
17
|
+
* DO NOT EDIT THIS FILE
|
|
18
|
+
* The code in this file is generated from files in ./src/
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
(function (global, factory) {
|
|
22
|
+
factory(exports) ;
|
|
23
|
+
})(commonjsGlobal, (function (exports) {
|
|
24
|
+
/**
|
|
25
|
+
* Gets the original marked default options.
|
|
26
|
+
*/
|
|
27
|
+
function _getDefaults() {
|
|
28
|
+
return {
|
|
29
|
+
async: false,
|
|
30
|
+
breaks: false,
|
|
31
|
+
extensions: null,
|
|
32
|
+
gfm: true,
|
|
33
|
+
hooks: null,
|
|
34
|
+
pedantic: false,
|
|
35
|
+
renderer: null,
|
|
36
|
+
silent: false,
|
|
37
|
+
tokenizer: null,
|
|
38
|
+
walkTokens: null,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
exports.defaults = _getDefaults();
|
|
42
|
+
function changeDefaults(newDefaults) {
|
|
43
|
+
exports.defaults = newDefaults;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Helpers
|
|
48
|
+
*/
|
|
49
|
+
const escapeTest = /[&<>"']/;
|
|
50
|
+
const escapeReplace = new RegExp(escapeTest.source, 'g');
|
|
51
|
+
const escapeTestNoEncode = /[<>"']|&(?!(#\d{1,7}|#[Xx][a-fA-F0-9]{1,6}|\w+);)/;
|
|
52
|
+
const escapeReplaceNoEncode = new RegExp(escapeTestNoEncode.source, 'g');
|
|
53
|
+
const escapeReplacements = {
|
|
54
|
+
'&': '&',
|
|
55
|
+
'<': '<',
|
|
56
|
+
'>': '>',
|
|
57
|
+
'"': '"',
|
|
58
|
+
"'": ''',
|
|
59
|
+
};
|
|
60
|
+
const getEscapeReplacement = (ch) => escapeReplacements[ch];
|
|
61
|
+
function escape$1(html, encode) {
|
|
62
|
+
if (encode) {
|
|
63
|
+
if (escapeTest.test(html)) {
|
|
64
|
+
return html.replace(escapeReplace, getEscapeReplacement);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
if (escapeTestNoEncode.test(html)) {
|
|
69
|
+
return html.replace(escapeReplaceNoEncode, getEscapeReplacement);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return html;
|
|
73
|
+
}
|
|
74
|
+
const caret = /(^|[^\[])\^/g;
|
|
75
|
+
function edit(regex, opt) {
|
|
76
|
+
let source = typeof regex === 'string' ? regex : regex.source;
|
|
77
|
+
opt = opt || '';
|
|
78
|
+
const obj = {
|
|
79
|
+
replace: (name, val) => {
|
|
80
|
+
let valSource = typeof val === 'string' ? val : val.source;
|
|
81
|
+
valSource = valSource.replace(caret, '$1');
|
|
82
|
+
source = source.replace(name, valSource);
|
|
83
|
+
return obj;
|
|
84
|
+
},
|
|
85
|
+
getRegex: () => {
|
|
86
|
+
return new RegExp(source, opt);
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
return obj;
|
|
90
|
+
}
|
|
91
|
+
function cleanUrl(href) {
|
|
92
|
+
try {
|
|
93
|
+
href = encodeURI(href).replace(/%25/g, '%');
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
return href;
|
|
99
|
+
}
|
|
100
|
+
const noopTest = { exec: () => null };
|
|
101
|
+
function splitCells(tableRow, count) {
|
|
102
|
+
// ensure that every cell-delimiting pipe has a space
|
|
103
|
+
// before it to distinguish it from an escaped pipe
|
|
104
|
+
const row = tableRow.replace(/\|/g, (match, offset, str) => {
|
|
105
|
+
let escaped = false;
|
|
106
|
+
let curr = offset;
|
|
107
|
+
while (--curr >= 0 && str[curr] === '\\')
|
|
108
|
+
escaped = !escaped;
|
|
109
|
+
if (escaped) {
|
|
110
|
+
// odd number of slashes means | is escaped
|
|
111
|
+
// so we leave it alone
|
|
112
|
+
return '|';
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
// add space before unescaped |
|
|
116
|
+
return ' |';
|
|
117
|
+
}
|
|
118
|
+
}), cells = row.split(/ \|/);
|
|
119
|
+
let i = 0;
|
|
120
|
+
// First/last cell in a row cannot be empty if it has no leading/trailing pipe
|
|
121
|
+
if (!cells[0].trim()) {
|
|
122
|
+
cells.shift();
|
|
123
|
+
}
|
|
124
|
+
if (cells.length > 0 && !cells[cells.length - 1].trim()) {
|
|
125
|
+
cells.pop();
|
|
126
|
+
}
|
|
127
|
+
if (count) {
|
|
128
|
+
if (cells.length > count) {
|
|
129
|
+
cells.splice(count);
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
while (cells.length < count)
|
|
133
|
+
cells.push('');
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
for (; i < cells.length; i++) {
|
|
137
|
+
// leading or trailing whitespace is ignored per the gfm spec
|
|
138
|
+
cells[i] = cells[i].trim().replace(/\\\|/g, '|');
|
|
139
|
+
}
|
|
140
|
+
return cells;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Remove trailing 'c's. Equivalent to str.replace(/c*$/, '').
|
|
144
|
+
* /c*$/ is vulnerable to REDOS.
|
|
145
|
+
*
|
|
146
|
+
* @param str
|
|
147
|
+
* @param c
|
|
148
|
+
* @param invert Remove suffix of non-c chars instead. Default falsey.
|
|
149
|
+
*/
|
|
150
|
+
function rtrim(str, c, invert) {
|
|
151
|
+
const l = str.length;
|
|
152
|
+
if (l === 0) {
|
|
153
|
+
return '';
|
|
154
|
+
}
|
|
155
|
+
// Length of suffix matching the invert condition.
|
|
156
|
+
let suffLen = 0;
|
|
157
|
+
// Step left until we fail to match the invert condition.
|
|
158
|
+
while (suffLen < l) {
|
|
159
|
+
const currChar = str.charAt(l - suffLen - 1);
|
|
160
|
+
if (currChar === c && !invert) {
|
|
161
|
+
suffLen++;
|
|
162
|
+
}
|
|
163
|
+
else if (currChar !== c && invert) {
|
|
164
|
+
suffLen++;
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return str.slice(0, l - suffLen);
|
|
171
|
+
}
|
|
172
|
+
function findClosingBracket(str, b) {
|
|
173
|
+
if (str.indexOf(b[1]) === -1) {
|
|
174
|
+
return -1;
|
|
175
|
+
}
|
|
176
|
+
let level = 0;
|
|
177
|
+
for (let i = 0; i < str.length; i++) {
|
|
178
|
+
if (str[i] === '\\') {
|
|
179
|
+
i++;
|
|
180
|
+
}
|
|
181
|
+
else if (str[i] === b[0]) {
|
|
182
|
+
level++;
|
|
183
|
+
}
|
|
184
|
+
else if (str[i] === b[1]) {
|
|
185
|
+
level--;
|
|
186
|
+
if (level < 0) {
|
|
187
|
+
return i;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return -1;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function outputLink(cap, link, raw, lexer) {
|
|
195
|
+
const href = link.href;
|
|
196
|
+
const title = link.title ? escape$1(link.title) : null;
|
|
197
|
+
const text = cap[1].replace(/\\([\[\]])/g, '$1');
|
|
198
|
+
if (cap[0].charAt(0) !== '!') {
|
|
199
|
+
lexer.state.inLink = true;
|
|
200
|
+
const token = {
|
|
201
|
+
type: 'link',
|
|
202
|
+
raw,
|
|
203
|
+
href,
|
|
204
|
+
title,
|
|
205
|
+
text,
|
|
206
|
+
tokens: lexer.inlineTokens(text),
|
|
207
|
+
};
|
|
208
|
+
lexer.state.inLink = false;
|
|
209
|
+
return token;
|
|
210
|
+
}
|
|
211
|
+
return {
|
|
212
|
+
type: 'image',
|
|
213
|
+
raw,
|
|
214
|
+
href,
|
|
215
|
+
title,
|
|
216
|
+
text: escape$1(text),
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
function indentCodeCompensation(raw, text) {
|
|
220
|
+
const matchIndentToCode = raw.match(/^(\s+)(?:```)/);
|
|
221
|
+
if (matchIndentToCode === null) {
|
|
222
|
+
return text;
|
|
223
|
+
}
|
|
224
|
+
const indentToCode = matchIndentToCode[1];
|
|
225
|
+
return text
|
|
226
|
+
.split('\n')
|
|
227
|
+
.map(node => {
|
|
228
|
+
const matchIndentInNode = node.match(/^\s+/);
|
|
229
|
+
if (matchIndentInNode === null) {
|
|
230
|
+
return node;
|
|
231
|
+
}
|
|
232
|
+
const [indentInNode] = matchIndentInNode;
|
|
233
|
+
if (indentInNode.length >= indentToCode.length) {
|
|
234
|
+
return node.slice(indentToCode.length);
|
|
235
|
+
}
|
|
236
|
+
return node;
|
|
237
|
+
})
|
|
238
|
+
.join('\n');
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Tokenizer
|
|
242
|
+
*/
|
|
243
|
+
class _Tokenizer {
|
|
244
|
+
options;
|
|
245
|
+
rules; // set by the lexer
|
|
246
|
+
lexer; // set by the lexer
|
|
247
|
+
constructor(options) {
|
|
248
|
+
this.options = options || exports.defaults;
|
|
249
|
+
}
|
|
250
|
+
space(src) {
|
|
251
|
+
const cap = this.rules.block.newline.exec(src);
|
|
252
|
+
if (cap && cap[0].length > 0) {
|
|
253
|
+
return {
|
|
254
|
+
type: 'space',
|
|
255
|
+
raw: cap[0],
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
code(src) {
|
|
260
|
+
const cap = this.rules.block.code.exec(src);
|
|
261
|
+
if (cap) {
|
|
262
|
+
const text = cap[0].replace(/^ {1,4}/gm, '');
|
|
263
|
+
return {
|
|
264
|
+
type: 'code',
|
|
265
|
+
raw: cap[0],
|
|
266
|
+
codeBlockStyle: 'indented',
|
|
267
|
+
text: !this.options.pedantic
|
|
268
|
+
? rtrim(text, '\n')
|
|
269
|
+
: text,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
fences(src) {
|
|
274
|
+
const cap = this.rules.block.fences.exec(src);
|
|
275
|
+
if (cap) {
|
|
276
|
+
const raw = cap[0];
|
|
277
|
+
const text = indentCodeCompensation(raw, cap[3] || '');
|
|
278
|
+
return {
|
|
279
|
+
type: 'code',
|
|
280
|
+
raw,
|
|
281
|
+
lang: cap[2] ? cap[2].trim().replace(this.rules.inline.anyPunctuation, '$1') : cap[2],
|
|
282
|
+
text,
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
heading(src) {
|
|
287
|
+
const cap = this.rules.block.heading.exec(src);
|
|
288
|
+
if (cap) {
|
|
289
|
+
let text = cap[2].trim();
|
|
290
|
+
// remove trailing #s
|
|
291
|
+
if (/#$/.test(text)) {
|
|
292
|
+
const trimmed = rtrim(text, '#');
|
|
293
|
+
if (this.options.pedantic) {
|
|
294
|
+
text = trimmed.trim();
|
|
295
|
+
}
|
|
296
|
+
else if (!trimmed || / $/.test(trimmed)) {
|
|
297
|
+
// CommonMark requires space before trailing #s
|
|
298
|
+
text = trimmed.trim();
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return {
|
|
302
|
+
type: 'heading',
|
|
303
|
+
raw: cap[0],
|
|
304
|
+
depth: cap[1].length,
|
|
305
|
+
text,
|
|
306
|
+
tokens: this.lexer.inline(text),
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
hr(src) {
|
|
311
|
+
const cap = this.rules.block.hr.exec(src);
|
|
312
|
+
if (cap) {
|
|
313
|
+
return {
|
|
314
|
+
type: 'hr',
|
|
315
|
+
raw: rtrim(cap[0], '\n'),
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
blockquote(src) {
|
|
320
|
+
const cap = this.rules.block.blockquote.exec(src);
|
|
321
|
+
if (cap) {
|
|
322
|
+
let lines = rtrim(cap[0], '\n').split('\n');
|
|
323
|
+
let raw = '';
|
|
324
|
+
let text = '';
|
|
325
|
+
const tokens = [];
|
|
326
|
+
while (lines.length > 0) {
|
|
327
|
+
let inBlockquote = false;
|
|
328
|
+
const currentLines = [];
|
|
329
|
+
let i;
|
|
330
|
+
for (i = 0; i < lines.length; i++) {
|
|
331
|
+
// get lines up to a continuation
|
|
332
|
+
if (/^ {0,3}>/.test(lines[i])) {
|
|
333
|
+
currentLines.push(lines[i]);
|
|
334
|
+
inBlockquote = true;
|
|
335
|
+
}
|
|
336
|
+
else if (!inBlockquote) {
|
|
337
|
+
currentLines.push(lines[i]);
|
|
338
|
+
}
|
|
339
|
+
else {
|
|
340
|
+
break;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
lines = lines.slice(i);
|
|
344
|
+
const currentRaw = currentLines.join('\n');
|
|
345
|
+
const currentText = currentRaw
|
|
346
|
+
// precede setext continuation with 4 spaces so it isn't a setext
|
|
347
|
+
.replace(/\n {0,3}((?:=+|-+) *)(?=\n|$)/g, '\n $1')
|
|
348
|
+
.replace(/^ {0,3}>[ \t]?/gm, '');
|
|
349
|
+
raw = raw ? `${raw}\n${currentRaw}` : currentRaw;
|
|
350
|
+
text = text ? `${text}\n${currentText}` : currentText;
|
|
351
|
+
// parse blockquote lines as top level tokens
|
|
352
|
+
// merge paragraphs if this is a continuation
|
|
353
|
+
const top = this.lexer.state.top;
|
|
354
|
+
this.lexer.state.top = true;
|
|
355
|
+
this.lexer.blockTokens(currentText, tokens, true);
|
|
356
|
+
this.lexer.state.top = top;
|
|
357
|
+
// if there is no continuation then we are done
|
|
358
|
+
if (lines.length === 0) {
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
const lastToken = tokens[tokens.length - 1];
|
|
362
|
+
if (lastToken?.type === 'code') {
|
|
363
|
+
// blockquote continuation cannot be preceded by a code block
|
|
364
|
+
break;
|
|
365
|
+
}
|
|
366
|
+
else if (lastToken?.type === 'blockquote') {
|
|
367
|
+
// include continuation in nested blockquote
|
|
368
|
+
const oldToken = lastToken;
|
|
369
|
+
const newText = oldToken.raw + '\n' + lines.join('\n');
|
|
370
|
+
const newToken = this.blockquote(newText);
|
|
371
|
+
tokens[tokens.length - 1] = newToken;
|
|
372
|
+
raw = raw.substring(0, raw.length - oldToken.raw.length) + newToken.raw;
|
|
373
|
+
text = text.substring(0, text.length - oldToken.text.length) + newToken.text;
|
|
374
|
+
break;
|
|
375
|
+
}
|
|
376
|
+
else if (lastToken?.type === 'list') {
|
|
377
|
+
// include continuation in nested list
|
|
378
|
+
const oldToken = lastToken;
|
|
379
|
+
const newText = oldToken.raw + '\n' + lines.join('\n');
|
|
380
|
+
const newToken = this.list(newText);
|
|
381
|
+
tokens[tokens.length - 1] = newToken;
|
|
382
|
+
raw = raw.substring(0, raw.length - lastToken.raw.length) + newToken.raw;
|
|
383
|
+
text = text.substring(0, text.length - oldToken.raw.length) + newToken.raw;
|
|
384
|
+
lines = newText.substring(tokens[tokens.length - 1].raw.length).split('\n');
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
return {
|
|
389
|
+
type: 'blockquote',
|
|
390
|
+
raw,
|
|
391
|
+
tokens,
|
|
392
|
+
text,
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
list(src) {
|
|
397
|
+
let cap = this.rules.block.list.exec(src);
|
|
398
|
+
if (cap) {
|
|
399
|
+
let bull = cap[1].trim();
|
|
400
|
+
const isordered = bull.length > 1;
|
|
401
|
+
const list = {
|
|
402
|
+
type: 'list',
|
|
403
|
+
raw: '',
|
|
404
|
+
ordered: isordered,
|
|
405
|
+
start: isordered ? +bull.slice(0, -1) : '',
|
|
406
|
+
loose: false,
|
|
407
|
+
items: [],
|
|
408
|
+
};
|
|
409
|
+
bull = isordered ? `\\d{1,9}\\${bull.slice(-1)}` : `\\${bull}`;
|
|
410
|
+
if (this.options.pedantic) {
|
|
411
|
+
bull = isordered ? bull : '[*+-]';
|
|
412
|
+
}
|
|
413
|
+
// Get next list item
|
|
414
|
+
const itemRegex = new RegExp(`^( {0,3}${bull})((?:[\t ][^\\n]*)?(?:\\n|$))`);
|
|
415
|
+
let endsWithBlankLine = false;
|
|
416
|
+
// Check if current bullet point can start a new List Item
|
|
417
|
+
while (src) {
|
|
418
|
+
let endEarly = false;
|
|
419
|
+
let raw = '';
|
|
420
|
+
let itemContents = '';
|
|
421
|
+
if (!(cap = itemRegex.exec(src))) {
|
|
422
|
+
break;
|
|
423
|
+
}
|
|
424
|
+
if (this.rules.block.hr.test(src)) { // End list if bullet was actually HR (possibly move into itemRegex?)
|
|
425
|
+
break;
|
|
426
|
+
}
|
|
427
|
+
raw = cap[0];
|
|
428
|
+
src = src.substring(raw.length);
|
|
429
|
+
let line = cap[2].split('\n', 1)[0].replace(/^\t+/, (t) => ' '.repeat(3 * t.length));
|
|
430
|
+
let nextLine = src.split('\n', 1)[0];
|
|
431
|
+
let blankLine = !line.trim();
|
|
432
|
+
let indent = 0;
|
|
433
|
+
if (this.options.pedantic) {
|
|
434
|
+
indent = 2;
|
|
435
|
+
itemContents = line.trimStart();
|
|
436
|
+
}
|
|
437
|
+
else if (blankLine) {
|
|
438
|
+
indent = cap[1].length + 1;
|
|
439
|
+
}
|
|
440
|
+
else {
|
|
441
|
+
indent = cap[2].search(/[^ ]/); // Find first non-space char
|
|
442
|
+
indent = indent > 4 ? 1 : indent; // Treat indented code blocks (> 4 spaces) as having only 1 indent
|
|
443
|
+
itemContents = line.slice(indent);
|
|
444
|
+
indent += cap[1].length;
|
|
445
|
+
}
|
|
446
|
+
if (blankLine && /^ *$/.test(nextLine)) { // Items begin with at most one blank line
|
|
447
|
+
raw += nextLine + '\n';
|
|
448
|
+
src = src.substring(nextLine.length + 1);
|
|
449
|
+
endEarly = true;
|
|
450
|
+
}
|
|
451
|
+
if (!endEarly) {
|
|
452
|
+
const nextBulletRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:[*+-]|\\d{1,9}[.)])((?:[ \t][^\\n]*)?(?:\\n|$))`);
|
|
453
|
+
const hrRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}((?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$)`);
|
|
454
|
+
const fencesBeginRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:\`\`\`|~~~)`);
|
|
455
|
+
const headingBeginRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}#`);
|
|
456
|
+
// Check if following lines should be included in List Item
|
|
457
|
+
while (src) {
|
|
458
|
+
const rawLine = src.split('\n', 1)[0];
|
|
459
|
+
nextLine = rawLine;
|
|
460
|
+
// Re-align to follow commonmark nesting rules
|
|
461
|
+
if (this.options.pedantic) {
|
|
462
|
+
nextLine = nextLine.replace(/^ {1,4}(?=( {4})*[^ ])/g, ' ');
|
|
463
|
+
}
|
|
464
|
+
// End list item if found code fences
|
|
465
|
+
if (fencesBeginRegex.test(nextLine)) {
|
|
466
|
+
break;
|
|
467
|
+
}
|
|
468
|
+
// End list item if found start of new heading
|
|
469
|
+
if (headingBeginRegex.test(nextLine)) {
|
|
470
|
+
break;
|
|
471
|
+
}
|
|
472
|
+
// End list item if found start of new bullet
|
|
473
|
+
if (nextBulletRegex.test(nextLine)) {
|
|
474
|
+
break;
|
|
475
|
+
}
|
|
476
|
+
// Horizontal rule found
|
|
477
|
+
if (hrRegex.test(src)) {
|
|
478
|
+
break;
|
|
479
|
+
}
|
|
480
|
+
if (nextLine.search(/[^ ]/) >= indent || !nextLine.trim()) { // Dedent if possible
|
|
481
|
+
itemContents += '\n' + nextLine.slice(indent);
|
|
482
|
+
}
|
|
483
|
+
else {
|
|
484
|
+
// not enough indentation
|
|
485
|
+
if (blankLine) {
|
|
486
|
+
break;
|
|
487
|
+
}
|
|
488
|
+
// paragraph continuation unless last line was a different block level element
|
|
489
|
+
if (line.search(/[^ ]/) >= 4) { // indented code block
|
|
490
|
+
break;
|
|
491
|
+
}
|
|
492
|
+
if (fencesBeginRegex.test(line)) {
|
|
493
|
+
break;
|
|
494
|
+
}
|
|
495
|
+
if (headingBeginRegex.test(line)) {
|
|
496
|
+
break;
|
|
497
|
+
}
|
|
498
|
+
if (hrRegex.test(line)) {
|
|
499
|
+
break;
|
|
500
|
+
}
|
|
501
|
+
itemContents += '\n' + nextLine;
|
|
502
|
+
}
|
|
503
|
+
if (!blankLine && !nextLine.trim()) { // Check if current line is blank
|
|
504
|
+
blankLine = true;
|
|
505
|
+
}
|
|
506
|
+
raw += rawLine + '\n';
|
|
507
|
+
src = src.substring(rawLine.length + 1);
|
|
508
|
+
line = nextLine.slice(indent);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
if (!list.loose) {
|
|
512
|
+
// If the previous item ended with a blank line, the list is loose
|
|
513
|
+
if (endsWithBlankLine) {
|
|
514
|
+
list.loose = true;
|
|
515
|
+
}
|
|
516
|
+
else if (/\n *\n *$/.test(raw)) {
|
|
517
|
+
endsWithBlankLine = true;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
let istask = null;
|
|
521
|
+
let ischecked;
|
|
522
|
+
// Check for task list items
|
|
523
|
+
if (this.options.gfm) {
|
|
524
|
+
istask = /^\[[ xX]\] /.exec(itemContents);
|
|
525
|
+
if (istask) {
|
|
526
|
+
ischecked = istask[0] !== '[ ] ';
|
|
527
|
+
itemContents = itemContents.replace(/^\[[ xX]\] +/, '');
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
list.items.push({
|
|
531
|
+
type: 'list_item',
|
|
532
|
+
raw,
|
|
533
|
+
task: !!istask,
|
|
534
|
+
checked: ischecked,
|
|
535
|
+
loose: false,
|
|
536
|
+
text: itemContents,
|
|
537
|
+
tokens: [],
|
|
538
|
+
});
|
|
539
|
+
list.raw += raw;
|
|
540
|
+
}
|
|
541
|
+
// Do not consume newlines at end of final item. Alternatively, make itemRegex *start* with any newlines to simplify/speed up endsWithBlankLine logic
|
|
542
|
+
list.items[list.items.length - 1].raw = list.items[list.items.length - 1].raw.trimEnd();
|
|
543
|
+
list.items[list.items.length - 1].text = list.items[list.items.length - 1].text.trimEnd();
|
|
544
|
+
list.raw = list.raw.trimEnd();
|
|
545
|
+
// Item child tokens handled here at end because we needed to have the final item to trim it first
|
|
546
|
+
for (let i = 0; i < list.items.length; i++) {
|
|
547
|
+
this.lexer.state.top = false;
|
|
548
|
+
list.items[i].tokens = this.lexer.blockTokens(list.items[i].text, []);
|
|
549
|
+
if (!list.loose) {
|
|
550
|
+
// Check if list should be loose
|
|
551
|
+
const spacers = list.items[i].tokens.filter(t => t.type === 'space');
|
|
552
|
+
const hasMultipleLineBreaks = spacers.length > 0 && spacers.some(t => /\n.*\n/.test(t.raw));
|
|
553
|
+
list.loose = hasMultipleLineBreaks;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
// Set all items to loose if list is loose
|
|
557
|
+
if (list.loose) {
|
|
558
|
+
for (let i = 0; i < list.items.length; i++) {
|
|
559
|
+
list.items[i].loose = true;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
return list;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
html(src) {
|
|
566
|
+
const cap = this.rules.block.html.exec(src);
|
|
567
|
+
if (cap) {
|
|
568
|
+
const token = {
|
|
569
|
+
type: 'html',
|
|
570
|
+
block: true,
|
|
571
|
+
raw: cap[0],
|
|
572
|
+
pre: cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style',
|
|
573
|
+
text: cap[0],
|
|
574
|
+
};
|
|
575
|
+
return token;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
def(src) {
|
|
579
|
+
const cap = this.rules.block.def.exec(src);
|
|
580
|
+
if (cap) {
|
|
581
|
+
const tag = cap[1].toLowerCase().replace(/\s+/g, ' ');
|
|
582
|
+
const href = cap[2] ? cap[2].replace(/^<(.*)>$/, '$1').replace(this.rules.inline.anyPunctuation, '$1') : '';
|
|
583
|
+
const title = cap[3] ? cap[3].substring(1, cap[3].length - 1).replace(this.rules.inline.anyPunctuation, '$1') : cap[3];
|
|
584
|
+
return {
|
|
585
|
+
type: 'def',
|
|
586
|
+
tag,
|
|
587
|
+
raw: cap[0],
|
|
588
|
+
href,
|
|
589
|
+
title,
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
table(src) {
|
|
594
|
+
const cap = this.rules.block.table.exec(src);
|
|
595
|
+
if (!cap) {
|
|
596
|
+
return;
|
|
597
|
+
}
|
|
598
|
+
if (!/[:|]/.test(cap[2])) {
|
|
599
|
+
// delimiter row must have a pipe (|) or colon (:) otherwise it is a setext heading
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
const headers = splitCells(cap[1]);
|
|
603
|
+
const aligns = cap[2].replace(/^\||\| *$/g, '').split('|');
|
|
604
|
+
const rows = cap[3] && cap[3].trim() ? cap[3].replace(/\n[ \t]*$/, '').split('\n') : [];
|
|
605
|
+
const item = {
|
|
606
|
+
type: 'table',
|
|
607
|
+
raw: cap[0],
|
|
608
|
+
header: [],
|
|
609
|
+
align: [],
|
|
610
|
+
rows: [],
|
|
611
|
+
};
|
|
612
|
+
if (headers.length !== aligns.length) {
|
|
613
|
+
// header and align columns must be equal, rows can be different.
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
for (const align of aligns) {
|
|
617
|
+
if (/^ *-+: *$/.test(align)) {
|
|
618
|
+
item.align.push('right');
|
|
619
|
+
}
|
|
620
|
+
else if (/^ *:-+: *$/.test(align)) {
|
|
621
|
+
item.align.push('center');
|
|
622
|
+
}
|
|
623
|
+
else if (/^ *:-+ *$/.test(align)) {
|
|
624
|
+
item.align.push('left');
|
|
625
|
+
}
|
|
626
|
+
else {
|
|
627
|
+
item.align.push(null);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
for (let i = 0; i < headers.length; i++) {
|
|
631
|
+
item.header.push({
|
|
632
|
+
text: headers[i],
|
|
633
|
+
tokens: this.lexer.inline(headers[i]),
|
|
634
|
+
header: true,
|
|
635
|
+
align: item.align[i],
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
for (const row of rows) {
|
|
639
|
+
item.rows.push(splitCells(row, item.header.length).map((cell, i) => {
|
|
640
|
+
return {
|
|
641
|
+
text: cell,
|
|
642
|
+
tokens: this.lexer.inline(cell),
|
|
643
|
+
header: false,
|
|
644
|
+
align: item.align[i],
|
|
645
|
+
};
|
|
646
|
+
}));
|
|
647
|
+
}
|
|
648
|
+
return item;
|
|
649
|
+
}
|
|
650
|
+
lheading(src) {
|
|
651
|
+
const cap = this.rules.block.lheading.exec(src);
|
|
652
|
+
if (cap) {
|
|
653
|
+
return {
|
|
654
|
+
type: 'heading',
|
|
655
|
+
raw: cap[0],
|
|
656
|
+
depth: cap[2].charAt(0) === '=' ? 1 : 2,
|
|
657
|
+
text: cap[1],
|
|
658
|
+
tokens: this.lexer.inline(cap[1]),
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
paragraph(src) {
|
|
663
|
+
const cap = this.rules.block.paragraph.exec(src);
|
|
664
|
+
if (cap) {
|
|
665
|
+
const text = cap[1].charAt(cap[1].length - 1) === '\n'
|
|
666
|
+
? cap[1].slice(0, -1)
|
|
667
|
+
: cap[1];
|
|
668
|
+
return {
|
|
669
|
+
type: 'paragraph',
|
|
670
|
+
raw: cap[0],
|
|
671
|
+
text,
|
|
672
|
+
tokens: this.lexer.inline(text),
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
text(src) {
|
|
677
|
+
const cap = this.rules.block.text.exec(src);
|
|
678
|
+
if (cap) {
|
|
679
|
+
return {
|
|
680
|
+
type: 'text',
|
|
681
|
+
raw: cap[0],
|
|
682
|
+
text: cap[0],
|
|
683
|
+
tokens: this.lexer.inline(cap[0]),
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
escape(src) {
|
|
688
|
+
const cap = this.rules.inline.escape.exec(src);
|
|
689
|
+
if (cap) {
|
|
690
|
+
return {
|
|
691
|
+
type: 'escape',
|
|
692
|
+
raw: cap[0],
|
|
693
|
+
text: escape$1(cap[1]),
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
tag(src) {
|
|
698
|
+
const cap = this.rules.inline.tag.exec(src);
|
|
699
|
+
if (cap) {
|
|
700
|
+
if (!this.lexer.state.inLink && /^<a /i.test(cap[0])) {
|
|
701
|
+
this.lexer.state.inLink = true;
|
|
702
|
+
}
|
|
703
|
+
else if (this.lexer.state.inLink && /^<\/a>/i.test(cap[0])) {
|
|
704
|
+
this.lexer.state.inLink = false;
|
|
705
|
+
}
|
|
706
|
+
if (!this.lexer.state.inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
|
|
707
|
+
this.lexer.state.inRawBlock = true;
|
|
708
|
+
}
|
|
709
|
+
else if (this.lexer.state.inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
|
|
710
|
+
this.lexer.state.inRawBlock = false;
|
|
711
|
+
}
|
|
712
|
+
return {
|
|
713
|
+
type: 'html',
|
|
714
|
+
raw: cap[0],
|
|
715
|
+
inLink: this.lexer.state.inLink,
|
|
716
|
+
inRawBlock: this.lexer.state.inRawBlock,
|
|
717
|
+
block: false,
|
|
718
|
+
text: cap[0],
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
link(src) {
|
|
723
|
+
const cap = this.rules.inline.link.exec(src);
|
|
724
|
+
if (cap) {
|
|
725
|
+
const trimmedUrl = cap[2].trim();
|
|
726
|
+
if (!this.options.pedantic && /^</.test(trimmedUrl)) {
|
|
727
|
+
// commonmark requires matching angle brackets
|
|
728
|
+
if (!(/>$/.test(trimmedUrl))) {
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
// ending angle bracket cannot be escaped
|
|
732
|
+
const rtrimSlash = rtrim(trimmedUrl.slice(0, -1), '\\');
|
|
733
|
+
if ((trimmedUrl.length - rtrimSlash.length) % 2 === 0) {
|
|
734
|
+
return;
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
else {
|
|
738
|
+
// find closing parenthesis
|
|
739
|
+
const lastParenIndex = findClosingBracket(cap[2], '()');
|
|
740
|
+
if (lastParenIndex > -1) {
|
|
741
|
+
const start = cap[0].indexOf('!') === 0 ? 5 : 4;
|
|
742
|
+
const linkLen = start + cap[1].length + lastParenIndex;
|
|
743
|
+
cap[2] = cap[2].substring(0, lastParenIndex);
|
|
744
|
+
cap[0] = cap[0].substring(0, linkLen).trim();
|
|
745
|
+
cap[3] = '';
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
let href = cap[2];
|
|
749
|
+
let title = '';
|
|
750
|
+
if (this.options.pedantic) {
|
|
751
|
+
// split pedantic href and title
|
|
752
|
+
const link = /^([^'"]*[^\s])\s+(['"])(.*)\2/.exec(href);
|
|
753
|
+
if (link) {
|
|
754
|
+
href = link[1];
|
|
755
|
+
title = link[3];
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
else {
|
|
759
|
+
title = cap[3] ? cap[3].slice(1, -1) : '';
|
|
760
|
+
}
|
|
761
|
+
href = href.trim();
|
|
762
|
+
if (/^</.test(href)) {
|
|
763
|
+
if (this.options.pedantic && !(/>$/.test(trimmedUrl))) {
|
|
764
|
+
// pedantic allows starting angle bracket without ending angle bracket
|
|
765
|
+
href = href.slice(1);
|
|
766
|
+
}
|
|
767
|
+
else {
|
|
768
|
+
href = href.slice(1, -1);
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
return outputLink(cap, {
|
|
772
|
+
href: href ? href.replace(this.rules.inline.anyPunctuation, '$1') : href,
|
|
773
|
+
title: title ? title.replace(this.rules.inline.anyPunctuation, '$1') : title,
|
|
774
|
+
}, cap[0], this.lexer);
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
reflink(src, links) {
|
|
778
|
+
let cap;
|
|
779
|
+
if ((cap = this.rules.inline.reflink.exec(src))
|
|
780
|
+
|| (cap = this.rules.inline.nolink.exec(src))) {
|
|
781
|
+
const linkString = (cap[2] || cap[1]).replace(/\s+/g, ' ');
|
|
782
|
+
const link = links[linkString.toLowerCase()];
|
|
783
|
+
if (!link) {
|
|
784
|
+
const text = cap[0].charAt(0);
|
|
785
|
+
return {
|
|
786
|
+
type: 'text',
|
|
787
|
+
raw: text,
|
|
788
|
+
text,
|
|
789
|
+
};
|
|
790
|
+
}
|
|
791
|
+
return outputLink(cap, link, cap[0], this.lexer);
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
emStrong(src, maskedSrc, prevChar = '') {
|
|
795
|
+
let match = this.rules.inline.emStrongLDelim.exec(src);
|
|
796
|
+
if (!match)
|
|
797
|
+
return;
|
|
798
|
+
// _ can't be between two alphanumerics. \p{L}\p{N} includes non-english alphabet/numbers as well
|
|
799
|
+
if (match[3] && prevChar.match(/[\p{L}\p{N}]/u))
|
|
800
|
+
return;
|
|
801
|
+
const nextChar = match[1] || match[2] || '';
|
|
802
|
+
if (!nextChar || !prevChar || this.rules.inline.punctuation.exec(prevChar)) {
|
|
803
|
+
// unicode Regex counts emoji as 1 char; spread into array for proper count (used multiple times below)
|
|
804
|
+
const lLength = [...match[0]].length - 1;
|
|
805
|
+
let rDelim, rLength, delimTotal = lLength, midDelimTotal = 0;
|
|
806
|
+
const endReg = match[0][0] === '*' ? this.rules.inline.emStrongRDelimAst : this.rules.inline.emStrongRDelimUnd;
|
|
807
|
+
endReg.lastIndex = 0;
|
|
808
|
+
// Clip maskedSrc to same section of string as src (move to lexer?)
|
|
809
|
+
maskedSrc = maskedSrc.slice(-1 * src.length + lLength);
|
|
810
|
+
while ((match = endReg.exec(maskedSrc)) != null) {
|
|
811
|
+
rDelim = match[1] || match[2] || match[3] || match[4] || match[5] || match[6];
|
|
812
|
+
if (!rDelim)
|
|
813
|
+
continue; // skip single * in __abc*abc__
|
|
814
|
+
rLength = [...rDelim].length;
|
|
815
|
+
if (match[3] || match[4]) { // found another Left Delim
|
|
816
|
+
delimTotal += rLength;
|
|
817
|
+
continue;
|
|
818
|
+
}
|
|
819
|
+
else if (match[5] || match[6]) { // either Left or Right Delim
|
|
820
|
+
if (lLength % 3 && !((lLength + rLength) % 3)) {
|
|
821
|
+
midDelimTotal += rLength;
|
|
822
|
+
continue; // CommonMark Emphasis Rules 9-10
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
delimTotal -= rLength;
|
|
826
|
+
if (delimTotal > 0)
|
|
827
|
+
continue; // Haven't found enough closing delimiters
|
|
828
|
+
// Remove extra characters. *a*** -> *a*
|
|
829
|
+
rLength = Math.min(rLength, rLength + delimTotal + midDelimTotal);
|
|
830
|
+
// char length can be >1 for unicode characters;
|
|
831
|
+
const lastCharLength = [...match[0]][0].length;
|
|
832
|
+
const raw = src.slice(0, lLength + match.index + lastCharLength + rLength);
|
|
833
|
+
// Create `em` if smallest delimiter has odd char count. *a***
|
|
834
|
+
if (Math.min(lLength, rLength) % 2) {
|
|
835
|
+
const text = raw.slice(1, -1);
|
|
836
|
+
return {
|
|
837
|
+
type: 'em',
|
|
838
|
+
raw,
|
|
839
|
+
text,
|
|
840
|
+
tokens: this.lexer.inlineTokens(text),
|
|
841
|
+
};
|
|
842
|
+
}
|
|
843
|
+
// Create 'strong' if smallest delimiter has even char count. **a***
|
|
844
|
+
const text = raw.slice(2, -2);
|
|
845
|
+
return {
|
|
846
|
+
type: 'strong',
|
|
847
|
+
raw,
|
|
848
|
+
text,
|
|
849
|
+
tokens: this.lexer.inlineTokens(text),
|
|
850
|
+
};
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
codespan(src) {
|
|
855
|
+
const cap = this.rules.inline.code.exec(src);
|
|
856
|
+
if (cap) {
|
|
857
|
+
let text = cap[2].replace(/\n/g, ' ');
|
|
858
|
+
const hasNonSpaceChars = /[^ ]/.test(text);
|
|
859
|
+
const hasSpaceCharsOnBothEnds = /^ /.test(text) && / $/.test(text);
|
|
860
|
+
if (hasNonSpaceChars && hasSpaceCharsOnBothEnds) {
|
|
861
|
+
text = text.substring(1, text.length - 1);
|
|
862
|
+
}
|
|
863
|
+
text = escape$1(text, true);
|
|
864
|
+
return {
|
|
865
|
+
type: 'codespan',
|
|
866
|
+
raw: cap[0],
|
|
867
|
+
text,
|
|
868
|
+
};
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
br(src) {
|
|
872
|
+
const cap = this.rules.inline.br.exec(src);
|
|
873
|
+
if (cap) {
|
|
874
|
+
return {
|
|
875
|
+
type: 'br',
|
|
876
|
+
raw: cap[0],
|
|
877
|
+
};
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
del(src) {
|
|
881
|
+
const cap = this.rules.inline.del.exec(src);
|
|
882
|
+
if (cap) {
|
|
883
|
+
return {
|
|
884
|
+
type: 'del',
|
|
885
|
+
raw: cap[0],
|
|
886
|
+
text: cap[2],
|
|
887
|
+
tokens: this.lexer.inlineTokens(cap[2]),
|
|
888
|
+
};
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
autolink(src) {
|
|
892
|
+
const cap = this.rules.inline.autolink.exec(src);
|
|
893
|
+
if (cap) {
|
|
894
|
+
let text, href;
|
|
895
|
+
if (cap[2] === '@') {
|
|
896
|
+
text = escape$1(cap[1]);
|
|
897
|
+
href = 'mailto:' + text;
|
|
898
|
+
}
|
|
899
|
+
else {
|
|
900
|
+
text = escape$1(cap[1]);
|
|
901
|
+
href = text;
|
|
902
|
+
}
|
|
903
|
+
return {
|
|
904
|
+
type: 'link',
|
|
905
|
+
raw: cap[0],
|
|
906
|
+
text,
|
|
907
|
+
href,
|
|
908
|
+
tokens: [
|
|
909
|
+
{
|
|
910
|
+
type: 'text',
|
|
911
|
+
raw: text,
|
|
912
|
+
text,
|
|
913
|
+
},
|
|
914
|
+
],
|
|
915
|
+
};
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
url(src) {
|
|
919
|
+
let cap;
|
|
920
|
+
if (cap = this.rules.inline.url.exec(src)) {
|
|
921
|
+
let text, href;
|
|
922
|
+
if (cap[2] === '@') {
|
|
923
|
+
text = escape$1(cap[0]);
|
|
924
|
+
href = 'mailto:' + text;
|
|
925
|
+
}
|
|
926
|
+
else {
|
|
927
|
+
// do extended autolink path validation
|
|
928
|
+
let prevCapZero;
|
|
929
|
+
do {
|
|
930
|
+
prevCapZero = cap[0];
|
|
931
|
+
cap[0] = this.rules.inline._backpedal.exec(cap[0])?.[0] ?? '';
|
|
932
|
+
} while (prevCapZero !== cap[0]);
|
|
933
|
+
text = escape$1(cap[0]);
|
|
934
|
+
if (cap[1] === 'www.') {
|
|
935
|
+
href = 'http://' + cap[0];
|
|
936
|
+
}
|
|
937
|
+
else {
|
|
938
|
+
href = cap[0];
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
return {
|
|
942
|
+
type: 'link',
|
|
943
|
+
raw: cap[0],
|
|
944
|
+
text,
|
|
945
|
+
href,
|
|
946
|
+
tokens: [
|
|
947
|
+
{
|
|
948
|
+
type: 'text',
|
|
949
|
+
raw: text,
|
|
950
|
+
text,
|
|
951
|
+
},
|
|
952
|
+
],
|
|
953
|
+
};
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
inlineText(src) {
|
|
957
|
+
const cap = this.rules.inline.text.exec(src);
|
|
958
|
+
if (cap) {
|
|
959
|
+
let text;
|
|
960
|
+
if (this.lexer.state.inRawBlock) {
|
|
961
|
+
text = cap[0];
|
|
962
|
+
}
|
|
963
|
+
else {
|
|
964
|
+
text = escape$1(cap[0]);
|
|
965
|
+
}
|
|
966
|
+
return {
|
|
967
|
+
type: 'text',
|
|
968
|
+
raw: cap[0],
|
|
969
|
+
text,
|
|
970
|
+
};
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
/**
|
|
976
|
+
* Block-Level Grammar
|
|
977
|
+
*/
|
|
978
|
+
const newline = /^(?: *(?:\n|$))+/;
|
|
979
|
+
const blockCode = /^( {4}[^\n]+(?:\n(?: *(?:\n|$))*)?)+/;
|
|
980
|
+
const fences = /^ {0,3}(`{3,}(?=[^`\n]*(?:\n|$))|~{3,})([^\n]*)(?:\n|$)(?:|([\s\S]*?)(?:\n|$))(?: {0,3}\1[~`]* *(?=\n|$)|$)/;
|
|
981
|
+
const hr = /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/;
|
|
982
|
+
const heading = /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/;
|
|
983
|
+
const bullet = /(?:[*+-]|\d{1,9}[.)])/;
|
|
984
|
+
const lheading = edit(/^(?!bull |blockCode|fences|blockquote|heading|html)((?:.|\n(?!\s*?\n|bull |blockCode|fences|blockquote|heading|html))+?)\n {0,3}(=+|-+) *(?:\n+|$)/)
|
|
985
|
+
.replace(/bull/g, bullet) // lists can interrupt
|
|
986
|
+
.replace(/blockCode/g, / {4}/) // indented code blocks can interrupt
|
|
987
|
+
.replace(/fences/g, / {0,3}(?:`{3,}|~{3,})/) // fenced code blocks can interrupt
|
|
988
|
+
.replace(/blockquote/g, / {0,3}>/) // blockquote can interrupt
|
|
989
|
+
.replace(/heading/g, / {0,3}#{1,6}/) // ATX heading can interrupt
|
|
990
|
+
.replace(/html/g, / {0,3}<[^\n>]+>\n/) // block html can interrupt
|
|
991
|
+
.getRegex();
|
|
992
|
+
const _paragraph = /^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html|table| +\n)[^\n]+)*)/;
|
|
993
|
+
const blockText = /^[^\n]+/;
|
|
994
|
+
const _blockLabel = /(?!\s*\])(?:\\.|[^\[\]\\])+/;
|
|
995
|
+
const def = edit(/^ {0,3}\[(label)\]: *(?:\n *)?([^<\s][^\s]*|<.*?>)(?:(?: +(?:\n *)?| *\n *)(title))? *(?:\n+|$)/)
|
|
996
|
+
.replace('label', _blockLabel)
|
|
997
|
+
.replace('title', /(?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))/)
|
|
998
|
+
.getRegex();
|
|
999
|
+
const list = edit(/^( {0,3}bull)([ \t][^\n]+?)?(?:\n|$)/)
|
|
1000
|
+
.replace(/bull/g, bullet)
|
|
1001
|
+
.getRegex();
|
|
1002
|
+
const _tag = 'address|article|aside|base|basefont|blockquote|body|caption'
|
|
1003
|
+
+ '|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption'
|
|
1004
|
+
+ '|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe'
|
|
1005
|
+
+ '|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option'
|
|
1006
|
+
+ '|p|param|search|section|summary|table|tbody|td|tfoot|th|thead|title'
|
|
1007
|
+
+ '|tr|track|ul';
|
|
1008
|
+
const _comment = /<!--(?:-?>|[\s\S]*?(?:-->|$))/;
|
|
1009
|
+
const html = edit('^ {0,3}(?:' // optional indentation
|
|
1010
|
+
+ '<(script|pre|style|textarea)[\\s>][\\s\\S]*?(?:</\\1>[^\\n]*\\n+|$)' // (1)
|
|
1011
|
+
+ '|comment[^\\n]*(\\n+|$)' // (2)
|
|
1012
|
+
+ '|<\\?[\\s\\S]*?(?:\\?>\\n*|$)' // (3)
|
|
1013
|
+
+ '|<![A-Z][\\s\\S]*?(?:>\\n*|$)' // (4)
|
|
1014
|
+
+ '|<!\\[CDATA\\[[\\s\\S]*?(?:\\]\\]>\\n*|$)' // (5)
|
|
1015
|
+
+ '|</?(tag)(?: +|\\n|/?>)[\\s\\S]*?(?:(?:\\n *)+\\n|$)' // (6)
|
|
1016
|
+
+ '|<(?!script|pre|style|textarea)([a-z][\\w-]*)(?:attribute)*? */?>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n *)+\\n|$)' // (7) open tag
|
|
1017
|
+
+ '|</(?!script|pre|style|textarea)[a-z][\\w-]*\\s*>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n *)+\\n|$)' // (7) closing tag
|
|
1018
|
+
+ ')', 'i')
|
|
1019
|
+
.replace('comment', _comment)
|
|
1020
|
+
.replace('tag', _tag)
|
|
1021
|
+
.replace('attribute', / +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?/)
|
|
1022
|
+
.getRegex();
|
|
1023
|
+
const paragraph = edit(_paragraph)
|
|
1024
|
+
.replace('hr', hr)
|
|
1025
|
+
.replace('heading', ' {0,3}#{1,6}(?:\\s|$)')
|
|
1026
|
+
.replace('|lheading', '') // setext headings don't interrupt commonmark paragraphs
|
|
1027
|
+
.replace('|table', '')
|
|
1028
|
+
.replace('blockquote', ' {0,3}>')
|
|
1029
|
+
.replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
|
|
1030
|
+
.replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
|
|
1031
|
+
.replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
|
|
1032
|
+
.replace('tag', _tag) // pars can be interrupted by type (6) html blocks
|
|
1033
|
+
.getRegex();
|
|
1034
|
+
const blockquote = edit(/^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/)
|
|
1035
|
+
.replace('paragraph', paragraph)
|
|
1036
|
+
.getRegex();
|
|
1037
|
+
/**
|
|
1038
|
+
* Normal Block Grammar
|
|
1039
|
+
*/
|
|
1040
|
+
const blockNormal = {
|
|
1041
|
+
blockquote,
|
|
1042
|
+
code: blockCode,
|
|
1043
|
+
def,
|
|
1044
|
+
fences,
|
|
1045
|
+
heading,
|
|
1046
|
+
hr,
|
|
1047
|
+
html,
|
|
1048
|
+
lheading,
|
|
1049
|
+
list,
|
|
1050
|
+
newline,
|
|
1051
|
+
paragraph,
|
|
1052
|
+
table: noopTest,
|
|
1053
|
+
text: blockText,
|
|
1054
|
+
};
|
|
1055
|
+
/**
|
|
1056
|
+
* GFM Block Grammar
|
|
1057
|
+
*/
|
|
1058
|
+
const gfmTable = edit('^ *([^\\n ].*)\\n' // Header
|
|
1059
|
+
+ ' {0,3}((?:\\| *)?:?-+:? *(?:\\| *:?-+:? *)*(?:\\| *)?)' // Align
|
|
1060
|
+
+ '(?:\\n((?:(?! *\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)') // Cells
|
|
1061
|
+
.replace('hr', hr)
|
|
1062
|
+
.replace('heading', ' {0,3}#{1,6}(?:\\s|$)')
|
|
1063
|
+
.replace('blockquote', ' {0,3}>')
|
|
1064
|
+
.replace('code', ' {4}[^\\n]')
|
|
1065
|
+
.replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
|
|
1066
|
+
.replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
|
|
1067
|
+
.replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
|
|
1068
|
+
.replace('tag', _tag) // tables can be interrupted by type (6) html blocks
|
|
1069
|
+
.getRegex();
|
|
1070
|
+
const blockGfm = {
|
|
1071
|
+
...blockNormal,
|
|
1072
|
+
table: gfmTable,
|
|
1073
|
+
paragraph: edit(_paragraph)
|
|
1074
|
+
.replace('hr', hr)
|
|
1075
|
+
.replace('heading', ' {0,3}#{1,6}(?:\\s|$)')
|
|
1076
|
+
.replace('|lheading', '') // setext headings don't interrupt commonmark paragraphs
|
|
1077
|
+
.replace('table', gfmTable) // interrupt paragraphs with table
|
|
1078
|
+
.replace('blockquote', ' {0,3}>')
|
|
1079
|
+
.replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
|
|
1080
|
+
.replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
|
|
1081
|
+
.replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
|
|
1082
|
+
.replace('tag', _tag) // pars can be interrupted by type (6) html blocks
|
|
1083
|
+
.getRegex(),
|
|
1084
|
+
};
|
|
1085
|
+
/**
|
|
1086
|
+
* Pedantic grammar (original John Gruber's loose markdown specification)
|
|
1087
|
+
*/
|
|
1088
|
+
const blockPedantic = {
|
|
1089
|
+
...blockNormal,
|
|
1090
|
+
html: edit('^ *(?:comment *(?:\\n|\\s*$)'
|
|
1091
|
+
+ '|<(tag)[\\s\\S]+?</\\1> *(?:\\n{2,}|\\s*$)' // closed tag
|
|
1092
|
+
+ '|<tag(?:"[^"]*"|\'[^\']*\'|\\s[^\'"/>\\s]*)*?/?> *(?:\\n{2,}|\\s*$))')
|
|
1093
|
+
.replace('comment', _comment)
|
|
1094
|
+
.replace(/tag/g, '(?!(?:'
|
|
1095
|
+
+ 'a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub'
|
|
1096
|
+
+ '|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)'
|
|
1097
|
+
+ '\\b)\\w+(?!:|[^\\w\\s@]*@)\\b')
|
|
1098
|
+
.getRegex(),
|
|
1099
|
+
def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/,
|
|
1100
|
+
heading: /^(#{1,6})(.*)(?:\n+|$)/,
|
|
1101
|
+
fences: noopTest, // fences not supported
|
|
1102
|
+
lheading: /^(.+?)\n {0,3}(=+|-+) *(?:\n+|$)/,
|
|
1103
|
+
paragraph: edit(_paragraph)
|
|
1104
|
+
.replace('hr', hr)
|
|
1105
|
+
.replace('heading', ' *#{1,6} *[^\n]')
|
|
1106
|
+
.replace('lheading', lheading)
|
|
1107
|
+
.replace('|table', '')
|
|
1108
|
+
.replace('blockquote', ' {0,3}>')
|
|
1109
|
+
.replace('|fences', '')
|
|
1110
|
+
.replace('|list', '')
|
|
1111
|
+
.replace('|html', '')
|
|
1112
|
+
.replace('|tag', '')
|
|
1113
|
+
.getRegex(),
|
|
1114
|
+
};
|
|
1115
|
+
/**
|
|
1116
|
+
* Inline-Level Grammar
|
|
1117
|
+
*/
|
|
1118
|
+
const escape = /^\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/;
|
|
1119
|
+
const inlineCode = /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/;
|
|
1120
|
+
const br = /^( {2,}|\\)\n(?!\s*$)/;
|
|
1121
|
+
const inlineText = /^(`+|[^`])(?:(?= {2,}\n)|[\s\S]*?(?:(?=[\\<!\[`*_]|\b_|$)|[^ ](?= {2,}\n)))/;
|
|
1122
|
+
// list of unicode punctuation marks, plus any missing characters from CommonMark spec
|
|
1123
|
+
const _punctuation = '\\p{P}\\p{S}';
|
|
1124
|
+
const punctuation = edit(/^((?![*_])[\spunctuation])/, 'u')
|
|
1125
|
+
.replace(/punctuation/g, _punctuation).getRegex();
|
|
1126
|
+
// sequences em should skip over [title](link), `code`, <html>
|
|
1127
|
+
const blockSkip = /\[[^[\]]*?\]\([^\(\)]*?\)|`[^`]*?`|<[^<>]*?>/g;
|
|
1128
|
+
const emStrongLDelim = edit(/^(?:\*+(?:((?!\*)[punct])|[^\s*]))|^_+(?:((?!_)[punct])|([^\s_]))/, 'u')
|
|
1129
|
+
.replace(/punct/g, _punctuation)
|
|
1130
|
+
.getRegex();
|
|
1131
|
+
const emStrongRDelimAst = edit('^[^_*]*?__[^_*]*?\\*[^_*]*?(?=__)' // Skip orphan inside strong
|
|
1132
|
+
+ '|[^*]+(?=[^*])' // Consume to delim
|
|
1133
|
+
+ '|(?!\\*)[punct](\\*+)(?=[\\s]|$)' // (1) #*** can only be a Right Delimiter
|
|
1134
|
+
+ '|[^punct\\s](\\*+)(?!\\*)(?=[punct\\s]|$)' // (2) a***#, a*** can only be a Right Delimiter
|
|
1135
|
+
+ '|(?!\\*)[punct\\s](\\*+)(?=[^punct\\s])' // (3) #***a, ***a can only be Left Delimiter
|
|
1136
|
+
+ '|[\\s](\\*+)(?!\\*)(?=[punct])' // (4) ***# can only be Left Delimiter
|
|
1137
|
+
+ '|(?!\\*)[punct](\\*+)(?!\\*)(?=[punct])' // (5) #***# can be either Left or Right Delimiter
|
|
1138
|
+
+ '|[^punct\\s](\\*+)(?=[^punct\\s])', 'gu') // (6) a***a can be either Left or Right Delimiter
|
|
1139
|
+
.replace(/punct/g, _punctuation)
|
|
1140
|
+
.getRegex();
|
|
1141
|
+
// (6) Not allowed for _
|
|
1142
|
+
const emStrongRDelimUnd = edit('^[^_*]*?\\*\\*[^_*]*?_[^_*]*?(?=\\*\\*)' // Skip orphan inside strong
|
|
1143
|
+
+ '|[^_]+(?=[^_])' // Consume to delim
|
|
1144
|
+
+ '|(?!_)[punct](_+)(?=[\\s]|$)' // (1) #___ can only be a Right Delimiter
|
|
1145
|
+
+ '|[^punct\\s](_+)(?!_)(?=[punct\\s]|$)' // (2) a___#, a___ can only be a Right Delimiter
|
|
1146
|
+
+ '|(?!_)[punct\\s](_+)(?=[^punct\\s])' // (3) #___a, ___a can only be Left Delimiter
|
|
1147
|
+
+ '|[\\s](_+)(?!_)(?=[punct])' // (4) ___# can only be Left Delimiter
|
|
1148
|
+
+ '|(?!_)[punct](_+)(?!_)(?=[punct])', 'gu') // (5) #___# can be either Left or Right Delimiter
|
|
1149
|
+
.replace(/punct/g, _punctuation)
|
|
1150
|
+
.getRegex();
|
|
1151
|
+
const anyPunctuation = edit(/\\([punct])/, 'gu')
|
|
1152
|
+
.replace(/punct/g, _punctuation)
|
|
1153
|
+
.getRegex();
|
|
1154
|
+
const autolink = edit(/^<(scheme:[^\s\x00-\x1f<>]*|email)>/)
|
|
1155
|
+
.replace('scheme', /[a-zA-Z][a-zA-Z0-9+.-]{1,31}/)
|
|
1156
|
+
.replace('email', /[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+(@)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(?![-_])/)
|
|
1157
|
+
.getRegex();
|
|
1158
|
+
const _inlineComment = edit(_comment).replace('(?:-->|$)', '-->').getRegex();
|
|
1159
|
+
const tag = edit('^comment'
|
|
1160
|
+
+ '|^</[a-zA-Z][\\w:-]*\\s*>' // self-closing tag
|
|
1161
|
+
+ '|^<[a-zA-Z][\\w-]*(?:attribute)*?\\s*/?>' // open tag
|
|
1162
|
+
+ '|^<\\?[\\s\\S]*?\\?>' // processing instruction, e.g. <?php ?>
|
|
1163
|
+
+ '|^<![a-zA-Z]+\\s[\\s\\S]*?>' // declaration, e.g. <!DOCTYPE html>
|
|
1164
|
+
+ '|^<!\\[CDATA\\[[\\s\\S]*?\\]\\]>') // CDATA section
|
|
1165
|
+
.replace('comment', _inlineComment)
|
|
1166
|
+
.replace('attribute', /\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?/)
|
|
1167
|
+
.getRegex();
|
|
1168
|
+
const _inlineLabel = /(?:\[(?:\\.|[^\[\]\\])*\]|\\.|`[^`]*`|[^\[\]\\`])*?/;
|
|
1169
|
+
const link = edit(/^!?\[(label)\]\(\s*(href)(?:\s+(title))?\s*\)/)
|
|
1170
|
+
.replace('label', _inlineLabel)
|
|
1171
|
+
.replace('href', /<(?:\\.|[^\n<>\\])+>|[^\s\x00-\x1f]*/)
|
|
1172
|
+
.replace('title', /"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/)
|
|
1173
|
+
.getRegex();
|
|
1174
|
+
const reflink = edit(/^!?\[(label)\]\[(ref)\]/)
|
|
1175
|
+
.replace('label', _inlineLabel)
|
|
1176
|
+
.replace('ref', _blockLabel)
|
|
1177
|
+
.getRegex();
|
|
1178
|
+
const nolink = edit(/^!?\[(ref)\](?:\[\])?/)
|
|
1179
|
+
.replace('ref', _blockLabel)
|
|
1180
|
+
.getRegex();
|
|
1181
|
+
const reflinkSearch = edit('reflink|nolink(?!\\()', 'g')
|
|
1182
|
+
.replace('reflink', reflink)
|
|
1183
|
+
.replace('nolink', nolink)
|
|
1184
|
+
.getRegex();
|
|
1185
|
+
/**
|
|
1186
|
+
* Normal Inline Grammar
|
|
1187
|
+
*/
|
|
1188
|
+
const inlineNormal = {
|
|
1189
|
+
_backpedal: noopTest, // only used for GFM url
|
|
1190
|
+
anyPunctuation,
|
|
1191
|
+
autolink,
|
|
1192
|
+
blockSkip,
|
|
1193
|
+
br,
|
|
1194
|
+
code: inlineCode,
|
|
1195
|
+
del: noopTest,
|
|
1196
|
+
emStrongLDelim,
|
|
1197
|
+
emStrongRDelimAst,
|
|
1198
|
+
emStrongRDelimUnd,
|
|
1199
|
+
escape,
|
|
1200
|
+
link,
|
|
1201
|
+
nolink,
|
|
1202
|
+
punctuation,
|
|
1203
|
+
reflink,
|
|
1204
|
+
reflinkSearch,
|
|
1205
|
+
tag,
|
|
1206
|
+
text: inlineText,
|
|
1207
|
+
url: noopTest,
|
|
1208
|
+
};
|
|
1209
|
+
/**
|
|
1210
|
+
* Pedantic Inline Grammar
|
|
1211
|
+
*/
|
|
1212
|
+
const inlinePedantic = {
|
|
1213
|
+
...inlineNormal,
|
|
1214
|
+
link: edit(/^!?\[(label)\]\((.*?)\)/)
|
|
1215
|
+
.replace('label', _inlineLabel)
|
|
1216
|
+
.getRegex(),
|
|
1217
|
+
reflink: edit(/^!?\[(label)\]\s*\[([^\]]*)\]/)
|
|
1218
|
+
.replace('label', _inlineLabel)
|
|
1219
|
+
.getRegex(),
|
|
1220
|
+
};
|
|
1221
|
+
/**
|
|
1222
|
+
* GFM Inline Grammar
|
|
1223
|
+
*/
|
|
1224
|
+
const inlineGfm = {
|
|
1225
|
+
...inlineNormal,
|
|
1226
|
+
escape: edit(escape).replace('])', '~|])').getRegex(),
|
|
1227
|
+
url: edit(/^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/, 'i')
|
|
1228
|
+
.replace('email', /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/)
|
|
1229
|
+
.getRegex(),
|
|
1230
|
+
_backpedal: /(?:[^?!.,:;*_'"~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_'"~)]+(?!$))+/,
|
|
1231
|
+
del: /^(~~?)(?=[^\s~])([\s\S]*?[^\s~])\1(?=[^~]|$)/,
|
|
1232
|
+
text: /^([`~]+|[^`~])(?:(?= {2,}\n)|(?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)|[\s\S]*?(?:(?=[\\<!\[`*~_]|\b_|https?:\/\/|ftp:\/\/|www\.|$)|[^ ](?= {2,}\n)|[^a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-](?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)))/,
|
|
1233
|
+
};
|
|
1234
|
+
/**
|
|
1235
|
+
* GFM + Line Breaks Inline Grammar
|
|
1236
|
+
*/
|
|
1237
|
+
const inlineBreaks = {
|
|
1238
|
+
...inlineGfm,
|
|
1239
|
+
br: edit(br).replace('{2,}', '*').getRegex(),
|
|
1240
|
+
text: edit(inlineGfm.text)
|
|
1241
|
+
.replace('\\b_', '\\b_| {2,}\\n')
|
|
1242
|
+
.replace(/\{2,\}/g, '*')
|
|
1243
|
+
.getRegex(),
|
|
1244
|
+
};
|
|
1245
|
+
/**
|
|
1246
|
+
* exports
|
|
1247
|
+
*/
|
|
1248
|
+
const block = {
|
|
1249
|
+
normal: blockNormal,
|
|
1250
|
+
gfm: blockGfm,
|
|
1251
|
+
pedantic: blockPedantic,
|
|
1252
|
+
};
|
|
1253
|
+
const inline = {
|
|
1254
|
+
normal: inlineNormal,
|
|
1255
|
+
gfm: inlineGfm,
|
|
1256
|
+
breaks: inlineBreaks,
|
|
1257
|
+
pedantic: inlinePedantic,
|
|
1258
|
+
};
|
|
1259
|
+
|
|
1260
|
+
/**
|
|
1261
|
+
* Block Lexer
|
|
1262
|
+
*/
|
|
1263
|
+
class _Lexer {
|
|
1264
|
+
tokens;
|
|
1265
|
+
options;
|
|
1266
|
+
state;
|
|
1267
|
+
tokenizer;
|
|
1268
|
+
inlineQueue;
|
|
1269
|
+
constructor(options) {
|
|
1270
|
+
// TokenList cannot be created in one go
|
|
1271
|
+
this.tokens = [];
|
|
1272
|
+
this.tokens.links = Object.create(null);
|
|
1273
|
+
this.options = options || exports.defaults;
|
|
1274
|
+
this.options.tokenizer = this.options.tokenizer || new _Tokenizer();
|
|
1275
|
+
this.tokenizer = this.options.tokenizer;
|
|
1276
|
+
this.tokenizer.options = this.options;
|
|
1277
|
+
this.tokenizer.lexer = this;
|
|
1278
|
+
this.inlineQueue = [];
|
|
1279
|
+
this.state = {
|
|
1280
|
+
inLink: false,
|
|
1281
|
+
inRawBlock: false,
|
|
1282
|
+
top: true,
|
|
1283
|
+
};
|
|
1284
|
+
const rules = {
|
|
1285
|
+
block: block.normal,
|
|
1286
|
+
inline: inline.normal,
|
|
1287
|
+
};
|
|
1288
|
+
if (this.options.pedantic) {
|
|
1289
|
+
rules.block = block.pedantic;
|
|
1290
|
+
rules.inline = inline.pedantic;
|
|
1291
|
+
}
|
|
1292
|
+
else if (this.options.gfm) {
|
|
1293
|
+
rules.block = block.gfm;
|
|
1294
|
+
if (this.options.breaks) {
|
|
1295
|
+
rules.inline = inline.breaks;
|
|
1296
|
+
}
|
|
1297
|
+
else {
|
|
1298
|
+
rules.inline = inline.gfm;
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
this.tokenizer.rules = rules;
|
|
1302
|
+
}
|
|
1303
|
+
/**
|
|
1304
|
+
* Expose Rules
|
|
1305
|
+
*/
|
|
1306
|
+
static get rules() {
|
|
1307
|
+
return {
|
|
1308
|
+
block,
|
|
1309
|
+
inline,
|
|
1310
|
+
};
|
|
1311
|
+
}
|
|
1312
|
+
/**
|
|
1313
|
+
* Static Lex Method
|
|
1314
|
+
*/
|
|
1315
|
+
static lex(src, options) {
|
|
1316
|
+
const lexer = new _Lexer(options);
|
|
1317
|
+
return lexer.lex(src);
|
|
1318
|
+
}
|
|
1319
|
+
/**
|
|
1320
|
+
* Static Lex Inline Method
|
|
1321
|
+
*/
|
|
1322
|
+
static lexInline(src, options) {
|
|
1323
|
+
const lexer = new _Lexer(options);
|
|
1324
|
+
return lexer.inlineTokens(src);
|
|
1325
|
+
}
|
|
1326
|
+
/**
|
|
1327
|
+
* Preprocessing
|
|
1328
|
+
*/
|
|
1329
|
+
lex(src) {
|
|
1330
|
+
src = src
|
|
1331
|
+
.replace(/\r\n|\r/g, '\n');
|
|
1332
|
+
this.blockTokens(src, this.tokens);
|
|
1333
|
+
for (let i = 0; i < this.inlineQueue.length; i++) {
|
|
1334
|
+
const next = this.inlineQueue[i];
|
|
1335
|
+
this.inlineTokens(next.src, next.tokens);
|
|
1336
|
+
}
|
|
1337
|
+
this.inlineQueue = [];
|
|
1338
|
+
return this.tokens;
|
|
1339
|
+
}
|
|
1340
|
+
blockTokens(src, tokens = [], lastParagraphClipped = false) {
|
|
1341
|
+
if (this.options.pedantic) {
|
|
1342
|
+
src = src.replace(/\t/g, ' ').replace(/^ +$/gm, '');
|
|
1343
|
+
}
|
|
1344
|
+
else {
|
|
1345
|
+
src = src.replace(/^( *)(\t+)/gm, (_, leading, tabs) => {
|
|
1346
|
+
return leading + ' '.repeat(tabs.length);
|
|
1347
|
+
});
|
|
1348
|
+
}
|
|
1349
|
+
let token;
|
|
1350
|
+
let lastToken;
|
|
1351
|
+
let cutSrc;
|
|
1352
|
+
while (src) {
|
|
1353
|
+
if (this.options.extensions
|
|
1354
|
+
&& this.options.extensions.block
|
|
1355
|
+
&& this.options.extensions.block.some((extTokenizer) => {
|
|
1356
|
+
if (token = extTokenizer.call({ lexer: this }, src, tokens)) {
|
|
1357
|
+
src = src.substring(token.raw.length);
|
|
1358
|
+
tokens.push(token);
|
|
1359
|
+
return true;
|
|
1360
|
+
}
|
|
1361
|
+
return false;
|
|
1362
|
+
})) {
|
|
1363
|
+
continue;
|
|
1364
|
+
}
|
|
1365
|
+
// newline
|
|
1366
|
+
if (token = this.tokenizer.space(src)) {
|
|
1367
|
+
src = src.substring(token.raw.length);
|
|
1368
|
+
if (token.raw.length === 1 && tokens.length > 0) {
|
|
1369
|
+
// if there's a single \n as a spacer, it's terminating the last line,
|
|
1370
|
+
// so move it there so that we don't get unnecessary paragraph tags
|
|
1371
|
+
tokens[tokens.length - 1].raw += '\n';
|
|
1372
|
+
}
|
|
1373
|
+
else {
|
|
1374
|
+
tokens.push(token);
|
|
1375
|
+
}
|
|
1376
|
+
continue;
|
|
1377
|
+
}
|
|
1378
|
+
// code
|
|
1379
|
+
if (token = this.tokenizer.code(src)) {
|
|
1380
|
+
src = src.substring(token.raw.length);
|
|
1381
|
+
lastToken = tokens[tokens.length - 1];
|
|
1382
|
+
// An indented code block cannot interrupt a paragraph.
|
|
1383
|
+
if (lastToken && (lastToken.type === 'paragraph' || lastToken.type === 'text')) {
|
|
1384
|
+
lastToken.raw += '\n' + token.raw;
|
|
1385
|
+
lastToken.text += '\n' + token.text;
|
|
1386
|
+
this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
|
|
1387
|
+
}
|
|
1388
|
+
else {
|
|
1389
|
+
tokens.push(token);
|
|
1390
|
+
}
|
|
1391
|
+
continue;
|
|
1392
|
+
}
|
|
1393
|
+
// fences
|
|
1394
|
+
if (token = this.tokenizer.fences(src)) {
|
|
1395
|
+
src = src.substring(token.raw.length);
|
|
1396
|
+
tokens.push(token);
|
|
1397
|
+
continue;
|
|
1398
|
+
}
|
|
1399
|
+
// heading
|
|
1400
|
+
if (token = this.tokenizer.heading(src)) {
|
|
1401
|
+
src = src.substring(token.raw.length);
|
|
1402
|
+
tokens.push(token);
|
|
1403
|
+
continue;
|
|
1404
|
+
}
|
|
1405
|
+
// hr
|
|
1406
|
+
if (token = this.tokenizer.hr(src)) {
|
|
1407
|
+
src = src.substring(token.raw.length);
|
|
1408
|
+
tokens.push(token);
|
|
1409
|
+
continue;
|
|
1410
|
+
}
|
|
1411
|
+
// blockquote
|
|
1412
|
+
if (token = this.tokenizer.blockquote(src)) {
|
|
1413
|
+
src = src.substring(token.raw.length);
|
|
1414
|
+
tokens.push(token);
|
|
1415
|
+
continue;
|
|
1416
|
+
}
|
|
1417
|
+
// list
|
|
1418
|
+
if (token = this.tokenizer.list(src)) {
|
|
1419
|
+
src = src.substring(token.raw.length);
|
|
1420
|
+
tokens.push(token);
|
|
1421
|
+
continue;
|
|
1422
|
+
}
|
|
1423
|
+
// html
|
|
1424
|
+
if (token = this.tokenizer.html(src)) {
|
|
1425
|
+
src = src.substring(token.raw.length);
|
|
1426
|
+
tokens.push(token);
|
|
1427
|
+
continue;
|
|
1428
|
+
}
|
|
1429
|
+
// def
|
|
1430
|
+
if (token = this.tokenizer.def(src)) {
|
|
1431
|
+
src = src.substring(token.raw.length);
|
|
1432
|
+
lastToken = tokens[tokens.length - 1];
|
|
1433
|
+
if (lastToken && (lastToken.type === 'paragraph' || lastToken.type === 'text')) {
|
|
1434
|
+
lastToken.raw += '\n' + token.raw;
|
|
1435
|
+
lastToken.text += '\n' + token.raw;
|
|
1436
|
+
this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
|
|
1437
|
+
}
|
|
1438
|
+
else if (!this.tokens.links[token.tag]) {
|
|
1439
|
+
this.tokens.links[token.tag] = {
|
|
1440
|
+
href: token.href,
|
|
1441
|
+
title: token.title,
|
|
1442
|
+
};
|
|
1443
|
+
}
|
|
1444
|
+
continue;
|
|
1445
|
+
}
|
|
1446
|
+
// table (gfm)
|
|
1447
|
+
if (token = this.tokenizer.table(src)) {
|
|
1448
|
+
src = src.substring(token.raw.length);
|
|
1449
|
+
tokens.push(token);
|
|
1450
|
+
continue;
|
|
1451
|
+
}
|
|
1452
|
+
// lheading
|
|
1453
|
+
if (token = this.tokenizer.lheading(src)) {
|
|
1454
|
+
src = src.substring(token.raw.length);
|
|
1455
|
+
tokens.push(token);
|
|
1456
|
+
continue;
|
|
1457
|
+
}
|
|
1458
|
+
// top-level paragraph
|
|
1459
|
+
// prevent paragraph consuming extensions by clipping 'src' to extension start
|
|
1460
|
+
cutSrc = src;
|
|
1461
|
+
if (this.options.extensions && this.options.extensions.startBlock) {
|
|
1462
|
+
let startIndex = Infinity;
|
|
1463
|
+
const tempSrc = src.slice(1);
|
|
1464
|
+
let tempStart;
|
|
1465
|
+
this.options.extensions.startBlock.forEach((getStartIndex) => {
|
|
1466
|
+
tempStart = getStartIndex.call({ lexer: this }, tempSrc);
|
|
1467
|
+
if (typeof tempStart === 'number' && tempStart >= 0) {
|
|
1468
|
+
startIndex = Math.min(startIndex, tempStart);
|
|
1469
|
+
}
|
|
1470
|
+
});
|
|
1471
|
+
if (startIndex < Infinity && startIndex >= 0) {
|
|
1472
|
+
cutSrc = src.substring(0, startIndex + 1);
|
|
1473
|
+
}
|
|
1474
|
+
}
|
|
1475
|
+
if (this.state.top && (token = this.tokenizer.paragraph(cutSrc))) {
|
|
1476
|
+
lastToken = tokens[tokens.length - 1];
|
|
1477
|
+
if (lastParagraphClipped && lastToken?.type === 'paragraph') {
|
|
1478
|
+
lastToken.raw += '\n' + token.raw;
|
|
1479
|
+
lastToken.text += '\n' + token.text;
|
|
1480
|
+
this.inlineQueue.pop();
|
|
1481
|
+
this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
|
|
1482
|
+
}
|
|
1483
|
+
else {
|
|
1484
|
+
tokens.push(token);
|
|
1485
|
+
}
|
|
1486
|
+
lastParagraphClipped = (cutSrc.length !== src.length);
|
|
1487
|
+
src = src.substring(token.raw.length);
|
|
1488
|
+
continue;
|
|
1489
|
+
}
|
|
1490
|
+
// text
|
|
1491
|
+
if (token = this.tokenizer.text(src)) {
|
|
1492
|
+
src = src.substring(token.raw.length);
|
|
1493
|
+
lastToken = tokens[tokens.length - 1];
|
|
1494
|
+
if (lastToken && lastToken.type === 'text') {
|
|
1495
|
+
lastToken.raw += '\n' + token.raw;
|
|
1496
|
+
lastToken.text += '\n' + token.text;
|
|
1497
|
+
this.inlineQueue.pop();
|
|
1498
|
+
this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
|
|
1499
|
+
}
|
|
1500
|
+
else {
|
|
1501
|
+
tokens.push(token);
|
|
1502
|
+
}
|
|
1503
|
+
continue;
|
|
1504
|
+
}
|
|
1505
|
+
if (src) {
|
|
1506
|
+
const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0);
|
|
1507
|
+
if (this.options.silent) {
|
|
1508
|
+
console.error(errMsg);
|
|
1509
|
+
break;
|
|
1510
|
+
}
|
|
1511
|
+
else {
|
|
1512
|
+
throw new Error(errMsg);
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
1515
|
+
}
|
|
1516
|
+
this.state.top = true;
|
|
1517
|
+
return tokens;
|
|
1518
|
+
}
|
|
1519
|
+
inline(src, tokens = []) {
|
|
1520
|
+
this.inlineQueue.push({ src, tokens });
|
|
1521
|
+
return tokens;
|
|
1522
|
+
}
|
|
1523
|
+
/**
|
|
1524
|
+
* Lexing/Compiling
|
|
1525
|
+
*/
|
|
1526
|
+
inlineTokens(src, tokens = []) {
|
|
1527
|
+
let token, lastToken, cutSrc;
|
|
1528
|
+
// String with links masked to avoid interference with em and strong
|
|
1529
|
+
let maskedSrc = src;
|
|
1530
|
+
let match;
|
|
1531
|
+
let keepPrevChar, prevChar;
|
|
1532
|
+
// Mask out reflinks
|
|
1533
|
+
if (this.tokens.links) {
|
|
1534
|
+
const links = Object.keys(this.tokens.links);
|
|
1535
|
+
if (links.length > 0) {
|
|
1536
|
+
while ((match = this.tokenizer.rules.inline.reflinkSearch.exec(maskedSrc)) != null) {
|
|
1537
|
+
if (links.includes(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
|
|
1538
|
+
maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex);
|
|
1539
|
+
}
|
|
1540
|
+
}
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
// Mask out other blocks
|
|
1544
|
+
while ((match = this.tokenizer.rules.inline.blockSkip.exec(maskedSrc)) != null) {
|
|
1545
|
+
maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
|
|
1546
|
+
}
|
|
1547
|
+
// Mask out escaped characters
|
|
1548
|
+
while ((match = this.tokenizer.rules.inline.anyPunctuation.exec(maskedSrc)) != null) {
|
|
1549
|
+
maskedSrc = maskedSrc.slice(0, match.index) + '++' + maskedSrc.slice(this.tokenizer.rules.inline.anyPunctuation.lastIndex);
|
|
1550
|
+
}
|
|
1551
|
+
while (src) {
|
|
1552
|
+
if (!keepPrevChar) {
|
|
1553
|
+
prevChar = '';
|
|
1554
|
+
}
|
|
1555
|
+
keepPrevChar = false;
|
|
1556
|
+
// extensions
|
|
1557
|
+
if (this.options.extensions
|
|
1558
|
+
&& this.options.extensions.inline
|
|
1559
|
+
&& this.options.extensions.inline.some((extTokenizer) => {
|
|
1560
|
+
if (token = extTokenizer.call({ lexer: this }, src, tokens)) {
|
|
1561
|
+
src = src.substring(token.raw.length);
|
|
1562
|
+
tokens.push(token);
|
|
1563
|
+
return true;
|
|
1564
|
+
}
|
|
1565
|
+
return false;
|
|
1566
|
+
})) {
|
|
1567
|
+
continue;
|
|
1568
|
+
}
|
|
1569
|
+
// escape
|
|
1570
|
+
if (token = this.tokenizer.escape(src)) {
|
|
1571
|
+
src = src.substring(token.raw.length);
|
|
1572
|
+
tokens.push(token);
|
|
1573
|
+
continue;
|
|
1574
|
+
}
|
|
1575
|
+
// tag
|
|
1576
|
+
if (token = this.tokenizer.tag(src)) {
|
|
1577
|
+
src = src.substring(token.raw.length);
|
|
1578
|
+
lastToken = tokens[tokens.length - 1];
|
|
1579
|
+
if (lastToken && token.type === 'text' && lastToken.type === 'text') {
|
|
1580
|
+
lastToken.raw += token.raw;
|
|
1581
|
+
lastToken.text += token.text;
|
|
1582
|
+
}
|
|
1583
|
+
else {
|
|
1584
|
+
tokens.push(token);
|
|
1585
|
+
}
|
|
1586
|
+
continue;
|
|
1587
|
+
}
|
|
1588
|
+
// link
|
|
1589
|
+
if (token = this.tokenizer.link(src)) {
|
|
1590
|
+
src = src.substring(token.raw.length);
|
|
1591
|
+
tokens.push(token);
|
|
1592
|
+
continue;
|
|
1593
|
+
}
|
|
1594
|
+
// reflink, nolink
|
|
1595
|
+
if (token = this.tokenizer.reflink(src, this.tokens.links)) {
|
|
1596
|
+
src = src.substring(token.raw.length);
|
|
1597
|
+
lastToken = tokens[tokens.length - 1];
|
|
1598
|
+
if (lastToken && token.type === 'text' && lastToken.type === 'text') {
|
|
1599
|
+
lastToken.raw += token.raw;
|
|
1600
|
+
lastToken.text += token.text;
|
|
1601
|
+
}
|
|
1602
|
+
else {
|
|
1603
|
+
tokens.push(token);
|
|
1604
|
+
}
|
|
1605
|
+
continue;
|
|
1606
|
+
}
|
|
1607
|
+
// em & strong
|
|
1608
|
+
if (token = this.tokenizer.emStrong(src, maskedSrc, prevChar)) {
|
|
1609
|
+
src = src.substring(token.raw.length);
|
|
1610
|
+
tokens.push(token);
|
|
1611
|
+
continue;
|
|
1612
|
+
}
|
|
1613
|
+
// code
|
|
1614
|
+
if (token = this.tokenizer.codespan(src)) {
|
|
1615
|
+
src = src.substring(token.raw.length);
|
|
1616
|
+
tokens.push(token);
|
|
1617
|
+
continue;
|
|
1618
|
+
}
|
|
1619
|
+
// br
|
|
1620
|
+
if (token = this.tokenizer.br(src)) {
|
|
1621
|
+
src = src.substring(token.raw.length);
|
|
1622
|
+
tokens.push(token);
|
|
1623
|
+
continue;
|
|
1624
|
+
}
|
|
1625
|
+
// del (gfm)
|
|
1626
|
+
if (token = this.tokenizer.del(src)) {
|
|
1627
|
+
src = src.substring(token.raw.length);
|
|
1628
|
+
tokens.push(token);
|
|
1629
|
+
continue;
|
|
1630
|
+
}
|
|
1631
|
+
// autolink
|
|
1632
|
+
if (token = this.tokenizer.autolink(src)) {
|
|
1633
|
+
src = src.substring(token.raw.length);
|
|
1634
|
+
tokens.push(token);
|
|
1635
|
+
continue;
|
|
1636
|
+
}
|
|
1637
|
+
// url (gfm)
|
|
1638
|
+
if (!this.state.inLink && (token = this.tokenizer.url(src))) {
|
|
1639
|
+
src = src.substring(token.raw.length);
|
|
1640
|
+
tokens.push(token);
|
|
1641
|
+
continue;
|
|
1642
|
+
}
|
|
1643
|
+
// text
|
|
1644
|
+
// prevent inlineText consuming extensions by clipping 'src' to extension start
|
|
1645
|
+
cutSrc = src;
|
|
1646
|
+
if (this.options.extensions && this.options.extensions.startInline) {
|
|
1647
|
+
let startIndex = Infinity;
|
|
1648
|
+
const tempSrc = src.slice(1);
|
|
1649
|
+
let tempStart;
|
|
1650
|
+
this.options.extensions.startInline.forEach((getStartIndex) => {
|
|
1651
|
+
tempStart = getStartIndex.call({ lexer: this }, tempSrc);
|
|
1652
|
+
if (typeof tempStart === 'number' && tempStart >= 0) {
|
|
1653
|
+
startIndex = Math.min(startIndex, tempStart);
|
|
1654
|
+
}
|
|
1655
|
+
});
|
|
1656
|
+
if (startIndex < Infinity && startIndex >= 0) {
|
|
1657
|
+
cutSrc = src.substring(0, startIndex + 1);
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
1660
|
+
if (token = this.tokenizer.inlineText(cutSrc)) {
|
|
1661
|
+
src = src.substring(token.raw.length);
|
|
1662
|
+
if (token.raw.slice(-1) !== '_') { // Track prevChar before string of ____ started
|
|
1663
|
+
prevChar = token.raw.slice(-1);
|
|
1664
|
+
}
|
|
1665
|
+
keepPrevChar = true;
|
|
1666
|
+
lastToken = tokens[tokens.length - 1];
|
|
1667
|
+
if (lastToken && lastToken.type === 'text') {
|
|
1668
|
+
lastToken.raw += token.raw;
|
|
1669
|
+
lastToken.text += token.text;
|
|
1670
|
+
}
|
|
1671
|
+
else {
|
|
1672
|
+
tokens.push(token);
|
|
1673
|
+
}
|
|
1674
|
+
continue;
|
|
1675
|
+
}
|
|
1676
|
+
if (src) {
|
|
1677
|
+
const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0);
|
|
1678
|
+
if (this.options.silent) {
|
|
1679
|
+
console.error(errMsg);
|
|
1680
|
+
break;
|
|
1681
|
+
}
|
|
1682
|
+
else {
|
|
1683
|
+
throw new Error(errMsg);
|
|
1684
|
+
}
|
|
1685
|
+
}
|
|
1686
|
+
}
|
|
1687
|
+
return tokens;
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
|
|
1691
|
+
/**
|
|
1692
|
+
* Renderer
|
|
1693
|
+
*/
|
|
1694
|
+
class _Renderer {
|
|
1695
|
+
options;
|
|
1696
|
+
parser; // set by the parser
|
|
1697
|
+
constructor(options) {
|
|
1698
|
+
this.options = options || exports.defaults;
|
|
1699
|
+
}
|
|
1700
|
+
space(token) {
|
|
1701
|
+
return '';
|
|
1702
|
+
}
|
|
1703
|
+
code({ text, lang, escaped }) {
|
|
1704
|
+
const langString = (lang || '').match(/^\S*/)?.[0];
|
|
1705
|
+
const code = text.replace(/\n$/, '') + '\n';
|
|
1706
|
+
if (!langString) {
|
|
1707
|
+
return '<pre><code>'
|
|
1708
|
+
+ (escaped ? code : escape$1(code, true))
|
|
1709
|
+
+ '</code></pre>\n';
|
|
1710
|
+
}
|
|
1711
|
+
return '<pre><code class="language-'
|
|
1712
|
+
+ escape$1(langString)
|
|
1713
|
+
+ '">'
|
|
1714
|
+
+ (escaped ? code : escape$1(code, true))
|
|
1715
|
+
+ '</code></pre>\n';
|
|
1716
|
+
}
|
|
1717
|
+
blockquote({ tokens }) {
|
|
1718
|
+
const body = this.parser.parse(tokens);
|
|
1719
|
+
return `<blockquote>\n${body}</blockquote>\n`;
|
|
1720
|
+
}
|
|
1721
|
+
html({ text }) {
|
|
1722
|
+
return text;
|
|
1723
|
+
}
|
|
1724
|
+
heading({ tokens, depth }) {
|
|
1725
|
+
return `<h${depth}>${this.parser.parseInline(tokens)}</h${depth}>\n`;
|
|
1726
|
+
}
|
|
1727
|
+
hr(token) {
|
|
1728
|
+
return '<hr>\n';
|
|
1729
|
+
}
|
|
1730
|
+
list(token) {
|
|
1731
|
+
const ordered = token.ordered;
|
|
1732
|
+
const start = token.start;
|
|
1733
|
+
let body = '';
|
|
1734
|
+
for (let j = 0; j < token.items.length; j++) {
|
|
1735
|
+
const item = token.items[j];
|
|
1736
|
+
body += this.listitem(item);
|
|
1737
|
+
}
|
|
1738
|
+
const type = ordered ? 'ol' : 'ul';
|
|
1739
|
+
const startAttr = (ordered && start !== 1) ? (' start="' + start + '"') : '';
|
|
1740
|
+
return '<' + type + startAttr + '>\n' + body + '</' + type + '>\n';
|
|
1741
|
+
}
|
|
1742
|
+
listitem(item) {
|
|
1743
|
+
let itemBody = '';
|
|
1744
|
+
if (item.task) {
|
|
1745
|
+
const checkbox = this.checkbox({ checked: !!item.checked });
|
|
1746
|
+
if (item.loose) {
|
|
1747
|
+
if (item.tokens.length > 0 && item.tokens[0].type === 'paragraph') {
|
|
1748
|
+
item.tokens[0].text = checkbox + ' ' + item.tokens[0].text;
|
|
1749
|
+
if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') {
|
|
1750
|
+
item.tokens[0].tokens[0].text = checkbox + ' ' + item.tokens[0].tokens[0].text;
|
|
1751
|
+
}
|
|
1752
|
+
}
|
|
1753
|
+
else {
|
|
1754
|
+
item.tokens.unshift({
|
|
1755
|
+
type: 'text',
|
|
1756
|
+
raw: checkbox + ' ',
|
|
1757
|
+
text: checkbox + ' ',
|
|
1758
|
+
});
|
|
1759
|
+
}
|
|
1760
|
+
}
|
|
1761
|
+
else {
|
|
1762
|
+
itemBody += checkbox + ' ';
|
|
1763
|
+
}
|
|
1764
|
+
}
|
|
1765
|
+
itemBody += this.parser.parse(item.tokens, !!item.loose);
|
|
1766
|
+
return `<li>${itemBody}</li>\n`;
|
|
1767
|
+
}
|
|
1768
|
+
checkbox({ checked }) {
|
|
1769
|
+
return '<input '
|
|
1770
|
+
+ (checked ? 'checked="" ' : '')
|
|
1771
|
+
+ 'disabled="" type="checkbox">';
|
|
1772
|
+
}
|
|
1773
|
+
paragraph({ tokens }) {
|
|
1774
|
+
return `<p>${this.parser.parseInline(tokens)}</p>\n`;
|
|
1775
|
+
}
|
|
1776
|
+
table(token) {
|
|
1777
|
+
let header = '';
|
|
1778
|
+
// header
|
|
1779
|
+
let cell = '';
|
|
1780
|
+
for (let j = 0; j < token.header.length; j++) {
|
|
1781
|
+
cell += this.tablecell(token.header[j]);
|
|
1782
|
+
}
|
|
1783
|
+
header += this.tablerow({ text: cell });
|
|
1784
|
+
let body = '';
|
|
1785
|
+
for (let j = 0; j < token.rows.length; j++) {
|
|
1786
|
+
const row = token.rows[j];
|
|
1787
|
+
cell = '';
|
|
1788
|
+
for (let k = 0; k < row.length; k++) {
|
|
1789
|
+
cell += this.tablecell(row[k]);
|
|
1790
|
+
}
|
|
1791
|
+
body += this.tablerow({ text: cell });
|
|
1792
|
+
}
|
|
1793
|
+
if (body)
|
|
1794
|
+
body = `<tbody>${body}</tbody>`;
|
|
1795
|
+
return '<table>\n'
|
|
1796
|
+
+ '<thead>\n'
|
|
1797
|
+
+ header
|
|
1798
|
+
+ '</thead>\n'
|
|
1799
|
+
+ body
|
|
1800
|
+
+ '</table>\n';
|
|
1801
|
+
}
|
|
1802
|
+
tablerow({ text }) {
|
|
1803
|
+
return `<tr>\n${text}</tr>\n`;
|
|
1804
|
+
}
|
|
1805
|
+
tablecell(token) {
|
|
1806
|
+
const content = this.parser.parseInline(token.tokens);
|
|
1807
|
+
const type = token.header ? 'th' : 'td';
|
|
1808
|
+
const tag = token.align
|
|
1809
|
+
? `<${type} align="${token.align}">`
|
|
1810
|
+
: `<${type}>`;
|
|
1811
|
+
return tag + content + `</${type}>\n`;
|
|
1812
|
+
}
|
|
1813
|
+
/**
|
|
1814
|
+
* span level renderer
|
|
1815
|
+
*/
|
|
1816
|
+
strong({ tokens }) {
|
|
1817
|
+
return `<strong>${this.parser.parseInline(tokens)}</strong>`;
|
|
1818
|
+
}
|
|
1819
|
+
em({ tokens }) {
|
|
1820
|
+
return `<em>${this.parser.parseInline(tokens)}</em>`;
|
|
1821
|
+
}
|
|
1822
|
+
codespan({ text }) {
|
|
1823
|
+
return `<code>${text}</code>`;
|
|
1824
|
+
}
|
|
1825
|
+
br(token) {
|
|
1826
|
+
return '<br>';
|
|
1827
|
+
}
|
|
1828
|
+
del({ tokens }) {
|
|
1829
|
+
return `<del>${this.parser.parseInline(tokens)}</del>`;
|
|
1830
|
+
}
|
|
1831
|
+
link({ href, title, tokens }) {
|
|
1832
|
+
const text = this.parser.parseInline(tokens);
|
|
1833
|
+
const cleanHref = cleanUrl(href);
|
|
1834
|
+
if (cleanHref === null) {
|
|
1835
|
+
return text;
|
|
1836
|
+
}
|
|
1837
|
+
href = cleanHref;
|
|
1838
|
+
let out = '<a href="' + href + '"';
|
|
1839
|
+
if (title) {
|
|
1840
|
+
out += ' title="' + title + '"';
|
|
1841
|
+
}
|
|
1842
|
+
out += '>' + text + '</a>';
|
|
1843
|
+
return out;
|
|
1844
|
+
}
|
|
1845
|
+
image({ href, title, text }) {
|
|
1846
|
+
const cleanHref = cleanUrl(href);
|
|
1847
|
+
if (cleanHref === null) {
|
|
1848
|
+
return text;
|
|
1849
|
+
}
|
|
1850
|
+
href = cleanHref;
|
|
1851
|
+
let out = `<img src="${href}" alt="${text}"`;
|
|
1852
|
+
if (title) {
|
|
1853
|
+
out += ` title="${title}"`;
|
|
1854
|
+
}
|
|
1855
|
+
out += '>';
|
|
1856
|
+
return out;
|
|
1857
|
+
}
|
|
1858
|
+
text(token) {
|
|
1859
|
+
return 'tokens' in token && token.tokens ? this.parser.parseInline(token.tokens) : token.text;
|
|
1860
|
+
}
|
|
1861
|
+
}
|
|
1862
|
+
|
|
1863
|
+
/**
|
|
1864
|
+
* TextRenderer
|
|
1865
|
+
* returns only the textual part of the token
|
|
1866
|
+
*/
|
|
1867
|
+
class _TextRenderer {
|
|
1868
|
+
// no need for block level renderers
|
|
1869
|
+
strong({ text }) {
|
|
1870
|
+
return text;
|
|
1871
|
+
}
|
|
1872
|
+
em({ text }) {
|
|
1873
|
+
return text;
|
|
1874
|
+
}
|
|
1875
|
+
codespan({ text }) {
|
|
1876
|
+
return text;
|
|
1877
|
+
}
|
|
1878
|
+
del({ text }) {
|
|
1879
|
+
return text;
|
|
1880
|
+
}
|
|
1881
|
+
html({ text }) {
|
|
1882
|
+
return text;
|
|
1883
|
+
}
|
|
1884
|
+
text({ text }) {
|
|
1885
|
+
return text;
|
|
1886
|
+
}
|
|
1887
|
+
link({ text }) {
|
|
1888
|
+
return '' + text;
|
|
1889
|
+
}
|
|
1890
|
+
image({ text }) {
|
|
1891
|
+
return '' + text;
|
|
1892
|
+
}
|
|
1893
|
+
br() {
|
|
1894
|
+
return '';
|
|
1895
|
+
}
|
|
1896
|
+
}
|
|
1897
|
+
|
|
1898
|
+
/**
|
|
1899
|
+
* Parsing & Compiling
|
|
1900
|
+
*/
|
|
1901
|
+
class _Parser {
|
|
1902
|
+
options;
|
|
1903
|
+
renderer;
|
|
1904
|
+
textRenderer;
|
|
1905
|
+
constructor(options) {
|
|
1906
|
+
this.options = options || exports.defaults;
|
|
1907
|
+
this.options.renderer = this.options.renderer || new _Renderer();
|
|
1908
|
+
this.renderer = this.options.renderer;
|
|
1909
|
+
this.renderer.options = this.options;
|
|
1910
|
+
this.renderer.parser = this;
|
|
1911
|
+
this.textRenderer = new _TextRenderer();
|
|
1912
|
+
}
|
|
1913
|
+
/**
|
|
1914
|
+
* Static Parse Method
|
|
1915
|
+
*/
|
|
1916
|
+
static parse(tokens, options) {
|
|
1917
|
+
const parser = new _Parser(options);
|
|
1918
|
+
return parser.parse(tokens);
|
|
1919
|
+
}
|
|
1920
|
+
/**
|
|
1921
|
+
* Static Parse Inline Method
|
|
1922
|
+
*/
|
|
1923
|
+
static parseInline(tokens, options) {
|
|
1924
|
+
const parser = new _Parser(options);
|
|
1925
|
+
return parser.parseInline(tokens);
|
|
1926
|
+
}
|
|
1927
|
+
/**
|
|
1928
|
+
* Parse Loop
|
|
1929
|
+
*/
|
|
1930
|
+
parse(tokens, top = true) {
|
|
1931
|
+
let out = '';
|
|
1932
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
1933
|
+
const anyToken = tokens[i];
|
|
1934
|
+
// Run any renderer extensions
|
|
1935
|
+
if (this.options.extensions && this.options.extensions.renderers && this.options.extensions.renderers[anyToken.type]) {
|
|
1936
|
+
const genericToken = anyToken;
|
|
1937
|
+
const ret = this.options.extensions.renderers[genericToken.type].call({ parser: this }, genericToken);
|
|
1938
|
+
if (ret !== false || !['space', 'hr', 'heading', 'code', 'table', 'blockquote', 'list', 'html', 'paragraph', 'text'].includes(genericToken.type)) {
|
|
1939
|
+
out += ret || '';
|
|
1940
|
+
continue;
|
|
1941
|
+
}
|
|
1942
|
+
}
|
|
1943
|
+
const token = anyToken;
|
|
1944
|
+
switch (token.type) {
|
|
1945
|
+
case 'space': {
|
|
1946
|
+
out += this.renderer.space(token);
|
|
1947
|
+
continue;
|
|
1948
|
+
}
|
|
1949
|
+
case 'hr': {
|
|
1950
|
+
out += this.renderer.hr(token);
|
|
1951
|
+
continue;
|
|
1952
|
+
}
|
|
1953
|
+
case 'heading': {
|
|
1954
|
+
out += this.renderer.heading(token);
|
|
1955
|
+
continue;
|
|
1956
|
+
}
|
|
1957
|
+
case 'code': {
|
|
1958
|
+
out += this.renderer.code(token);
|
|
1959
|
+
continue;
|
|
1960
|
+
}
|
|
1961
|
+
case 'table': {
|
|
1962
|
+
out += this.renderer.table(token);
|
|
1963
|
+
continue;
|
|
1964
|
+
}
|
|
1965
|
+
case 'blockquote': {
|
|
1966
|
+
out += this.renderer.blockquote(token);
|
|
1967
|
+
continue;
|
|
1968
|
+
}
|
|
1969
|
+
case 'list': {
|
|
1970
|
+
out += this.renderer.list(token);
|
|
1971
|
+
continue;
|
|
1972
|
+
}
|
|
1973
|
+
case 'html': {
|
|
1974
|
+
out += this.renderer.html(token);
|
|
1975
|
+
continue;
|
|
1976
|
+
}
|
|
1977
|
+
case 'paragraph': {
|
|
1978
|
+
out += this.renderer.paragraph(token);
|
|
1979
|
+
continue;
|
|
1980
|
+
}
|
|
1981
|
+
case 'text': {
|
|
1982
|
+
let textToken = token;
|
|
1983
|
+
let body = this.renderer.text(textToken);
|
|
1984
|
+
while (i + 1 < tokens.length && tokens[i + 1].type === 'text') {
|
|
1985
|
+
textToken = tokens[++i];
|
|
1986
|
+
body += '\n' + this.renderer.text(textToken);
|
|
1987
|
+
}
|
|
1988
|
+
if (top) {
|
|
1989
|
+
out += this.renderer.paragraph({
|
|
1990
|
+
type: 'paragraph',
|
|
1991
|
+
raw: body,
|
|
1992
|
+
text: body,
|
|
1993
|
+
tokens: [{ type: 'text', raw: body, text: body }],
|
|
1994
|
+
});
|
|
1995
|
+
}
|
|
1996
|
+
else {
|
|
1997
|
+
out += body;
|
|
1998
|
+
}
|
|
1999
|
+
continue;
|
|
2000
|
+
}
|
|
2001
|
+
default: {
|
|
2002
|
+
const errMsg = 'Token with "' + token.type + '" type was not found.';
|
|
2003
|
+
if (this.options.silent) {
|
|
2004
|
+
console.error(errMsg);
|
|
2005
|
+
return '';
|
|
2006
|
+
}
|
|
2007
|
+
else {
|
|
2008
|
+
throw new Error(errMsg);
|
|
2009
|
+
}
|
|
2010
|
+
}
|
|
2011
|
+
}
|
|
2012
|
+
}
|
|
2013
|
+
return out;
|
|
2014
|
+
}
|
|
2015
|
+
/**
|
|
2016
|
+
* Parse Inline Tokens
|
|
2017
|
+
*/
|
|
2018
|
+
parseInline(tokens, renderer) {
|
|
2019
|
+
renderer = renderer || this.renderer;
|
|
2020
|
+
let out = '';
|
|
2021
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
2022
|
+
const anyToken = tokens[i];
|
|
2023
|
+
// Run any renderer extensions
|
|
2024
|
+
if (this.options.extensions && this.options.extensions.renderers && this.options.extensions.renderers[anyToken.type]) {
|
|
2025
|
+
const ret = this.options.extensions.renderers[anyToken.type].call({ parser: this }, anyToken);
|
|
2026
|
+
if (ret !== false || !['escape', 'html', 'link', 'image', 'strong', 'em', 'codespan', 'br', 'del', 'text'].includes(anyToken.type)) {
|
|
2027
|
+
out += ret || '';
|
|
2028
|
+
continue;
|
|
2029
|
+
}
|
|
2030
|
+
}
|
|
2031
|
+
const token = anyToken;
|
|
2032
|
+
switch (token.type) {
|
|
2033
|
+
case 'escape': {
|
|
2034
|
+
out += renderer.text(token);
|
|
2035
|
+
break;
|
|
2036
|
+
}
|
|
2037
|
+
case 'html': {
|
|
2038
|
+
out += renderer.html(token);
|
|
2039
|
+
break;
|
|
2040
|
+
}
|
|
2041
|
+
case 'link': {
|
|
2042
|
+
out += renderer.link(token);
|
|
2043
|
+
break;
|
|
2044
|
+
}
|
|
2045
|
+
case 'image': {
|
|
2046
|
+
out += renderer.image(token);
|
|
2047
|
+
break;
|
|
2048
|
+
}
|
|
2049
|
+
case 'strong': {
|
|
2050
|
+
out += renderer.strong(token);
|
|
2051
|
+
break;
|
|
2052
|
+
}
|
|
2053
|
+
case 'em': {
|
|
2054
|
+
out += renderer.em(token);
|
|
2055
|
+
break;
|
|
2056
|
+
}
|
|
2057
|
+
case 'codespan': {
|
|
2058
|
+
out += renderer.codespan(token);
|
|
2059
|
+
break;
|
|
2060
|
+
}
|
|
2061
|
+
case 'br': {
|
|
2062
|
+
out += renderer.br(token);
|
|
2063
|
+
break;
|
|
2064
|
+
}
|
|
2065
|
+
case 'del': {
|
|
2066
|
+
out += renderer.del(token);
|
|
2067
|
+
break;
|
|
2068
|
+
}
|
|
2069
|
+
case 'text': {
|
|
2070
|
+
out += renderer.text(token);
|
|
2071
|
+
break;
|
|
2072
|
+
}
|
|
2073
|
+
default: {
|
|
2074
|
+
const errMsg = 'Token with "' + token.type + '" type was not found.';
|
|
2075
|
+
if (this.options.silent) {
|
|
2076
|
+
console.error(errMsg);
|
|
2077
|
+
return '';
|
|
2078
|
+
}
|
|
2079
|
+
else {
|
|
2080
|
+
throw new Error(errMsg);
|
|
2081
|
+
}
|
|
2082
|
+
}
|
|
2083
|
+
}
|
|
2084
|
+
}
|
|
2085
|
+
return out;
|
|
2086
|
+
}
|
|
2087
|
+
}
|
|
2088
|
+
|
|
2089
|
+
class _Hooks {
|
|
2090
|
+
options;
|
|
2091
|
+
constructor(options) {
|
|
2092
|
+
this.options = options || exports.defaults;
|
|
2093
|
+
}
|
|
2094
|
+
static passThroughHooks = new Set([
|
|
2095
|
+
'preprocess',
|
|
2096
|
+
'postprocess',
|
|
2097
|
+
'processAllTokens',
|
|
2098
|
+
]);
|
|
2099
|
+
/**
|
|
2100
|
+
* Process markdown before marked
|
|
2101
|
+
*/
|
|
2102
|
+
preprocess(markdown) {
|
|
2103
|
+
return markdown;
|
|
2104
|
+
}
|
|
2105
|
+
/**
|
|
2106
|
+
* Process HTML after marked is finished
|
|
2107
|
+
*/
|
|
2108
|
+
postprocess(html) {
|
|
2109
|
+
return html;
|
|
2110
|
+
}
|
|
2111
|
+
/**
|
|
2112
|
+
* Process all tokens before walk tokens
|
|
2113
|
+
*/
|
|
2114
|
+
processAllTokens(tokens) {
|
|
2115
|
+
return tokens;
|
|
2116
|
+
}
|
|
2117
|
+
}
|
|
2118
|
+
|
|
2119
|
+
class Marked {
|
|
2120
|
+
defaults = _getDefaults();
|
|
2121
|
+
options = this.setOptions;
|
|
2122
|
+
parse = this.parseMarkdown(_Lexer.lex, _Parser.parse);
|
|
2123
|
+
parseInline = this.parseMarkdown(_Lexer.lexInline, _Parser.parseInline);
|
|
2124
|
+
Parser = _Parser;
|
|
2125
|
+
Renderer = _Renderer;
|
|
2126
|
+
TextRenderer = _TextRenderer;
|
|
2127
|
+
Lexer = _Lexer;
|
|
2128
|
+
Tokenizer = _Tokenizer;
|
|
2129
|
+
Hooks = _Hooks;
|
|
2130
|
+
constructor(...args) {
|
|
2131
|
+
this.use(...args);
|
|
2132
|
+
}
|
|
2133
|
+
/**
|
|
2134
|
+
* Run callback for every token
|
|
2135
|
+
*/
|
|
2136
|
+
walkTokens(tokens, callback) {
|
|
2137
|
+
let values = [];
|
|
2138
|
+
for (const token of tokens) {
|
|
2139
|
+
values = values.concat(callback.call(this, token));
|
|
2140
|
+
switch (token.type) {
|
|
2141
|
+
case 'table': {
|
|
2142
|
+
const tableToken = token;
|
|
2143
|
+
for (const cell of tableToken.header) {
|
|
2144
|
+
values = values.concat(this.walkTokens(cell.tokens, callback));
|
|
2145
|
+
}
|
|
2146
|
+
for (const row of tableToken.rows) {
|
|
2147
|
+
for (const cell of row) {
|
|
2148
|
+
values = values.concat(this.walkTokens(cell.tokens, callback));
|
|
2149
|
+
}
|
|
2150
|
+
}
|
|
2151
|
+
break;
|
|
2152
|
+
}
|
|
2153
|
+
case 'list': {
|
|
2154
|
+
const listToken = token;
|
|
2155
|
+
values = values.concat(this.walkTokens(listToken.items, callback));
|
|
2156
|
+
break;
|
|
2157
|
+
}
|
|
2158
|
+
default: {
|
|
2159
|
+
const genericToken = token;
|
|
2160
|
+
if (this.defaults.extensions?.childTokens?.[genericToken.type]) {
|
|
2161
|
+
this.defaults.extensions.childTokens[genericToken.type].forEach((childTokens) => {
|
|
2162
|
+
const tokens = genericToken[childTokens].flat(Infinity);
|
|
2163
|
+
values = values.concat(this.walkTokens(tokens, callback));
|
|
2164
|
+
});
|
|
2165
|
+
}
|
|
2166
|
+
else if (genericToken.tokens) {
|
|
2167
|
+
values = values.concat(this.walkTokens(genericToken.tokens, callback));
|
|
2168
|
+
}
|
|
2169
|
+
}
|
|
2170
|
+
}
|
|
2171
|
+
}
|
|
2172
|
+
return values;
|
|
2173
|
+
}
|
|
2174
|
+
use(...args) {
|
|
2175
|
+
const extensions = this.defaults.extensions || { renderers: {}, childTokens: {} };
|
|
2176
|
+
args.forEach((pack) => {
|
|
2177
|
+
// copy options to new object
|
|
2178
|
+
const opts = { ...pack };
|
|
2179
|
+
// set async to true if it was set to true before
|
|
2180
|
+
opts.async = this.defaults.async || opts.async || false;
|
|
2181
|
+
// ==-- Parse "addon" extensions --== //
|
|
2182
|
+
if (pack.extensions) {
|
|
2183
|
+
pack.extensions.forEach((ext) => {
|
|
2184
|
+
if (!ext.name) {
|
|
2185
|
+
throw new Error('extension name required');
|
|
2186
|
+
}
|
|
2187
|
+
if ('renderer' in ext) { // Renderer extensions
|
|
2188
|
+
const prevRenderer = extensions.renderers[ext.name];
|
|
2189
|
+
if (prevRenderer) {
|
|
2190
|
+
// Replace extension with func to run new extension but fall back if false
|
|
2191
|
+
extensions.renderers[ext.name] = function (...args) {
|
|
2192
|
+
let ret = ext.renderer.apply(this, args);
|
|
2193
|
+
if (ret === false) {
|
|
2194
|
+
ret = prevRenderer.apply(this, args);
|
|
2195
|
+
}
|
|
2196
|
+
return ret;
|
|
2197
|
+
};
|
|
2198
|
+
}
|
|
2199
|
+
else {
|
|
2200
|
+
extensions.renderers[ext.name] = ext.renderer;
|
|
2201
|
+
}
|
|
2202
|
+
}
|
|
2203
|
+
if ('tokenizer' in ext) { // Tokenizer Extensions
|
|
2204
|
+
if (!ext.level || (ext.level !== 'block' && ext.level !== 'inline')) {
|
|
2205
|
+
throw new Error("extension level must be 'block' or 'inline'");
|
|
2206
|
+
}
|
|
2207
|
+
const extLevel = extensions[ext.level];
|
|
2208
|
+
if (extLevel) {
|
|
2209
|
+
extLevel.unshift(ext.tokenizer);
|
|
2210
|
+
}
|
|
2211
|
+
else {
|
|
2212
|
+
extensions[ext.level] = [ext.tokenizer];
|
|
2213
|
+
}
|
|
2214
|
+
if (ext.start) { // Function to check for start of token
|
|
2215
|
+
if (ext.level === 'block') {
|
|
2216
|
+
if (extensions.startBlock) {
|
|
2217
|
+
extensions.startBlock.push(ext.start);
|
|
2218
|
+
}
|
|
2219
|
+
else {
|
|
2220
|
+
extensions.startBlock = [ext.start];
|
|
2221
|
+
}
|
|
2222
|
+
}
|
|
2223
|
+
else if (ext.level === 'inline') {
|
|
2224
|
+
if (extensions.startInline) {
|
|
2225
|
+
extensions.startInline.push(ext.start);
|
|
2226
|
+
}
|
|
2227
|
+
else {
|
|
2228
|
+
extensions.startInline = [ext.start];
|
|
2229
|
+
}
|
|
2230
|
+
}
|
|
2231
|
+
}
|
|
2232
|
+
}
|
|
2233
|
+
if ('childTokens' in ext && ext.childTokens) { // Child tokens to be visited by walkTokens
|
|
2234
|
+
extensions.childTokens[ext.name] = ext.childTokens;
|
|
2235
|
+
}
|
|
2236
|
+
});
|
|
2237
|
+
opts.extensions = extensions;
|
|
2238
|
+
}
|
|
2239
|
+
// ==-- Parse "overwrite" extensions --== //
|
|
2240
|
+
if (pack.renderer) {
|
|
2241
|
+
const renderer = this.defaults.renderer || new _Renderer(this.defaults);
|
|
2242
|
+
for (const prop in pack.renderer) {
|
|
2243
|
+
if (!(prop in renderer)) {
|
|
2244
|
+
throw new Error(`renderer '${prop}' does not exist`);
|
|
2245
|
+
}
|
|
2246
|
+
if (['options', 'parser'].includes(prop)) {
|
|
2247
|
+
// ignore options property
|
|
2248
|
+
continue;
|
|
2249
|
+
}
|
|
2250
|
+
const rendererProp = prop;
|
|
2251
|
+
const rendererFunc = pack.renderer[rendererProp];
|
|
2252
|
+
const prevRenderer = renderer[rendererProp];
|
|
2253
|
+
// Replace renderer with func to run extension, but fall back if false
|
|
2254
|
+
renderer[rendererProp] = (...args) => {
|
|
2255
|
+
let ret = rendererFunc.apply(renderer, args);
|
|
2256
|
+
if (ret === false) {
|
|
2257
|
+
ret = prevRenderer.apply(renderer, args);
|
|
2258
|
+
}
|
|
2259
|
+
return ret || '';
|
|
2260
|
+
};
|
|
2261
|
+
}
|
|
2262
|
+
opts.renderer = renderer;
|
|
2263
|
+
}
|
|
2264
|
+
if (pack.tokenizer) {
|
|
2265
|
+
const tokenizer = this.defaults.tokenizer || new _Tokenizer(this.defaults);
|
|
2266
|
+
for (const prop in pack.tokenizer) {
|
|
2267
|
+
if (!(prop in tokenizer)) {
|
|
2268
|
+
throw new Error(`tokenizer '${prop}' does not exist`);
|
|
2269
|
+
}
|
|
2270
|
+
if (['options', 'rules', 'lexer'].includes(prop)) {
|
|
2271
|
+
// ignore options, rules, and lexer properties
|
|
2272
|
+
continue;
|
|
2273
|
+
}
|
|
2274
|
+
const tokenizerProp = prop;
|
|
2275
|
+
const tokenizerFunc = pack.tokenizer[tokenizerProp];
|
|
2276
|
+
const prevTokenizer = tokenizer[tokenizerProp];
|
|
2277
|
+
// Replace tokenizer with func to run extension, but fall back if false
|
|
2278
|
+
// @ts-expect-error cannot type tokenizer function dynamically
|
|
2279
|
+
tokenizer[tokenizerProp] = (...args) => {
|
|
2280
|
+
let ret = tokenizerFunc.apply(tokenizer, args);
|
|
2281
|
+
if (ret === false) {
|
|
2282
|
+
ret = prevTokenizer.apply(tokenizer, args);
|
|
2283
|
+
}
|
|
2284
|
+
return ret;
|
|
2285
|
+
};
|
|
2286
|
+
}
|
|
2287
|
+
opts.tokenizer = tokenizer;
|
|
2288
|
+
}
|
|
2289
|
+
// ==-- Parse Hooks extensions --== //
|
|
2290
|
+
if (pack.hooks) {
|
|
2291
|
+
const hooks = this.defaults.hooks || new _Hooks();
|
|
2292
|
+
for (const prop in pack.hooks) {
|
|
2293
|
+
if (!(prop in hooks)) {
|
|
2294
|
+
throw new Error(`hook '${prop}' does not exist`);
|
|
2295
|
+
}
|
|
2296
|
+
if (prop === 'options') {
|
|
2297
|
+
// ignore options property
|
|
2298
|
+
continue;
|
|
2299
|
+
}
|
|
2300
|
+
const hooksProp = prop;
|
|
2301
|
+
const hooksFunc = pack.hooks[hooksProp];
|
|
2302
|
+
const prevHook = hooks[hooksProp];
|
|
2303
|
+
if (_Hooks.passThroughHooks.has(prop)) {
|
|
2304
|
+
// @ts-expect-error cannot type hook function dynamically
|
|
2305
|
+
hooks[hooksProp] = (arg) => {
|
|
2306
|
+
if (this.defaults.async) {
|
|
2307
|
+
return Promise.resolve(hooksFunc.call(hooks, arg)).then(ret => {
|
|
2308
|
+
return prevHook.call(hooks, ret);
|
|
2309
|
+
});
|
|
2310
|
+
}
|
|
2311
|
+
const ret = hooksFunc.call(hooks, arg);
|
|
2312
|
+
return prevHook.call(hooks, ret);
|
|
2313
|
+
};
|
|
2314
|
+
}
|
|
2315
|
+
else {
|
|
2316
|
+
// @ts-expect-error cannot type hook function dynamically
|
|
2317
|
+
hooks[hooksProp] = (...args) => {
|
|
2318
|
+
let ret = hooksFunc.apply(hooks, args);
|
|
2319
|
+
if (ret === false) {
|
|
2320
|
+
ret = prevHook.apply(hooks, args);
|
|
2321
|
+
}
|
|
2322
|
+
return ret;
|
|
2323
|
+
};
|
|
2324
|
+
}
|
|
2325
|
+
}
|
|
2326
|
+
opts.hooks = hooks;
|
|
2327
|
+
}
|
|
2328
|
+
// ==-- Parse WalkTokens extensions --== //
|
|
2329
|
+
if (pack.walkTokens) {
|
|
2330
|
+
const walkTokens = this.defaults.walkTokens;
|
|
2331
|
+
const packWalktokens = pack.walkTokens;
|
|
2332
|
+
opts.walkTokens = function (token) {
|
|
2333
|
+
let values = [];
|
|
2334
|
+
values.push(packWalktokens.call(this, token));
|
|
2335
|
+
if (walkTokens) {
|
|
2336
|
+
values = values.concat(walkTokens.call(this, token));
|
|
2337
|
+
}
|
|
2338
|
+
return values;
|
|
2339
|
+
};
|
|
2340
|
+
}
|
|
2341
|
+
this.defaults = { ...this.defaults, ...opts };
|
|
2342
|
+
});
|
|
2343
|
+
return this;
|
|
2344
|
+
}
|
|
2345
|
+
setOptions(opt) {
|
|
2346
|
+
this.defaults = { ...this.defaults, ...opt };
|
|
2347
|
+
return this;
|
|
2348
|
+
}
|
|
2349
|
+
lexer(src, options) {
|
|
2350
|
+
return _Lexer.lex(src, options ?? this.defaults);
|
|
2351
|
+
}
|
|
2352
|
+
parser(tokens, options) {
|
|
2353
|
+
return _Parser.parse(tokens, options ?? this.defaults);
|
|
2354
|
+
}
|
|
2355
|
+
parseMarkdown(lexer, parser) {
|
|
2356
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2357
|
+
const parse = (src, options) => {
|
|
2358
|
+
const origOpt = { ...options };
|
|
2359
|
+
const opt = { ...this.defaults, ...origOpt };
|
|
2360
|
+
const throwError = this.onError(!!opt.silent, !!opt.async);
|
|
2361
|
+
// throw error if an extension set async to true but parse was called with async: false
|
|
2362
|
+
if (this.defaults.async === true && origOpt.async === false) {
|
|
2363
|
+
return throwError(new Error('marked(): The async option was set to true by an extension. Remove async: false from the parse options object to return a Promise.'));
|
|
2364
|
+
}
|
|
2365
|
+
// throw error in case of non string input
|
|
2366
|
+
if (typeof src === 'undefined' || src === null) {
|
|
2367
|
+
return throwError(new Error('marked(): input parameter is undefined or null'));
|
|
2368
|
+
}
|
|
2369
|
+
if (typeof src !== 'string') {
|
|
2370
|
+
return throwError(new Error('marked(): input parameter is of type '
|
|
2371
|
+
+ Object.prototype.toString.call(src) + ', string expected'));
|
|
2372
|
+
}
|
|
2373
|
+
if (opt.hooks) {
|
|
2374
|
+
opt.hooks.options = opt;
|
|
2375
|
+
}
|
|
2376
|
+
if (opt.async) {
|
|
2377
|
+
return Promise.resolve(opt.hooks ? opt.hooks.preprocess(src) : src)
|
|
2378
|
+
.then(src => lexer(src, opt))
|
|
2379
|
+
.then(tokens => opt.hooks ? opt.hooks.processAllTokens(tokens) : tokens)
|
|
2380
|
+
.then(tokens => opt.walkTokens ? Promise.all(this.walkTokens(tokens, opt.walkTokens)).then(() => tokens) : tokens)
|
|
2381
|
+
.then(tokens => parser(tokens, opt))
|
|
2382
|
+
.then(html => opt.hooks ? opt.hooks.postprocess(html) : html)
|
|
2383
|
+
.catch(throwError);
|
|
2384
|
+
}
|
|
2385
|
+
try {
|
|
2386
|
+
if (opt.hooks) {
|
|
2387
|
+
src = opt.hooks.preprocess(src);
|
|
2388
|
+
}
|
|
2389
|
+
let tokens = lexer(src, opt);
|
|
2390
|
+
if (opt.hooks) {
|
|
2391
|
+
tokens = opt.hooks.processAllTokens(tokens);
|
|
2392
|
+
}
|
|
2393
|
+
if (opt.walkTokens) {
|
|
2394
|
+
this.walkTokens(tokens, opt.walkTokens);
|
|
2395
|
+
}
|
|
2396
|
+
let html = parser(tokens, opt);
|
|
2397
|
+
if (opt.hooks) {
|
|
2398
|
+
html = opt.hooks.postprocess(html);
|
|
2399
|
+
}
|
|
2400
|
+
return html;
|
|
2401
|
+
}
|
|
2402
|
+
catch (e) {
|
|
2403
|
+
return throwError(e);
|
|
2404
|
+
}
|
|
2405
|
+
};
|
|
2406
|
+
return parse;
|
|
2407
|
+
}
|
|
2408
|
+
onError(silent, async) {
|
|
2409
|
+
return (e) => {
|
|
2410
|
+
e.message += '\nPlease report this to https://github.com/markedjs/marked.';
|
|
2411
|
+
if (silent) {
|
|
2412
|
+
const msg = '<p>An error occurred:</p><pre>'
|
|
2413
|
+
+ escape$1(e.message + '', true)
|
|
2414
|
+
+ '</pre>';
|
|
2415
|
+
if (async) {
|
|
2416
|
+
return Promise.resolve(msg);
|
|
2417
|
+
}
|
|
2418
|
+
return msg;
|
|
2419
|
+
}
|
|
2420
|
+
if (async) {
|
|
2421
|
+
return Promise.reject(e);
|
|
2422
|
+
}
|
|
2423
|
+
throw e;
|
|
2424
|
+
};
|
|
2425
|
+
}
|
|
2426
|
+
}
|
|
2427
|
+
|
|
2428
|
+
const markedInstance = new Marked();
|
|
2429
|
+
function marked(src, opt) {
|
|
2430
|
+
return markedInstance.parse(src, opt);
|
|
2431
|
+
}
|
|
2432
|
+
/**
|
|
2433
|
+
* Sets the default options.
|
|
2434
|
+
*
|
|
2435
|
+
* @param options Hash of options
|
|
2436
|
+
*/
|
|
2437
|
+
marked.options =
|
|
2438
|
+
marked.setOptions = function (options) {
|
|
2439
|
+
markedInstance.setOptions(options);
|
|
2440
|
+
marked.defaults = markedInstance.defaults;
|
|
2441
|
+
changeDefaults(marked.defaults);
|
|
2442
|
+
return marked;
|
|
2443
|
+
};
|
|
2444
|
+
/**
|
|
2445
|
+
* Gets the original marked default options.
|
|
2446
|
+
*/
|
|
2447
|
+
marked.getDefaults = _getDefaults;
|
|
2448
|
+
marked.defaults = exports.defaults;
|
|
2449
|
+
/**
|
|
2450
|
+
* Use Extension
|
|
2451
|
+
*/
|
|
2452
|
+
marked.use = function (...args) {
|
|
2453
|
+
markedInstance.use(...args);
|
|
2454
|
+
marked.defaults = markedInstance.defaults;
|
|
2455
|
+
changeDefaults(marked.defaults);
|
|
2456
|
+
return marked;
|
|
2457
|
+
};
|
|
2458
|
+
/**
|
|
2459
|
+
* Run callback for every token
|
|
2460
|
+
*/
|
|
2461
|
+
marked.walkTokens = function (tokens, callback) {
|
|
2462
|
+
return markedInstance.walkTokens(tokens, callback);
|
|
2463
|
+
};
|
|
2464
|
+
/**
|
|
2465
|
+
* Compiles markdown to HTML without enclosing `p` tag.
|
|
2466
|
+
*
|
|
2467
|
+
* @param src String of markdown source to be compiled
|
|
2468
|
+
* @param options Hash of options
|
|
2469
|
+
* @return String of compiled HTML
|
|
2470
|
+
*/
|
|
2471
|
+
marked.parseInline = markedInstance.parseInline;
|
|
2472
|
+
/**
|
|
2473
|
+
* Expose
|
|
2474
|
+
*/
|
|
2475
|
+
marked.Parser = _Parser;
|
|
2476
|
+
marked.parser = _Parser.parse;
|
|
2477
|
+
marked.Renderer = _Renderer;
|
|
2478
|
+
marked.TextRenderer = _TextRenderer;
|
|
2479
|
+
marked.Lexer = _Lexer;
|
|
2480
|
+
marked.lexer = _Lexer.lex;
|
|
2481
|
+
marked.Tokenizer = _Tokenizer;
|
|
2482
|
+
marked.Hooks = _Hooks;
|
|
2483
|
+
marked.parse = marked;
|
|
2484
|
+
const options = marked.options;
|
|
2485
|
+
const setOptions = marked.setOptions;
|
|
2486
|
+
const use = marked.use;
|
|
2487
|
+
const walkTokens = marked.walkTokens;
|
|
2488
|
+
const parseInline = marked.parseInline;
|
|
2489
|
+
const parse = marked;
|
|
2490
|
+
const parser = _Parser.parse;
|
|
2491
|
+
const lexer = _Lexer.lex;
|
|
2492
|
+
|
|
2493
|
+
exports.Hooks = _Hooks;
|
|
2494
|
+
exports.Lexer = _Lexer;
|
|
2495
|
+
exports.Marked = Marked;
|
|
2496
|
+
exports.Parser = _Parser;
|
|
2497
|
+
exports.Renderer = _Renderer;
|
|
2498
|
+
exports.TextRenderer = _TextRenderer;
|
|
2499
|
+
exports.Tokenizer = _Tokenizer;
|
|
2500
|
+
exports.getDefaults = _getDefaults;
|
|
2501
|
+
exports.lexer = lexer;
|
|
2502
|
+
exports.marked = marked;
|
|
2503
|
+
exports.options = options;
|
|
2504
|
+
exports.parse = parse;
|
|
2505
|
+
exports.parseInline = parseInline;
|
|
2506
|
+
exports.parser = parser;
|
|
2507
|
+
exports.setOptions = setOptions;
|
|
2508
|
+
exports.use = use;
|
|
2509
|
+
exports.walkTokens = walkTokens;
|
|
2510
|
+
|
|
2511
|
+
}));
|
|
2512
|
+
|
|
2513
|
+
}(marked_umd, marked_umd.exports));
|
|
2514
|
+
|
|
2515
|
+
var purify = {exports: {}};
|
|
2516
|
+
|
|
2517
|
+
/*! @license DOMPurify 3.1.6 | (c) Cure53 and other contributors | Released under the Apache license 2.0 and Mozilla Public License 2.0 | github.com/cure53/DOMPurify/blob/3.1.6/LICENSE */
|
|
2518
|
+
|
|
2519
|
+
(function (module, exports) {
|
|
2520
|
+
(function (global, factory) {
|
|
2521
|
+
module.exports = factory() ;
|
|
2522
|
+
})(commonjsGlobal, (function () {
|
|
2523
|
+
const {
|
|
2524
|
+
entries,
|
|
2525
|
+
setPrototypeOf,
|
|
2526
|
+
isFrozen,
|
|
2527
|
+
getPrototypeOf,
|
|
2528
|
+
getOwnPropertyDescriptor
|
|
2529
|
+
} = Object;
|
|
2530
|
+
let {
|
|
2531
|
+
freeze,
|
|
2532
|
+
seal,
|
|
2533
|
+
create
|
|
2534
|
+
} = Object; // eslint-disable-line import/no-mutable-exports
|
|
2535
|
+
let {
|
|
2536
|
+
apply,
|
|
2537
|
+
construct
|
|
2538
|
+
} = typeof Reflect !== 'undefined' && Reflect;
|
|
2539
|
+
if (!freeze) {
|
|
2540
|
+
freeze = function freeze(x) {
|
|
2541
|
+
return x;
|
|
2542
|
+
};
|
|
2543
|
+
}
|
|
2544
|
+
if (!seal) {
|
|
2545
|
+
seal = function seal(x) {
|
|
2546
|
+
return x;
|
|
2547
|
+
};
|
|
2548
|
+
}
|
|
2549
|
+
if (!apply) {
|
|
2550
|
+
apply = function apply(fun, thisValue, args) {
|
|
2551
|
+
return fun.apply(thisValue, args);
|
|
2552
|
+
};
|
|
2553
|
+
}
|
|
2554
|
+
if (!construct) {
|
|
2555
|
+
construct = function construct(Func, args) {
|
|
2556
|
+
return new Func(...args);
|
|
2557
|
+
};
|
|
2558
|
+
}
|
|
2559
|
+
const arrayForEach = unapply(Array.prototype.forEach);
|
|
2560
|
+
const arrayPop = unapply(Array.prototype.pop);
|
|
2561
|
+
const arrayPush = unapply(Array.prototype.push);
|
|
2562
|
+
const stringToLowerCase = unapply(String.prototype.toLowerCase);
|
|
2563
|
+
const stringToString = unapply(String.prototype.toString);
|
|
2564
|
+
const stringMatch = unapply(String.prototype.match);
|
|
2565
|
+
const stringReplace = unapply(String.prototype.replace);
|
|
2566
|
+
const stringIndexOf = unapply(String.prototype.indexOf);
|
|
2567
|
+
const stringTrim = unapply(String.prototype.trim);
|
|
2568
|
+
const objectHasOwnProperty = unapply(Object.prototype.hasOwnProperty);
|
|
2569
|
+
const regExpTest = unapply(RegExp.prototype.test);
|
|
2570
|
+
const typeErrorCreate = unconstruct(TypeError);
|
|
2571
|
+
|
|
2572
|
+
/**
|
|
2573
|
+
* Creates a new function that calls the given function with a specified thisArg and arguments.
|
|
2574
|
+
*
|
|
2575
|
+
* @param {Function} func - The function to be wrapped and called.
|
|
2576
|
+
* @returns {Function} A new function that calls the given function with a specified thisArg and arguments.
|
|
2577
|
+
*/
|
|
2578
|
+
function unapply(func) {
|
|
2579
|
+
return function (thisArg) {
|
|
2580
|
+
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
2581
|
+
args[_key - 1] = arguments[_key];
|
|
2582
|
+
}
|
|
2583
|
+
return apply(func, thisArg, args);
|
|
2584
|
+
};
|
|
2585
|
+
}
|
|
2586
|
+
|
|
2587
|
+
/**
|
|
2588
|
+
* Creates a new function that constructs an instance of the given constructor function with the provided arguments.
|
|
2589
|
+
*
|
|
2590
|
+
* @param {Function} func - The constructor function to be wrapped and called.
|
|
2591
|
+
* @returns {Function} A new function that constructs an instance of the given constructor function with the provided arguments.
|
|
2592
|
+
*/
|
|
2593
|
+
function unconstruct(func) {
|
|
2594
|
+
return function () {
|
|
2595
|
+
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
2596
|
+
args[_key2] = arguments[_key2];
|
|
2597
|
+
}
|
|
2598
|
+
return construct(func, args);
|
|
2599
|
+
};
|
|
2600
|
+
}
|
|
2601
|
+
|
|
2602
|
+
/**
|
|
2603
|
+
* Add properties to a lookup table
|
|
2604
|
+
*
|
|
2605
|
+
* @param {Object} set - The set to which elements will be added.
|
|
2606
|
+
* @param {Array} array - The array containing elements to be added to the set.
|
|
2607
|
+
* @param {Function} transformCaseFunc - An optional function to transform the case of each element before adding to the set.
|
|
2608
|
+
* @returns {Object} The modified set with added elements.
|
|
2609
|
+
*/
|
|
2610
|
+
function addToSet(set, array) {
|
|
2611
|
+
let transformCaseFunc = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : stringToLowerCase;
|
|
2612
|
+
if (setPrototypeOf) {
|
|
2613
|
+
// Make 'in' and truthy checks like Boolean(set.constructor)
|
|
2614
|
+
// independent of any properties defined on Object.prototype.
|
|
2615
|
+
// Prevent prototype setters from intercepting set as a this value.
|
|
2616
|
+
setPrototypeOf(set, null);
|
|
2617
|
+
}
|
|
2618
|
+
let l = array.length;
|
|
2619
|
+
while (l--) {
|
|
2620
|
+
let element = array[l];
|
|
2621
|
+
if (typeof element === 'string') {
|
|
2622
|
+
const lcElement = transformCaseFunc(element);
|
|
2623
|
+
if (lcElement !== element) {
|
|
2624
|
+
// Config presets (e.g. tags.js, attrs.js) are immutable.
|
|
2625
|
+
if (!isFrozen(array)) {
|
|
2626
|
+
array[l] = lcElement;
|
|
2627
|
+
}
|
|
2628
|
+
element = lcElement;
|
|
2629
|
+
}
|
|
2630
|
+
}
|
|
2631
|
+
set[element] = true;
|
|
2632
|
+
}
|
|
2633
|
+
return set;
|
|
2634
|
+
}
|
|
2635
|
+
|
|
2636
|
+
/**
|
|
2637
|
+
* Clean up an array to harden against CSPP
|
|
2638
|
+
*
|
|
2639
|
+
* @param {Array} array - The array to be cleaned.
|
|
2640
|
+
* @returns {Array} The cleaned version of the array
|
|
2641
|
+
*/
|
|
2642
|
+
function cleanArray(array) {
|
|
2643
|
+
for (let index = 0; index < array.length; index++) {
|
|
2644
|
+
const isPropertyExist = objectHasOwnProperty(array, index);
|
|
2645
|
+
if (!isPropertyExist) {
|
|
2646
|
+
array[index] = null;
|
|
2647
|
+
}
|
|
2648
|
+
}
|
|
2649
|
+
return array;
|
|
2650
|
+
}
|
|
2651
|
+
|
|
2652
|
+
/**
|
|
2653
|
+
* Shallow clone an object
|
|
2654
|
+
*
|
|
2655
|
+
* @param {Object} object - The object to be cloned.
|
|
2656
|
+
* @returns {Object} A new object that copies the original.
|
|
2657
|
+
*/
|
|
2658
|
+
function clone(object) {
|
|
2659
|
+
const newObject = create(null);
|
|
2660
|
+
for (const [property, value] of entries(object)) {
|
|
2661
|
+
const isPropertyExist = objectHasOwnProperty(object, property);
|
|
2662
|
+
if (isPropertyExist) {
|
|
2663
|
+
if (Array.isArray(value)) {
|
|
2664
|
+
newObject[property] = cleanArray(value);
|
|
2665
|
+
} else if (value && typeof value === 'object' && value.constructor === Object) {
|
|
2666
|
+
newObject[property] = clone(value);
|
|
2667
|
+
} else {
|
|
2668
|
+
newObject[property] = value;
|
|
2669
|
+
}
|
|
2670
|
+
}
|
|
2671
|
+
}
|
|
2672
|
+
return newObject;
|
|
2673
|
+
}
|
|
2674
|
+
|
|
2675
|
+
/**
|
|
2676
|
+
* This method automatically checks if the prop is function or getter and behaves accordingly.
|
|
2677
|
+
*
|
|
2678
|
+
* @param {Object} object - The object to look up the getter function in its prototype chain.
|
|
2679
|
+
* @param {String} prop - The property name for which to find the getter function.
|
|
2680
|
+
* @returns {Function} The getter function found in the prototype chain or a fallback function.
|
|
2681
|
+
*/
|
|
2682
|
+
function lookupGetter(object, prop) {
|
|
2683
|
+
while (object !== null) {
|
|
2684
|
+
const desc = getOwnPropertyDescriptor(object, prop);
|
|
2685
|
+
if (desc) {
|
|
2686
|
+
if (desc.get) {
|
|
2687
|
+
return unapply(desc.get);
|
|
2688
|
+
}
|
|
2689
|
+
if (typeof desc.value === 'function') {
|
|
2690
|
+
return unapply(desc.value);
|
|
2691
|
+
}
|
|
2692
|
+
}
|
|
2693
|
+
object = getPrototypeOf(object);
|
|
2694
|
+
}
|
|
2695
|
+
function fallbackValue() {
|
|
2696
|
+
return null;
|
|
2697
|
+
}
|
|
2698
|
+
return fallbackValue;
|
|
2699
|
+
}
|
|
2700
|
+
|
|
2701
|
+
const html$1 = freeze(['a', 'abbr', 'acronym', 'address', 'area', 'article', 'aside', 'audio', 'b', 'bdi', 'bdo', 'big', 'blink', 'blockquote', 'body', 'br', 'button', 'canvas', 'caption', 'center', 'cite', 'code', 'col', 'colgroup', 'content', 'data', 'datalist', 'dd', 'decorator', 'del', 'details', 'dfn', 'dialog', 'dir', 'div', 'dl', 'dt', 'element', 'em', 'fieldset', 'figcaption', 'figure', 'font', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hgroup', 'hr', 'html', 'i', 'img', 'input', 'ins', 'kbd', 'label', 'legend', 'li', 'main', 'map', 'mark', 'marquee', 'menu', 'menuitem', 'meter', 'nav', 'nobr', 'ol', 'optgroup', 'option', 'output', 'p', 'picture', 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'section', 'select', 'shadow', 'small', 'source', 'spacer', 'span', 'strike', 'strong', 'style', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'template', 'textarea', 'tfoot', 'th', 'thead', 'time', 'tr', 'track', 'tt', 'u', 'ul', 'var', 'video', 'wbr']);
|
|
2702
|
+
|
|
2703
|
+
// SVG
|
|
2704
|
+
const svg$1 = freeze(['svg', 'a', 'altglyph', 'altglyphdef', 'altglyphitem', 'animatecolor', 'animatemotion', 'animatetransform', 'circle', 'clippath', 'defs', 'desc', 'ellipse', 'filter', 'font', 'g', 'glyph', 'glyphref', 'hkern', 'image', 'line', 'lineargradient', 'marker', 'mask', 'metadata', 'mpath', 'path', 'pattern', 'polygon', 'polyline', 'radialgradient', 'rect', 'stop', 'style', 'switch', 'symbol', 'text', 'textpath', 'title', 'tref', 'tspan', 'view', 'vkern']);
|
|
2705
|
+
const svgFilters = freeze(['feBlend', 'feColorMatrix', 'feComponentTransfer', 'feComposite', 'feConvolveMatrix', 'feDiffuseLighting', 'feDisplacementMap', 'feDistantLight', 'feDropShadow', 'feFlood', 'feFuncA', 'feFuncB', 'feFuncG', 'feFuncR', 'feGaussianBlur', 'feImage', 'feMerge', 'feMergeNode', 'feMorphology', 'feOffset', 'fePointLight', 'feSpecularLighting', 'feSpotLight', 'feTile', 'feTurbulence']);
|
|
2706
|
+
|
|
2707
|
+
// List of SVG elements that are disallowed by default.
|
|
2708
|
+
// We still need to know them so that we can do namespace
|
|
2709
|
+
// checks properly in case one wants to add them to
|
|
2710
|
+
// allow-list.
|
|
2711
|
+
const svgDisallowed = freeze(['animate', 'color-profile', 'cursor', 'discard', 'font-face', 'font-face-format', 'font-face-name', 'font-face-src', 'font-face-uri', 'foreignobject', 'hatch', 'hatchpath', 'mesh', 'meshgradient', 'meshpatch', 'meshrow', 'missing-glyph', 'script', 'set', 'solidcolor', 'unknown', 'use']);
|
|
2712
|
+
const mathMl$1 = freeze(['math', 'menclose', 'merror', 'mfenced', 'mfrac', 'mglyph', 'mi', 'mlabeledtr', 'mmultiscripts', 'mn', 'mo', 'mover', 'mpadded', 'mphantom', 'mroot', 'mrow', 'ms', 'mspace', 'msqrt', 'mstyle', 'msub', 'msup', 'msubsup', 'mtable', 'mtd', 'mtext', 'mtr', 'munder', 'munderover', 'mprescripts']);
|
|
2713
|
+
|
|
2714
|
+
// Similarly to SVG, we want to know all MathML elements,
|
|
2715
|
+
// even those that we disallow by default.
|
|
2716
|
+
const mathMlDisallowed = freeze(['maction', 'maligngroup', 'malignmark', 'mlongdiv', 'mscarries', 'mscarry', 'msgroup', 'mstack', 'msline', 'msrow', 'semantics', 'annotation', 'annotation-xml', 'mprescripts', 'none']);
|
|
2717
|
+
const text = freeze(['#text']);
|
|
2718
|
+
|
|
2719
|
+
const html = freeze(['accept', 'action', 'align', 'alt', 'autocapitalize', 'autocomplete', 'autopictureinpicture', 'autoplay', 'background', 'bgcolor', 'border', 'capture', 'cellpadding', 'cellspacing', 'checked', 'cite', 'class', 'clear', 'color', 'cols', 'colspan', 'controls', 'controlslist', 'coords', 'crossorigin', 'datetime', 'decoding', 'default', 'dir', 'disabled', 'disablepictureinpicture', 'disableremoteplayback', 'download', 'draggable', 'enctype', 'enterkeyhint', 'face', 'for', 'headers', 'height', 'hidden', 'high', 'href', 'hreflang', 'id', 'inputmode', 'integrity', 'ismap', 'kind', 'label', 'lang', 'list', 'loading', 'loop', 'low', 'max', 'maxlength', 'media', 'method', 'min', 'minlength', 'multiple', 'muted', 'name', 'nonce', 'noshade', 'novalidate', 'nowrap', 'open', 'optimum', 'pattern', 'placeholder', 'playsinline', 'popover', 'popovertarget', 'popovertargetaction', 'poster', 'preload', 'pubdate', 'radiogroup', 'readonly', 'rel', 'required', 'rev', 'reversed', 'role', 'rows', 'rowspan', 'spellcheck', 'scope', 'selected', 'shape', 'size', 'sizes', 'span', 'srclang', 'start', 'src', 'srcset', 'step', 'style', 'summary', 'tabindex', 'title', 'translate', 'type', 'usemap', 'valign', 'value', 'width', 'wrap', 'xmlns', 'slot']);
|
|
2720
|
+
const svg = freeze(['accent-height', 'accumulate', 'additive', 'alignment-baseline', 'ascent', 'attributename', 'attributetype', 'azimuth', 'basefrequency', 'baseline-shift', 'begin', 'bias', 'by', 'class', 'clip', 'clippathunits', 'clip-path', 'clip-rule', 'color', 'color-interpolation', 'color-interpolation-filters', 'color-profile', 'color-rendering', 'cx', 'cy', 'd', 'dx', 'dy', 'diffuseconstant', 'direction', 'display', 'divisor', 'dur', 'edgemode', 'elevation', 'end', 'fill', 'fill-opacity', 'fill-rule', 'filter', 'filterunits', 'flood-color', 'flood-opacity', 'font-family', 'font-size', 'font-size-adjust', 'font-stretch', 'font-style', 'font-variant', 'font-weight', 'fx', 'fy', 'g1', 'g2', 'glyph-name', 'glyphref', 'gradientunits', 'gradienttransform', 'height', 'href', 'id', 'image-rendering', 'in', 'in2', 'k', 'k1', 'k2', 'k3', 'k4', 'kerning', 'keypoints', 'keysplines', 'keytimes', 'lang', 'lengthadjust', 'letter-spacing', 'kernelmatrix', 'kernelunitlength', 'lighting-color', 'local', 'marker-end', 'marker-mid', 'marker-start', 'markerheight', 'markerunits', 'markerwidth', 'maskcontentunits', 'maskunits', 'max', 'mask', 'media', 'method', 'mode', 'min', 'name', 'numoctaves', 'offset', 'operator', 'opacity', 'order', 'orient', 'orientation', 'origin', 'overflow', 'paint-order', 'path', 'pathlength', 'patterncontentunits', 'patterntransform', 'patternunits', 'points', 'preservealpha', 'preserveaspectratio', 'primitiveunits', 'r', 'rx', 'ry', 'radius', 'refx', 'refy', 'repeatcount', 'repeatdur', 'restart', 'result', 'rotate', 'scale', 'seed', 'shape-rendering', 'specularconstant', 'specularexponent', 'spreadmethod', 'startoffset', 'stddeviation', 'stitchtiles', 'stop-color', 'stop-opacity', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke', 'stroke-width', 'style', 'surfacescale', 'systemlanguage', 'tabindex', 'targetx', 'targety', 'transform', 'transform-origin', 'text-anchor', 'text-decoration', 'text-rendering', 'textlength', 'type', 'u1', 'u2', 'unicode', 'values', 'viewbox', 'visibility', 'version', 'vert-adv-y', 'vert-origin-x', 'vert-origin-y', 'width', 'word-spacing', 'wrap', 'writing-mode', 'xchannelselector', 'ychannelselector', 'x', 'x1', 'x2', 'xmlns', 'y', 'y1', 'y2', 'z', 'zoomandpan']);
|
|
2721
|
+
const mathMl = freeze(['accent', 'accentunder', 'align', 'bevelled', 'close', 'columnsalign', 'columnlines', 'columnspan', 'denomalign', 'depth', 'dir', 'display', 'displaystyle', 'encoding', 'fence', 'frame', 'height', 'href', 'id', 'largeop', 'length', 'linethickness', 'lspace', 'lquote', 'mathbackground', 'mathcolor', 'mathsize', 'mathvariant', 'maxsize', 'minsize', 'movablelimits', 'notation', 'numalign', 'open', 'rowalign', 'rowlines', 'rowspacing', 'rowspan', 'rspace', 'rquote', 'scriptlevel', 'scriptminsize', 'scriptsizemultiplier', 'selection', 'separator', 'separators', 'stretchy', 'subscriptshift', 'supscriptshift', 'symmetric', 'voffset', 'width', 'xmlns']);
|
|
2722
|
+
const xml = freeze(['xlink:href', 'xml:id', 'xlink:title', 'xml:space', 'xmlns:xlink']);
|
|
2723
|
+
|
|
2724
|
+
// eslint-disable-next-line unicorn/better-regex
|
|
2725
|
+
const MUSTACHE_EXPR = seal(/\{\{[\w\W]*|[\w\W]*\}\}/gm); // Specify template detection regex for SAFE_FOR_TEMPLATES mode
|
|
2726
|
+
const ERB_EXPR = seal(/<%[\w\W]*|[\w\W]*%>/gm);
|
|
2727
|
+
const TMPLIT_EXPR = seal(/\${[\w\W]*}/gm);
|
|
2728
|
+
const DATA_ATTR = seal(/^data-[\-\w.\u00B7-\uFFFF]/); // eslint-disable-line no-useless-escape
|
|
2729
|
+
const ARIA_ATTR = seal(/^aria-[\-\w]+$/); // eslint-disable-line no-useless-escape
|
|
2730
|
+
const IS_ALLOWED_URI = seal(/^(?:(?:(?:f|ht)tps?|mailto|tel|callto|sms|cid|xmpp):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i // eslint-disable-line no-useless-escape
|
|
2731
|
+
);
|
|
2732
|
+
const IS_SCRIPT_OR_DATA = seal(/^(?:\w+script|data):/i);
|
|
2733
|
+
const ATTR_WHITESPACE = seal(/[\u0000-\u0020\u00A0\u1680\u180E\u2000-\u2029\u205F\u3000]/g // eslint-disable-line no-control-regex
|
|
2734
|
+
);
|
|
2735
|
+
const DOCTYPE_NAME = seal(/^html$/i);
|
|
2736
|
+
const CUSTOM_ELEMENT = seal(/^[a-z][.\w]*(-[.\w]+)+$/i);
|
|
2737
|
+
|
|
2738
|
+
var EXPRESSIONS = /*#__PURE__*/Object.freeze({
|
|
2739
|
+
__proto__: null,
|
|
2740
|
+
MUSTACHE_EXPR: MUSTACHE_EXPR,
|
|
2741
|
+
ERB_EXPR: ERB_EXPR,
|
|
2742
|
+
TMPLIT_EXPR: TMPLIT_EXPR,
|
|
2743
|
+
DATA_ATTR: DATA_ATTR,
|
|
2744
|
+
ARIA_ATTR: ARIA_ATTR,
|
|
2745
|
+
IS_ALLOWED_URI: IS_ALLOWED_URI,
|
|
2746
|
+
IS_SCRIPT_OR_DATA: IS_SCRIPT_OR_DATA,
|
|
2747
|
+
ATTR_WHITESPACE: ATTR_WHITESPACE,
|
|
2748
|
+
DOCTYPE_NAME: DOCTYPE_NAME,
|
|
2749
|
+
CUSTOM_ELEMENT: CUSTOM_ELEMENT
|
|
2750
|
+
});
|
|
2751
|
+
|
|
2752
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType
|
|
2753
|
+
const NODE_TYPE = {
|
|
2754
|
+
element: 1,
|
|
2755
|
+
attribute: 2,
|
|
2756
|
+
text: 3,
|
|
2757
|
+
cdataSection: 4,
|
|
2758
|
+
entityReference: 5,
|
|
2759
|
+
// Deprecated
|
|
2760
|
+
entityNode: 6,
|
|
2761
|
+
// Deprecated
|
|
2762
|
+
progressingInstruction: 7,
|
|
2763
|
+
comment: 8,
|
|
2764
|
+
document: 9,
|
|
2765
|
+
documentType: 10,
|
|
2766
|
+
documentFragment: 11,
|
|
2767
|
+
notation: 12 // Deprecated
|
|
2768
|
+
};
|
|
2769
|
+
const getGlobal = function getGlobal() {
|
|
2770
|
+
return typeof window === 'undefined' ? null : window;
|
|
2771
|
+
};
|
|
2772
|
+
|
|
2773
|
+
/**
|
|
2774
|
+
* Creates a no-op policy for internal use only.
|
|
2775
|
+
* Don't export this function outside this module!
|
|
2776
|
+
* @param {TrustedTypePolicyFactory} trustedTypes The policy factory.
|
|
2777
|
+
* @param {HTMLScriptElement} purifyHostElement The Script element used to load DOMPurify (to determine policy name suffix).
|
|
2778
|
+
* @return {TrustedTypePolicy} The policy created (or null, if Trusted Types
|
|
2779
|
+
* are not supported or creating the policy failed).
|
|
2780
|
+
*/
|
|
2781
|
+
const _createTrustedTypesPolicy = function _createTrustedTypesPolicy(trustedTypes, purifyHostElement) {
|
|
2782
|
+
if (typeof trustedTypes !== 'object' || typeof trustedTypes.createPolicy !== 'function') {
|
|
2783
|
+
return null;
|
|
2784
|
+
}
|
|
2785
|
+
|
|
2786
|
+
// Allow the callers to control the unique policy name
|
|
2787
|
+
// by adding a data-tt-policy-suffix to the script element with the DOMPurify.
|
|
2788
|
+
// Policy creation with duplicate names throws in Trusted Types.
|
|
2789
|
+
let suffix = null;
|
|
2790
|
+
const ATTR_NAME = 'data-tt-policy-suffix';
|
|
2791
|
+
if (purifyHostElement && purifyHostElement.hasAttribute(ATTR_NAME)) {
|
|
2792
|
+
suffix = purifyHostElement.getAttribute(ATTR_NAME);
|
|
2793
|
+
}
|
|
2794
|
+
const policyName = 'dompurify' + (suffix ? '#' + suffix : '');
|
|
2795
|
+
try {
|
|
2796
|
+
return trustedTypes.createPolicy(policyName, {
|
|
2797
|
+
createHTML(html) {
|
|
2798
|
+
return html;
|
|
2799
|
+
},
|
|
2800
|
+
createScriptURL(scriptUrl) {
|
|
2801
|
+
return scriptUrl;
|
|
2802
|
+
}
|
|
2803
|
+
});
|
|
2804
|
+
} catch (_) {
|
|
2805
|
+
// Policy creation failed (most likely another DOMPurify script has
|
|
2806
|
+
// already run). Skip creating the policy, as this will only cause errors
|
|
2807
|
+
// if TT are enforced.
|
|
2808
|
+
console.warn('TrustedTypes policy ' + policyName + ' could not be created.');
|
|
2809
|
+
return null;
|
|
2810
|
+
}
|
|
2811
|
+
};
|
|
2812
|
+
function createDOMPurify() {
|
|
2813
|
+
let window = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getGlobal();
|
|
2814
|
+
const DOMPurify = root => createDOMPurify(root);
|
|
2815
|
+
|
|
2816
|
+
/**
|
|
2817
|
+
* Version label, exposed for easier checks
|
|
2818
|
+
* if DOMPurify is up to date or not
|
|
2819
|
+
*/
|
|
2820
|
+
DOMPurify.version = '3.1.6';
|
|
2821
|
+
|
|
2822
|
+
/**
|
|
2823
|
+
* Array of elements that DOMPurify removed during sanitation.
|
|
2824
|
+
* Empty if nothing was removed.
|
|
2825
|
+
*/
|
|
2826
|
+
DOMPurify.removed = [];
|
|
2827
|
+
if (!window || !window.document || window.document.nodeType !== NODE_TYPE.document) {
|
|
2828
|
+
// Not running in a browser, provide a factory function
|
|
2829
|
+
// so that you can pass your own Window
|
|
2830
|
+
DOMPurify.isSupported = false;
|
|
2831
|
+
return DOMPurify;
|
|
2832
|
+
}
|
|
2833
|
+
let {
|
|
2834
|
+
document
|
|
2835
|
+
} = window;
|
|
2836
|
+
const originalDocument = document;
|
|
2837
|
+
const currentScript = originalDocument.currentScript;
|
|
2838
|
+
const {
|
|
2839
|
+
DocumentFragment,
|
|
2840
|
+
HTMLTemplateElement,
|
|
2841
|
+
Node,
|
|
2842
|
+
Element,
|
|
2843
|
+
NodeFilter,
|
|
2844
|
+
NamedNodeMap = window.NamedNodeMap || window.MozNamedAttrMap,
|
|
2845
|
+
HTMLFormElement,
|
|
2846
|
+
DOMParser,
|
|
2847
|
+
trustedTypes
|
|
2848
|
+
} = window;
|
|
2849
|
+
const ElementPrototype = Element.prototype;
|
|
2850
|
+
const cloneNode = lookupGetter(ElementPrototype, 'cloneNode');
|
|
2851
|
+
const remove = lookupGetter(ElementPrototype, 'remove');
|
|
2852
|
+
const getNextSibling = lookupGetter(ElementPrototype, 'nextSibling');
|
|
2853
|
+
const getChildNodes = lookupGetter(ElementPrototype, 'childNodes');
|
|
2854
|
+
const getParentNode = lookupGetter(ElementPrototype, 'parentNode');
|
|
2855
|
+
|
|
2856
|
+
// As per issue #47, the web-components registry is inherited by a
|
|
2857
|
+
// new document created via createHTMLDocument. As per the spec
|
|
2858
|
+
// (http://w3c.github.io/webcomponents/spec/custom/#creating-and-passing-registries)
|
|
2859
|
+
// a new empty registry is used when creating a template contents owner
|
|
2860
|
+
// document, so we use that as our parent document to ensure nothing
|
|
2861
|
+
// is inherited.
|
|
2862
|
+
if (typeof HTMLTemplateElement === 'function') {
|
|
2863
|
+
const template = document.createElement('template');
|
|
2864
|
+
if (template.content && template.content.ownerDocument) {
|
|
2865
|
+
document = template.content.ownerDocument;
|
|
2866
|
+
}
|
|
2867
|
+
}
|
|
2868
|
+
let trustedTypesPolicy;
|
|
2869
|
+
let emptyHTML = '';
|
|
2870
|
+
const {
|
|
2871
|
+
implementation,
|
|
2872
|
+
createNodeIterator,
|
|
2873
|
+
createDocumentFragment,
|
|
2874
|
+
getElementsByTagName
|
|
2875
|
+
} = document;
|
|
2876
|
+
const {
|
|
2877
|
+
importNode
|
|
2878
|
+
} = originalDocument;
|
|
2879
|
+
let hooks = {};
|
|
2880
|
+
|
|
2881
|
+
/**
|
|
2882
|
+
* Expose whether this browser supports running the full DOMPurify.
|
|
2883
|
+
*/
|
|
2884
|
+
DOMPurify.isSupported = typeof entries === 'function' && typeof getParentNode === 'function' && implementation && implementation.createHTMLDocument !== undefined;
|
|
2885
|
+
const {
|
|
2886
|
+
MUSTACHE_EXPR,
|
|
2887
|
+
ERB_EXPR,
|
|
2888
|
+
TMPLIT_EXPR,
|
|
2889
|
+
DATA_ATTR,
|
|
2890
|
+
ARIA_ATTR,
|
|
2891
|
+
IS_SCRIPT_OR_DATA,
|
|
2892
|
+
ATTR_WHITESPACE,
|
|
2893
|
+
CUSTOM_ELEMENT
|
|
2894
|
+
} = EXPRESSIONS;
|
|
2895
|
+
let {
|
|
2896
|
+
IS_ALLOWED_URI: IS_ALLOWED_URI$1
|
|
2897
|
+
} = EXPRESSIONS;
|
|
2898
|
+
|
|
2899
|
+
/**
|
|
2900
|
+
* We consider the elements and attributes below to be safe. Ideally
|
|
2901
|
+
* don't add any new ones but feel free to remove unwanted ones.
|
|
2902
|
+
*/
|
|
2903
|
+
|
|
2904
|
+
/* allowed element names */
|
|
2905
|
+
let ALLOWED_TAGS = null;
|
|
2906
|
+
const DEFAULT_ALLOWED_TAGS = addToSet({}, [...html$1, ...svg$1, ...svgFilters, ...mathMl$1, ...text]);
|
|
2907
|
+
|
|
2908
|
+
/* Allowed attribute names */
|
|
2909
|
+
let ALLOWED_ATTR = null;
|
|
2910
|
+
const DEFAULT_ALLOWED_ATTR = addToSet({}, [...html, ...svg, ...mathMl, ...xml]);
|
|
2911
|
+
|
|
2912
|
+
/*
|
|
2913
|
+
* Configure how DOMPUrify should handle custom elements and their attributes as well as customized built-in elements.
|
|
2914
|
+
* @property {RegExp|Function|null} tagNameCheck one of [null, regexPattern, predicate]. Default: `null` (disallow any custom elements)
|
|
2915
|
+
* @property {RegExp|Function|null} attributeNameCheck one of [null, regexPattern, predicate]. Default: `null` (disallow any attributes not on the allow list)
|
|
2916
|
+
* @property {boolean} allowCustomizedBuiltInElements allow custom elements derived from built-ins if they pass CUSTOM_ELEMENT_HANDLING.tagNameCheck. Default: `false`.
|
|
2917
|
+
*/
|
|
2918
|
+
let CUSTOM_ELEMENT_HANDLING = Object.seal(create(null, {
|
|
2919
|
+
tagNameCheck: {
|
|
2920
|
+
writable: true,
|
|
2921
|
+
configurable: false,
|
|
2922
|
+
enumerable: true,
|
|
2923
|
+
value: null
|
|
2924
|
+
},
|
|
2925
|
+
attributeNameCheck: {
|
|
2926
|
+
writable: true,
|
|
2927
|
+
configurable: false,
|
|
2928
|
+
enumerable: true,
|
|
2929
|
+
value: null
|
|
2930
|
+
},
|
|
2931
|
+
allowCustomizedBuiltInElements: {
|
|
2932
|
+
writable: true,
|
|
2933
|
+
configurable: false,
|
|
2934
|
+
enumerable: true,
|
|
2935
|
+
value: false
|
|
2936
|
+
}
|
|
2937
|
+
}));
|
|
2938
|
+
|
|
2939
|
+
/* Explicitly forbidden tags (overrides ALLOWED_TAGS/ADD_TAGS) */
|
|
2940
|
+
let FORBID_TAGS = null;
|
|
2941
|
+
|
|
2942
|
+
/* Explicitly forbidden attributes (overrides ALLOWED_ATTR/ADD_ATTR) */
|
|
2943
|
+
let FORBID_ATTR = null;
|
|
2944
|
+
|
|
2945
|
+
/* Decide if ARIA attributes are okay */
|
|
2946
|
+
let ALLOW_ARIA_ATTR = true;
|
|
2947
|
+
|
|
2948
|
+
/* Decide if custom data attributes are okay */
|
|
2949
|
+
let ALLOW_DATA_ATTR = true;
|
|
2950
|
+
|
|
2951
|
+
/* Decide if unknown protocols are okay */
|
|
2952
|
+
let ALLOW_UNKNOWN_PROTOCOLS = false;
|
|
2953
|
+
|
|
2954
|
+
/* Decide if self-closing tags in attributes are allowed.
|
|
2955
|
+
* Usually removed due to a mXSS issue in jQuery 3.0 */
|
|
2956
|
+
let ALLOW_SELF_CLOSE_IN_ATTR = true;
|
|
2957
|
+
|
|
2958
|
+
/* Output should be safe for common template engines.
|
|
2959
|
+
* This means, DOMPurify removes data attributes, mustaches and ERB
|
|
2960
|
+
*/
|
|
2961
|
+
let SAFE_FOR_TEMPLATES = false;
|
|
2962
|
+
|
|
2963
|
+
/* Output should be safe even for XML used within HTML and alike.
|
|
2964
|
+
* This means, DOMPurify removes comments when containing risky content.
|
|
2965
|
+
*/
|
|
2966
|
+
let SAFE_FOR_XML = true;
|
|
2967
|
+
|
|
2968
|
+
/* Decide if document with <html>... should be returned */
|
|
2969
|
+
let WHOLE_DOCUMENT = false;
|
|
2970
|
+
|
|
2971
|
+
/* Track whether config is already set on this instance of DOMPurify. */
|
|
2972
|
+
let SET_CONFIG = false;
|
|
2973
|
+
|
|
2974
|
+
/* Decide if all elements (e.g. style, script) must be children of
|
|
2975
|
+
* document.body. By default, browsers might move them to document.head */
|
|
2976
|
+
let FORCE_BODY = false;
|
|
2977
|
+
|
|
2978
|
+
/* Decide if a DOM `HTMLBodyElement` should be returned, instead of a html
|
|
2979
|
+
* string (or a TrustedHTML object if Trusted Types are supported).
|
|
2980
|
+
* If `WHOLE_DOCUMENT` is enabled a `HTMLHtmlElement` will be returned instead
|
|
2981
|
+
*/
|
|
2982
|
+
let RETURN_DOM = false;
|
|
2983
|
+
|
|
2984
|
+
/* Decide if a DOM `DocumentFragment` should be returned, instead of a html
|
|
2985
|
+
* string (or a TrustedHTML object if Trusted Types are supported) */
|
|
2986
|
+
let RETURN_DOM_FRAGMENT = false;
|
|
2987
|
+
|
|
2988
|
+
/* Try to return a Trusted Type object instead of a string, return a string in
|
|
2989
|
+
* case Trusted Types are not supported */
|
|
2990
|
+
let RETURN_TRUSTED_TYPE = false;
|
|
2991
|
+
|
|
2992
|
+
/* Output should be free from DOM clobbering attacks?
|
|
2993
|
+
* This sanitizes markups named with colliding, clobberable built-in DOM APIs.
|
|
2994
|
+
*/
|
|
2995
|
+
let SANITIZE_DOM = true;
|
|
2996
|
+
|
|
2997
|
+
/* Achieve full DOM Clobbering protection by isolating the namespace of named
|
|
2998
|
+
* properties and JS variables, mitigating attacks that abuse the HTML/DOM spec rules.
|
|
2999
|
+
*
|
|
3000
|
+
* HTML/DOM spec rules that enable DOM Clobbering:
|
|
3001
|
+
* - Named Access on Window (§7.3.3)
|
|
3002
|
+
* - DOM Tree Accessors (§3.1.5)
|
|
3003
|
+
* - Form Element Parent-Child Relations (§4.10.3)
|
|
3004
|
+
* - Iframe srcdoc / Nested WindowProxies (§4.8.5)
|
|
3005
|
+
* - HTMLCollection (§4.2.10.2)
|
|
3006
|
+
*
|
|
3007
|
+
* Namespace isolation is implemented by prefixing `id` and `name` attributes
|
|
3008
|
+
* with a constant string, i.e., `user-content-`
|
|
3009
|
+
*/
|
|
3010
|
+
let SANITIZE_NAMED_PROPS = false;
|
|
3011
|
+
const SANITIZE_NAMED_PROPS_PREFIX = 'user-content-';
|
|
3012
|
+
|
|
3013
|
+
/* Keep element content when removing element? */
|
|
3014
|
+
let KEEP_CONTENT = true;
|
|
3015
|
+
|
|
3016
|
+
/* If a `Node` is passed to sanitize(), then performs sanitization in-place instead
|
|
3017
|
+
* of importing it into a new Document and returning a sanitized copy */
|
|
3018
|
+
let IN_PLACE = false;
|
|
3019
|
+
|
|
3020
|
+
/* Allow usage of profiles like html, svg and mathMl */
|
|
3021
|
+
let USE_PROFILES = {};
|
|
3022
|
+
|
|
3023
|
+
/* Tags to ignore content of when KEEP_CONTENT is true */
|
|
3024
|
+
let FORBID_CONTENTS = null;
|
|
3025
|
+
const DEFAULT_FORBID_CONTENTS = addToSet({}, ['annotation-xml', 'audio', 'colgroup', 'desc', 'foreignobject', 'head', 'iframe', 'math', 'mi', 'mn', 'mo', 'ms', 'mtext', 'noembed', 'noframes', 'noscript', 'plaintext', 'script', 'style', 'svg', 'template', 'thead', 'title', 'video', 'xmp']);
|
|
3026
|
+
|
|
3027
|
+
/* Tags that are safe for data: URIs */
|
|
3028
|
+
let DATA_URI_TAGS = null;
|
|
3029
|
+
const DEFAULT_DATA_URI_TAGS = addToSet({}, ['audio', 'video', 'img', 'source', 'image', 'track']);
|
|
3030
|
+
|
|
3031
|
+
/* Attributes safe for values like "javascript:" */
|
|
3032
|
+
let URI_SAFE_ATTRIBUTES = null;
|
|
3033
|
+
const DEFAULT_URI_SAFE_ATTRIBUTES = addToSet({}, ['alt', 'class', 'for', 'id', 'label', 'name', 'pattern', 'placeholder', 'role', 'summary', 'title', 'value', 'style', 'xmlns']);
|
|
3034
|
+
const MATHML_NAMESPACE = 'http://www.w3.org/1998/Math/MathML';
|
|
3035
|
+
const SVG_NAMESPACE = 'http://www.w3.org/2000/svg';
|
|
3036
|
+
const HTML_NAMESPACE = 'http://www.w3.org/1999/xhtml';
|
|
3037
|
+
/* Document namespace */
|
|
3038
|
+
let NAMESPACE = HTML_NAMESPACE;
|
|
3039
|
+
let IS_EMPTY_INPUT = false;
|
|
3040
|
+
|
|
3041
|
+
/* Allowed XHTML+XML namespaces */
|
|
3042
|
+
let ALLOWED_NAMESPACES = null;
|
|
3043
|
+
const DEFAULT_ALLOWED_NAMESPACES = addToSet({}, [MATHML_NAMESPACE, SVG_NAMESPACE, HTML_NAMESPACE], stringToString);
|
|
3044
|
+
|
|
3045
|
+
/* Parsing of strict XHTML documents */
|
|
3046
|
+
let PARSER_MEDIA_TYPE = null;
|
|
3047
|
+
const SUPPORTED_PARSER_MEDIA_TYPES = ['application/xhtml+xml', 'text/html'];
|
|
3048
|
+
const DEFAULT_PARSER_MEDIA_TYPE = 'text/html';
|
|
3049
|
+
let transformCaseFunc = null;
|
|
3050
|
+
|
|
3051
|
+
/* Keep a reference to config to pass to hooks */
|
|
3052
|
+
let CONFIG = null;
|
|
3053
|
+
|
|
3054
|
+
/* Ideally, do not touch anything below this line */
|
|
3055
|
+
/* ______________________________________________ */
|
|
3056
|
+
|
|
3057
|
+
const formElement = document.createElement('form');
|
|
3058
|
+
const isRegexOrFunction = function isRegexOrFunction(testValue) {
|
|
3059
|
+
return testValue instanceof RegExp || testValue instanceof Function;
|
|
3060
|
+
};
|
|
3061
|
+
|
|
3062
|
+
/**
|
|
3063
|
+
* _parseConfig
|
|
3064
|
+
*
|
|
3065
|
+
* @param {Object} cfg optional config literal
|
|
3066
|
+
*/
|
|
3067
|
+
// eslint-disable-next-line complexity
|
|
3068
|
+
const _parseConfig = function _parseConfig() {
|
|
3069
|
+
let cfg = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
3070
|
+
if (CONFIG && CONFIG === cfg) {
|
|
3071
|
+
return;
|
|
3072
|
+
}
|
|
3073
|
+
|
|
3074
|
+
/* Shield configuration object from tampering */
|
|
3075
|
+
if (!cfg || typeof cfg !== 'object') {
|
|
3076
|
+
cfg = {};
|
|
3077
|
+
}
|
|
3078
|
+
|
|
3079
|
+
/* Shield configuration object from prototype pollution */
|
|
3080
|
+
cfg = clone(cfg);
|
|
3081
|
+
PARSER_MEDIA_TYPE =
|
|
3082
|
+
// eslint-disable-next-line unicorn/prefer-includes
|
|
3083
|
+
SUPPORTED_PARSER_MEDIA_TYPES.indexOf(cfg.PARSER_MEDIA_TYPE) === -1 ? DEFAULT_PARSER_MEDIA_TYPE : cfg.PARSER_MEDIA_TYPE;
|
|
3084
|
+
|
|
3085
|
+
// HTML tags and attributes are not case-sensitive, converting to lowercase. Keeping XHTML as is.
|
|
3086
|
+
transformCaseFunc = PARSER_MEDIA_TYPE === 'application/xhtml+xml' ? stringToString : stringToLowerCase;
|
|
3087
|
+
|
|
3088
|
+
/* Set configuration parameters */
|
|
3089
|
+
ALLOWED_TAGS = objectHasOwnProperty(cfg, 'ALLOWED_TAGS') ? addToSet({}, cfg.ALLOWED_TAGS, transformCaseFunc) : DEFAULT_ALLOWED_TAGS;
|
|
3090
|
+
ALLOWED_ATTR = objectHasOwnProperty(cfg, 'ALLOWED_ATTR') ? addToSet({}, cfg.ALLOWED_ATTR, transformCaseFunc) : DEFAULT_ALLOWED_ATTR;
|
|
3091
|
+
ALLOWED_NAMESPACES = objectHasOwnProperty(cfg, 'ALLOWED_NAMESPACES') ? addToSet({}, cfg.ALLOWED_NAMESPACES, stringToString) : DEFAULT_ALLOWED_NAMESPACES;
|
|
3092
|
+
URI_SAFE_ATTRIBUTES = objectHasOwnProperty(cfg, 'ADD_URI_SAFE_ATTR') ? addToSet(clone(DEFAULT_URI_SAFE_ATTRIBUTES),
|
|
3093
|
+
// eslint-disable-line indent
|
|
3094
|
+
cfg.ADD_URI_SAFE_ATTR,
|
|
3095
|
+
// eslint-disable-line indent
|
|
3096
|
+
transformCaseFunc // eslint-disable-line indent
|
|
3097
|
+
) // eslint-disable-line indent
|
|
3098
|
+
: DEFAULT_URI_SAFE_ATTRIBUTES;
|
|
3099
|
+
DATA_URI_TAGS = objectHasOwnProperty(cfg, 'ADD_DATA_URI_TAGS') ? addToSet(clone(DEFAULT_DATA_URI_TAGS),
|
|
3100
|
+
// eslint-disable-line indent
|
|
3101
|
+
cfg.ADD_DATA_URI_TAGS,
|
|
3102
|
+
// eslint-disable-line indent
|
|
3103
|
+
transformCaseFunc // eslint-disable-line indent
|
|
3104
|
+
) // eslint-disable-line indent
|
|
3105
|
+
: DEFAULT_DATA_URI_TAGS;
|
|
3106
|
+
FORBID_CONTENTS = objectHasOwnProperty(cfg, 'FORBID_CONTENTS') ? addToSet({}, cfg.FORBID_CONTENTS, transformCaseFunc) : DEFAULT_FORBID_CONTENTS;
|
|
3107
|
+
FORBID_TAGS = objectHasOwnProperty(cfg, 'FORBID_TAGS') ? addToSet({}, cfg.FORBID_TAGS, transformCaseFunc) : {};
|
|
3108
|
+
FORBID_ATTR = objectHasOwnProperty(cfg, 'FORBID_ATTR') ? addToSet({}, cfg.FORBID_ATTR, transformCaseFunc) : {};
|
|
3109
|
+
USE_PROFILES = objectHasOwnProperty(cfg, 'USE_PROFILES') ? cfg.USE_PROFILES : false;
|
|
3110
|
+
ALLOW_ARIA_ATTR = cfg.ALLOW_ARIA_ATTR !== false; // Default true
|
|
3111
|
+
ALLOW_DATA_ATTR = cfg.ALLOW_DATA_ATTR !== false; // Default true
|
|
3112
|
+
ALLOW_UNKNOWN_PROTOCOLS = cfg.ALLOW_UNKNOWN_PROTOCOLS || false; // Default false
|
|
3113
|
+
ALLOW_SELF_CLOSE_IN_ATTR = cfg.ALLOW_SELF_CLOSE_IN_ATTR !== false; // Default true
|
|
3114
|
+
SAFE_FOR_TEMPLATES = cfg.SAFE_FOR_TEMPLATES || false; // Default false
|
|
3115
|
+
SAFE_FOR_XML = cfg.SAFE_FOR_XML !== false; // Default true
|
|
3116
|
+
WHOLE_DOCUMENT = cfg.WHOLE_DOCUMENT || false; // Default false
|
|
3117
|
+
RETURN_DOM = cfg.RETURN_DOM || false; // Default false
|
|
3118
|
+
RETURN_DOM_FRAGMENT = cfg.RETURN_DOM_FRAGMENT || false; // Default false
|
|
3119
|
+
RETURN_TRUSTED_TYPE = cfg.RETURN_TRUSTED_TYPE || false; // Default false
|
|
3120
|
+
FORCE_BODY = cfg.FORCE_BODY || false; // Default false
|
|
3121
|
+
SANITIZE_DOM = cfg.SANITIZE_DOM !== false; // Default true
|
|
3122
|
+
SANITIZE_NAMED_PROPS = cfg.SANITIZE_NAMED_PROPS || false; // Default false
|
|
3123
|
+
KEEP_CONTENT = cfg.KEEP_CONTENT !== false; // Default true
|
|
3124
|
+
IN_PLACE = cfg.IN_PLACE || false; // Default false
|
|
3125
|
+
IS_ALLOWED_URI$1 = cfg.ALLOWED_URI_REGEXP || IS_ALLOWED_URI;
|
|
3126
|
+
NAMESPACE = cfg.NAMESPACE || HTML_NAMESPACE;
|
|
3127
|
+
CUSTOM_ELEMENT_HANDLING = cfg.CUSTOM_ELEMENT_HANDLING || {};
|
|
3128
|
+
if (cfg.CUSTOM_ELEMENT_HANDLING && isRegexOrFunction(cfg.CUSTOM_ELEMENT_HANDLING.tagNameCheck)) {
|
|
3129
|
+
CUSTOM_ELEMENT_HANDLING.tagNameCheck = cfg.CUSTOM_ELEMENT_HANDLING.tagNameCheck;
|
|
3130
|
+
}
|
|
3131
|
+
if (cfg.CUSTOM_ELEMENT_HANDLING && isRegexOrFunction(cfg.CUSTOM_ELEMENT_HANDLING.attributeNameCheck)) {
|
|
3132
|
+
CUSTOM_ELEMENT_HANDLING.attributeNameCheck = cfg.CUSTOM_ELEMENT_HANDLING.attributeNameCheck;
|
|
3133
|
+
}
|
|
3134
|
+
if (cfg.CUSTOM_ELEMENT_HANDLING && typeof cfg.CUSTOM_ELEMENT_HANDLING.allowCustomizedBuiltInElements === 'boolean') {
|
|
3135
|
+
CUSTOM_ELEMENT_HANDLING.allowCustomizedBuiltInElements = cfg.CUSTOM_ELEMENT_HANDLING.allowCustomizedBuiltInElements;
|
|
3136
|
+
}
|
|
3137
|
+
if (SAFE_FOR_TEMPLATES) {
|
|
3138
|
+
ALLOW_DATA_ATTR = false;
|
|
3139
|
+
}
|
|
3140
|
+
if (RETURN_DOM_FRAGMENT) {
|
|
3141
|
+
RETURN_DOM = true;
|
|
3142
|
+
}
|
|
3143
|
+
|
|
3144
|
+
/* Parse profile info */
|
|
3145
|
+
if (USE_PROFILES) {
|
|
3146
|
+
ALLOWED_TAGS = addToSet({}, text);
|
|
3147
|
+
ALLOWED_ATTR = [];
|
|
3148
|
+
if (USE_PROFILES.html === true) {
|
|
3149
|
+
addToSet(ALLOWED_TAGS, html$1);
|
|
3150
|
+
addToSet(ALLOWED_ATTR, html);
|
|
3151
|
+
}
|
|
3152
|
+
if (USE_PROFILES.svg === true) {
|
|
3153
|
+
addToSet(ALLOWED_TAGS, svg$1);
|
|
3154
|
+
addToSet(ALLOWED_ATTR, svg);
|
|
3155
|
+
addToSet(ALLOWED_ATTR, xml);
|
|
3156
|
+
}
|
|
3157
|
+
if (USE_PROFILES.svgFilters === true) {
|
|
3158
|
+
addToSet(ALLOWED_TAGS, svgFilters);
|
|
3159
|
+
addToSet(ALLOWED_ATTR, svg);
|
|
3160
|
+
addToSet(ALLOWED_ATTR, xml);
|
|
3161
|
+
}
|
|
3162
|
+
if (USE_PROFILES.mathMl === true) {
|
|
3163
|
+
addToSet(ALLOWED_TAGS, mathMl$1);
|
|
3164
|
+
addToSet(ALLOWED_ATTR, mathMl);
|
|
3165
|
+
addToSet(ALLOWED_ATTR, xml);
|
|
3166
|
+
}
|
|
3167
|
+
}
|
|
3168
|
+
|
|
3169
|
+
/* Merge configuration parameters */
|
|
3170
|
+
if (cfg.ADD_TAGS) {
|
|
3171
|
+
if (ALLOWED_TAGS === DEFAULT_ALLOWED_TAGS) {
|
|
3172
|
+
ALLOWED_TAGS = clone(ALLOWED_TAGS);
|
|
3173
|
+
}
|
|
3174
|
+
addToSet(ALLOWED_TAGS, cfg.ADD_TAGS, transformCaseFunc);
|
|
3175
|
+
}
|
|
3176
|
+
if (cfg.ADD_ATTR) {
|
|
3177
|
+
if (ALLOWED_ATTR === DEFAULT_ALLOWED_ATTR) {
|
|
3178
|
+
ALLOWED_ATTR = clone(ALLOWED_ATTR);
|
|
3179
|
+
}
|
|
3180
|
+
addToSet(ALLOWED_ATTR, cfg.ADD_ATTR, transformCaseFunc);
|
|
3181
|
+
}
|
|
3182
|
+
if (cfg.ADD_URI_SAFE_ATTR) {
|
|
3183
|
+
addToSet(URI_SAFE_ATTRIBUTES, cfg.ADD_URI_SAFE_ATTR, transformCaseFunc);
|
|
3184
|
+
}
|
|
3185
|
+
if (cfg.FORBID_CONTENTS) {
|
|
3186
|
+
if (FORBID_CONTENTS === DEFAULT_FORBID_CONTENTS) {
|
|
3187
|
+
FORBID_CONTENTS = clone(FORBID_CONTENTS);
|
|
3188
|
+
}
|
|
3189
|
+
addToSet(FORBID_CONTENTS, cfg.FORBID_CONTENTS, transformCaseFunc);
|
|
3190
|
+
}
|
|
3191
|
+
|
|
3192
|
+
/* Add #text in case KEEP_CONTENT is set to true */
|
|
3193
|
+
if (KEEP_CONTENT) {
|
|
3194
|
+
ALLOWED_TAGS['#text'] = true;
|
|
3195
|
+
}
|
|
3196
|
+
|
|
3197
|
+
/* Add html, head and body to ALLOWED_TAGS in case WHOLE_DOCUMENT is true */
|
|
3198
|
+
if (WHOLE_DOCUMENT) {
|
|
3199
|
+
addToSet(ALLOWED_TAGS, ['html', 'head', 'body']);
|
|
3200
|
+
}
|
|
3201
|
+
|
|
3202
|
+
/* Add tbody to ALLOWED_TAGS in case tables are permitted, see #286, #365 */
|
|
3203
|
+
if (ALLOWED_TAGS.table) {
|
|
3204
|
+
addToSet(ALLOWED_TAGS, ['tbody']);
|
|
3205
|
+
delete FORBID_TAGS.tbody;
|
|
3206
|
+
}
|
|
3207
|
+
if (cfg.TRUSTED_TYPES_POLICY) {
|
|
3208
|
+
if (typeof cfg.TRUSTED_TYPES_POLICY.createHTML !== 'function') {
|
|
3209
|
+
throw typeErrorCreate('TRUSTED_TYPES_POLICY configuration option must provide a "createHTML" hook.');
|
|
3210
|
+
}
|
|
3211
|
+
if (typeof cfg.TRUSTED_TYPES_POLICY.createScriptURL !== 'function') {
|
|
3212
|
+
throw typeErrorCreate('TRUSTED_TYPES_POLICY configuration option must provide a "createScriptURL" hook.');
|
|
3213
|
+
}
|
|
3214
|
+
|
|
3215
|
+
// Overwrite existing TrustedTypes policy.
|
|
3216
|
+
trustedTypesPolicy = cfg.TRUSTED_TYPES_POLICY;
|
|
3217
|
+
|
|
3218
|
+
// Sign local variables required by `sanitize`.
|
|
3219
|
+
emptyHTML = trustedTypesPolicy.createHTML('');
|
|
3220
|
+
} else {
|
|
3221
|
+
// Uninitialized policy, attempt to initialize the internal dompurify policy.
|
|
3222
|
+
if (trustedTypesPolicy === undefined) {
|
|
3223
|
+
trustedTypesPolicy = _createTrustedTypesPolicy(trustedTypes, currentScript);
|
|
3224
|
+
}
|
|
3225
|
+
|
|
3226
|
+
// If creating the internal policy succeeded sign internal variables.
|
|
3227
|
+
if (trustedTypesPolicy !== null && typeof emptyHTML === 'string') {
|
|
3228
|
+
emptyHTML = trustedTypesPolicy.createHTML('');
|
|
3229
|
+
}
|
|
3230
|
+
}
|
|
3231
|
+
|
|
3232
|
+
// Prevent further manipulation of configuration.
|
|
3233
|
+
// Not available in IE8, Safari 5, etc.
|
|
3234
|
+
if (freeze) {
|
|
3235
|
+
freeze(cfg);
|
|
3236
|
+
}
|
|
3237
|
+
CONFIG = cfg;
|
|
3238
|
+
};
|
|
3239
|
+
const MATHML_TEXT_INTEGRATION_POINTS = addToSet({}, ['mi', 'mo', 'mn', 'ms', 'mtext']);
|
|
3240
|
+
const HTML_INTEGRATION_POINTS = addToSet({}, ['foreignobject', 'annotation-xml']);
|
|
3241
|
+
|
|
3242
|
+
// Certain elements are allowed in both SVG and HTML
|
|
3243
|
+
// namespace. We need to specify them explicitly
|
|
3244
|
+
// so that they don't get erroneously deleted from
|
|
3245
|
+
// HTML namespace.
|
|
3246
|
+
const COMMON_SVG_AND_HTML_ELEMENTS = addToSet({}, ['title', 'style', 'font', 'a', 'script']);
|
|
3247
|
+
|
|
3248
|
+
/* Keep track of all possible SVG and MathML tags
|
|
3249
|
+
* so that we can perform the namespace checks
|
|
3250
|
+
* correctly. */
|
|
3251
|
+
const ALL_SVG_TAGS = addToSet({}, [...svg$1, ...svgFilters, ...svgDisallowed]);
|
|
3252
|
+
const ALL_MATHML_TAGS = addToSet({}, [...mathMl$1, ...mathMlDisallowed]);
|
|
3253
|
+
|
|
3254
|
+
/**
|
|
3255
|
+
* @param {Element} element a DOM element whose namespace is being checked
|
|
3256
|
+
* @returns {boolean} Return false if the element has a
|
|
3257
|
+
* namespace that a spec-compliant parser would never
|
|
3258
|
+
* return. Return true otherwise.
|
|
3259
|
+
*/
|
|
3260
|
+
const _checkValidNamespace = function _checkValidNamespace(element) {
|
|
3261
|
+
let parent = getParentNode(element);
|
|
3262
|
+
|
|
3263
|
+
// In JSDOM, if we're inside shadow DOM, then parentNode
|
|
3264
|
+
// can be null. We just simulate parent in this case.
|
|
3265
|
+
if (!parent || !parent.tagName) {
|
|
3266
|
+
parent = {
|
|
3267
|
+
namespaceURI: NAMESPACE,
|
|
3268
|
+
tagName: 'template'
|
|
3269
|
+
};
|
|
3270
|
+
}
|
|
3271
|
+
const tagName = stringToLowerCase(element.tagName);
|
|
3272
|
+
const parentTagName = stringToLowerCase(parent.tagName);
|
|
3273
|
+
if (!ALLOWED_NAMESPACES[element.namespaceURI]) {
|
|
3274
|
+
return false;
|
|
3275
|
+
}
|
|
3276
|
+
if (element.namespaceURI === SVG_NAMESPACE) {
|
|
3277
|
+
// The only way to switch from HTML namespace to SVG
|
|
3278
|
+
// is via <svg>. If it happens via any other tag, then
|
|
3279
|
+
// it should be killed.
|
|
3280
|
+
if (parent.namespaceURI === HTML_NAMESPACE) {
|
|
3281
|
+
return tagName === 'svg';
|
|
3282
|
+
}
|
|
3283
|
+
|
|
3284
|
+
// The only way to switch from MathML to SVG is via`
|
|
3285
|
+
// svg if parent is either <annotation-xml> or MathML
|
|
3286
|
+
// text integration points.
|
|
3287
|
+
if (parent.namespaceURI === MATHML_NAMESPACE) {
|
|
3288
|
+
return tagName === 'svg' && (parentTagName === 'annotation-xml' || MATHML_TEXT_INTEGRATION_POINTS[parentTagName]);
|
|
3289
|
+
}
|
|
3290
|
+
|
|
3291
|
+
// We only allow elements that are defined in SVG
|
|
3292
|
+
// spec. All others are disallowed in SVG namespace.
|
|
3293
|
+
return Boolean(ALL_SVG_TAGS[tagName]);
|
|
3294
|
+
}
|
|
3295
|
+
if (element.namespaceURI === MATHML_NAMESPACE) {
|
|
3296
|
+
// The only way to switch from HTML namespace to MathML
|
|
3297
|
+
// is via <math>. If it happens via any other tag, then
|
|
3298
|
+
// it should be killed.
|
|
3299
|
+
if (parent.namespaceURI === HTML_NAMESPACE) {
|
|
3300
|
+
return tagName === 'math';
|
|
3301
|
+
}
|
|
3302
|
+
|
|
3303
|
+
// The only way to switch from SVG to MathML is via
|
|
3304
|
+
// <math> and HTML integration points
|
|
3305
|
+
if (parent.namespaceURI === SVG_NAMESPACE) {
|
|
3306
|
+
return tagName === 'math' && HTML_INTEGRATION_POINTS[parentTagName];
|
|
3307
|
+
}
|
|
3308
|
+
|
|
3309
|
+
// We only allow elements that are defined in MathML
|
|
3310
|
+
// spec. All others are disallowed in MathML namespace.
|
|
3311
|
+
return Boolean(ALL_MATHML_TAGS[tagName]);
|
|
3312
|
+
}
|
|
3313
|
+
if (element.namespaceURI === HTML_NAMESPACE) {
|
|
3314
|
+
// The only way to switch from SVG to HTML is via
|
|
3315
|
+
// HTML integration points, and from MathML to HTML
|
|
3316
|
+
// is via MathML text integration points
|
|
3317
|
+
if (parent.namespaceURI === SVG_NAMESPACE && !HTML_INTEGRATION_POINTS[parentTagName]) {
|
|
3318
|
+
return false;
|
|
3319
|
+
}
|
|
3320
|
+
if (parent.namespaceURI === MATHML_NAMESPACE && !MATHML_TEXT_INTEGRATION_POINTS[parentTagName]) {
|
|
3321
|
+
return false;
|
|
3322
|
+
}
|
|
3323
|
+
|
|
3324
|
+
// We disallow tags that are specific for MathML
|
|
3325
|
+
// or SVG and should never appear in HTML namespace
|
|
3326
|
+
return !ALL_MATHML_TAGS[tagName] && (COMMON_SVG_AND_HTML_ELEMENTS[tagName] || !ALL_SVG_TAGS[tagName]);
|
|
3327
|
+
}
|
|
3328
|
+
|
|
3329
|
+
// For XHTML and XML documents that support custom namespaces
|
|
3330
|
+
if (PARSER_MEDIA_TYPE === 'application/xhtml+xml' && ALLOWED_NAMESPACES[element.namespaceURI]) {
|
|
3331
|
+
return true;
|
|
3332
|
+
}
|
|
3333
|
+
|
|
3334
|
+
// The code should never reach this place (this means
|
|
3335
|
+
// that the element somehow got namespace that is not
|
|
3336
|
+
// HTML, SVG, MathML or allowed via ALLOWED_NAMESPACES).
|
|
3337
|
+
// Return false just in case.
|
|
3338
|
+
return false;
|
|
3339
|
+
};
|
|
3340
|
+
|
|
3341
|
+
/**
|
|
3342
|
+
* _forceRemove
|
|
3343
|
+
*
|
|
3344
|
+
* @param {Node} node a DOM node
|
|
3345
|
+
*/
|
|
3346
|
+
const _forceRemove = function _forceRemove(node) {
|
|
3347
|
+
arrayPush(DOMPurify.removed, {
|
|
3348
|
+
element: node
|
|
3349
|
+
});
|
|
3350
|
+
try {
|
|
3351
|
+
// eslint-disable-next-line unicorn/prefer-dom-node-remove
|
|
3352
|
+
getParentNode(node).removeChild(node);
|
|
3353
|
+
} catch (_) {
|
|
3354
|
+
remove(node);
|
|
3355
|
+
}
|
|
3356
|
+
};
|
|
3357
|
+
|
|
3358
|
+
/**
|
|
3359
|
+
* _removeAttribute
|
|
3360
|
+
*
|
|
3361
|
+
* @param {String} name an Attribute name
|
|
3362
|
+
* @param {Node} node a DOM node
|
|
3363
|
+
*/
|
|
3364
|
+
const _removeAttribute = function _removeAttribute(name, node) {
|
|
3365
|
+
try {
|
|
3366
|
+
arrayPush(DOMPurify.removed, {
|
|
3367
|
+
attribute: node.getAttributeNode(name),
|
|
3368
|
+
from: node
|
|
3369
|
+
});
|
|
3370
|
+
} catch (_) {
|
|
3371
|
+
arrayPush(DOMPurify.removed, {
|
|
3372
|
+
attribute: null,
|
|
3373
|
+
from: node
|
|
3374
|
+
});
|
|
3375
|
+
}
|
|
3376
|
+
node.removeAttribute(name);
|
|
3377
|
+
|
|
3378
|
+
// We void attribute values for unremovable "is"" attributes
|
|
3379
|
+
if (name === 'is' && !ALLOWED_ATTR[name]) {
|
|
3380
|
+
if (RETURN_DOM || RETURN_DOM_FRAGMENT) {
|
|
3381
|
+
try {
|
|
3382
|
+
_forceRemove(node);
|
|
3383
|
+
} catch (_) {}
|
|
3384
|
+
} else {
|
|
3385
|
+
try {
|
|
3386
|
+
node.setAttribute(name, '');
|
|
3387
|
+
} catch (_) {}
|
|
3388
|
+
}
|
|
3389
|
+
}
|
|
3390
|
+
};
|
|
3391
|
+
|
|
3392
|
+
/**
|
|
3393
|
+
* _initDocument
|
|
3394
|
+
*
|
|
3395
|
+
* @param {String} dirty a string of dirty markup
|
|
3396
|
+
* @return {Document} a DOM, filled with the dirty markup
|
|
3397
|
+
*/
|
|
3398
|
+
const _initDocument = function _initDocument(dirty) {
|
|
3399
|
+
/* Create a HTML document */
|
|
3400
|
+
let doc = null;
|
|
3401
|
+
let leadingWhitespace = null;
|
|
3402
|
+
if (FORCE_BODY) {
|
|
3403
|
+
dirty = '<remove></remove>' + dirty;
|
|
3404
|
+
} else {
|
|
3405
|
+
/* If FORCE_BODY isn't used, leading whitespace needs to be preserved manually */
|
|
3406
|
+
const matches = stringMatch(dirty, /^[\r\n\t ]+/);
|
|
3407
|
+
leadingWhitespace = matches && matches[0];
|
|
3408
|
+
}
|
|
3409
|
+
if (PARSER_MEDIA_TYPE === 'application/xhtml+xml' && NAMESPACE === HTML_NAMESPACE) {
|
|
3410
|
+
// Root of XHTML doc must contain xmlns declaration (see https://www.w3.org/TR/xhtml1/normative.html#strict)
|
|
3411
|
+
dirty = '<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body>' + dirty + '</body></html>';
|
|
3412
|
+
}
|
|
3413
|
+
const dirtyPayload = trustedTypesPolicy ? trustedTypesPolicy.createHTML(dirty) : dirty;
|
|
3414
|
+
/*
|
|
3415
|
+
* Use the DOMParser API by default, fallback later if needs be
|
|
3416
|
+
* DOMParser not work for svg when has multiple root element.
|
|
3417
|
+
*/
|
|
3418
|
+
if (NAMESPACE === HTML_NAMESPACE) {
|
|
3419
|
+
try {
|
|
3420
|
+
doc = new DOMParser().parseFromString(dirtyPayload, PARSER_MEDIA_TYPE);
|
|
3421
|
+
} catch (_) {}
|
|
3422
|
+
}
|
|
3423
|
+
|
|
3424
|
+
/* Use createHTMLDocument in case DOMParser is not available */
|
|
3425
|
+
if (!doc || !doc.documentElement) {
|
|
3426
|
+
doc = implementation.createDocument(NAMESPACE, 'template', null);
|
|
3427
|
+
try {
|
|
3428
|
+
doc.documentElement.innerHTML = IS_EMPTY_INPUT ? emptyHTML : dirtyPayload;
|
|
3429
|
+
} catch (_) {
|
|
3430
|
+
// Syntax error if dirtyPayload is invalid xml
|
|
3431
|
+
}
|
|
3432
|
+
}
|
|
3433
|
+
const body = doc.body || doc.documentElement;
|
|
3434
|
+
if (dirty && leadingWhitespace) {
|
|
3435
|
+
body.insertBefore(document.createTextNode(leadingWhitespace), body.childNodes[0] || null);
|
|
3436
|
+
}
|
|
3437
|
+
|
|
3438
|
+
/* Work on whole document or just its body */
|
|
3439
|
+
if (NAMESPACE === HTML_NAMESPACE) {
|
|
3440
|
+
return getElementsByTagName.call(doc, WHOLE_DOCUMENT ? 'html' : 'body')[0];
|
|
3441
|
+
}
|
|
3442
|
+
return WHOLE_DOCUMENT ? doc.documentElement : body;
|
|
3443
|
+
};
|
|
3444
|
+
|
|
3445
|
+
/**
|
|
3446
|
+
* Creates a NodeIterator object that you can use to traverse filtered lists of nodes or elements in a document.
|
|
3447
|
+
*
|
|
3448
|
+
* @param {Node} root The root element or node to start traversing on.
|
|
3449
|
+
* @return {NodeIterator} The created NodeIterator
|
|
3450
|
+
*/
|
|
3451
|
+
const _createNodeIterator = function _createNodeIterator(root) {
|
|
3452
|
+
return createNodeIterator.call(root.ownerDocument || root, root,
|
|
3453
|
+
// eslint-disable-next-line no-bitwise
|
|
3454
|
+
NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_COMMENT | NodeFilter.SHOW_TEXT | NodeFilter.SHOW_PROCESSING_INSTRUCTION | NodeFilter.SHOW_CDATA_SECTION, null);
|
|
3455
|
+
};
|
|
3456
|
+
|
|
3457
|
+
/**
|
|
3458
|
+
* _isClobbered
|
|
3459
|
+
*
|
|
3460
|
+
* @param {Node} elm element to check for clobbering attacks
|
|
3461
|
+
* @return {Boolean} true if clobbered, false if safe
|
|
3462
|
+
*/
|
|
3463
|
+
const _isClobbered = function _isClobbered(elm) {
|
|
3464
|
+
return elm instanceof HTMLFormElement && (typeof elm.nodeName !== 'string' || typeof elm.textContent !== 'string' || typeof elm.removeChild !== 'function' || !(elm.attributes instanceof NamedNodeMap) || typeof elm.removeAttribute !== 'function' || typeof elm.setAttribute !== 'function' || typeof elm.namespaceURI !== 'string' || typeof elm.insertBefore !== 'function' || typeof elm.hasChildNodes !== 'function');
|
|
3465
|
+
};
|
|
3466
|
+
|
|
3467
|
+
/**
|
|
3468
|
+
* Checks whether the given object is a DOM node.
|
|
3469
|
+
*
|
|
3470
|
+
* @param {Node} object object to check whether it's a DOM node
|
|
3471
|
+
* @return {Boolean} true is object is a DOM node
|
|
3472
|
+
*/
|
|
3473
|
+
const _isNode = function _isNode(object) {
|
|
3474
|
+
return typeof Node === 'function' && object instanceof Node;
|
|
3475
|
+
};
|
|
3476
|
+
|
|
3477
|
+
/**
|
|
3478
|
+
* _executeHook
|
|
3479
|
+
* Execute user configurable hooks
|
|
3480
|
+
*
|
|
3481
|
+
* @param {String} entryPoint Name of the hook's entry point
|
|
3482
|
+
* @param {Node} currentNode node to work on with the hook
|
|
3483
|
+
* @param {Object} data additional hook parameters
|
|
3484
|
+
*/
|
|
3485
|
+
const _executeHook = function _executeHook(entryPoint, currentNode, data) {
|
|
3486
|
+
if (!hooks[entryPoint]) {
|
|
3487
|
+
return;
|
|
3488
|
+
}
|
|
3489
|
+
arrayForEach(hooks[entryPoint], hook => {
|
|
3490
|
+
hook.call(DOMPurify, currentNode, data, CONFIG);
|
|
3491
|
+
});
|
|
3492
|
+
};
|
|
3493
|
+
|
|
3494
|
+
/**
|
|
3495
|
+
* _sanitizeElements
|
|
3496
|
+
*
|
|
3497
|
+
* @protect nodeName
|
|
3498
|
+
* @protect textContent
|
|
3499
|
+
* @protect removeChild
|
|
3500
|
+
*
|
|
3501
|
+
* @param {Node} currentNode to check for permission to exist
|
|
3502
|
+
* @return {Boolean} true if node was killed, false if left alive
|
|
3503
|
+
*/
|
|
3504
|
+
const _sanitizeElements = function _sanitizeElements(currentNode) {
|
|
3505
|
+
let content = null;
|
|
3506
|
+
|
|
3507
|
+
/* Execute a hook if present */
|
|
3508
|
+
_executeHook('beforeSanitizeElements', currentNode, null);
|
|
3509
|
+
|
|
3510
|
+
/* Check if element is clobbered or can clobber */
|
|
3511
|
+
if (_isClobbered(currentNode)) {
|
|
3512
|
+
_forceRemove(currentNode);
|
|
3513
|
+
return true;
|
|
3514
|
+
}
|
|
3515
|
+
|
|
3516
|
+
/* Now let's check the element's type and name */
|
|
3517
|
+
const tagName = transformCaseFunc(currentNode.nodeName);
|
|
3518
|
+
|
|
3519
|
+
/* Execute a hook if present */
|
|
3520
|
+
_executeHook('uponSanitizeElement', currentNode, {
|
|
3521
|
+
tagName,
|
|
3522
|
+
allowedTags: ALLOWED_TAGS
|
|
3523
|
+
});
|
|
3524
|
+
|
|
3525
|
+
/* Detect mXSS attempts abusing namespace confusion */
|
|
3526
|
+
if (currentNode.hasChildNodes() && !_isNode(currentNode.firstElementChild) && regExpTest(/<[/\w]/g, currentNode.innerHTML) && regExpTest(/<[/\w]/g, currentNode.textContent)) {
|
|
3527
|
+
_forceRemove(currentNode);
|
|
3528
|
+
return true;
|
|
3529
|
+
}
|
|
3530
|
+
|
|
3531
|
+
/* Remove any occurrence of processing instructions */
|
|
3532
|
+
if (currentNode.nodeType === NODE_TYPE.progressingInstruction) {
|
|
3533
|
+
_forceRemove(currentNode);
|
|
3534
|
+
return true;
|
|
3535
|
+
}
|
|
3536
|
+
|
|
3537
|
+
/* Remove any kind of possibly harmful comments */
|
|
3538
|
+
if (SAFE_FOR_XML && currentNode.nodeType === NODE_TYPE.comment && regExpTest(/<[/\w]/g, currentNode.data)) {
|
|
3539
|
+
_forceRemove(currentNode);
|
|
3540
|
+
return true;
|
|
3541
|
+
}
|
|
3542
|
+
|
|
3543
|
+
/* Remove element if anything forbids its presence */
|
|
3544
|
+
if (!ALLOWED_TAGS[tagName] || FORBID_TAGS[tagName]) {
|
|
3545
|
+
/* Check if we have a custom element to handle */
|
|
3546
|
+
if (!FORBID_TAGS[tagName] && _isBasicCustomElement(tagName)) {
|
|
3547
|
+
if (CUSTOM_ELEMENT_HANDLING.tagNameCheck instanceof RegExp && regExpTest(CUSTOM_ELEMENT_HANDLING.tagNameCheck, tagName)) {
|
|
3548
|
+
return false;
|
|
3549
|
+
}
|
|
3550
|
+
if (CUSTOM_ELEMENT_HANDLING.tagNameCheck instanceof Function && CUSTOM_ELEMENT_HANDLING.tagNameCheck(tagName)) {
|
|
3551
|
+
return false;
|
|
3552
|
+
}
|
|
3553
|
+
}
|
|
3554
|
+
|
|
3555
|
+
/* Keep content except for bad-listed elements */
|
|
3556
|
+
if (KEEP_CONTENT && !FORBID_CONTENTS[tagName]) {
|
|
3557
|
+
const parentNode = getParentNode(currentNode) || currentNode.parentNode;
|
|
3558
|
+
const childNodes = getChildNodes(currentNode) || currentNode.childNodes;
|
|
3559
|
+
if (childNodes && parentNode) {
|
|
3560
|
+
const childCount = childNodes.length;
|
|
3561
|
+
for (let i = childCount - 1; i >= 0; --i) {
|
|
3562
|
+
const childClone = cloneNode(childNodes[i], true);
|
|
3563
|
+
childClone.__removalCount = (currentNode.__removalCount || 0) + 1;
|
|
3564
|
+
parentNode.insertBefore(childClone, getNextSibling(currentNode));
|
|
3565
|
+
}
|
|
3566
|
+
}
|
|
3567
|
+
}
|
|
3568
|
+
_forceRemove(currentNode);
|
|
3569
|
+
return true;
|
|
3570
|
+
}
|
|
3571
|
+
|
|
3572
|
+
/* Check whether element has a valid namespace */
|
|
3573
|
+
if (currentNode instanceof Element && !_checkValidNamespace(currentNode)) {
|
|
3574
|
+
_forceRemove(currentNode);
|
|
3575
|
+
return true;
|
|
3576
|
+
}
|
|
3577
|
+
|
|
3578
|
+
/* Make sure that older browsers don't get fallback-tag mXSS */
|
|
3579
|
+
if ((tagName === 'noscript' || tagName === 'noembed' || tagName === 'noframes') && regExpTest(/<\/no(script|embed|frames)/i, currentNode.innerHTML)) {
|
|
3580
|
+
_forceRemove(currentNode);
|
|
3581
|
+
return true;
|
|
3582
|
+
}
|
|
3583
|
+
|
|
3584
|
+
/* Sanitize element content to be template-safe */
|
|
3585
|
+
if (SAFE_FOR_TEMPLATES && currentNode.nodeType === NODE_TYPE.text) {
|
|
3586
|
+
/* Get the element's text content */
|
|
3587
|
+
content = currentNode.textContent;
|
|
3588
|
+
arrayForEach([MUSTACHE_EXPR, ERB_EXPR, TMPLIT_EXPR], expr => {
|
|
3589
|
+
content = stringReplace(content, expr, ' ');
|
|
3590
|
+
});
|
|
3591
|
+
if (currentNode.textContent !== content) {
|
|
3592
|
+
arrayPush(DOMPurify.removed, {
|
|
3593
|
+
element: currentNode.cloneNode()
|
|
3594
|
+
});
|
|
3595
|
+
currentNode.textContent = content;
|
|
3596
|
+
}
|
|
3597
|
+
}
|
|
3598
|
+
|
|
3599
|
+
/* Execute a hook if present */
|
|
3600
|
+
_executeHook('afterSanitizeElements', currentNode, null);
|
|
3601
|
+
return false;
|
|
3602
|
+
};
|
|
3603
|
+
|
|
3604
|
+
/**
|
|
3605
|
+
* _isValidAttribute
|
|
3606
|
+
*
|
|
3607
|
+
* @param {string} lcTag Lowercase tag name of containing element.
|
|
3608
|
+
* @param {string} lcName Lowercase attribute name.
|
|
3609
|
+
* @param {string} value Attribute value.
|
|
3610
|
+
* @return {Boolean} Returns true if `value` is valid, otherwise false.
|
|
3611
|
+
*/
|
|
3612
|
+
// eslint-disable-next-line complexity
|
|
3613
|
+
const _isValidAttribute = function _isValidAttribute(lcTag, lcName, value) {
|
|
3614
|
+
/* Make sure attribute cannot clobber */
|
|
3615
|
+
if (SANITIZE_DOM && (lcName === 'id' || lcName === 'name') && (value in document || value in formElement)) {
|
|
3616
|
+
return false;
|
|
3617
|
+
}
|
|
3618
|
+
|
|
3619
|
+
/* Allow valid data-* attributes: At least one character after "-"
|
|
3620
|
+
(https://html.spec.whatwg.org/multipage/dom.html#embedding-custom-non-visible-data-with-the-data-*-attributes)
|
|
3621
|
+
XML-compatible (https://html.spec.whatwg.org/multipage/infrastructure.html#xml-compatible and http://www.w3.org/TR/xml/#d0e804)
|
|
3622
|
+
We don't need to check the value; it's always URI safe. */
|
|
3623
|
+
if (ALLOW_DATA_ATTR && !FORBID_ATTR[lcName] && regExpTest(DATA_ATTR, lcName)) ; else if (ALLOW_ARIA_ATTR && regExpTest(ARIA_ATTR, lcName)) ; else if (!ALLOWED_ATTR[lcName] || FORBID_ATTR[lcName]) {
|
|
3624
|
+
if (
|
|
3625
|
+
// First condition does a very basic check if a) it's basically a valid custom element tagname AND
|
|
3626
|
+
// b) if the tagName passes whatever the user has configured for CUSTOM_ELEMENT_HANDLING.tagNameCheck
|
|
3627
|
+
// and c) if the attribute name passes whatever the user has configured for CUSTOM_ELEMENT_HANDLING.attributeNameCheck
|
|
3628
|
+
_isBasicCustomElement(lcTag) && (CUSTOM_ELEMENT_HANDLING.tagNameCheck instanceof RegExp && regExpTest(CUSTOM_ELEMENT_HANDLING.tagNameCheck, lcTag) || CUSTOM_ELEMENT_HANDLING.tagNameCheck instanceof Function && CUSTOM_ELEMENT_HANDLING.tagNameCheck(lcTag)) && (CUSTOM_ELEMENT_HANDLING.attributeNameCheck instanceof RegExp && regExpTest(CUSTOM_ELEMENT_HANDLING.attributeNameCheck, lcName) || CUSTOM_ELEMENT_HANDLING.attributeNameCheck instanceof Function && CUSTOM_ELEMENT_HANDLING.attributeNameCheck(lcName)) ||
|
|
3629
|
+
// Alternative, second condition checks if it's an `is`-attribute, AND
|
|
3630
|
+
// the value passes whatever the user has configured for CUSTOM_ELEMENT_HANDLING.tagNameCheck
|
|
3631
|
+
lcName === 'is' && CUSTOM_ELEMENT_HANDLING.allowCustomizedBuiltInElements && (CUSTOM_ELEMENT_HANDLING.tagNameCheck instanceof RegExp && regExpTest(CUSTOM_ELEMENT_HANDLING.tagNameCheck, value) || CUSTOM_ELEMENT_HANDLING.tagNameCheck instanceof Function && CUSTOM_ELEMENT_HANDLING.tagNameCheck(value))) ; else {
|
|
3632
|
+
return false;
|
|
3633
|
+
}
|
|
3634
|
+
/* Check value is safe. First, is attr inert? If so, is safe */
|
|
3635
|
+
} else if (URI_SAFE_ATTRIBUTES[lcName]) ; else if (regExpTest(IS_ALLOWED_URI$1, stringReplace(value, ATTR_WHITESPACE, ''))) ; else if ((lcName === 'src' || lcName === 'xlink:href' || lcName === 'href') && lcTag !== 'script' && stringIndexOf(value, 'data:') === 0 && DATA_URI_TAGS[lcTag]) ; else if (ALLOW_UNKNOWN_PROTOCOLS && !regExpTest(IS_SCRIPT_OR_DATA, stringReplace(value, ATTR_WHITESPACE, ''))) ; else if (value) {
|
|
3636
|
+
return false;
|
|
3637
|
+
} else ;
|
|
3638
|
+
return true;
|
|
3639
|
+
};
|
|
3640
|
+
|
|
3641
|
+
/**
|
|
3642
|
+
* _isBasicCustomElement
|
|
3643
|
+
* checks if at least one dash is included in tagName, and it's not the first char
|
|
3644
|
+
* for more sophisticated checking see https://github.com/sindresorhus/validate-element-name
|
|
3645
|
+
*
|
|
3646
|
+
* @param {string} tagName name of the tag of the node to sanitize
|
|
3647
|
+
* @returns {boolean} Returns true if the tag name meets the basic criteria for a custom element, otherwise false.
|
|
3648
|
+
*/
|
|
3649
|
+
const _isBasicCustomElement = function _isBasicCustomElement(tagName) {
|
|
3650
|
+
return tagName !== 'annotation-xml' && stringMatch(tagName, CUSTOM_ELEMENT);
|
|
3651
|
+
};
|
|
3652
|
+
|
|
3653
|
+
/**
|
|
3654
|
+
* _sanitizeAttributes
|
|
3655
|
+
*
|
|
3656
|
+
* @protect attributes
|
|
3657
|
+
* @protect nodeName
|
|
3658
|
+
* @protect removeAttribute
|
|
3659
|
+
* @protect setAttribute
|
|
3660
|
+
*
|
|
3661
|
+
* @param {Node} currentNode to sanitize
|
|
3662
|
+
*/
|
|
3663
|
+
const _sanitizeAttributes = function _sanitizeAttributes(currentNode) {
|
|
3664
|
+
/* Execute a hook if present */
|
|
3665
|
+
_executeHook('beforeSanitizeAttributes', currentNode, null);
|
|
3666
|
+
const {
|
|
3667
|
+
attributes
|
|
3668
|
+
} = currentNode;
|
|
3669
|
+
|
|
3670
|
+
/* Check if we have attributes; if not we might have a text node */
|
|
3671
|
+
if (!attributes) {
|
|
3672
|
+
return;
|
|
3673
|
+
}
|
|
3674
|
+
const hookEvent = {
|
|
3675
|
+
attrName: '',
|
|
3676
|
+
attrValue: '',
|
|
3677
|
+
keepAttr: true,
|
|
3678
|
+
allowedAttributes: ALLOWED_ATTR
|
|
3679
|
+
};
|
|
3680
|
+
let l = attributes.length;
|
|
3681
|
+
|
|
3682
|
+
/* Go backwards over all attributes; safely remove bad ones */
|
|
3683
|
+
while (l--) {
|
|
3684
|
+
const attr = attributes[l];
|
|
3685
|
+
const {
|
|
3686
|
+
name,
|
|
3687
|
+
namespaceURI,
|
|
3688
|
+
value: attrValue
|
|
3689
|
+
} = attr;
|
|
3690
|
+
const lcName = transformCaseFunc(name);
|
|
3691
|
+
let value = name === 'value' ? attrValue : stringTrim(attrValue);
|
|
3692
|
+
|
|
3693
|
+
/* Execute a hook if present */
|
|
3694
|
+
hookEvent.attrName = lcName;
|
|
3695
|
+
hookEvent.attrValue = value;
|
|
3696
|
+
hookEvent.keepAttr = true;
|
|
3697
|
+
hookEvent.forceKeepAttr = undefined; // Allows developers to see this is a property they can set
|
|
3698
|
+
_executeHook('uponSanitizeAttribute', currentNode, hookEvent);
|
|
3699
|
+
value = hookEvent.attrValue;
|
|
3700
|
+
|
|
3701
|
+
/* Work around a security issue with comments inside attributes */
|
|
3702
|
+
if (SAFE_FOR_XML && regExpTest(/((--!?|])>)|<\/(style|title)/i, value)) {
|
|
3703
|
+
_removeAttribute(name, currentNode);
|
|
3704
|
+
continue;
|
|
3705
|
+
}
|
|
3706
|
+
|
|
3707
|
+
/* Did the hooks approve of the attribute? */
|
|
3708
|
+
if (hookEvent.forceKeepAttr) {
|
|
3709
|
+
continue;
|
|
3710
|
+
}
|
|
3711
|
+
|
|
3712
|
+
/* Remove attribute */
|
|
3713
|
+
_removeAttribute(name, currentNode);
|
|
3714
|
+
|
|
3715
|
+
/* Did the hooks approve of the attribute? */
|
|
3716
|
+
if (!hookEvent.keepAttr) {
|
|
3717
|
+
continue;
|
|
3718
|
+
}
|
|
3719
|
+
|
|
3720
|
+
/* Work around a security issue in jQuery 3.0 */
|
|
3721
|
+
if (!ALLOW_SELF_CLOSE_IN_ATTR && regExpTest(/\/>/i, value)) {
|
|
3722
|
+
_removeAttribute(name, currentNode);
|
|
3723
|
+
continue;
|
|
3724
|
+
}
|
|
3725
|
+
|
|
3726
|
+
/* Sanitize attribute content to be template-safe */
|
|
3727
|
+
if (SAFE_FOR_TEMPLATES) {
|
|
3728
|
+
arrayForEach([MUSTACHE_EXPR, ERB_EXPR, TMPLIT_EXPR], expr => {
|
|
3729
|
+
value = stringReplace(value, expr, ' ');
|
|
3730
|
+
});
|
|
3731
|
+
}
|
|
3732
|
+
|
|
3733
|
+
/* Is `value` valid for this attribute? */
|
|
3734
|
+
const lcTag = transformCaseFunc(currentNode.nodeName);
|
|
3735
|
+
if (!_isValidAttribute(lcTag, lcName, value)) {
|
|
3736
|
+
continue;
|
|
3737
|
+
}
|
|
3738
|
+
|
|
3739
|
+
/* Full DOM Clobbering protection via namespace isolation,
|
|
3740
|
+
* Prefix id and name attributes with `user-content-`
|
|
3741
|
+
*/
|
|
3742
|
+
if (SANITIZE_NAMED_PROPS && (lcName === 'id' || lcName === 'name')) {
|
|
3743
|
+
// Remove the attribute with this value
|
|
3744
|
+
_removeAttribute(name, currentNode);
|
|
3745
|
+
|
|
3746
|
+
// Prefix the value and later re-create the attribute with the sanitized value
|
|
3747
|
+
value = SANITIZE_NAMED_PROPS_PREFIX + value;
|
|
3748
|
+
}
|
|
3749
|
+
|
|
3750
|
+
/* Handle attributes that require Trusted Types */
|
|
3751
|
+
if (trustedTypesPolicy && typeof trustedTypes === 'object' && typeof trustedTypes.getAttributeType === 'function') {
|
|
3752
|
+
if (namespaceURI) ; else {
|
|
3753
|
+
switch (trustedTypes.getAttributeType(lcTag, lcName)) {
|
|
3754
|
+
case 'TrustedHTML':
|
|
3755
|
+
{
|
|
3756
|
+
value = trustedTypesPolicy.createHTML(value);
|
|
3757
|
+
break;
|
|
3758
|
+
}
|
|
3759
|
+
case 'TrustedScriptURL':
|
|
3760
|
+
{
|
|
3761
|
+
value = trustedTypesPolicy.createScriptURL(value);
|
|
3762
|
+
break;
|
|
3763
|
+
}
|
|
3764
|
+
}
|
|
3765
|
+
}
|
|
3766
|
+
}
|
|
3767
|
+
|
|
3768
|
+
/* Handle invalid data-* attribute set by try-catching it */
|
|
3769
|
+
try {
|
|
3770
|
+
if (namespaceURI) {
|
|
3771
|
+
currentNode.setAttributeNS(namespaceURI, name, value);
|
|
3772
|
+
} else {
|
|
3773
|
+
/* Fallback to setAttribute() for browser-unrecognized namespaces e.g. "x-schema". */
|
|
3774
|
+
currentNode.setAttribute(name, value);
|
|
3775
|
+
}
|
|
3776
|
+
if (_isClobbered(currentNode)) {
|
|
3777
|
+
_forceRemove(currentNode);
|
|
3778
|
+
} else {
|
|
3779
|
+
arrayPop(DOMPurify.removed);
|
|
3780
|
+
}
|
|
3781
|
+
} catch (_) {}
|
|
3782
|
+
}
|
|
3783
|
+
|
|
3784
|
+
/* Execute a hook if present */
|
|
3785
|
+
_executeHook('afterSanitizeAttributes', currentNode, null);
|
|
3786
|
+
};
|
|
3787
|
+
|
|
3788
|
+
/**
|
|
3789
|
+
* _sanitizeShadowDOM
|
|
3790
|
+
*
|
|
3791
|
+
* @param {DocumentFragment} fragment to iterate over recursively
|
|
3792
|
+
*/
|
|
3793
|
+
const _sanitizeShadowDOM = function _sanitizeShadowDOM(fragment) {
|
|
3794
|
+
let shadowNode = null;
|
|
3795
|
+
const shadowIterator = _createNodeIterator(fragment);
|
|
3796
|
+
|
|
3797
|
+
/* Execute a hook if present */
|
|
3798
|
+
_executeHook('beforeSanitizeShadowDOM', fragment, null);
|
|
3799
|
+
while (shadowNode = shadowIterator.nextNode()) {
|
|
3800
|
+
/* Execute a hook if present */
|
|
3801
|
+
_executeHook('uponSanitizeShadowNode', shadowNode, null);
|
|
3802
|
+
|
|
3803
|
+
/* Sanitize tags and elements */
|
|
3804
|
+
if (_sanitizeElements(shadowNode)) {
|
|
3805
|
+
continue;
|
|
3806
|
+
}
|
|
3807
|
+
|
|
3808
|
+
/* Deep shadow DOM detected */
|
|
3809
|
+
if (shadowNode.content instanceof DocumentFragment) {
|
|
3810
|
+
_sanitizeShadowDOM(shadowNode.content);
|
|
3811
|
+
}
|
|
3812
|
+
|
|
3813
|
+
/* Check attributes, sanitize if necessary */
|
|
3814
|
+
_sanitizeAttributes(shadowNode);
|
|
3815
|
+
}
|
|
3816
|
+
|
|
3817
|
+
/* Execute a hook if present */
|
|
3818
|
+
_executeHook('afterSanitizeShadowDOM', fragment, null);
|
|
3819
|
+
};
|
|
3820
|
+
|
|
3821
|
+
/**
|
|
3822
|
+
* Sanitize
|
|
3823
|
+
* Public method providing core sanitation functionality
|
|
3824
|
+
*
|
|
3825
|
+
* @param {String|Node} dirty string or DOM node
|
|
3826
|
+
* @param {Object} cfg object
|
|
3827
|
+
*/
|
|
3828
|
+
// eslint-disable-next-line complexity
|
|
3829
|
+
DOMPurify.sanitize = function (dirty) {
|
|
3830
|
+
let cfg = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
3831
|
+
let body = null;
|
|
3832
|
+
let importedNode = null;
|
|
3833
|
+
let currentNode = null;
|
|
3834
|
+
let returnNode = null;
|
|
3835
|
+
/* Make sure we have a string to sanitize.
|
|
3836
|
+
DO NOT return early, as this will return the wrong type if
|
|
3837
|
+
the user has requested a DOM object rather than a string */
|
|
3838
|
+
IS_EMPTY_INPUT = !dirty;
|
|
3839
|
+
if (IS_EMPTY_INPUT) {
|
|
3840
|
+
dirty = '<!-->';
|
|
3841
|
+
}
|
|
3842
|
+
|
|
3843
|
+
/* Stringify, in case dirty is an object */
|
|
3844
|
+
if (typeof dirty !== 'string' && !_isNode(dirty)) {
|
|
3845
|
+
if (typeof dirty.toString === 'function') {
|
|
3846
|
+
dirty = dirty.toString();
|
|
3847
|
+
if (typeof dirty !== 'string') {
|
|
3848
|
+
throw typeErrorCreate('dirty is not a string, aborting');
|
|
3849
|
+
}
|
|
3850
|
+
} else {
|
|
3851
|
+
throw typeErrorCreate('toString is not a function');
|
|
3852
|
+
}
|
|
3853
|
+
}
|
|
3854
|
+
|
|
3855
|
+
/* Return dirty HTML if DOMPurify cannot run */
|
|
3856
|
+
if (!DOMPurify.isSupported) {
|
|
3857
|
+
return dirty;
|
|
3858
|
+
}
|
|
3859
|
+
|
|
3860
|
+
/* Assign config vars */
|
|
3861
|
+
if (!SET_CONFIG) {
|
|
3862
|
+
_parseConfig(cfg);
|
|
3863
|
+
}
|
|
3864
|
+
|
|
3865
|
+
/* Clean up removed elements */
|
|
3866
|
+
DOMPurify.removed = [];
|
|
3867
|
+
|
|
3868
|
+
/* Check if dirty is correctly typed for IN_PLACE */
|
|
3869
|
+
if (typeof dirty === 'string') {
|
|
3870
|
+
IN_PLACE = false;
|
|
3871
|
+
}
|
|
3872
|
+
if (IN_PLACE) {
|
|
3873
|
+
/* Do some early pre-sanitization to avoid unsafe root nodes */
|
|
3874
|
+
if (dirty.nodeName) {
|
|
3875
|
+
const tagName = transformCaseFunc(dirty.nodeName);
|
|
3876
|
+
if (!ALLOWED_TAGS[tagName] || FORBID_TAGS[tagName]) {
|
|
3877
|
+
throw typeErrorCreate('root node is forbidden and cannot be sanitized in-place');
|
|
3878
|
+
}
|
|
3879
|
+
}
|
|
3880
|
+
} else if (dirty instanceof Node) {
|
|
3881
|
+
/* If dirty is a DOM element, append to an empty document to avoid
|
|
3882
|
+
elements being stripped by the parser */
|
|
3883
|
+
body = _initDocument('<!---->');
|
|
3884
|
+
importedNode = body.ownerDocument.importNode(dirty, true);
|
|
3885
|
+
if (importedNode.nodeType === NODE_TYPE.element && importedNode.nodeName === 'BODY') {
|
|
3886
|
+
/* Node is already a body, use as is */
|
|
3887
|
+
body = importedNode;
|
|
3888
|
+
} else if (importedNode.nodeName === 'HTML') {
|
|
3889
|
+
body = importedNode;
|
|
3890
|
+
} else {
|
|
3891
|
+
// eslint-disable-next-line unicorn/prefer-dom-node-append
|
|
3892
|
+
body.appendChild(importedNode);
|
|
3893
|
+
}
|
|
3894
|
+
} else {
|
|
3895
|
+
/* Exit directly if we have nothing to do */
|
|
3896
|
+
if (!RETURN_DOM && !SAFE_FOR_TEMPLATES && !WHOLE_DOCUMENT &&
|
|
3897
|
+
// eslint-disable-next-line unicorn/prefer-includes
|
|
3898
|
+
dirty.indexOf('<') === -1) {
|
|
3899
|
+
return trustedTypesPolicy && RETURN_TRUSTED_TYPE ? trustedTypesPolicy.createHTML(dirty) : dirty;
|
|
3900
|
+
}
|
|
3901
|
+
|
|
3902
|
+
/* Initialize the document to work on */
|
|
3903
|
+
body = _initDocument(dirty);
|
|
3904
|
+
|
|
3905
|
+
/* Check we have a DOM node from the data */
|
|
3906
|
+
if (!body) {
|
|
3907
|
+
return RETURN_DOM ? null : RETURN_TRUSTED_TYPE ? emptyHTML : '';
|
|
3908
|
+
}
|
|
3909
|
+
}
|
|
3910
|
+
|
|
3911
|
+
/* Remove first element node (ours) if FORCE_BODY is set */
|
|
3912
|
+
if (body && FORCE_BODY) {
|
|
3913
|
+
_forceRemove(body.firstChild);
|
|
3914
|
+
}
|
|
3915
|
+
|
|
3916
|
+
/* Get node iterator */
|
|
3917
|
+
const nodeIterator = _createNodeIterator(IN_PLACE ? dirty : body);
|
|
3918
|
+
|
|
3919
|
+
/* Now start iterating over the created document */
|
|
3920
|
+
while (currentNode = nodeIterator.nextNode()) {
|
|
3921
|
+
/* Sanitize tags and elements */
|
|
3922
|
+
if (_sanitizeElements(currentNode)) {
|
|
3923
|
+
continue;
|
|
3924
|
+
}
|
|
3925
|
+
|
|
3926
|
+
/* Shadow DOM detected, sanitize it */
|
|
3927
|
+
if (currentNode.content instanceof DocumentFragment) {
|
|
3928
|
+
_sanitizeShadowDOM(currentNode.content);
|
|
3929
|
+
}
|
|
3930
|
+
|
|
3931
|
+
/* Check attributes, sanitize if necessary */
|
|
3932
|
+
_sanitizeAttributes(currentNode);
|
|
3933
|
+
}
|
|
3934
|
+
|
|
3935
|
+
/* If we sanitized `dirty` in-place, return it. */
|
|
3936
|
+
if (IN_PLACE) {
|
|
3937
|
+
return dirty;
|
|
3938
|
+
}
|
|
3939
|
+
|
|
3940
|
+
/* Return sanitized string or DOM */
|
|
3941
|
+
if (RETURN_DOM) {
|
|
3942
|
+
if (RETURN_DOM_FRAGMENT) {
|
|
3943
|
+
returnNode = createDocumentFragment.call(body.ownerDocument);
|
|
3944
|
+
while (body.firstChild) {
|
|
3945
|
+
// eslint-disable-next-line unicorn/prefer-dom-node-append
|
|
3946
|
+
returnNode.appendChild(body.firstChild);
|
|
3947
|
+
}
|
|
3948
|
+
} else {
|
|
3949
|
+
returnNode = body;
|
|
3950
|
+
}
|
|
3951
|
+
if (ALLOWED_ATTR.shadowroot || ALLOWED_ATTR.shadowrootmode) {
|
|
3952
|
+
/*
|
|
3953
|
+
AdoptNode() is not used because internal state is not reset
|
|
3954
|
+
(e.g. the past names map of a HTMLFormElement), this is safe
|
|
3955
|
+
in theory but we would rather not risk another attack vector.
|
|
3956
|
+
The state that is cloned by importNode() is explicitly defined
|
|
3957
|
+
by the specs.
|
|
3958
|
+
*/
|
|
3959
|
+
returnNode = importNode.call(originalDocument, returnNode, true);
|
|
3960
|
+
}
|
|
3961
|
+
return returnNode;
|
|
3962
|
+
}
|
|
3963
|
+
let serializedHTML = WHOLE_DOCUMENT ? body.outerHTML : body.innerHTML;
|
|
3964
|
+
|
|
3965
|
+
/* Serialize doctype if allowed */
|
|
3966
|
+
if (WHOLE_DOCUMENT && ALLOWED_TAGS['!doctype'] && body.ownerDocument && body.ownerDocument.doctype && body.ownerDocument.doctype.name && regExpTest(DOCTYPE_NAME, body.ownerDocument.doctype.name)) {
|
|
3967
|
+
serializedHTML = '<!DOCTYPE ' + body.ownerDocument.doctype.name + '>\n' + serializedHTML;
|
|
3968
|
+
}
|
|
3969
|
+
|
|
3970
|
+
/* Sanitize final string template-safe */
|
|
3971
|
+
if (SAFE_FOR_TEMPLATES) {
|
|
3972
|
+
arrayForEach([MUSTACHE_EXPR, ERB_EXPR, TMPLIT_EXPR], expr => {
|
|
3973
|
+
serializedHTML = stringReplace(serializedHTML, expr, ' ');
|
|
3974
|
+
});
|
|
3975
|
+
}
|
|
3976
|
+
return trustedTypesPolicy && RETURN_TRUSTED_TYPE ? trustedTypesPolicy.createHTML(serializedHTML) : serializedHTML;
|
|
3977
|
+
};
|
|
3978
|
+
|
|
3979
|
+
/**
|
|
3980
|
+
* Public method to set the configuration once
|
|
3981
|
+
* setConfig
|
|
3982
|
+
*
|
|
3983
|
+
* @param {Object} cfg configuration object
|
|
3984
|
+
*/
|
|
3985
|
+
DOMPurify.setConfig = function () {
|
|
3986
|
+
let cfg = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
3987
|
+
_parseConfig(cfg);
|
|
3988
|
+
SET_CONFIG = true;
|
|
3989
|
+
};
|
|
3990
|
+
|
|
3991
|
+
/**
|
|
3992
|
+
* Public method to remove the configuration
|
|
3993
|
+
* clearConfig
|
|
3994
|
+
*
|
|
3995
|
+
*/
|
|
3996
|
+
DOMPurify.clearConfig = function () {
|
|
3997
|
+
CONFIG = null;
|
|
3998
|
+
SET_CONFIG = false;
|
|
3999
|
+
};
|
|
4000
|
+
|
|
4001
|
+
/**
|
|
4002
|
+
* Public method to check if an attribute value is valid.
|
|
4003
|
+
* Uses last set config, if any. Otherwise, uses config defaults.
|
|
4004
|
+
* isValidAttribute
|
|
4005
|
+
*
|
|
4006
|
+
* @param {String} tag Tag name of containing element.
|
|
4007
|
+
* @param {String} attr Attribute name.
|
|
4008
|
+
* @param {String} value Attribute value.
|
|
4009
|
+
* @return {Boolean} Returns true if `value` is valid. Otherwise, returns false.
|
|
4010
|
+
*/
|
|
4011
|
+
DOMPurify.isValidAttribute = function (tag, attr, value) {
|
|
4012
|
+
/* Initialize shared config vars if necessary. */
|
|
4013
|
+
if (!CONFIG) {
|
|
4014
|
+
_parseConfig({});
|
|
4015
|
+
}
|
|
4016
|
+
const lcTag = transformCaseFunc(tag);
|
|
4017
|
+
const lcName = transformCaseFunc(attr);
|
|
4018
|
+
return _isValidAttribute(lcTag, lcName, value);
|
|
4019
|
+
};
|
|
4020
|
+
|
|
4021
|
+
/**
|
|
4022
|
+
* AddHook
|
|
4023
|
+
* Public method to add DOMPurify hooks
|
|
4024
|
+
*
|
|
4025
|
+
* @param {String} entryPoint entry point for the hook to add
|
|
4026
|
+
* @param {Function} hookFunction function to execute
|
|
4027
|
+
*/
|
|
4028
|
+
DOMPurify.addHook = function (entryPoint, hookFunction) {
|
|
4029
|
+
if (typeof hookFunction !== 'function') {
|
|
4030
|
+
return;
|
|
4031
|
+
}
|
|
4032
|
+
hooks[entryPoint] = hooks[entryPoint] || [];
|
|
4033
|
+
arrayPush(hooks[entryPoint], hookFunction);
|
|
4034
|
+
};
|
|
4035
|
+
|
|
4036
|
+
/**
|
|
4037
|
+
* RemoveHook
|
|
4038
|
+
* Public method to remove a DOMPurify hook at a given entryPoint
|
|
4039
|
+
* (pops it from the stack of hooks if more are present)
|
|
4040
|
+
*
|
|
4041
|
+
* @param {String} entryPoint entry point for the hook to remove
|
|
4042
|
+
* @return {Function} removed(popped) hook
|
|
4043
|
+
*/
|
|
4044
|
+
DOMPurify.removeHook = function (entryPoint) {
|
|
4045
|
+
if (hooks[entryPoint]) {
|
|
4046
|
+
return arrayPop(hooks[entryPoint]);
|
|
4047
|
+
}
|
|
4048
|
+
};
|
|
4049
|
+
|
|
4050
|
+
/**
|
|
4051
|
+
* RemoveHooks
|
|
4052
|
+
* Public method to remove all DOMPurify hooks at a given entryPoint
|
|
4053
|
+
*
|
|
4054
|
+
* @param {String} entryPoint entry point for the hooks to remove
|
|
4055
|
+
*/
|
|
4056
|
+
DOMPurify.removeHooks = function (entryPoint) {
|
|
4057
|
+
if (hooks[entryPoint]) {
|
|
4058
|
+
hooks[entryPoint] = [];
|
|
4059
|
+
}
|
|
4060
|
+
};
|
|
4061
|
+
|
|
4062
|
+
/**
|
|
4063
|
+
* RemoveAllHooks
|
|
4064
|
+
* Public method to remove all DOMPurify hooks
|
|
4065
|
+
*/
|
|
4066
|
+
DOMPurify.removeAllHooks = function () {
|
|
4067
|
+
hooks = {};
|
|
4068
|
+
};
|
|
4069
|
+
return DOMPurify;
|
|
4070
|
+
}
|
|
4071
|
+
var purify = createDOMPurify();
|
|
4072
|
+
|
|
4073
|
+
return purify;
|
|
4074
|
+
|
|
4075
|
+
}));
|
|
4076
|
+
|
|
4077
|
+
}(purify));
|
|
4078
|
+
|
|
4079
|
+
const DOMPurify = purify.exports;
|
|
4080
|
+
|
|
4081
|
+
const bielMessageCss = ".biel-message{display:flex;align-items:flex-start;max-width:calc(100% - var(--biel-message-avatar-width));width:fit-content;padding:var(--biel-message-padding);margin:5px 0;border-radius:var(--biel-message-border-radius)}.biel-message--user{color:var(--biel-message-user-text-color);background-color:var(--biel-message-user-background-color)}.biel-message--ai{color:var(--biel-message-biel-text-color);background-color:var(--biel-message-biel-background-color)}.biel-message__content *{margin:0}.biel-message__avatar{width:var(--biel-message-avatar-width);height:var(--biel-message-avatar-height);border-radius:50%;margin-right:var(--biel-message-avatar-margin-right)}.biel-message__content{max-width:calc(100% - var(--biel-message-avatar-width) - var(--biel-message-avatar-margin-right));display:flex;flex-direction:column;word-wrap:break-word;overflow-wrap:break-word}";
|
|
4082
|
+
const BielMessageStyle0 = bielMessageCss;
|
|
4083
|
+
|
|
4084
|
+
const BielMessage = /*@__PURE__*/ proxyCustomElement(class BielMessage extends H {
|
|
4085
|
+
constructor() {
|
|
4086
|
+
super();
|
|
4087
|
+
this.__registerHost();
|
|
4088
|
+
this.__attachShadow();
|
|
4089
|
+
this.sender = undefined;
|
|
4090
|
+
this.text = undefined;
|
|
4091
|
+
this.content = [];
|
|
4092
|
+
}
|
|
4093
|
+
componentWillLoad() {
|
|
4094
|
+
this.processMarkdown(this.text);
|
|
4095
|
+
}
|
|
4096
|
+
processMarkdown(text) {
|
|
4097
|
+
const html = DOMPurify.sanitize(marked_umd.exports.marked(text, { breaks: true }));
|
|
4098
|
+
const tempDiv = document.createElement('div');
|
|
4099
|
+
tempDiv.innerHTML = html;
|
|
4100
|
+
const elements = Array.from(tempDiv.children);
|
|
4101
|
+
const processedContent = elements.map((element) => {
|
|
4102
|
+
if (element.tagName.toLowerCase() === 'pre' && element.querySelector('code')) {
|
|
4103
|
+
return this.processCodeSnippet(element);
|
|
4104
|
+
}
|
|
4105
|
+
else if (element.tagName.toLowerCase() === 'table') {
|
|
4106
|
+
return this.processTable(element);
|
|
4107
|
+
}
|
|
4108
|
+
else {
|
|
4109
|
+
return h("div", { innerHTML: element.outerHTML });
|
|
4110
|
+
}
|
|
4111
|
+
});
|
|
4112
|
+
this.content = processedContent;
|
|
4113
|
+
}
|
|
4114
|
+
processCodeSnippet(element) {
|
|
4115
|
+
const codeElement = element.querySelector('code');
|
|
4116
|
+
const code = codeElement.textContent || '';
|
|
4117
|
+
return h("code-snippet", { code: code });
|
|
4118
|
+
}
|
|
4119
|
+
processTable(element) {
|
|
4120
|
+
return h("biel-table", { tableData: element.outerHTML });
|
|
4121
|
+
}
|
|
4122
|
+
render() {
|
|
4123
|
+
return (h("div", { key: 'a138a41a0fc16eedcd03a7ccb0bc1ae727c51265', class: `biel-message biel-message--${this.sender}` }, h("div", { key: '6217f8e3d006fdef85e9a195d51d87d56f1b5edc', class: "biel-message__avatar" }, this.sender === 'user' && (h("svg", { key: 'dd1fd63afc8970b7988e3ec74c08c7380d48f06e', xmlns: "http://www.w3.org/2000/svg", width: "20", height: "20", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", "stroke-width": "2", "stroke-linecap": "round", "stroke-linejoin": "round", class: "feather feather-user" }, h("path", { key: 'ba8a841b1bf00e2ba70827d400c655d789904568', d: "M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2" }), h("circle", { key: '1a0731730baeb9f279781cdd57cd4945f479735f', cx: "12", cy: "7", r: "4" }))), this.sender === 'ai' && (h("svg", { key: '1d85e63bc9628efb7c674cb10c3995d0f838e252', xmlns: "http://www.w3.org/2000/svg", fill: "none", viewBox: "-0.5 -0.5 24 24", id: "Ai-Sparkles--Streamline-Plump.svg", height: "20", width: "20" }, h("desc", { key: 'ad3de24e63cfb4e4a51b3f7685427ca03cdb2891' }, "Ai Sparkles Streamline Icon: https://streamlinehq.com"), h("g", { key: '50d8b97c30c439ed484def4d1de0e3cf945a7849', id: "ai-sparkles--artificial-intelligence-ai-sparks-sparkles" }, h("path", { key: '05d549b519b7970993518161e4a38a5e4c3827a9', id: "Union", fill: "currentColor", "fill-rule": "evenodd", d: "M3.9802362500000004 0.9029320833333333c0.4543841666666667 -0.8308098333333334 1.6476241666666667 -0.8308069583333333 2.102017916666667 0l0.7849229166666667 1.4352191666666667c0.19789583333333333 0.36179479166666667 0.49536250000000004 0.6592614583333334 0.8571333333333333 0.8571381250000001l1.4352 0.784946875c0.8308270833333333 0.4543841666666667 0.8308270833333333 1.6476241666666667 0 2.102017916666667l-1.4352 0.7849229166666667c-0.3617708333333334 0.19789583333333333 -0.6592375 0.49536250000000004 -0.8571333333333333 0.8571333333333333l-0.7849229166666667 1.4352c-0.45439375000000004 0.8308270833333333 -1.6476385416666668 0.8308270833333333 -2.102022708333333 0l-0.7849420833333334 -1.4352c-0.19787666666666667 -0.3617708333333334 -0.49534333333333336 -0.6592375 -0.8571381250000001 -0.8571333333333333l-1.4352191666666667 -0.7849229166666667c-0.8308098333333334 -0.45439375000000004 -0.8308069583333333 -1.6476385416666668 0 -2.102022708333333l1.4352191666666667 -0.7849420833333334c0.36179479166666667 -0.19787666666666667 0.6592614583333334 -0.49534333333333336 0.8571381250000001 -0.8571429166666668l0.784946875 -1.435214375ZM12.774727083333334 5.822689583333333c0.48467708333333337 -0.8861708333333334 1.7574875 -0.8861708333333334 2.242164583333333 0l1.6161333333333332 2.9549729166666667c0.5496520833333334 1.0050041666666667 1.3759270833333335 1.8313270833333333 2.3809312499999997 2.380979166666667l2.9549729166666667 1.6160854166666667c0.8861708333333334 0.48467708333333337 0.8861708333333334 1.7574875 0 2.242164583333333l-2.9549729166666667 1.6161333333333332c-1.0050041666666667 0.5496520833333334 -1.8312791666666668 1.3759270833333335 -2.3809312499999997 2.3809312499999997l-1.6161333333333332 2.9549729166666667c-0.48467708333333337 0.8861708333333334 -1.7574875 0.8861708333333334 -2.242164583333333 0l-1.6160854166666667 -2.9549729166666667c-0.5496520833333334 -1.0050041666666667 -1.375975 -1.8312791666666668 -2.380979166666667 -2.3809312499999997l-2.9549729166666667 -1.6161333333333332c-0.8861708333333334 -0.48467708333333337 -0.8861708333333334 -1.7574875 0 -2.242164583333333l2.9549729166666667 -1.6160854166666667c1.0050041666666667 -0.5496520833333334 1.8313270833333333 -1.375975 2.380979166666667 -2.380979166666667l1.6160854166666667 -2.9549729166666667Z", "clip-rule": "evenodd", "stroke-width": "1" }))))), h("div", { key: 'c924e48e19fd2a3e3a614fb3e27631cdbc00d050', class: "biel-message__content" }, this.content.map((el) => el))));
|
|
4124
|
+
}
|
|
4125
|
+
static get style() { return BielMessageStyle0; }
|
|
4126
|
+
}, [1, "biel-message", {
|
|
4127
|
+
"sender": [1],
|
|
4128
|
+
"text": [1],
|
|
4129
|
+
"content": [32]
|
|
4130
|
+
}]);
|
|
4131
|
+
function defineCustomElement() {
|
|
4132
|
+
if (typeof customElements === "undefined") {
|
|
4133
|
+
return;
|
|
4134
|
+
}
|
|
4135
|
+
const components = ["biel-message", "biel-table", "code-snippet"];
|
|
4136
|
+
components.forEach(tagName => { switch (tagName) {
|
|
4137
|
+
case "biel-message":
|
|
4138
|
+
if (!customElements.get(tagName)) {
|
|
4139
|
+
customElements.define(tagName, BielMessage);
|
|
4140
|
+
}
|
|
4141
|
+
break;
|
|
4142
|
+
case "biel-table":
|
|
4143
|
+
if (!customElements.get(tagName)) {
|
|
4144
|
+
defineCustomElement$2();
|
|
4145
|
+
}
|
|
4146
|
+
break;
|
|
4147
|
+
case "code-snippet":
|
|
4148
|
+
if (!customElements.get(tagName)) {
|
|
4149
|
+
defineCustomElement$1();
|
|
4150
|
+
}
|
|
4151
|
+
break;
|
|
4152
|
+
} });
|
|
4153
|
+
}
|
|
4154
|
+
defineCustomElement();
|
|
4155
|
+
|
|
4156
|
+
export { BielMessage as B, defineCustomElement as d };
|
|
4157
|
+
|
|
4158
|
+
//# sourceMappingURL=p-8e08748d.js.map
|