From 0a65652b161677e39205b0e6f7b133d776332353 Mon Sep 17 00:00:00 2001 From: szydlovsky <9szydlowski9@gmail.com> Date: Mon, 30 Nov 2026 10:25:23 +0100 Subject: [PATCH] fix: make gumbo and web normalizers switch codeblock newlines to p tags --- cpp/parser/GumboNormalizer.c | 139 +++++++++++++++++++++-- cpp/tests/GumboParserTest.cpp | 26 +++++ src/web/__tests__/htmlNormalizer.test.ts | 34 ++++++ src/web/normalization/htmlNormalizer.ts | 91 ++++++++++++++- 4 files changed, 278 insertions(+), 12 deletions(-) diff --git a/cpp/parser/GumboNormalizer.c b/cpp/parser/GumboNormalizer.c index f12a7266d..ab972ea0d 100644 --- a/cpp/parser/GumboNormalizer.c +++ b/cpp/parser/GumboNormalizer.c @@ -936,6 +936,129 @@ static void walk_children(GumboNode *node, buffer_t *out, } } +/* ------------------------------------------------------------------ */ +/* Code block lines */ +/* ------------------------------------------------------------------ */ + +#define CODE_LINE_MAX_OPEN_TAGS 32 + +/** True for a `
`, `
` or `
` tag. */ +static bool is_br_tag(const char *tag, size_t tag_len) { + return tag_len >= 4 && strncmp(tag, "' || tag[3] == '/' || tag[3] == ' '); +} + +/** True if normalized inline HTML has a line break: a newline in text or a + *
. Newlines inside tags (attribute values) don't count. */ +static bool has_code_line_break(const char *html, size_t len) { + size_t i = 0; + while (i < len) { + if (html[i] == '<') { + const char *end = memchr(html + i, '>', len - i); + if (!end) + return false; + size_t tag_len = (size_t)(end - (html + i)) + 1; + if (is_br_tag(html + i, tag_len)) + return true; + i += tag_len; + continue; + } + if (html[i] == '\n') + return true; + i++; + } + return false; +} + +/** Closes the open inline tags, emits the line as a

(or
when it has + * no content) and reopens the tags for the next line. */ +static void flush_code_line(buffer_t *line, buffer_t *out, const char *html, + const size_t *open_start, const size_t *open_len, + int depth, bool has_content) { + for (int d = depth - 1; d >= 0; d--) { + const char *tag = html + open_start[d]; + size_t name_len = 1; + while (name_len < open_len[d] && tag[name_len] != ' ' && + tag[name_len] != '>') + name_len++; + buffer_append_str(line, ""); + } + if (has_content) { + buffer_append_str(out, "

"); + buffer_append(out, line->data, line->len); + buffer_append_str(out, "

"); + } else { + buffer_append_str(out, "
"); + } + buffer_clear(line); + for (int d = 0; d < depth; d++) + buffer_append(line, html + open_start[d], open_len[d]); +} + +/** + * Emits normalized inline HTML of a code block (from
 or ) as
+ * one 

per line, since line breaks in code are significant. A line break is + * a newline in text or a
. Inline tags open across a break are closed + * before it and reopened after it. An empty line becomes
, and a trailing + * empty line (`a\n

`) is dropped, as browsers don't render it either. + */ +static void emit_code_lines(const char *html, size_t len, buffer_t *out) { + if (!has_code_line_break(html, len)) { + buffer_append_str(out, "

"); + buffer_append(out, html, len); + buffer_append_str(out, "

"); + return; + } + + size_t open_start[CODE_LINE_MAX_OPEN_TAGS]; + size_t open_len[CODE_LINE_MAX_OPEN_TAGS]; + int depth = 0; + buffer_t line = buffer_create(64); + bool has_content = false; + + size_t i = 0; + while (i < len) { + if (html[i] == '<') { + const char *end = memchr(html + i, '>', len - i); + size_t tag_len = end ? (size_t)(end - (html + i)) + 1 : len - i; + const char *tag = html + i; + if (is_br_tag(tag, tag_len)) { + flush_code_line(&line, out, html, open_start, open_len, depth, + has_content); + has_content = false; + } else { + buffer_append(&line, tag, tag_len); + if (tag_len > 1 && tag[1] == '/') { + if (depth > 0) + depth--; + } else if (tag_len > 1 && tag[tag_len - 2] == '/') { + has_content = true; /* self-closing, e.g. */ + } else if (depth < CODE_LINE_MAX_OPEN_TAGS) { + open_start[depth] = i; + open_len[depth] = tag_len; + depth++; + } + } + i += tag_len; + continue; + } + if (html[i] == '\n') { + flush_code_line(&line, out, html, open_start, open_len, depth, + has_content); + has_content = false; + } else { + buffer_append(&line, &html[i], 1); + has_content = true; + } + i++; + } + if (has_content) + flush_code_line(&line, out, html, open_start, open_len, depth, true); + free(line.data); +} + /* ------------------------------------------------------------------ */ /* walk_node — process a single DOM node */ /* ------------------------------------------------------------------ */ @@ -1177,15 +1300,17 @@ static void walk_node(GumboNode *node, buffer_t *out, break; } - /* : wrap inline content in

*/ + /* : wrap inline content in a

per line */ if (strcmp(out_name, "codeblock") == 0) { - bool wrap = is_purely_inline(node); buffer_append_str(out, ""); - if (wrap) - buffer_append_str(out, "

"); - walk_children(node, out, inherited); - if (wrap) - buffer_append_str(out, "

"); + if (is_purely_inline(node)) { + buffer_t code = buffer_create(64); + walk_children(node, &code, inherited); + emit_code_lines(code.data, code.len, out); + free(code.data); + } else { + walk_children(node, out, inherited); + } buffer_append_str(out, "
"); break; } diff --git a/cpp/tests/GumboParserTest.cpp b/cpp/tests/GumboParserTest.cpp index 5075f1185..53819020a 100644 --- a/cpp/tests/GumboParserTest.cpp +++ b/cpp/tests/GumboParserTest.cpp @@ -353,6 +353,32 @@ TEST(GumboParserTest, SpanRemappings) { "x"); } +TEST(GumboParserTest, CodeBlockLines) { + // Each line of code becomes its own

+ EXPECT_EQ(GumboParser::normalizeHtml("

a\nb
"), + "

a

b

"); + EXPECT_EQ(GumboParser::normalizeHtml("a\nb"), + "

a

b

"); + EXPECT_EQ(GumboParser::normalizeHtml("
a
b
"), + "

a

b

"); + + // Indentation is kept + EXPECT_EQ(GumboParser::normalizeHtml("
if (x) {\n  y();\n}
"), + "

if (x) {

y();

}

"); + + // Empty lines become
, the first newline after
 and a trailing one
+  // are dropped
+  EXPECT_EQ(GumboParser::normalizeHtml("
\na\n\nb\n
"), + "

a


b

"); + + // Inline tags spanning a line break are closed and reopened + EXPECT_EQ(GumboParser::normalizeHtml("
a\nbc
"), + "

a

bc

"); + EXPECT_EQ( + GumboParser::normalizeHtml("
a\nb
"), + "

a

b

"); +} + TEST(GumboParserTest, EnrichedTagRemappings) { // Block elements EXPECT_EQ(GumboParser::normalizeHtml("x"), diff --git a/src/web/__tests__/htmlNormalizer.test.ts b/src/web/__tests__/htmlNormalizer.test.ts index 5ea7924a8..1f99f32ea 100644 --- a/src/web/__tests__/htmlNormalizer.test.ts +++ b/src/web/__tests__/htmlNormalizer.test.ts @@ -233,6 +233,40 @@ describe('htmlNormalizer', () => { }); }); + describe('CodeBlockLines', () => { + test.each([ + // Each line of code becomes its own

+ ['

a\nb
', '

a

b

'], + [ + 'a\nb', + '

a

b

', + ], + ['
a
b
', '

a

b

'], + + // Indentation is kept + [ + '
if (x) {\n  y();\n}
', + '

if (x) {

y();

}

', + ], + + // Empty lines become
, the first newline after
 and a trailing
+      // one are dropped
+      ['
\na\n\nb\n
', '

a


b

'], + + // Inline tags spanning a line break are closed and reopened + [ + '
a\nbc
', + '

a

bc

', + ], + [ + '
a\nb
', + '

a

b

', + ], + ])('%j → %s', (input, expected) => { + expect(normalizeHtml(input)).toBe(expected); + }); + }); + describe('EnrichedTagRemappings', () => { test.each([ // Block elements diff --git a/src/web/normalization/htmlNormalizer.ts b/src/web/normalization/htmlNormalizer.ts index 670995cf8..802a13e94 100644 --- a/src/web/normalization/htmlNormalizer.ts +++ b/src/web/normalization/htmlNormalizer.ts @@ -658,6 +658,84 @@ function walkChildren( } } +/** `
`, `
` or `
`. */ +const BR_TAG_RE = /^]/; + +/** True if normalized inline HTML has a line break: a newline in text or a + *
. Newlines inside tags (attribute values) don't count. */ +function hasCodeLineBreak(html: string): boolean { + let i = 0; + while (i < html.length) { + if (html[i] === '<') { + const end = html.indexOf('>', i); + if (end === -1) return false; + if (BR_TAG_RE.test(html.slice(i, end + 1))) return true; + i = end + 1; + continue; + } + if (html[i] === '\n') return true; + i++; + } + return false; +} + +/** + * Emits normalized inline HTML of a code block (from
 or ) as
+ * one 

per line, since line breaks in code are significant. A line break is + * a newline in text or a
. Inline tags open across a break are closed + * before it and reopened after it. An empty line becomes
, and a trailing + * empty line (`a\n

`) is dropped, as browsers don't render it either. + */ +function emitCodeLines(html: string): string { + if (!hasCodeLineBreak(html)) return `

${html}

`; + + let out = ''; + const openTags: string[] = []; + let line = ''; + let hasContent = false; + + const flushLine = () => { + for (let d = openTags.length - 1; d >= 0; d--) { + const name = openTags[d]!.slice(1).split(/[\s>]/, 1)[0]; + line += ``; + } + out += hasContent ? `

${line}

` : '
'; + line = openTags.join(''); + hasContent = false; + }; + + let i = 0; + while (i < html.length) { + if (html[i] === '<') { + const end = html.indexOf('>', i); + const tag = end === -1 ? html.slice(i) : html.slice(i, end + 1); + if (BR_TAG_RE.test(tag)) { + flushLine(); + } else { + line += tag; + if (tag.startsWith('')) { + hasContent = true; // self-closing, e.g. + } else { + openTags.push(tag); + } + } + i += tag.length; + continue; + } + if (html[i] === '\n') { + flushLine(); + } else { + line += html[i]; + hasContent = true; + } + i++; + } + if (hasContent) flushLine(); + return out; +} + function walkNode( node: Node, out: { buf: string }, @@ -805,13 +883,16 @@ function walkNode( return; } - // : wrap inline content in

+ // : wrap inline content in a

per line if (outName === 'codeblock') { - const wrap = isPurelyInline(node); out.buf += ''; - if (wrap) out.buf += '

'; - walkChildren(node, out, inherited); - if (wrap) out.buf += '

'; + if (isPurelyInline(node)) { + const code = { buf: '' }; + walkChildren(node, code, inherited); + out.buf += emitCodeLines(code.buf); + } else { + walkChildren(node, out, inherited); + } out.buf += '
'; return; }