ren(html): fix encoding soft break

This commit is contained in:
Dmitriy Pleshevskiy 2022-06-12 14:18:45 +03:00
parent 53c4f4039c
commit c9598e7997
Signed by: pleshevskiy
GPG Key ID: 1B59187B161C0215
2 changed files with 28 additions and 12 deletions

View File

@ -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>');
},
});

View File

@ -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(