-
Notifications
You must be signed in to change notification settings - Fork 1
/
markup.js
49 lines (40 loc) · 1.01 KB
/
markup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"use strict";
function Markup(parts) {
if (!Array.isArray(parts) || !("raw" in parts)) {
throw new TypeError("Markup should be written as a template string tag, as in Markup`<br>`; use Markup.unsafe() to create an instance from an arbitrary string.");
}
if (parts.length !== 1) {
throw new TypeError("Template literal used with Markup should not have ${…} substitutions");
}
return Markup.unsafe(parts[0]);
}
const unsafe = html => {
if (typeof html !== "string") {
throw new TypeError("HTML passed to Markup.unsafe must be a string");
}
return Object.create(Markup.prototype, {
_html: {
configurable: true,
value: html,
},
});
};
const unwrap = markup => {
if (!(markup instanceof Markup)) {
throw new TypeError("Unescaped content must be an instance of Markup");
}
return markup._html;
};
Object.defineProperties(Markup, {
unsafe: {
configurable: true,
writable: true,
value: unsafe,
},
unwrap: {
configurable: true,
writable: true,
value: unwrap,
},
});
module.exports = Markup;