|
1 | 1 | import "regenerator-runtime/runtime"; // needed for ``await`` support |
2 | 2 | import Base from "@patternslib/patternslib/src/core/base"; |
3 | 3 | import Parser from "@patternslib/patternslib/src/core/parser"; |
| 4 | +import utils from "@patternslib/patternslib/src/core/utils"; |
4 | 5 |
|
5 | 6 | export const parser = new Parser("code-editor"); |
6 | 7 | parser.addArgument("language", null); // programming language to use. |
@@ -35,9 +36,17 @@ export default Base.extend({ |
35 | 36 |
|
36 | 37 | let el = this.el; |
37 | 38 | if (["textarea", "input"].includes(this.el.nodeName.toLowerCase())) { |
38 | | - el = document.createElement("pre"); |
39 | | - el.innerHTML = `<code contenteditable>${this.el.value}</code>`; |
40 | | - this.el.parentNode.insertBefore(el, this.el); |
| 39 | + const unescaped_html = utils.unescape_html(this.el.value); |
| 40 | + const language_class = this.options.language |
| 41 | + ? `language-${this.options.language}` |
| 42 | + : ""; |
| 43 | + const pre_el = document.createElement("pre"); |
| 44 | + el = document.createElement("code"); |
| 45 | + el.setAttribute("class", language_class); |
| 46 | + el.setAttribute("contenteditable", ""); |
| 47 | + el.textContent = unescaped_html; |
| 48 | + pre_el.append(el); |
| 49 | + this.el.parentNode.insertBefore(pre_el, this.el); |
41 | 50 | this.el.setAttribute("hidden", ""); |
42 | 51 | } |
43 | 52 |
|
|
0 commit comments