Skip to content

Commit 726b7ea

Browse files
committed
feat: pass all mention attrs in normalization process
1 parent a95a17c commit 726b7ea

4 files changed

Lines changed: 28 additions & 14 deletions

File tree

cpp/parser/GumboNormalizer.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,9 +428,10 @@ static void emit_attributes(GumboElement *el, const char *tag_name,
428428
if (gumbo_get_attribute(&el->attributes, "checked") != NULL)
429429
buffer_append_str(out, " checked");
430430
} else if (strcmp(tag_name, "mention") == 0) {
431-
emit_one_attr(out, el, "id");
432-
emit_one_attr(out, el, "text");
433-
emit_one_attr(out, el, "indicator");
431+
for (unsigned int i = 0; i < el->attributes.length; i++) {
432+
GumboAttribute *attr = (GumboAttribute *)el->attributes.data[i];
433+
emit_one_attr(out, el, attr->name);
434+
}
434435
}
435436
}
436437

cpp/tests/GumboParserTest.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,20 @@ TEST(GumboParserTest, EnrichedTagRemappings) {
307307
EXPECT_EQ(
308308
GumboParser::normalizeHtml(
309309
"<mention text='@John Doe' indicator='@' id='1'>@John Doe</mention>"),
310-
"<mention id=\"1\" text=\"@John Doe\" indicator=\"@\">@John "
310+
"<mention text=\"@John Doe\" indicator=\"@\" id=\"1\">@John "
311311
"Doe</mention>");
312312
EXPECT_EQ(
313313
GumboParser::normalizeHtml("<mention text=\"@John Doe\" indicator=\"@\" "
314314
"id=\"1\">@John Doe</mention>"),
315-
"<mention id=\"1\" text=\"@John Doe\" indicator=\"@\">@John "
315+
"<mention text=\"@John Doe\" indicator=\"@\" id=\"1\">@John "
316316
"Doe</mention>");
317+
// Custom mention attributes are preserved
318+
EXPECT_EQ(
319+
GumboParser::normalizeHtml(
320+
"<mention id=\"1\" text=\"@John Doe\" indicator=\"@\" type=\"user\" "
321+
"data-custom=\"custom data\">@John Doe</mention>"),
322+
"<mention id=\"1\" text=\"@John Doe\" indicator=\"@\" type=\"user\" "
323+
"data-custom=\"custom data\">@John Doe</mention>");
317324

318325
// Link
319326
EXPECT_EQ(GumboParser::normalizeHtml(

src/web/__tests__/htmlNormalizer.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,14 +285,19 @@ describe('htmlNormalizer', () => {
285285
'<ul data-type="checkbox"><li checked>x</li></ul>',
286286
],
287287

288-
// Mentions (note: cpp reorders attrs to id, text, indicator)
288+
// Mentions
289289
[
290290
"<mention text='@John Doe' indicator='@' id='1'>@John Doe</mention>",
291-
'<mention id="1" text="@John Doe" indicator="@">@John Doe</mention>',
291+
'<mention text="@John Doe" indicator="@" id="1">@John Doe</mention>',
292292
],
293293
[
294294
'<mention text="@John Doe" indicator="@" id="1">@John Doe</mention>',
295-
'<mention id="1" text="@John Doe" indicator="@">@John Doe</mention>',
295+
'<mention text="@John Doe" indicator="@" id="1">@John Doe</mention>',
296+
],
297+
// Custom mention attributes are preserved
298+
[
299+
'<mention id="1" text="@John Doe" indicator="@" type="user" data-custom="custom data">@John Doe</mention>',
300+
'<mention id="1" text="@John Doe" indicator="@" type="user" data-custom="custom data">@John Doe</mention>',
296301
],
297302

298303
// Link

src/web/normalization/htmlNormalizer.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,13 @@ function emitAttributes(el: Element, name: string): string {
255255
}
256256
case 'li':
257257
return el.hasAttribute('checked') ? ' checked' : '';
258-
case 'mention':
259-
return (
260-
emitOneAttr(el, 'id') +
261-
emitOneAttr(el, 'text') +
262-
emitOneAttr(el, 'indicator')
263-
);
258+
case 'mention': {
259+
let out = '';
260+
for (const attr of Array.from(el.attributes)) {
261+
out += emitOneAttr(el, attr.name);
262+
}
263+
return out;
264+
}
264265
default:
265266
return '';
266267
}

0 commit comments

Comments
 (0)