From eacc0339294765d87f3ffcc2ec881e1a7228dba3 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 16 Jul 2021 11:32:37 -0700 Subject: [PATCH] Make code blocks accept non-code tags. --- .../session/room/timeline/deserialize.js | 22 ++++++++++++++++--- src/platform/web/parsehtml.js | 2 +- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/domain/session/room/timeline/deserialize.js b/src/domain/session/room/timeline/deserialize.js index d477853d..d987332b 100644 --- a/src/domain/session/room/timeline/deserialize.js +++ b/src/domain/session/room/timeline/deserialize.js @@ -67,10 +67,10 @@ class Deserializer { codeNode = child; break; } + let language = null; if (!this._ensureElement(codeNode, "CODE")) { - return null; + return new CodeBlock(language, this.result.getNodeText(node)); } - let language = ""; const cl = result.getAttributeValue(codeNode, "class") || "" for (const clname of cl.split(" ")) { if (clname.startsWith("language-") && !clname.startsWith("language-_")) { @@ -78,7 +78,7 @@ class Deserializer { break; } } - return new CodeBlock(language, codeNode.textContent); + return new CodeBlock(language, this.result.getNodeText(codeNode)); } parseImage(node) { @@ -443,6 +443,22 @@ export function tests() { ]; test(assert, input, output); }, + "Text with code block but no tag": assert => { + const code = 'main :: IO ()\nmain = putStrLn "Hello"' + const input = `
${code}
`; + const output = [ + new CodeBlock(null, code) + ]; + test(assert, input, output); + }, + "Text with code block and 'unsupported' tag": assert => { + const code = 'Hello, world' + const input = `
${code}
`; + const output = [ + new CodeBlock(null, code) + ]; + test(assert, input, output); + } /* Doesnt work: HTML library doesn't handle
 properly.
         "Text with code block": assert => {
             const code = 'main :: IO ()\nmain = putStrLn "Hello"'
diff --git a/src/platform/web/parsehtml.js b/src/platform/web/parsehtml.js
index 0efd7d4f..e2017928 100644
--- a/src/platform/web/parsehtml.js
+++ b/src/platform/web/parsehtml.js
@@ -26,7 +26,7 @@ class HTMLParseResult {
     }
 
     getNodeText(node) {
-        return node.nodeValue;
+        return node.textContent;
     }
 
     isElementNode(node) {