Don't intercept clicks for links that open in external frames #1352 (#1354)

This commit is contained in:
cores-system 2018-09-03 12:31:37 +03:00 committed by Steve Sanderson
parent c97cb8c18b
commit 223a2fed97
3 changed files with 33 additions and 1 deletions

View File

@ -27,9 +27,11 @@ function enableNavigationInterception(assemblyName: string, functionName: string
if (anchorTarget && anchorTarget.hasAttribute(hrefAttributeName) && event.button === 0) {
const href = anchorTarget.getAttribute(hrefAttributeName)!;
const absoluteHref = toAbsoluteUri(href);
const targetAttributeValue = anchorTarget.getAttribute('target');
const opensInSameFrame = !targetAttributeValue || targetAttributeValue === '_self';
// Don't stop ctrl/meta-click (etc) from opening links in new tabs/windows
if (isWithinBaseUriSpace(absoluteHref) && !eventHasSpecialKey(event)) {
if (isWithinBaseUriSpace(absoluteHref) && !eventHasSpecialKey(event) && opensInSameFrame) {
event.preventDefault();
performInternalNavigation(absoluteHref);
}

View File

@ -117,6 +117,34 @@ namespace Microsoft.AspNetCore.Blazor.E2ETest.Tests
}
}
[Fact]
public void CanFollowLinkToTargetBlankClick()
{
try
{
SetUrlViaPushState("/");
var app = MountTestComponent<TestRouter>();
app.FindElement(By.LinkText("Target (_blank)")).Click();
WaitAssert.Equal(2, () => Browser.WindowHandles.Count);
}
finally
{
// Closing newly opened windows if a new one was opened
while (Browser.WindowHandles.Count > 1)
{
Browser.SwitchTo().Window(Browser.WindowHandles.Last());
Browser.Close();
}
// Needed otherwise Selenium tries to direct subsequent commands
// to the tab that has already been closed
Browser.SwitchTo().Window(Browser.WindowHandles.First());
}
}
[Fact]
public void CanFollowLinkToOtherPageDoesNotOpenNewWindow()
{

View File

@ -20,3 +20,5 @@
<a id="anchor-with-no-href">
Anchor tag with no href attribute
</a>
<a href="/" target="_blank">Target (_blank)</a>