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
"); - buffer_append(out, line->data, line->len); - buffer_append_str(out, "
"); - } else { - buffer_append_str(out, "or`) is dropped, as browsers don't render it either. + * Splits normalized inline HTML of a code block (from) 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
or`) 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, ") + * 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
"); - 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., 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, " 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`) is dropped, as browsers don't render it either. + * Splits normalized inline HTML of a code block (from) 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
or`) is dropped, as browsers + * don't render it either. */ -function emitCodeLines(html: string): string { - if (!hasCodeLineBreak(html)) return `) + * 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
${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 += `${name}>`; } - out += hasContent ? `${line}
` : ' 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}
` : '; 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, " 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 per line (see splitCodeLines).
- * An empty line becomes
. */
-function emitCodeLines(html: string): string {
- if (!hasCodeLineBreak(html)) return `
${html}
`; + * An empty line becomes. */ +function emitCodeLines(html: string, pAttrs = ''): string { + if (!hasCodeLineBreak(html)) return `
${html}
`; let out = ''; splitCodeLines(html, (line, hasContent) => { - out += hasContent ? `${line}
` : '${line}
` : '