Skip to content

Commit eda54f3

Browse files
committed
fix: imgs with empty src and no src are properly handled
1 parent d389e5f commit eda54f3

5 files changed

Lines changed: 21 additions & 3 deletions

File tree

cpp/parser/GumboNormalizer.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,12 @@ static void emit_attributes(GumboElement *el, const char *tag_name,
491491
if (strcmp(tag_name, "a") == 0) {
492492
emit_one_attr(out, el, "href");
493493
} else if (strcmp(tag_name, "img") == 0) {
494-
emit_one_attr(out, el, "src");
494+
const char *src_val = get_attr(el, "src");
495+
if (src_val && src_val[0]) {
496+
emit_one_attr(out, el, "src");
497+
} else {
498+
buffer_append_str(out, " src=\"\"");
499+
}
495500
emit_one_attr(out, el, "alt");
496501
emit_one_attr(out, el, "width");
497502
emit_one_attr(out, el, "height");

cpp/tests/GumboParserTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,11 @@ TEST(GumboParserTest, EnrichedTagRemappings) {
282282
EXPECT_EQ(
283283
GumboParser::normalizeHtml("<img src='x' width='100' height='100' />"),
284284
"<img src=\"x\" width=\"100\" height=\"100\" />");
285+
EXPECT_EQ(GumboParser::normalizeHtml("<img width=\"100\" height=\"100\" />"),
286+
"<img src=\"\" width=\"100\" height=\"100\" />");
287+
EXPECT_EQ(GumboParser::normalizeHtml(
288+
"<img src=\"\" width=\"100\" height=\"100\" />"),
289+
"<img src=\"\" width=\"100\" height=\"100\" />");
285290

286291
// Lists
287292
EXPECT_EQ(GumboParser::normalizeHtml("<ul><li>x</li></ul>"),

ios/htmlParser/HtmlParser.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ + (NSArray *_Nonnull)getTextAndStylesFromHtml:(NSString *_Nonnull)fixedHtml
670670
[styleArr addObject:@([ItalicStyle getType])];
671671
} else if ([tagName isEqualToString:@"img"]) {
672672
NSRegularExpression *srcRegex =
673-
[NSRegularExpression regularExpressionWithPattern:@"src=\"([^\"]+)\""
673+
[NSRegularExpression regularExpressionWithPattern:@"src=\"([^\"]*)\""
674674
options:0
675675
error:nullptr];
676676
NSTextCheckingResult *match =

src/web/__tests__/htmlNormalizer.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,14 @@ describe('htmlNormalizer', () => {
262262
"<img src='x' width='100' height='100' />",
263263
'<img src="x" width="100" height="100" />',
264264
],
265+
[
266+
'<img width="100" height="100" />',
267+
'<img src="" width="100" height="100" />',
268+
],
269+
[
270+
'<img src="" width="100" height="100" />',
271+
'<img src="" width="100" height="100" />',
272+
],
265273

266274
// Lists
267275
['<ul><li>x</li></ul>', '<ul><li>x</li></ul>'],

src/web/normalization/htmlNormalizer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ function emitAttributes(el: Element, name: string): string {
267267
return emitOneAttr(el, 'href');
268268
case 'img':
269269
return (
270-
emitOneAttr(el, 'src') +
270+
(el.getAttribute('src') ? emitOneAttr(el, 'src') : ' src=""') +
271271
emitOneAttr(el, 'alt') +
272272
emitOneAttr(el, 'width') +
273273
emitOneAttr(el, 'height')

0 commit comments

Comments
 (0)