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, "");
+ buffer_append(line, tag + 1, name_len - 1);
+ 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, "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, ") 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
"); + 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. */
+ /* per line */
if (strcmp(out_name, "codeblock") == 0) {
- bool wrap = is_purely_inline(node);
buffer_append_str(out, " ");
- walk_children(node, out, inherited);
- if (wrap)
- buffer_append_str(out, "
+ EXPECT_EQ(GumboParser::normalizeHtml(" a b a b a b if (x) { y(); } a b a bc
+ [' a b a b a b if (x) { y(); } a b a bc per line, since line breaks in code are significant. A line break is
+ * a newline in text or a ${html} ${line}
+ // per line
if (outName === 'codeblock') {
- const wrap = isPurelyInline(node);
out.buf += ' ';
- walkChildren(node, out, inherited);
- if (wrap) out.buf += 'a\nb
"),
+ "a
"),
+ "
bif (x) {\n y();\n}"),
+ "
, the first newline after and a trailing one
+ // are dropped
+ EXPECT_EQ(GumboParser::normalizeHtml("\na\n\nb\n
"),
+ "a\nbc
"),
+ "
"),
+ "a\nbaba\nb
', 'a
', '
bif (x) {\n y();\n}',
+ '
, the first newline after and a trailing
+ // one are dropped
+ ['
\na\n\nb\n
', 'a\nbc
',
+ '
',
+ 'a\nbab
`, `
` 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
`) is dropped, as browsers don't render it either.
+ */
+function emitCodeLines(html: string): string {
+ if (!hasCodeLineBreak(html)) return `
. 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
';
+ 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('')) {
+ openTags.pop();
+ } else if (tag.endsWith('/>')) {
+ 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;
}
- //