Rollup merge of #108129 - GuillaumeGomez:correctly-handle-links-starting-with-whitespace, r=petrochenkov

Correctly handle links starting with whitespace

Part of https://github.com/rust-lang/rust/issues/107995.

I just got this issue, wrote a fix and then saw the issue. So here's the PR. ^^'

r? `@petrochenkov`
This commit is contained in:
Guillaume Gomez 2023-02-19 14:47:55 +01:00 committed by GitHub
commit bd63edc07a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 2 deletions

View File

@ -339,6 +339,7 @@ pub fn inner_docs(attrs: &[ast::Attribute]) -> bool {
fn preprocess_link(link: &str) -> String {
let link = link.replace('`', "");
let link = link.split('#').next().unwrap();
let link = link.trim();
let link = link.rsplit('@').next().unwrap();
let link = link.strip_suffix("()").unwrap_or(link);
let link = link.strip_suffix("{}").unwrap_or(link);

View File

@ -884,7 +884,8 @@ fn preprocess_link(
let mut parts = stripped.split('#');
let link = parts.next().unwrap();
if link.trim().is_empty() {
let link = link.trim();
if link.is_empty() {
// This is an anchor to an element of the current page, nothing to do in here!
return None;
}
@ -897,7 +898,7 @@ fn preprocess_link(
// Parse and strip the disambiguator from the link, if present.
let (disambiguator, path_str, link_text) = match Disambiguator::from_str(link) {
Ok(Some((d, path, link_text))) => (Some(d), path.trim(), link_text.trim()),
Ok(None) => (None, link.trim(), link.trim()),
Ok(None) => (None, link, link),
Err((err_msg, relative_range)) => {
// Only report error if we would not have ignored this link. See issue #83859.
if !should_ignore_link_with_disambiguators(link) {

View File

@ -0,0 +1,28 @@
// Regression test for <https://github.com/rust-lang/rust/issues/107995>.
#![crate_name = "foo"]
// @has 'foo/fn.foo.html'
// @has - '//*[@class="docblock"]//a[@href="fn.bar.html"]' 'bar`'
/// A foo, see also [ bar`]
pub fn foo() {}
// @has 'foo/fn.bar.html'
// @has - '//*[@class="docblock"]' 'line Path line'
// @has - '//*[@class="docblock"]//a[@href="struct.Path.html"]' 'Path'
#[doc = "line ["]
#[doc = "Path"]
#[doc = "] line"]
pub fn bar() {}
// @has 'foo/fn.another.html'
// @has - '//*[@class="docblock"]//a[@href="struct.Path.html"]' 'Path'
/// [ `Path`]
pub fn another() {}
// @has 'foo/fn.last.html'
// @has - '//*[@class="docblock"]//a[@href="struct.Path.html"]' 'Path'
/// [ Path`]
pub fn last() {}
pub struct Path;