From 2d04d700a1f1e2755c8e3e3ac44918738ac27575 Mon Sep 17 00:00:00 2001 From: Daniil Suleiman <31325372+sulemanof@users.noreply.github.com> Date: Tue, 8 Oct 2019 00:41:27 +0300 Subject: [PATCH] URL field formatter fix (#46332) (#47464) * Fix url render relative hyperlinks * Fix test cases * Make prefix less errorable * Add some tests for url formatter --- .../field_formats/types/__tests__/url.js | 24 +++++++++++++++++++ .../kibana/common/field_formats/types/url.js | 4 +++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/legacy/core_plugins/kibana/common/field_formats/types/__tests__/url.js b/src/legacy/core_plugins/kibana/common/field_formats/types/__tests__/url.js index 16895d9e25a8d..4f7a6f4ebe82f 100644 --- a/src/legacy/core_plugins/kibana/common/field_formats/types/__tests__/url.js +++ b/src/legacy/core_plugins/kibana/common/field_formats/types/__tests__/url.js @@ -189,5 +189,29 @@ describe('UrlFormat', function () { expect(converter('../foo/bar', null, null, parsedUrl)) .to.be('../foo/bar'); }); + + it('should support multiple types of urls w/o basePath', function () { + const url = new UrlFormat(); + const parsedUrl = { + origin: 'http://kibana.host.com', + pathname: '/app/kibana', + }; + const converter = url.getConverterFor('html'); + + expect(converter('10.22.55.66', null, null, parsedUrl)) + .to.be('10.22.55.66'); + + expect(converter('http://www.domain.name/app/kibana#/dashboard/', null, null, parsedUrl)) + .to.be('http://www.domain.name/app/kibana#/dashboard/'); + + expect(converter('/app/kibana', null, null, parsedUrl)) + .to.be('/app/kibana'); + + expect(converter('kibana#/dashboard/', null, null, parsedUrl)) + .to.be('kibana#/dashboard/'); + + expect(converter('#/dashboard/', null, null, parsedUrl)) + .to.be('#/dashboard/'); + }); }); }); diff --git a/src/legacy/core_plugins/kibana/common/field_formats/types/url.js b/src/legacy/core_plugins/kibana/common/field_formats/types/url.js index 4058ef61bbeb6..0b65e103d20ea 100644 --- a/src/legacy/core_plugins/kibana/common/field_formats/types/url.js +++ b/src/legacy/core_plugins/kibana/common/field_formats/types/url.js @@ -157,7 +157,9 @@ export function createUrlFormat(FieldFormat) { } // Handle urls like: `../app/kibana` else { - prefix = `${parsedUrl.origin}${parsedUrl.basePath}/app/`; + const prefixEnd = url[0] === '/' ? '' : '/'; + + prefix = `${parsedUrl.origin}${parsedUrl.basePath || ''}/app${prefixEnd}`; } }