Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 132 additions & 7 deletions cpp/parser/GumboNormalizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<br>`, `<br/>` or `<br ...>` tag. */
static bool is_br_tag(const char *tag, size_t tag_len) {
return tag_len >= 4 && strncmp(tag, "<br", 3) == 0 &&
(tag[3] == '>' || tag[3] == '/' || tag[3] == ' ');
}

/** True if normalized inline HTML has a line break: a newline in text or a
* <br>. 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 <p> (or <br> 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, "<p>");
buffer_append(out, line->data, line->len);
buffer_append_str(out, "</p>");
} else {
buffer_append_str(out, "<br>");
}
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 <pre> or <codeblock>) as
* one <p> per line, since line breaks in code are significant. A line break is
* a newline in text or a <br>. Inline tags open across a break are closed
* before it and reopened after it. An empty line becomes <br>, and a trailing
* empty line (`a\n</pre>`) 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, "<p>");
buffer_append(out, html, len);
buffer_append_str(out, "</p>");
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. <img /> */
} 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 */
/* ------------------------------------------------------------------ */
Expand Down Expand Up @@ -1177,15 +1300,17 @@ static void walk_node(GumboNode *node, buffer_t *out,
break;
}

/* <codeblock>: wrap inline content in <p> */
/* <codeblock>: wrap inline content in a <p> per line */
if (strcmp(out_name, "codeblock") == 0) {
bool wrap = is_purely_inline(node);
buffer_append_str(out, "<codeblock>");
if (wrap)
buffer_append_str(out, "<p>");
walk_children(node, out, inherited);
if (wrap)
buffer_append_str(out, "</p>");
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, "</codeblock>");
break;
}
Expand Down
26 changes: 26 additions & 0 deletions cpp/tests/GumboParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,32 @@ TEST(GumboParserTest, SpanRemappings) {
"<span style=\"font-family: &quot;Black &amp; White&quot;\">x</span>");
}

TEST(GumboParserTest, CodeBlockLines) {
// Each line of code becomes its own <p>
EXPECT_EQ(GumboParser::normalizeHtml("<pre>a\nb</pre>"),
"<codeblock><p>a</p><p>b</p></codeblock>");
EXPECT_EQ(GumboParser::normalizeHtml("<codeblock>a\nb</codeblock>"),
"<codeblock><p>a</p><p>b</p></codeblock>");
EXPECT_EQ(GumboParser::normalizeHtml("<pre>a<br>b</pre>"),
"<codeblock><p>a</p><p>b</p></codeblock>");

// Indentation is kept
EXPECT_EQ(GumboParser::normalizeHtml("<pre>if (x) {\n y();\n}</pre>"),
"<codeblock><p>if (x) {</p><p> y();</p><p>}</p></codeblock>");

// Empty lines become <br>, the first newline after <pre> and a trailing one
// are dropped
EXPECT_EQ(GumboParser::normalizeHtml("<pre>\na\n\nb\n</pre>"),
"<codeblock><p>a</p><br><p>b</p></codeblock>");

// Inline tags spanning a line break are closed and reopened
EXPECT_EQ(GumboParser::normalizeHtml("<pre><b>a\nb</b>c</pre>"),
"<codeblock><p><b>a</b></p><p><b>b</b>c</p></codeblock>");
EXPECT_EQ(
GumboParser::normalizeHtml("<pre><code>a\nb</code></pre>"),
"<codeblock><p><code>a</code></p><p><code>b</code></p></codeblock>");
}

TEST(GumboParserTest, EnrichedTagRemappings) {
// Block elements
EXPECT_EQ(GumboParser::normalizeHtml("<codeblock>x</codeblock>"),
Expand Down
34 changes: 34 additions & 0 deletions src/web/__tests__/htmlNormalizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,40 @@ describe('htmlNormalizer', () => {
});
});

describe('CodeBlockLines', () => {
test.each([
// Each line of code becomes its own <p>
['<pre>a\nb</pre>', '<codeblock><p>a</p><p>b</p></codeblock>'],
[
'<codeblock>a\nb</codeblock>',
'<codeblock><p>a</p><p>b</p></codeblock>',
],
['<pre>a<br>b</pre>', '<codeblock><p>a</p><p>b</p></codeblock>'],

// Indentation is kept
[
'<pre>if (x) {\n y();\n}</pre>',
'<codeblock><p>if (x) {</p><p> y();</p><p>}</p></codeblock>',
],

// Empty lines become <br>, the first newline after <pre> and a trailing
// one are dropped
['<pre>\na\n\nb\n</pre>', '<codeblock><p>a</p><br><p>b</p></codeblock>'],

// Inline tags spanning a line break are closed and reopened
[
'<pre><b>a\nb</b>c</pre>',
'<codeblock><p><b>a</b></p><p><b>b</b>c</p></codeblock>',
],
[
'<pre><code>a\nb</code></pre>',
'<codeblock><p><code>a</code></p><p><code>b</code></p></codeblock>',
],
])('%j → %s', (input, expected) => {
expect(normalizeHtml(input)).toBe(expected);
});
});

describe('EnrichedTagRemappings', () => {
test.each([
// Block elements
Expand Down
91 changes: 86 additions & 5 deletions src/web/normalization/htmlNormalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,84 @@ function walkChildren(
}
}

/** `<br>`, `<br/>` or `<br ...>`. */
const BR_TAG_RE = /^<br[\s/>]/;

/** True if normalized inline HTML has a line break: a newline in text or a
* <br>. 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 <pre> or <codeblock>) as
* one <p> per line, since line breaks in code are significant. A line break is
* a newline in text or a <br>. Inline tags open across a break are closed
* before it and reopened after it. An empty line becomes <br>, and a trailing
* empty line (`a\n</pre>`) is dropped, as browsers don't render it either.
*/
function emitCodeLines(html: string): string {
if (!hasCodeLineBreak(html)) return `<p>${html}</p>`;

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 += `</${name}>`;
}
out += hasContent ? `<p>${line}</p>` : '<br>';
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. <img />
} 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 },
Expand Down Expand Up @@ -805,13 +883,16 @@ function walkNode(
return;
}

// <codeblock>: wrap inline content in <p>
// <codeblock>: wrap inline content in a <p> per line
if (outName === 'codeblock') {
const wrap = isPurelyInline(node);
out.buf += '<codeblock>';
if (wrap) out.buf += '<p>';
walkChildren(node, out, inherited);
if (wrap) out.buf += '</p>';
if (isPurelyInline(node)) {
const code = { buf: '' };
walkChildren(node, code, inherited);
out.buf += emitCodeLines(code.buf);
} else {
walkChildren(node, out, inherited);
}
out.buf += '</codeblock>';
return;
}
Expand Down
Loading