@@ -505,6 +505,11 @@ pub(crate) async fn get_all_platforms(
505505 Extension ( pool) : Extension < Pool > ,
506506 uri : Uri ,
507507) -> AxumResult < AxumResponse > {
508+ let is_crate_root = params
509+ . path
510+ . as_ref ( )
511+ . map ( |path| path == "index.html" )
512+ . unwrap_or ( true ) ;
508513 let req_path: String = params. path . unwrap_or_default ( ) ;
509514 let req_path: Vec < & str > = req_path. split ( '/' ) . collect ( ) ;
510515
@@ -627,7 +632,7 @@ pub(crate) async fn get_all_platforms(
627632 doc_targets,
628633 } ,
629634 inner_path,
630- use_direct_platform_links : true ,
635+ use_direct_platform_links : is_crate_root ,
631636 current_target,
632637 } ;
633638 Ok ( res. into_response ( ) )
@@ -1244,40 +1249,84 @@ mod tests {
12441249
12451250 #[ test]
12461251 fn platform_links_are_direct_and_without_nofollow ( ) {
1252+ fn check_links ( response_text : String , ajax : bool , should_contain_redirect : bool ) {
1253+ let platform_links: Vec < ( String , String ) > = kuchikiki:: parse_html ( )
1254+ . one ( response_text)
1255+ . select ( & format ! ( r#"{}li a"# , if ajax { "" } else { "#platforms " } ) )
1256+ . expect ( "invalid selector" )
1257+ . map ( |el| {
1258+ let attributes = el. attributes . borrow ( ) ;
1259+ let url = attributes. get ( "href" ) . expect ( "href" ) . to_string ( ) ;
1260+ let rel = attributes. get ( "rel" ) . unwrap_or ( "" ) . to_string ( ) ;
1261+ ( url, rel)
1262+ } )
1263+ . collect ( ) ;
1264+
1265+ assert_eq ! ( platform_links. len( ) , 2 ) ;
1266+
1267+ for ( url, rel) in platform_links {
1268+ assert_eq ! (
1269+ url. contains( "/target-redirect/" ) ,
1270+ should_contain_redirect,
1271+ "ajax: {ajax:?}, should_contain_redirect: {should_contain_redirect:?}" ,
1272+ ) ;
1273+ if !should_contain_redirect {
1274+ assert_eq ! ( rel, "" ) ;
1275+ } else {
1276+ assert_eq ! ( rel, "nofollow" ) ;
1277+ }
1278+ }
1279+ }
1280+
12471281 wrapper ( |env| {
12481282 env. fake_release ( )
12491283 . name ( "dummy" )
12501284 . version ( "0.4.0" )
12511285 . rustdoc_file ( "dummy/index.html" )
12521286 . rustdoc_file ( "x86_64-pc-windows-msvc/dummy/index.html" )
1287+ . rustdoc_file ( "x86_64-pc-windows-msvc/dummy/struct.A.html" )
12531288 . default_target ( "x86_64-unknown-linux-gnu" )
12541289 . add_target ( "x86_64-pc-windows-msvc" )
12551290 . create ( ) ?;
12561291
1292+ let response = env. frontend ( ) . get ( "/dummy/latest/dummy" ) . send ( ) ?;
1293+ assert ! ( response. status( ) . is_success( ) ) ;
1294+ check_links ( response. text ( ) ?, false , true ) ;
1295+ // Same test with AJAX endpoint.
12571296 let response = env
12581297 . frontend ( )
1259- . get ( "/-/menus/platforms/dummy/0.4.0/x86_64-pc-windows-msvc " )
1298+ . get ( "/-/menus/platforms/dummy/latest/dummy " )
12601299 . send ( ) ?;
12611300 assert ! ( response. status( ) . is_success( ) ) ;
1301+ check_links ( response. text ( ) ?, true , false ) ;
12621302
1263- let platform_links : Vec < ( String , String ) > = kuchikiki :: parse_html ( )
1264- . one ( response . text ( ) ? )
1265- . select ( r#"li a"# )
1266- . expect ( "invalid selector" )
1267- . map ( |el| {
1268- let attributes = el . attributes . borrow ( ) ;
1269- let url = attributes . get ( "href" ) . expect ( "href" ) . to_string ( ) ;
1270- let rel = attributes . get ( "rel" ) . unwrap_or ( "" ) . to_string ( ) ;
1271- ( url , rel )
1272- } )
1273- . collect ( ) ;
1274-
1275- assert_eq ! ( platform_links . len ( ) , 2 ) ;
1303+ let response = env
1304+ . frontend ( )
1305+ . get ( "/dummy/0.4.0/x86_64-pc-windows-msvc/dummy" )
1306+ . send ( ) ? ;
1307+ assert ! ( response . status ( ) . is_success ( ) ) ;
1308+ check_links ( response . text ( ) ? , false , true ) ;
1309+ // Same test with AJAX endpoint.
1310+ let response = env
1311+ . frontend ( )
1312+ . get ( "/-/menus/platforms/dummy/0.4.0/x86_64-pc-windows-msvc/dummy" )
1313+ . send ( ) ? ;
1314+ assert ! ( response . status ( ) . is_success ( ) ) ;
1315+ check_links ( response . text ( ) ? , true , true ) ;
12761316
1277- for ( url, rel) in platform_links {
1278- assert ! ( !url. contains( "/target-redirect/" ) ) ;
1279- assert_eq ! ( rel, "" ) ;
1280- }
1317+ let response = env
1318+ . frontend ( )
1319+ . get ( "/dummy/0.4.0/x86_64-pc-windows-msvc/dummy/struct.A.html" )
1320+ . send ( ) ?;
1321+ assert ! ( response. status( ) . is_success( ) ) ;
1322+ check_links ( response. text ( ) ?, false , true ) ;
1323+ // Same test with AJAX endpoint.
1324+ let response = env
1325+ . frontend ( )
1326+ . get ( "/-/menus/platforms/dummy/0.4.0/x86_64-pc-windows-msvc/dummy/struct.A.html" )
1327+ . send ( ) ?;
1328+ assert ! ( response. status( ) . is_success( ) ) ;
1329+ check_links ( response. text ( ) ?, true , true ) ;
12811330
12821331 Ok ( ( ) )
12831332 } ) ;
0 commit comments