Skip to content

Commit

Permalink
Avoid generating links with empty inlines
Browse files Browse the repository at this point in the history
  • Loading branch information
liamwhite committed May 19, 2024
1 parent 0bf8223 commit 6a03dab
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/parser/inlines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use crate::nodes::{
use crate::parser::shortcodes::NodeShortCode;
use crate::parser::{unwrap_into_2, unwrap_into_copy, AutolinkType, Callback, Options, Reference};
use crate::scanners;
use crate::strings;
use crate::strings::Case;
use crate::strings::{self, is_blank, Case};
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::convert::TryFrom;
Expand Down Expand Up @@ -1366,6 +1365,32 @@ impl<'a, 'r, 'o, 'd, 'i, 'c, 'subj> Subject<'a, 'r, 'o, 'd, 'i, 'c, 'subj> {
));
}

// Ensure there was text if this was a link and not an image link
if !is_image {
let mut non_blank_found = false;
let mut tmpch = self.brackets[brackets_len - 1].inl_text.next_sibling();
while let Some(tmp) = tmpch {
match tmp.data.borrow().value {
NodeValue::Text(ref s) if is_blank(s.as_bytes()) => (),
_ => {
non_blank_found = true;
break;
}
}

tmpch = tmp.next_sibling();
}

if !non_blank_found {
self.brackets.pop();
return Some(self.make_inline(
NodeValue::Text("]".to_string()),
self.pos - 1,
self.pos - 1,
));
}
}

let after_link_text_pos = self.pos;

// Try to find a link destination within parenthesis
Expand Down
29 changes: 29 additions & 0 deletions src/tests/philomena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,35 @@ fn philomena_images() {
);
}

#[test]
fn no_empty_link() {
html(
"[](https://example.com/evil.domain.for.seo.spam)",
"<p>[](https://example.com/evil.domain.for.seo.spam)</p>\n",
);

html(
"[ ](https://example.com/evil.domain.for.seo.spam)",
"<p>[ ](https://example.com/evil.domain.for.seo.spam)</p>\n",
);
}

#[test]
fn empty_image_allowed() {
html(
"![ ](https://example.com/evil.domain.for.seo.spam)",
"<p><img src=\"https://example.com/evil.domain.for.seo.spam\" alt=\" \" /></p>\n",
);
}

#[test]
fn image_inside_link_allowed() {
html(
"[![](https://example.com/image.png)](https://example.com/)",
"<p><a href=\"https://example.com/\"><img src=\"https://example.com/image.png\" alt=\"\" /></a></p>\n",
);
}

#[test]
fn image_mention() {
html_opts_no_roundtrip(
Expand Down

0 comments on commit 6a03dab

Please sign in to comment.