From 324447327a8f28ad8e382d8e26662e50e8000282 Mon Sep 17 00:00:00 2001 From: szydlovsky <9szydlowski9@gmail.com> Date: Tue, 1 Dec 2026 18:09:56 +0100 Subject: [PATCH 1/2] fix: remove nested tags limit and fix collapsing nested codeblocks --- cpp/parser/GumboNormalizer.c | 161 +++++++++++++++++------- src/web/normalization/htmlNormalizer.ts | 53 ++++++-- 2 files changed, 158 insertions(+), 56 deletions(-) diff --git a/cpp/parser/GumboNormalizer.c b/cpp/parser/GumboNormalizer.c index ab972ea0d..ad6347dc0 100644 --- a/cpp/parser/GumboNormalizer.c +++ b/cpp/parser/GumboNormalizer.c @@ -197,6 +197,12 @@ static bool is_br_node(GumboNode *node) { return n && strcmp(n, "br") == 0; } +static bool is_code_block_node(GumboNode *node) { + char buf[64]; + const char *n = get_tag_name(node, buf, sizeof(buf)); + return n && (strcmp(n, "pre") == 0 || strcmp(n, "codeblock") == 0); +} + static bool is_block_producing(GumboNode *node) { char buf[64]; const char *n = get_tag_name(node, buf, sizeof(buf)); @@ -680,6 +686,15 @@ static bool has_styled_span_descendant(GumboNode *node) { static void walk_node(GumboNode *node, buffer_t *out, const custom_style_t *inherited); +/** Receives one line of a code block, with its open inline tags closed. + * has_content is false for an empty line. */ +typedef void (*code_line_fn)(buffer_t *line, bool has_content, void *ctx); + +static bool has_code_line_break(const char *html, size_t len); +static void split_code_lines(const char *html, size_t len, + code_line_fn emit_line, void *ctx); +static void emit_code_lines(const char *html, size_t len, buffer_t *out); + /* ------------------------------------------------------------------ */ /* Blockquote content flattening */ /* ------------------------------------------------------------------ */ @@ -761,6 +776,13 @@ static void flatten_bq_node(GumboNode *node, buffer_t *ib, buffer_t *out, if (is_block_producing(node) || is_blockquote_node(node)) { flush_inline_p(ib, out, NULL); flatten_bq_children(node, ib, out, inherited); + // A code block can't be nested in a blockquote, so its lines become + // separate paragraphs. + if (is_code_block_node(node) && has_code_line_break(ib->data, ib->len)) { + emit_code_lines(ib->data, ib->len, out); + buffer_clear(ib); + return; + } // The flattened block becomes a

; carry over its text-align (if any). flush_inline_p(ib, out, &node->v.element); return; @@ -803,6 +825,19 @@ static void flush_li_buffer(buffer_t *ib, buffer_t *out, li_ctx_t *ctx) { ctx->has_emitted = true; } +typedef struct { + buffer_t *out; + li_ctx_t *li; +} li_code_line_ctx_t; + +/** Emits a code line as its own

  • . Empty lines are dropped, as with
    . */ +static void emit_li_code_line(buffer_t *line, bool has_content, void *ctx) { + if (!has_content) + return; + li_code_line_ctx_t *c = (li_code_line_ctx_t *)ctx; + flush_li_buffer(line, c->out, c->li); +} + static void flatten_li_children(GumboNode *node, buffer_t *ib, buffer_t *out, li_ctx_t *ctx) { if (!is_element(node)) @@ -850,6 +885,14 @@ static void flatten_li_node(GumboNode *node, buffer_t *ib, buffer_t *out, if (is_block_producing(node) || is_blockquote_node(node)) { flush_li_buffer(ib, out, ctx); flatten_li_children(node, ib, out, ctx); + // A code block can't be nested in a list item, so its lines become + // separate items, like lines split by
    . + if (is_code_block_node(node) && has_code_line_break(ib->data, ib->len)) { + li_code_line_ctx_t line_ctx = {out, ctx}; + split_code_lines(ib->data, ib->len, emit_li_code_line, &line_ctx); + buffer_clear(ib); + return; + } flush_li_buffer(ib, out, ctx); return; } @@ -940,8 +983,6 @@ 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, " (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]; +/** Inline tags open at the current point of a code block, as offsets into its + * HTML. Grows as needed, so any nesting depth is closed and reopened. */ +typedef struct { + size_t *start; + size_t *len; + int depth; + int cap; +} open_tags_t; + +static void open_tags_push(open_tags_t *t, size_t start, size_t len) { + if (t->depth == t->cap) { + t->cap = t->cap ? t->cap * 2 : 8; + t->start = (size_t *)realloc(t->start, (size_t)t->cap * sizeof(size_t)); + t->len = (size_t *)realloc(t->len, (size_t)t->cap * sizeof(size_t)); + } + t->start[t->depth] = start; + t->len[t->depth] = len; + t->depth++; +} + +/** Closes the open inline tags, hands the line to emit_line and reopens the + * tags for the next line. */ +static void flush_code_line(buffer_t *line, const char *html, + const open_tags_t *open, bool has_content, + code_line_fn emit_line, void *ctx) { + for (int d = open->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] != ' ' && + 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, "
    "); - } + emit_line(line, has_content, ctx); buffer_clear(line); - for (int d = 0; d < depth; d++) - buffer_append(line, html + open_start[d], open_len[d]); + for (int d = 0; d < open->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. + * Splits normalized inline HTML of a code block (from
     or )
    + * into lines, 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. 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; +static void split_code_lines(const char *html, size_t len, + code_line_fn emit_line, void *ctx) { + open_tags_t open = {NULL, NULL, 0, 0}; buffer_t line = buffer_create(64); bool has_content = false; @@ -1025,28 +1072,24 @@ static void emit_code_lines(const char *html, size_t len, buffer_t *out) { 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); + flush_code_line(&line, html, &open, has_content, emit_line, ctx); has_content = false; } else { buffer_append(&line, tag, tag_len); if (tag_len > 1 && tag[1] == '/') { - if (depth > 0) - depth--; + if (open.depth > 0) + open.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++; + } else { + open_tags_push(&open, i, tag_len); } } i += tag_len; continue; } if (html[i] == '\n') { - flush_code_line(&line, out, html, open_start, open_len, depth, - has_content); + flush_code_line(&line, html, &open, has_content, emit_line, ctx); has_content = false; } else { buffer_append(&line, &html[i], 1); @@ -1055,8 +1098,34 @@ static void emit_code_lines(const char *html, size_t len, buffer_t *out) { i++; } if (has_content) - flush_code_line(&line, out, html, open_start, open_len, depth, true); + flush_code_line(&line, html, &open, true, emit_line, ctx); free(line.data); + free(open.start); + free(open.len); +} + +/** Emits a code line as a

    , or an empty one as
    . */ +static void emit_code_line_p(buffer_t *line, bool has_content, void *ctx) { + buffer_t *out = (buffer_t *)ctx; + if (has_content) { + buffer_append_str(out, "

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

    "); + } else { + buffer_append_str(out, "
    "); + } +} + +/** Emits a code block's inline HTML as one

    per line (see + * split_code_lines). An empty line becomes
    . */ +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; + } + split_code_lines(html, len, emit_code_line_p, out); } /* ------------------------------------------------------------------ */ diff --git a/src/web/normalization/htmlNormalizer.ts b/src/web/normalization/htmlNormalizer.ts index 802a13e94..1529aa928 100644 --- a/src/web/normalization/htmlNormalizer.ts +++ b/src/web/normalization/htmlNormalizer.ts @@ -117,6 +117,11 @@ function isBrNode(node: Node | null): boolean { return tagName(node) === 'br'; } +function isCodeBlockNode(node: Node | null): boolean { + const name = tagName(node); + return name === 'pre' || name === 'codeblock'; +} + function isBlockProducing(node: Node | null): boolean { const n = tagName(node); if (!n) return false; @@ -486,6 +491,13 @@ function flattenBqNode( if (isBlockProducing(node) || isBlockquoteNode(node)) { flushInlineP(ib, out); flattenBqChildren(node, ib, out, inherited); + // A code block can't be nested in a blockquote, so its lines become + // separate paragraphs. + if (isCodeBlockNode(node) && hasCodeLineBreak(ib.buf)) { + out.buf += emitCodeLines(ib.buf); + ib.buf = ''; + return; + } // The flattened block becomes a

    ; carry over its text-align (if any). flushInlineP(ib, out, emitAlignment(node, 'p')); return; @@ -565,6 +577,15 @@ function flattenLiNode( if (isBlockProducing(node) || isBlockquoteNode(node)) { flushLiBuffer(ib, out, ctx); flattenLiChildren(node, ib, out, ctx); + // A code block can't be nested in a list item, so its lines become + // separate items, like lines split by
    . Empty lines are dropped. + if (isCodeBlockNode(node) && hasCodeLineBreak(ib.buf)) { + splitCodeLines(ib.buf, (line, hasContent) => { + if (hasContent) flushLiBuffer({ buf: line }, out, ctx); + }); + ib.buf = ''; + return; + } flushLiBuffer(ib, out, ctx); return; } @@ -680,16 +701,17 @@ function hasCodeLineBreak(html: string): boolean { } /** - * 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. + * Splits normalized inline HTML of a code block (from
     or )
    + * into lines, 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. `emitLine` gets each line, with `hasContent` false + * for an empty one. 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 = ''; +function splitCodeLines( + html: string, + emitLine: (line: string, hasContent: boolean) => void +): void { const openTags: string[] = []; let line = ''; let hasContent = false; @@ -699,7 +721,7 @@ function emitCodeLines(html: string): string { const name = openTags[d]!.slice(1).split(/[\s>]/, 1)[0]; line += ``; } - out += hasContent ? `

    ${line}

    ` : '
    '; + emitLine(line, hasContent); line = openTags.join(''); hasContent = false; }; @@ -733,6 +755,17 @@ function emitCodeLines(html: string): string { i++; } if (hasContent) flushLine(); +} + +/** Emits a code block's inline HTML as one

    per line (see splitCodeLines). + * An empty line becomes
    . */ +function emitCodeLines(html: string): string { + if (!hasCodeLineBreak(html)) return `

    ${html}

    `; + + let out = ''; + splitCodeLines(html, (line, hasContent) => { + out += hasContent ? `

    ${line}

    ` : '
    '; + }); return out; } From 9f85dc04a56cae25566d275983aacdccf9a9042c Mon Sep 17 00:00:00 2001 From: szydlovsky <9szydlowski9@gmail.com> Date: Wed, 2 Dec 2026 14:36:19 +0100 Subject: [PATCH 2/2] fix: coderabbit-found improvements --- cpp/parser/GumboNormalizer.c | 71 ++++++++++++++++++++----- src/web/normalization/htmlNormalizer.ts | 41 ++++++++++++-- 2 files changed, 93 insertions(+), 19 deletions(-) diff --git a/cpp/parser/GumboNormalizer.c b/cpp/parser/GumboNormalizer.c index ad6347dc0..1709e7a9b 100644 --- a/cpp/parser/GumboNormalizer.c +++ b/cpp/parser/GumboNormalizer.c @@ -690,10 +690,13 @@ static void walk_node(GumboNode *node, buffer_t *out, * has_content is false for an empty line. */ typedef void (*code_line_fn)(buffer_t *line, bool has_content, void *ctx); +static void walk_children(GumboNode *node, buffer_t *out, + const custom_style_t *inherited); static bool has_code_line_break(const char *html, size_t len); static void split_code_lines(const char *html, size_t len, code_line_fn emit_line, void *ctx); -static void emit_code_lines(const char *html, size_t len, buffer_t *out); +static void emit_code_lines(const char *html, size_t len, buffer_t *out, + GumboElement *align_el); /* ------------------------------------------------------------------ */ /* Blockquote content flattening */ @@ -773,13 +776,27 @@ static void flatten_bq_node(GumboNode *node, buffer_t *ib, buffer_t *out, } return; } + // A code block can't be nested in a blockquote, so its lines become separate + // paragraphs. Its inline content is collected whole first (as for a + // top-level code block), so both newlines and
    s split it. + if (is_code_block_node(node) && is_purely_inline(node)) { + flush_inline_p(ib, out, NULL); + walk_children(node, ib, inherited); + if (has_code_line_break(ib->data, ib->len)) { + emit_code_lines(ib->data, ib->len, out, &node->v.element); + buffer_clear(ib); + } else { + flush_inline_p(ib, out, &node->v.element); + } + return; + } if (is_block_producing(node) || is_blockquote_node(node)) { flush_inline_p(ib, out, NULL); flatten_bq_children(node, ib, out, inherited); // A code block can't be nested in a blockquote, so its lines become // separate paragraphs. if (is_code_block_node(node) && has_code_line_break(ib->data, ib->len)) { - emit_code_lines(ib->data, ib->len, out); + emit_code_lines(ib->data, ib->len, out, &node->v.element); buffer_clear(ib); return; } @@ -882,6 +899,22 @@ static void flatten_li_node(GumboNode *node, buffer_t *ib, buffer_t *out, flush_li_buffer(ib, out, ctx); return; } + // A code block can't be nested in a list item, so its lines become separate + // items, like lines split by
    . Its inline content is collected whole + // first (as for a top-level code block), so both newlines and
    s split + // it. + if (is_code_block_node(node) && is_purely_inline(node)) { + flush_li_buffer(ib, out, ctx); + walk_children(node, ib, ctx->inherited); + if (has_code_line_break(ib->data, ib->len)) { + li_code_line_ctx_t line_ctx = {out, ctx}; + split_code_lines(ib->data, ib->len, emit_li_code_line, &line_ctx); + buffer_clear(ib); + } else { + flush_li_buffer(ib, out, ctx); + } + return; + } if (is_block_producing(node) || is_blockquote_node(node)) { flush_li_buffer(ib, out, ctx); flatten_li_children(node, ib, out, ctx); @@ -1104,28 +1137,38 @@ static void split_code_lines(const char *html, size_t len, free(open.len); } +typedef struct { + buffer_t *out; + GumboElement *align_el; /* text-align source for each

    ; may be NULL */ +} code_p_ctx_t; + /** Emits a code line as a

    , or an empty one as
    . */ static void emit_code_line_p(buffer_t *line, bool has_content, void *ctx) { - buffer_t *out = (buffer_t *)ctx; + code_p_ctx_t *c = (code_p_ctx_t *)ctx; if (has_content) { - buffer_append_str(out, "

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

    "); + buffer_append_str(c->out, "align_el) + emit_alignment(c->align_el, "p", c->out); + buffer_append_str(c->out, ">"); + buffer_append(c->out, line->data, line->len); + buffer_append_str(c->out, "

    "); } else { - buffer_append_str(out, "
    "); + buffer_append_str(c->out, "
    "); } } /** Emits a code block's inline HTML as one

    per line (see - * split_code_lines). An empty line becomes
    . */ -static void emit_code_lines(const char *html, size_t len, buffer_t *out) { + * split_code_lines). An empty line becomes
    . Each

    gets the + * text-align of align_el, if given. */ +static void emit_code_lines(const char *html, size_t len, buffer_t *out, + GumboElement *align_el) { + code_p_ctx_t ctx = {out, align_el}; if (!has_code_line_break(html, len)) { - buffer_append_str(out, "

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

    "); + buffer_t line = {(char *)html, len, len}; + emit_code_line_p(&line, true, &ctx); return; } - split_code_lines(html, len, emit_code_line_p, out); + split_code_lines(html, len, emit_code_line_p, &ctx); } /* ------------------------------------------------------------------ */ @@ -1375,7 +1418,7 @@ static void walk_node(GumboNode *node, buffer_t *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); + emit_code_lines(code.data, code.len, out, NULL); free(code.data); } else { walk_children(node, out, inherited); diff --git a/src/web/normalization/htmlNormalizer.ts b/src/web/normalization/htmlNormalizer.ts index 1529aa928..33552548f 100644 --- a/src/web/normalization/htmlNormalizer.ts +++ b/src/web/normalization/htmlNormalizer.ts @@ -488,13 +488,27 @@ function flattenBqNode( } return; } + // A code block can't be nested in a blockquote, so its lines become separate + // paragraphs. Its inline content is collected whole first (as for a + // top-level code block), so both newlines and
    s split it. + if (isCodeBlockNode(node) && isPurelyInline(node)) { + flushInlineP(ib, out); + walkChildren(node, ib, inherited); + if (hasCodeLineBreak(ib.buf)) { + out.buf += emitCodeLines(ib.buf, emitAlignment(node, 'p')); + ib.buf = ''; + } else { + flushInlineP(ib, out, emitAlignment(node, 'p')); + } + return; + } if (isBlockProducing(node) || isBlockquoteNode(node)) { flushInlineP(ib, out); flattenBqChildren(node, ib, out, inherited); // A code block can't be nested in a blockquote, so its lines become // separate paragraphs. if (isCodeBlockNode(node) && hasCodeLineBreak(ib.buf)) { - out.buf += emitCodeLines(ib.buf); + out.buf += emitCodeLines(ib.buf, emitAlignment(node, 'p')); ib.buf = ''; return; } @@ -574,6 +588,23 @@ function flattenLiNode( flushLiBuffer(ib, out, ctx); return; } + // A code block can't be nested in a list item, so its lines become separate + // items, like lines split by
    . Its inline content is collected whole + // first (as for a top-level code block), so both newlines and
    s split + // it. Empty lines are dropped. + if (isCodeBlockNode(node) && isPurelyInline(node)) { + flushLiBuffer(ib, out, ctx); + walkChildren(node, ib, ctx.inherited); + if (hasCodeLineBreak(ib.buf)) { + splitCodeLines(ib.buf, (line, hasContent) => { + if (hasContent) flushLiBuffer({ buf: line }, out, ctx); + }); + ib.buf = ''; + } else { + flushLiBuffer(ib, out, ctx); + } + return; + } if (isBlockProducing(node) || isBlockquoteNode(node)) { flushLiBuffer(ib, out, ctx); flattenLiChildren(node, ib, out, ctx); @@ -758,13 +789,13 @@ function splitCodeLines( } /** Emits a code block's inline HTML as one

    per line (see splitCodeLines). - * An empty line becomes
    . */ -function emitCodeLines(html: string): string { - if (!hasCodeLineBreak(html)) return `

    ${html}

    `; + * An empty line becomes
    . `pAttrs` (e.g. text-align) go on every

    . */ +function emitCodeLines(html: string, pAttrs = ''): string { + if (!hasCodeLineBreak(html)) return `${html}

    `; let out = ''; splitCodeLines(html, (line, hasContent) => { - out += hasContent ? `

    ${line}

    ` : '
    '; + out += hasContent ? `${line}

    ` : '
    '; }); return out; }