From c9598e7997fed0133aa43a172fbd93aef50cad07 Mon Sep 17 00:00:00 2001
From: Dmitriy Pleshevskiy <dmitriy@ideascup.me>
Date: Sun, 12 Jun 2022 14:18:45 +0300
Subject: [PATCH] ren(html): fix encoding soft break

---
 ren/html_str.test.ts | 30 ++++++++++++++++++++----------
 ren/html_str.ts      | 10 ++++++++--
 2 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/ren/html_str.test.ts b/ren/html_str.test.ts
index 823dcb1..5156ad5 100644
--- a/ren/html_str.test.ts
+++ b/ren/html_str.test.ts
@@ -19,24 +19,34 @@ Deno.test({
 Deno.test({
   name: "should render element",
   fn: () => {
-    const el = E("p", [], "hello world");
-
     const ren = new HtmlStrRenderer();
-    const res = ren.render(el);
 
-    assertEquals(res, "<p>hello world</p>");
+    assertEquals(ren.render(E("p", [])), "<p></p>");
+    assertEquals(ren.render(E("p", [], "hello world")), "<p>hello world</p>");
+    assertEquals(
+      ren.render(E("p", [], ["hello", "world"])),
+      "<p>hello world</p>",
+    );
+    assertEquals(
+      ren.render(E("p", [], [E("span", [], "hello"), E("span", [], "world")])),
+      "<p><span>hello</span><span>world</span></p>",
+    );
+    assertEquals(
+      ren.render(E("p", [], ["hello", E("span", [], "world")])),
+      "<p>hello <span>world</span></p>",
+    );
+    assertEquals(
+      ren.render(E("p", [], [E("span", [], "hello"), "world"])),
+      "<p><span>hello</span> world</p>",
+    );
   },
 });
 
 Deno.test({
   name: "should render empty fragment as empty string",
   fn: () => {
-    const frag = F([]);
-
     const ren = new HtmlStrRenderer();
-    const res = ren.render(frag);
-
-    assertEquals(res, "");
+    assertEquals(ren.render(F([])), "");
   },
 });
 
@@ -52,7 +62,7 @@ Deno.test({
     const ren = new HtmlStrRenderer();
     const res = ren.render(frag);
 
-    assertEquals(res, 'hello world<div class="hello"></div><p>world</p>');
+    assertEquals(res, 'hello world <div class="hello"></div><p>world</p>');
   },
 });
 
diff --git a/ren/html_str.ts b/ren/html_str.ts
index 5aa279c..acb261a 100644
--- a/ren/html_str.ts
+++ b/ren/html_str.ts
@@ -79,7 +79,9 @@ function encodeHtmlFragment(
   node: Fragment,
   hooks: HtmlStrRendererHooks,
 ): string {
-  return concat(node.children.map((ch) => encodeAnyNode(ch, hooks)));
+  return concatEncodedNodes(
+    node.children.map((ch) => encodeAnyNode(ch, hooks)),
+  );
 }
 
 function encodeHtmlElement(
@@ -90,7 +92,11 @@ function encodeHtmlElement(
   if (isSelfClosedTagName(tagName)) return open;
 
   const encodedChildren = children.map((ch) => encodeAnyNode(ch, hooks));
-  return `${open}${concat(encodedChildren)}</${tagName}>`;
+  return `${open}${concatEncodedNodes(encodedChildren)}</${tagName}>`;
+}
+
+function concatEncodedNodes(encodedChildren: string[]): string {
+  return join(" ", encodedChildren).replace(/>\s+?</g, "><");
 }
 
 function encodeAttrs(