From a6e7911fe67a3f431665f6d76386038bd743f07c Mon Sep 17 00:00:00 2001 From: Maxime Thirouin Date: Tue, 22 Nov 2016 17:33:07 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed:=20when=20a=20````?= =?UTF-8?q?=20explicitly=20receive=20an=20absolute=20url=20from=20the=20sa?= =?UTF-8?q?me=20domain,=20but=20not=20the=20same=20project=20base=20path,?= =?UTF-8?q?=20a=20normal=20````=20is=20rendered?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Link/__tests__/index.js | 33 +++++++++++++++++++++++++- src/components/Link/index.js | 19 +++++++++++---- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/components/Link/__tests__/index.js b/src/components/Link/__tests__/index.js index 363aebd07..c6e5b376c 100644 --- a/src/components/Link/__tests__/index.js +++ b/src/components/Link/__tests__/index.js @@ -7,8 +7,9 @@ import expectJSX from "expect-jsx" import dom from "../../../_utils/jsdom" import Link from "../" -dom("http://localhost/test/") +process.env.PHENOMIC_USER_URL = "http://localhost/test/" process.env.PHENOMIC_USER_PATHNAME = "/test/" +dom(process.env.PHENOMIC_USER_URL) expect.extend(expectJSX) @@ -67,6 +68,36 @@ test("should render normal tag if to is not 'local'", () => { .toEqual("http://test.com") }) +test("should render normal tag if to is not in the same root path", () => { + const component = renderer( + createElement( + Link, + { + to: "http://localhost/root", + className: "foo", + children: , + }, + ), + { + router: { + isActive: () => false, + }, + } + ) + + expect(component.props.href) + .toEqual("http://localhost/root") + + expect(component).toEqualJSX( + + + + ) +}) + test("should allow passing props to tag", () => { const component = renderer( createElement( diff --git a/src/components/Link/index.js b/src/components/Link/index.js index 2f96c9917..50c059cea 100644 --- a/src/components/Link/index.js +++ b/src/components/Link/index.js @@ -38,11 +38,20 @@ function Link( return simpleLink } - const link = document.createElement("a") - link.href = to + const toLink = document.createElement("a") + toLink.href = to if ( - origin(link) === origin(window.location) + // parent absolute url with the same domain + // should not be Link + to.indexOf("://") > -1 && + to.indexOf(process.env.PHENOMIC_USER_URL) === -1 + ) { + return simpleLink + } + + if ( + origin(toLink) === origin(window.location) // we might want to restrict Link to path including the pathname // but this will require to preprend pathname to all Links from the // collection, which sucks. @@ -50,8 +59,8 @@ function Link( // you will need to includes the entire url, / won't work at it will use // the react-router basename defined by Phenomic. // && - // link.pathname.includes(process.env.PHENOMIC_USER_PATHNAME) - // link.pathname.indexOf(process.env.PHENOMIC_USER_PATHNAME) > -1 + // toLink.pathname.includes(process.env.PHENOMIC_USER_PATHNAME) + // toLink.pathname.indexOf(process.env.PHENOMIC_USER_PATHNAME) > -1 ) { return (