diff --git a/cpp/parser/GumboNormalizer.c b/cpp/parser/GumboNormalizer.c
index ab972ea0d..1709e7a9b 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,18 @@ 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 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,
+ GumboElement *align_el);
+
/* ------------------------------------------------------------------ */
/* Blockquote content flattening */
/* ------------------------------------------------------------------ */
@@ -758,9 +776,30 @@ 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, &node->v.element);
+ 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 +842,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 +1105,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.; 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) {
+ code_p_ctx_t *c = (code_p_ctx_t *)ctx;
+ if (has_content) {
+ 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(c->out, " per line (see
+ * 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_t line = {(char *)html, len, len};
+ emit_code_line_p(&line, true, &ctx);
+ return;
+ }
+ split_code_lines(html, len, emit_code_line_p, &ctx);
}
/* ------------------------------------------------------------------ */
@@ -1306,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 802a13e94..33552548f 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;
@@ -483,9 +488,30 @@ 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, emitAlignment(node, 'p'));
+ ib.buf = '';
+ return;
+ }
// The flattened block becomes a
; carry over its text-align (if any).
flushInlineP(ib, out, emitAlignment(node, 'p'));
return;
@@ -562,9 +588,35 @@ 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);
+ // 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 +732,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 +752,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
. `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}
` : '