dotnet / dotnet/aspnetcore

CSS fallback script checks the wrong element in LinkTagHelper

Open
#28,124 5 comments 0 reactions 0 assignees View on GitHub
area-mvc feature-mvc-razor-views investigate
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Describe the bug
The `` tag generated by LinkTagHelper (as seen [here in the LinkTagHelper class](https://github.com/dotnet/aspnetcore/blob/v5.0.0/src/Mvc/Mvc.TagHelpers/src/LinkTagHelper.cs#L367-L409) and [here in the LinkTagHelper_FallbackJavaScript.js](https://github.com/dotnet/aspnetcore/blob/v5.0.0/src/Mvc/Mvc.TagHelpers/src/js/LinkTagHelper_FallbackJavaScript.js)) references the last script tag on the page, and then takes the `.previousElementSibling` of that HTML element. If I have any other script tags on the page, this fallback script will fail and will always load in the fallback source.

### To Reproduce

Will be using PaperCSS as example here <https://www.getpapercss.com/>, hosted via UNPKG.

1. Create ASP.NET MVC project, ex:
```sh
dotnet new mvc -n LinkTagHelperIssue
```

2. Create directory for fallback file at `wwwroot/lib/papercss/dist`, ex:
```sh
mkdir -pv LinkTagHelperIssue/wwwroot/lib/papercss/dist
```

3. Download <https://unpkg.com/papercss@1.8.1/dist/paper.min.css> and place the downloaded file at `wwwroot/lib/papercss/dist/paper.min.css`, ex:
```sh
wget 'https://unpkg.com/papercss@1.8.1/dist/paper.min.css' -O LinkTagHelperIssue/wwwroot/lib/papercss/dist/paper.min.css
```

4. Edit `Views/Shared/_Layout.cshtml`, add the following to the end of the `<head>` tag:
```cshtml
<link rel="stylesheet"
href="https://unpkg.com/papercss@1.8.1/dist/paper.min.css"
integrity="sha384-R9lNp8nWuBGkBw8HAeT4RYv8Fmj1mD45av7OFm17UfasvI47Yuk/Sy8wUO5p/184"
crossorigin="anonymous"
asp-fallback-href="~/lib/papercss/dist/paper.min.css"
asp-fallback-test-class="paper" asp-fallback-test-property="border" asp-fallback-test-value="1px solid #c1c0bd" />
```

5. Run the project, ex:
```sh
dotnet run -p LinkTagHelperIssue
```

6. Open the page in web browser, for example Chrome. Open the inspector tool and look inside the `<head>` tag, and see that there are two `<link>` tags referencing `paper.min.css`, but one from `unpkg.com` and one from the same site:
![image](https://user-images.githubusercontent.com/2477952/100147132-35104780-2e9b-11eb-85cb-09dc04acc428.png)

### Further technical details
- ASP.NET Core version: 5.0.0 (have NOT tested on any other version though)
- Ran this from dotnet CLI on both Windows and Ubuntu, and from VS on Windows
- VS: Microsoft Visual Studio Community 2019, Version 16.8.2

<details>
<summary>dotnet --info on WSL instance I've tried this on</summary>

```
❯ dotnet --info
.NET SDK (reflecting any global.json):
Version: 5.0.100
Commit: 5044b93829

Runtime Environment:
OS Name: ubuntu
OS Version: 20.10
OS Platform: Linux
RID: ubuntu.20.10-x64
Base Path: /usr/share/dotnet/sdk/5.0.100/

Host (useful for support):
Version: 5.0.0
Commit: cf258a14b7

.NET SDKs installed:
3.1.404 [/usr/share/dotnet/sdk]
5.0.100 [/usr/share/dotnet/sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 3.1.10 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.0 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.10 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.0 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
```
</details>

<details>
<summary>dotnet --info from Windows instance I've tried this on</summary>

```
.NET SDK (reflecting any global.json):
Version: 5.0.100
Commit: 5044b93829

Runtime Environment:
OS Name: Windows
OS Version: 10.0.19041
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\5.0.100\

Host (useful for support):
Version: 5.0.0
Commit: cf258a14b7

.NET SDKs installed:
3.0.101 [C:\Program Files\dotnet\sdk]
3.1.100 [C:\Program Files\dotnet\sdk]
3.1.302 [C:\Program Files\dotnet\sdk]
5.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.All 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
```
</details>

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.