Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cpp/parser/GumboNormalizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -638,8 +638,12 @@ static void flatten_li_node(GumboNode *node, buffer_t *ib, buffer_t *out,
li_ctx_t *ctx);

static void flush_li_buffer(buffer_t *ib, buffer_t *out, li_ctx_t *ctx) {
if (ib->len == 0)
/* Whitespace around blocks inside an <li> (e.g. `<li>\n<p>a</p>\n</li>`)
* is layout noise, not an extra list item. */
if (is_whitespace_only(ib->data, ib->len)) {
buffer_clear(ib);
return;
}
buffer_append_str(out, "<li");
emit_attributes(ctx->el, "li", out);
buffer_append_str(out, ">");
Expand Down
7 changes: 7 additions & 0 deletions cpp/tests/GumboParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,13 @@ TEST(GumboParserTest, InterBlockWhitespace) {
EXPECT_EQ(GumboParser::normalizeHtml("<p>Asdasd</p> <p>Asdasd</p>"),
"<p>Asdasd</p><p>Asdasd</p>");

// Whitespace around blocks inside a list item must not add empty items.
EXPECT_EQ(GumboParser::normalizeHtml(
"<ul><li>\n<p>a</p>\n</li><li>\n<p>b</p>\n</li></ul>"),
"<ul><li>a</li><li>b</li></ul>");
EXPECT_EQ(GumboParser::normalizeHtml("<ol><li><p>a</p> <p>b</p></li></ol>"),
"<ol><li>a</li><li>b</li></ol>");

// Significant inline content between blocks is still wrapped in <p>.
EXPECT_EQ(GumboParser::normalizeHtml("<p>a</p> hello <p>b</p>"),
"<p>a</p><p> hello </p><p>b</p>");
Expand Down
6 changes: 6 additions & 0 deletions src/web/__tests__/htmlNormalizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,12 @@ describe('htmlNormalizer', () => {
'<p>Asdasd</p><p>Asdasd</p><p>Asdasda</p>',
],
['<p>Asdasd</p> <p>Asdasd</p>', '<p>Asdasd</p><p>Asdasd</p>'],
// Whitespace around blocks inside a list item must not add empty items.
[
'<ul><li>\n<p>a</p>\n</li><li>\n<p>b</p>\n</li></ul>',
'<ul><li>a</li><li>b</li></ul>',
],
['<ol><li><p>a</p> <p>b</p></li></ol>', '<ol><li>a</li><li>b</li></ol>'],
// Significant inline content between blocks is still wrapped in <p>.
['<p>a</p> hello <p>b</p>', '<p>a</p><p> hello </p><p>b</p>'],
// Spaces inside text / between inlines must be preserved.
Expand Down
7 changes: 6 additions & 1 deletion src/web/normalization/htmlNormalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,12 @@ function flushLiBuffer(
out: { buf: string },
ctx: LiCtx
): void {
if (ib.buf.length === 0) return;
// Whitespace around blocks inside an <li> (e.g. `<li>\n<p>a</p>\n</li>`)
// is layout noise, not an extra list item.
if (isWhitespaceOnly(ib.buf)) {
ib.buf = '';
return;
}
out.buf += `<li${emitAttributes(ctx.el, 'li')}>`;
out.buf += emitStylesOpen(ctx.styles);
out.buf += ib.buf;
Expand Down