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
7 changes: 6 additions & 1 deletion cpp/parser/GumboNormalizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,12 @@ static void emit_attributes(GumboElement *el, const char *tag_name,
if (strcmp(tag_name, "a") == 0) {
emit_one_attr(out, el, "href");
} else if (strcmp(tag_name, "img") == 0) {
emit_one_attr(out, el, "src");
const char *src_val = get_attr(el, "src");
if (src_val && src_val[0]) {
emit_one_attr(out, el, "src");
} else {
buffer_append_str(out, " src=\"\"");
}
emit_one_attr(out, el, "alt");
emit_one_attr(out, el, "width");
emit_one_attr(out, el, "height");
Expand Down
5 changes: 5 additions & 0 deletions cpp/tests/GumboParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ TEST(GumboParserTest, EnrichedTagRemappings) {
EXPECT_EQ(
GumboParser::normalizeHtml("<img src='x' width='100' height='100' />"),
"<img src=\"x\" width=\"100\" height=\"100\" />");
EXPECT_EQ(GumboParser::normalizeHtml("<img width=\"100\" height=\"100\" />"),
"<img src=\"\" width=\"100\" height=\"100\" />");
EXPECT_EQ(GumboParser::normalizeHtml(
"<img src=\"\" width=\"100\" height=\"100\" />"),
"<img src=\"\" width=\"100\" height=\"100\" />");

// Lists
EXPECT_EQ(GumboParser::normalizeHtml("<ul><li>x</li></ul>"),
Expand Down
2 changes: 1 addition & 1 deletion ios/htmlParser/HtmlParser.mm
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ + (NSArray *_Nonnull)getTextAndStylesFromHtml:(NSString *_Nonnull)fixedHtml
[styleArr addObject:@([ItalicStyle getType])];
} else if ([tagName isEqualToString:@"img"]) {
NSRegularExpression *srcRegex =
[NSRegularExpression regularExpressionWithPattern:@"src=\"([^\"]+)\""
[NSRegularExpression regularExpressionWithPattern:@"src=\"([^\"]*)\""
options:0
error:nullptr];
NSTextCheckingResult *match =
Expand Down
8 changes: 8 additions & 0 deletions src/web/__tests__/htmlNormalizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ describe('htmlNormalizer', () => {
"<img src='x' width='100' height='100' />",
'<img src="x" width="100" height="100" />',
],
[
'<img width="100" height="100" />',
'<img src="" width="100" height="100" />',
],
[
'<img src="" width="100" height="100" />',
'<img src="" width="100" height="100" />',
],

// Lists
['<ul><li>x</li></ul>', '<ul><li>x</li></ul>'],
Expand Down
2 changes: 1 addition & 1 deletion src/web/normalization/htmlNormalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ function emitAttributes(el: Element, name: string): string {
return emitOneAttr(el, 'href');
case 'img':
return (
emitOneAttr(el, 'src') +
(el.getAttribute('src') ? emitOneAttr(el, 'src') : ' src=""') +
emitOneAttr(el, 'alt') +
emitOneAttr(el, 'width') +
emitOneAttr(el, 'height')
Expand Down
Loading